Skip to content

Commit

Permalink
Merge branch 'v7.0.0-beta' into minor-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrashed-dev committed Jan 30, 2024
2 parents 7a59c32 + 24eca5f commit 86959a6
Show file tree
Hide file tree
Showing 37 changed files with 2,239 additions and 212 deletions.
21 changes: 20 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
# Changelog

### 7.0.0-beta.3 / TBD
### Unreleased
* Changed `clientSecret` to optional for token exchange methods; defaults to API Key now
* Fix missing `type` field in `Event` model

### 7.0.0-beta.4 / 2024-01-12
* **BREAKING CHANGE**: Moved grants API out of `Auth` to `NylasClient`
* **BREAKING CHANGE**: Moved `Grants.create()` to `Auth.customAuthentication()`
* Added support for the folders API
* Added support for the attachments API
* Added support for the contacts API
* Added support for send-RSVP
* Added email value for CodeExchange response
* Fix issue with form-data not importing correctly for ESM projects
* Fix typing errors in TrackingOptions and Connector

### 7.0.0-beta.3 / 2023-10-23
* Added support for the messages, drafts, and threads endpoints
* Added support for the free-busy endpoint
* Added support for native/custom authentication
* Added support for Connectors & Credentials APIs
* Fix `getAvailability` response type
* Fix path for destroying a redirect URI
* Fix model for updating an Event
* Fix response type for creating a webhook

### 7.0.0-beta.2 / 2023-08-21
* Fix issue with getting the current Nylas SDK package version
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nylas",
"version": "7.0.0-beta.2",
"version": "7.0.0-beta.4",
"description": "A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.",
"main": "lib/cjs/nylas.js",
"types": "lib/types/nylas.d.ts",
Expand Down
100 changes: 61 additions & 39 deletions src/apiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,55 @@ export default class APIClient {
};
}

private async sendRequest(options: RequestOptionsParams): Promise<Response> {
const req = this.newRequest(options);
const controller: AbortController = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
throw new NylasSdkTimeoutError(req.url, this.timeout);
}, this.timeout);

try {
const response = await fetch(req, { signal: controller.signal });
clearTimeout(timeout);

if (typeof response === 'undefined') {
throw new Error('Failed to fetch response');
}

if (response.status > 299) {
const text = await response.text();
let error: Error;
try {
const parsedError = JSON.parse(text);
const camelCaseError = objKeysToCamelCase(parsedError);

// Check if the request is an authentication request
const isAuthRequest =
options.path.includes('connect/token') ||
options.path.includes('connect/revoke');

if (isAuthRequest) {
error = new NylasOAuthError(camelCaseError, response.status);
} else {
error = new NylasApiError(camelCaseError, response.status);
}
} catch (e) {
throw new Error(
`Received an error but could not parse response from the server: ${text}`
);
}

throw error;
}

return response;
} catch (error) {
clearTimeout(timeout);
throw error;
}
}

requestOptions(optionParams: RequestOptionsParams): RequestOptions {
const requestOptions = {} as RequestOptions;

Expand Down Expand Up @@ -166,46 +215,19 @@ export default class APIClient {
}

async request<T>(options: RequestOptionsParams): Promise<T> {
const req = this.newRequest(options);
const controller: AbortController = new AbortController();
const timeout = setTimeout(() => {
controller.abort();
throw new NylasSdkTimeoutError(req.url, this.timeout);
}, this.timeout);

const response = await fetch(req, { signal: controller.signal });
clearTimeout(timeout);

if (typeof response === 'undefined') {
throw new Error('Failed to fetch response');
}

// handle error response
if (response.status > 299) {
const authErrorResponse =
options.path.includes('connect/token') ||
options.path.includes('connect/revoke');

const text = await response.text();
let error: Error;
try {
const parsedError = JSON.parse(text);
const camelCaseError = objKeysToCamelCase(parsedError);

if (authErrorResponse) {
error = new NylasOAuthError(camelCaseError, response.status);
} else {
error = new NylasApiError(camelCaseError, response.status);
}
} catch (e) {
throw new Error(
`Received an error but could not parse response from the server: ${text}`
);
}
const response = await this.sendRequest(options);
return this.requestWithResponse(response);
}

throw error;
}
async requestRaw(options: RequestOptionsParams): Promise<Buffer> {
const response = await this.sendRequest(options);
return response.buffer();
}

return this.requestWithResponse(response);
async requestStream(
options: RequestOptionsParams
): Promise<NodeJS.ReadableStream> {
const response = await this.sendRequest(options);
return response.body;
}
}
49 changes: 49 additions & 0 deletions src/models/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* Interface of an attachment object from Nylas.
*/
export interface Attachment {
/**
* A globally unique object identifier.
*/
id: string;

/**
* Attachment's name.
*/
filename: string;

/**
* Attachment's content type.
*/
contentType: string;

/**
* Grant ID of the Nylas account.
*/
grantId: string;

/**
* If it's an inline attachment.
*/
isInline: boolean;

/**
* Attachment's size in bytes.
*/
size: number;
}

/**
* Interface representing of the query parameters for finding an attachment's metadata.
*/
export interface FindAttachmentQueryParams {
/**
* ID of the message the attachment belongs to.
*/
messageId: string;
}

/**
* Interface representing of the query parameters for downloading an attachment.
*/
export type DownloadAttachmentQueryParams = FindAttachmentQueryParams;
14 changes: 9 additions & 5 deletions src/models/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type AccessType = 'online' | 'offline';
/**
* Type for the different OAuth providers Nylas supports.
*/
export type Provider = 'google' | 'imap' | 'microsoft';
export type Provider = 'google' | 'imap' | 'microsoft' | 'virtual-calendar';

/**
* Configuration for generating a URL for OAuth 2.0 authentication.
Expand Down Expand Up @@ -80,9 +80,9 @@ export interface CodeExchangeRequest {
*/
clientId: string;
/**
* Client secret of the application.
* Client secret of the application. If not provided, the API Key will be used instead.
*/
clientSecret: string;
clientSecret?: string;
/**
* The original plain text code verifier (code_challenge) used in the initial authorization request (PKCE).
*/
Expand All @@ -106,9 +106,9 @@ export interface TokenExchangeRequest {
*/
clientId: string;
/**
* Client secret of the application.
* Client secret of the application. If not provided, the API Key will be used instead.
*/
clientSecret: string;
clientSecret?: string;
}

/**
Expand Down Expand Up @@ -141,6 +141,10 @@ export interface CodeExchangeResponse {
* Nylas grant ID that is now successfully created.
*/
grantId: string;
/**
* Email address of the grant that is created.
*/
email: string;
/**
* The remaining lifetime of the access token in seconds.
*/
Expand Down
Loading

0 comments on commit 86959a6

Please sign in to comment.