Skip to content
This repository has been archived by the owner on Mar 10, 2024. It is now read-only.

Commit

Permalink
chore: Refactor the supaglue client to also use the new createOpenapi…
Browse files Browse the repository at this point in the history
…Client
  • Loading branch information
tonyxiao committed Nov 20, 2023
1 parent ede7916 commit b4f0275
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 30 deletions.
18 changes: 10 additions & 8 deletions apps/api/routes/engagement/v2/sequence.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @jest-environment ./integration-test-environment
*/

import { HTTPError } from '@supaglue/schemas';
import type {
CreateContactRequest,
CreateContactResponse,
Expand Down Expand Up @@ -92,17 +93,18 @@ describe('sequence', () => {
test(`Error body`, async () => {
testSequence.steps[0].interval_seconds = 123;

const res = await supaglueClient.engagement.POST('/sequences', {
body: { record: testSequence },
// TODO: Make it so that x-customer-id can be omitted if it was passed into the client at creation time.
params: { header: { 'x-provider-name': 'salesloft', 'x-customer-id': process.env.CUSTOMER_ID! } },
});
const res = (await supaglueClient.engagement
.POST('/sequences', {
body: { record: testSequence },
// TODO: Make it so that x-customer-id can be omitted if it was passed into the client at creation time.
params: { header: { 'x-provider-name': 'salesloft', 'x-customer-id': process.env.CUSTOMER_ID! } },
})
.catch((e) => e as HTTPError<unknown>)) as HTTPError<unknown>;

expect(res).toBeInstanceOf(HTTPError);
expect(res.response.status).toEqual(400);
// Our API spec is wrong and should specify 400 return code with ability to have errors
expect((res.error as typeof res.data)?.errors?.[0].title).toMatch(
'Salesloft only supports intervals in whole days'
);
expect((res.error as any)?.errors?.[0].title).toMatch('Salesloft only supports intervals in whole days');
});

describe.each(['outreach', 'apollo', 'salesloft'])('%s', (providerName) => {
Expand Down
1 change: 0 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@types/uuid": "^9.0.1",
"concurrently": "^8.2.2",
"openapi-typescript": "^6.7.1",
"openapi-typescript-helpers": "0.0.4",
"prettier": "^3.1.0",
"ts-toolbelt": "9.6.0",
"typescript": "^4.9.5"
Expand Down
2 changes: 1 addition & 1 deletion packages/core/remotes/impl/apollo/apollo.client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createOpenapiClient } from '../../utils/createOpenapiClient';
import { createOpenapiClient } from '@supaglue/schemas';

import type { paths } from './apollo.openapi.gen';
import oas from './apollo.openapi.json';
Expand Down
12 changes: 12 additions & 0 deletions packages/core/remotes/impl/apollo/apollo.openapi.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/core/remotes/impl/apollo/apollo.openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@ export function outputOpenApi() {
openapi: '3.1.0',
info: { title: 'Apollo API', version: '0.0.0' },
servers: [{ url: 'https://app.apollo.io/api' }],
security: [{ api_key: [] }],
components: {
securitySchemes: { api_key: { type: 'apiKey', name: 'api_key', in: 'query' } },
},
paths: {
'/v1/emailer_campaigns/{id}': {
get: jsonOperation('getEmailerCampaign', {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/remotes/impl/outreach/outreach.client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OAuthClientOptions } from '@supaglue/schemas';
import { createOpenapiOauthClient, HTTPError } from '@supaglue/schemas';
import { SGConnectionNoLongerAuthenticatedError } from '../../../errors';
import type { OAuthClientOptions } from '../../utils/createOpenapiClient';
import { createOpenapiOauthClient, HTTPError } from '../../utils/createOpenapiClient';

import type { paths } from './outreach.openapi.gen';
import oas from './outreach.openapi.json';
Expand Down
4 changes: 2 additions & 2 deletions packages/core/remotes/impl/salesloft/salesloft.client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { OAuthClientOptions } from '@supaglue/schemas';
import { createOpenapiOauthClient } from '@supaglue/schemas';
import type { ConnectorAuthConfig } from '../../base';
import type { OAuthClientOptions } from '../../utils/createOpenapiClient';
import { createOpenapiOauthClient } from '../../utils/createOpenapiClient';

import type { paths } from './salesloft.openapi.gen';
import oas from './salesloft.openapi.json';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FetchOptions, FetchResponse } from 'openapi-fetch';
import createClient from 'openapi-fetch';
// @ts-expect-error Not sure we get The current file is a CommonJS module whose imports will
// produce 'require' calls error, but it's ireelevant and thus we will suppress it
// @ts-expect-error Not sure why this is needed
import type { PathsWithMethod } from 'openapi-typescript-helpers';

type HTTPMethod = 'GET' | 'PUT' | 'POST' | 'DELETE' | 'OPTIONS' | 'HEAD' | 'PATCH' | 'TRACE';
Expand Down
1 change: 1 addition & 0 deletions packages/schemas/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './supaglue-client';
export * from './createOpenapiClient'
1 change: 1 addition & 0 deletions packages/schemas/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
},
"devDependencies": {
"openapi-typescript": "^6.7.0",
"openapi-typescript-helpers": "^0.0.4",
"tsx": "^3.12.3"
}
}
23 changes: 11 additions & 12 deletions packages/schemas/supaglue-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import createClient from 'openapi-fetch';

import { createOpenapiClient } from './createOpenapiClient';
import type { paths as actions } from './gen/v2/actions';
import type { paths as crm } from './gen/v2/crm';
import type { paths as data } from './gen/v2/data';
Expand All @@ -25,18 +24,18 @@ export function createSupaglueClient({ apiUrl = 'https://api.supaglue.io', ...op
({
baseUrl: new URL(segment, apiUrl).toString(),
headers: { ['x-api-key']: options.apiKey },
} satisfies Parameters<typeof createClient>[0]);
}) satisfies Parameters<typeof createOpenapiClient>[0];

return {
actions: createClient<actions>(getOptions('actions/v2')),
crm: createClient<crm>(getOptions('crm/v2')),
data: createClient<data>(getOptions('data/v2')),
engagement: createClient<engagement>(getOptions('engagement/v2')),
enrichment: createClient<enrichment>(getOptions('enrichment/v2')),
marketingAutomation: createClient<marketingAutomation>(getOptions('marketing-automation/v2')),
metadata: createClient<metadata>(getOptions('metadata/v2')),
ticketing: createClient<ticketing>(getOptions('ticketing/v2')),
actions: createOpenapiClient<actions>(getOptions('actions/v2')),
crm: createOpenapiClient<crm>(getOptions('crm/v2')),
data: createOpenapiClient<data>(getOptions('data/v2')),
engagement: createOpenapiClient<engagement>(getOptions('engagement/v2')),
enrichment: createOpenapiClient<enrichment>(getOptions('enrichment/v2')),
marketingAutomation: createOpenapiClient<marketingAutomation>(getOptions('marketing-automation/v2')),
metadata: createOpenapiClient<metadata>(getOptions('metadata/v2')),
ticketing: createOpenapiClient<ticketing>(getOptions('ticketing/v2')),
// TODO: Mgmt uses different headers, we should fix that
mgmt: createClient<mgmt>(getOptions('mgmt/v2')),
mgmt: createOpenapiClient<mgmt>(getOptions('mgmt/v2')),
};
}
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6322,7 +6322,6 @@ __metadata:
odata: ^1.3.2
openapi-fetch: ^0.8.1
openapi-typescript: ^6.7.1
openapi-typescript-helpers: 0.0.4
pg: ^8.10.2
pg-connection-string: ^2.6.2
pg-copy-streams: ^6.0.6
Expand Down Expand Up @@ -6364,6 +6363,7 @@ __metadata:
dependencies:
openapi-fetch: 0.8.1
openapi-typescript: ^6.7.0
openapi-typescript-helpers: ^0.0.4
tsx: ^3.12.3
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -18546,7 +18546,7 @@ __metadata:
languageName: node
linkType: hard

"openapi-typescript-helpers@npm:0.0.4, openapi-typescript-helpers@npm:^0.0.4":
"openapi-typescript-helpers@npm:^0.0.4":
version: 0.0.4
resolution: "openapi-typescript-helpers@npm:0.0.4"
checksum: d06d62f9669db5110f32ef36a70f9f2698dd7ad03ba41968baf13e1091caaf23c4af6f8a79103cfd68f65046da4284dcc2f75e9950e9170115189b545e3030fc
Expand Down

0 comments on commit b4f0275

Please sign in to comment.