From ebf4992620fb5be5c04e6f662f7212202bb6ab93 Mon Sep 17 00:00:00 2001 From: Subash Pradhan Date: Thu, 28 Nov 2024 12:56:00 +0100 Subject: [PATCH] Fix Credentials endpoint (#610) * Fix Credentials endpoint * Fix lint --------- Co-authored-by: Subash Pradhan --- src/resources/credentials.ts | 18 ++-- tests/resources/credentials.spec.ts | 151 ++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+), 9 deletions(-) create mode 100644 tests/resources/credentials.spec.ts diff --git a/src/resources/credentials.ts b/src/resources/credentials.ts index 17ab38c8..97b34db8 100644 --- a/src/resources/credentials.ts +++ b/src/resources/credentials.ts @@ -1,17 +1,17 @@ -import { AsyncListResponse, Resource } from './resource.js'; +import { Overrides } from '../config.js'; +import { Provider } from '../models/auth.js'; import { - Credential, CreateCredentialRequest, + Credential, ListCredentialsQueryParams, UpdateCredentialRequest, } from '../models/credentials.js'; -import { Overrides } from '../config.js'; import { NylasBaseResponse, NylasListResponse, NylasResponse, } from '../models/response.js'; -import { Provider } from '../models/auth.js'; +import { AsyncListResponse, Resource } from './resource.js'; /** * The parameters for the {@link Credentials.find} method @@ -80,7 +80,7 @@ export class Credentials extends Resource { return super._list>({ queryParams, overrides, - path: `/v3/credentials/${provider}/creds`, + path: `/v3/connectors/${provider}/creds`, }); } @@ -94,7 +94,7 @@ export class Credentials extends Resource { overrides, }: FindCredentialParams & Overrides): Promise> { return super._find({ - path: `/v3/credentials/${provider}/creds/${credentialsId}`, + path: `/v3/connectors/${provider}/creds/${credentialsId}`, overrides, }); } @@ -109,7 +109,7 @@ export class Credentials extends Resource { overrides, }: CreateCredentialParams & Overrides): Promise> { return super._create({ - path: `/v3/credentials/${provider}/creds`, + path: `/v3/connectors/${provider}/creds`, requestBody, overrides, }); @@ -126,7 +126,7 @@ export class Credentials extends Resource { overrides, }: UpdateCredentialParams & Overrides): Promise> { return super._update({ - path: `/v3/credentials/${provider}/creds/${credentialsId}}`, + path: `/v3/connectors/${provider}/creds/${credentialsId}`, requestBody, overrides, }); @@ -142,7 +142,7 @@ export class Credentials extends Resource { overrides, }: DestroyCredentialParams & Overrides): Promise { return super._destroy({ - path: `/v3/credentials/${provider}/creds/${credentialsId}}`, + path: `/v3/connectors/${provider}/creds/${credentialsId}`, overrides, }); } diff --git a/tests/resources/credentials.spec.ts b/tests/resources/credentials.spec.ts new file mode 100644 index 00000000..8b12ae42 --- /dev/null +++ b/tests/resources/credentials.spec.ts @@ -0,0 +1,151 @@ +import APIClient from '../../src/apiClient'; +import { CredentialType } from '../../src/models/credentials'; +import { Credentials } from '../../src/resources/credentials'; +jest.mock('../src/apiClient'); + +describe('Credentials', () => { + let apiClient: jest.Mocked; + let credentials: Credentials; + + beforeAll(() => { + apiClient = new APIClient({ + apiKey: 'apiKey', + apiUri: 'https://test.api.nylas.com', + timeout: 30, + headers: {}, + }) as jest.Mocked; + + credentials = new Credentials(apiClient); + apiClient.request.mockResolvedValue({}); + }); + + describe('list', () => { + it('should call apiClient.request with the correct params', async () => { + await credentials.list({ + provider: 'microsoft', + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/connectors/microsoft/creds', + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + }); + }); + + describe('find', () => { + it('should call apiClient.request with the correct params', async () => { + await credentials.find({ + provider: 'microsoft', + credentialsId: 'microsoft-id123', + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'GET', + path: '/v3/connectors/microsoft/creds/microsoft-id123', + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + }); + }); + + describe('create', () => { + it('should call apiClient.request with the correct params', async () => { + await credentials.create({ + provider: 'microsoft', + requestBody: { + name: 'My Microsoft Connector', + credentialType: CredentialType.CONNECTOR, + credentialData: { + clientID: '', + clientSecret: '', + }, + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'POST', + path: '/v3/connectors/microsoft/creds', + body: { + name: 'My Microsoft Connector', + credentialType: CredentialType.CONNECTOR, + credentialData: { + clientID: '', + clientSecret: '', + }, + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + }); + }); + + describe('update', () => { + it('should call apiClient.request with the correct params', async () => { + await credentials.update({ + provider: 'microsoft', + credentialsId: 'microsoft-123', + requestBody: { + name: 'Changed Name', + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'PUT', + path: '/v3/connectors/microsoft/creds/microsoft-123', + body: { + name: 'Changed Name', + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + }); + }); + + describe('destroy', () => { + it('should call apiClient.request with the correct params', async () => { + await credentials.destroy({ + provider: 'microsoft', + credentialsId: 'microsoft-1234', + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'DELETE', + path: '/v3/connectors/microsoft/creds/microsoft-1234', + overrides: { + apiUri: 'https://test.api.nylas.com', + headers: { override: 'bar' }, + }, + }); + }); + }); +});