diff --git a/CHANGELOG.md b/CHANGELOG.md index dbecb112..e8882d1e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ### 7.0.0-beta.4 / TBD +* **BREAKING CHANGE**: Moved grants API out of `Auth` to `NylasClient` +* **BREAKING CHANGE**: Moved `Grants.create()` to `Auth.customAuthentication()` * Fix issue with form-data not importing correctly for ESM projects ### 7.0.0-beta.3 / 2023-10-23 diff --git a/src/nylas.ts b/src/nylas.ts index f40541a7..658084c8 100644 --- a/src/nylas.ts +++ b/src/nylas.ts @@ -10,6 +10,7 @@ import { Drafts } from './resources/drafts.js'; import { Threads } from './resources/threads.js'; import { Connectors } from './resources/connectors.js'; import { Folders } from './resources/folders.js'; +import { Grants } from './resources/grants.js'; import { Contacts } from './resources/contacts.js'; import { Attachments } from './resources/attachments.js'; @@ -52,6 +53,10 @@ export default class Nylas { * Access the Events API */ public events: Events; + /** + * Access the Grants API + */ + public grants: Grants; /** * Access the Messages API */ @@ -91,6 +96,7 @@ export default class Nylas { this.connectors = new Connectors(this.apiClient); this.drafts = new Drafts(this.apiClient); this.events = new Events(this.apiClient); + this.grants = new Grants(this.apiClient); this.messages = new Messages(this.apiClient); this.threads = new Threads(this.apiClient); this.webhooks = new Webhooks(this.apiClient); diff --git a/src/resources/auth.ts b/src/resources/auth.ts index 824880ad..bc2aa7e1 100644 --- a/src/resources/auth.ts +++ b/src/resources/auth.ts @@ -1,8 +1,6 @@ import { v4 as uuid } from 'uuid'; import { createHash } from 'node:crypto'; -import APIClient from '../apiClient.js'; import { Resource } from './resource.js'; -import { Grants } from './grants.js'; import { URLForAdminConsentConfig, URLForAuthenticationConfig, @@ -13,6 +11,16 @@ import { ProviderDetectParams, ProviderDetectResponse, } from '../models/auth.js'; +import { Overrides } from '../config.js'; +import { NylasResponse } from '../models/response.js'; +import { CreateGrantRequest, Grant } from '../models/grants.js'; + +/** + * @property requestBody The values to create the Grant with. + */ +interface CreateGrantParams { + requestBody: CreateGrantRequest; +} /** * A collection of authentication related API endpoints @@ -21,22 +29,6 @@ import { * Also contains the Grants API and collection of provider API endpoints. */ export class Auth extends Resource { - /** - * Access the Grants API - */ - public grants: Grants; - - apiClient: APIClient; - - /** - * @param apiClient The configured Nylas API client - */ - constructor(apiClient: APIClient) { - super(apiClient); - this.apiClient = apiClient; - this.grants = new Grants(apiClient); - } - /** * Build the URL for authenticating users to your application with OAuth 2.0 * @param config The configuration for building the URL @@ -117,6 +109,22 @@ export class Auth extends Resource { return url.toString(); } + /** + * Create a grant via Custom Authentication + * @return The created grant + */ + public customAuthentication({ + requestBody, + overrides, + }: CreateGrantParams & Overrides): Promise> { + return this.apiClient.request>({ + method: 'POST', + path: `/v3/connect/custom`, + body: requestBody, + overrides, + }); + } + /** * Revoke a token (and the grant attached to the token) * @param token The token to revoke diff --git a/src/resources/contacts.ts b/src/resources/contacts.ts index 67a52ecc..5fbabfe5 100644 --- a/src/resources/contacts.ts +++ b/src/resources/contacts.ts @@ -10,7 +10,7 @@ import { import { NylasResponse, NylasListResponse, - NylasDeleteResponse, + NylasBaseResponse, } from '../models/response.js'; import { AsyncListResponse, Resource } from './resource.js'; @@ -152,7 +152,7 @@ export class Contacts extends Resource { identifier, contactId, overrides, - }: DestroyContactParams & Overrides): Promise { + }: DestroyContactParams & Overrides): Promise { return super._destroy({ path: `/v3/grants/${identifier}/contacts/${contactId}`, overrides, diff --git a/src/resources/grants.ts b/src/resources/grants.ts index c34be680..6b644471 100644 --- a/src/resources/grants.ts +++ b/src/resources/grants.ts @@ -6,7 +6,6 @@ import { NylasListResponse, } from '../models/response.js'; import { - CreateGrantRequest, Grant, ListGrantsQueryParams, UpdateGrantRequest, @@ -19,13 +18,6 @@ interface FindGrantParams { grantId: string; } -/** - * @property requestBody The values to create the Grant with. - */ -interface CreateGrantParams { - requestBody: CreateGrantRequest; -} - /** * @property grantId The id of the Grant to update. * @property requestBody The values to update the Grant with. @@ -77,21 +69,6 @@ export class Grants extends Resource { }); } - /** - * Create a Grant via Custom Authentication - * @return The created Grant - */ - public create({ - requestBody, - overrides, - }: CreateGrantParams & Overrides): Promise> { - return super._create({ - path: `/v3/connect/custom`, - requestBody, - overrides, - }); - } - /** * Update a Grant * @return The updated Grant diff --git a/tests/apiClient.spec.ts b/tests/apiClient.spec.ts index 24347eb8..b396f450 100644 --- a/tests/apiClient.spec.ts +++ b/tests/apiClient.spec.ts @@ -216,30 +216,6 @@ describe('APIClient', () => { ).rejects.toThrow(new NylasOAuthError(payload)); }); - // it('should throw a TokenValidationError if the error comes from connect/tokeninfo', async () => { - // const payload = { - // success: false, - // error: { - // httpCode: 400, - // eventCode: 10020, - // message: 'Invalid access token', - // type: 'AuthenticationError', - // requestId: 'abc123', - // }, - // }; - // mockedFetch.mockImplementation(() => new Response('', { status: 400 })); - // jest - // .spyOn(Response.prototype, 'text') - // .mockImplementation(() => Promise.resolve(JSON.stringify(payload))); - // - // await expect( - // client.request({ - // path: '/connect/tokeninfo', - // method: 'POST', - // }) - // ).rejects.toThrow(new NylasTokenValidationError(payload)); - // }); - it('should throw a NylasApiError if the error comes from the other non-auth endpoints', async () => { const payload = { requestId: 'abc123', diff --git a/tests/resources/auth.spec.ts b/tests/resources/auth.spec.ts index 2fad2b01..e4eeb424 100644 --- a/tests/resources/auth.spec.ts +++ b/tests/resources/auth.spec.ts @@ -94,35 +94,39 @@ describe('Auth', () => { }); }); }); - // describe('Validating token', () => { - // describe('validateIDToken', () => { - // it('should call apiClient.request with the correct params', async () => { - // await auth.validateIDToken('id123'); - // - // expect(apiClient.request).toHaveBeenCalledWith({ - // method: 'GET', - // path: '/v3/connect/tokeninfo', - // queryParams: { - // idToken: 'id123', - // }, - // }); - // }); - // }); - // - // describe('validateAccessToken', () => { - // it('should call apiClient.request with the correct params', async () => { - // await auth.validateAccessToken('accessToken123'); - // - // expect(apiClient.request).toHaveBeenCalledWith({ - // method: 'GET', - // path: '/v3/connect/tokeninfo', - // queryParams: { - // accessToken: 'accessToken123', - // }, - // }); - // }); - // }); - // }); + describe('customAuthentication', () => { + it('should call apiClient.request with the correct params', async () => { + await auth.customAuthentication({ + requestBody: { + provider: 'google', + settings: { + test_setting: 'abc123', + }, + scope: ['calendar'], + state: 'state123', + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + + expect(apiClient.request).toHaveBeenCalledWith({ + method: 'POST', + path: '/v3/connect/custom', + body: { + provider: 'google', + settings: { + test_setting: 'abc123', + }, + scope: ['calendar'], + state: 'state123', + }, + overrides: { + apiUri: 'https://test.api.nylas.com', + }, + }); + }); + }); describe('URL building', () => { describe('urlForAuthentication', () => { it('should build the correct url', () => { diff --git a/tests/resources/grants.spec.ts b/tests/resources/grants.spec.ts index 7ec6d516..aa48adf4 100644 --- a/tests/resources/grants.spec.ts +++ b/tests/resources/grants.spec.ts @@ -63,40 +63,6 @@ describe('Grants', () => { }); }); - describe('create', () => { - it('should call apiClient.request with the correct params', async () => { - await grants.create({ - requestBody: { - provider: 'google', - settings: { - test_setting: 'abc123', - }, - scope: ['calendar'], - state: 'state123', - }, - overrides: { - apiUri: 'https://test.api.nylas.com', - }, - }); - - expect(apiClient.request).toHaveBeenCalledWith({ - method: 'POST', - path: '/v3/connect/custom', - body: { - provider: 'google', - settings: { - test_setting: 'abc123', - }, - scope: ['calendar'], - state: 'state123', - }, - overrides: { - apiUri: 'https://test.api.nylas.com', - }, - }); - }); - }); - describe('update', () => { it('should call apiClient.request with the correct params', async () => { await grants.update({