-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
38f075c
commit 3908446
Showing
7 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* Interface of an attachment object from Nylas. | ||
*/ | ||
export interface Attachment { | ||
/** | ||
* A globally unique object identifier. | ||
* Constraints: Minimum 1 character. | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* Attachment's name. | ||
* Constraints: Minimum 1 character. | ||
*/ | ||
filename: string; | ||
|
||
/** | ||
* Attachment's content type. | ||
* Constraints: Minimum 1 character. | ||
*/ | ||
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 = Partial<FindAttachmentQueryParams>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Overrides } from '../config.js'; | ||
import { | ||
Attachment, | ||
FindAttachmentQueryParams, | ||
DownloadAttachmentQueryParams, | ||
} from '../models/attachments.js'; | ||
import { NylasResponse } from '../models/response.js'; | ||
import { Resource } from './resource.js'; | ||
|
||
/** | ||
* @property identifier The ID of the grant to act upon. Use "me" to refer to the grant associated with an access token. | ||
* @property attachmentId The ID of the attachment to act upon. | ||
* @property queryParams The query parameters to include in the request | ||
*/ | ||
interface FindAttachmentParams { | ||
identifier: string; | ||
attachmentId: string; | ||
queryParams: FindAttachmentQueryParams; | ||
} | ||
|
||
/** | ||
* @property identifier The ID of the grant to act upon. Use "me" to refer to the grant associated with an access token. | ||
* @property attachmentId The ID of the attachment to act upon. | ||
* @property queryParams The query parameters to include in the request | ||
*/ | ||
interface DownloadAttachmentParams { | ||
identifier: string; | ||
attachmentId: string; | ||
queryParams: DownloadAttachmentQueryParams; | ||
} | ||
|
||
/** | ||
* Nylas Attachments API | ||
* | ||
* The Nylas Attachments API allows you to retrieve metadata and download attachments. | ||
* | ||
* You can use the attachments schema in a Send v3 request to send attachments regardless of the email provider. | ||
* To include attachments in a Send v3 request, all attachments must be base64 encoded and the encoded data must be placed in the content field in the request body. | ||
* | ||
* If you are using draft support, the draft including the attachment is stored on the provider. If you are not using draft support, Nylas stores the attachment. | ||
* | ||
* Attachment size is currently limited by each provider. | ||
* 3MB for Gmail messages | ||
* 10MB for Microsoft messages | ||
* | ||
* You can also send attachments inline in an email message, for example for images that should be displayed in the email body content. | ||
*/ | ||
export class Attachments extends Resource { | ||
/** | ||
* Returns an attachment by ID. | ||
* @return The Attachment metadata | ||
*/ | ||
public find({ | ||
identifier, | ||
attachmentId, | ||
queryParams, | ||
overrides, | ||
}: FindAttachmentParams & Overrides): Promise<NylasResponse<Attachment>> { | ||
return super._find({ | ||
path: `/v3/grants/${identifier}/attachments/${attachmentId}`, | ||
queryParams, | ||
overrides, | ||
}); | ||
} | ||
|
||
/** | ||
* Returns an attachment by ID. | ||
* @return The Attachment file in binary format | ||
*/ | ||
public download({ | ||
identifier, | ||
attachmentId, | ||
queryParams, | ||
overrides, | ||
}: DownloadAttachmentParams & Overrides): Promise<Buffer> { | ||
return super._getRaw({ | ||
path: `/v3/grants/${identifier}/attachments/${attachmentId}/download`, | ||
queryParams, | ||
overrides, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import APIClient from '../../src/apiClient'; | ||
import { Attachments } from '../../src/resources/attachments'; | ||
jest.mock('../src/apiClient'); | ||
|
||
describe('Attachments', () => { | ||
let apiClient: jest.Mocked<APIClient>; | ||
let attachments: Attachments; | ||
|
||
beforeAll(() => { | ||
apiClient = new APIClient({ | ||
apiKey: 'apiKey', | ||
apiUri: 'https://test.api.nylas.com', | ||
timeout: 30, | ||
}) as jest.Mocked<APIClient>; | ||
|
||
attachments = new Attachments(apiClient); | ||
apiClient.request.mockResolvedValue({}); | ||
apiClient.requestRaw.mockResolvedValue(Buffer.from('')); | ||
}); | ||
|
||
describe('find', () => { | ||
it('should call apiClient.request with the correct params for attachment metadata', async () => { | ||
await attachments.find({ | ||
identifier: 'id123', | ||
attachmentId: 'attach123', | ||
queryParams: { | ||
messageId: 'message123', | ||
}, | ||
overrides: { | ||
apiUri: 'https://test.api.nylas.com', | ||
}, | ||
}); | ||
|
||
expect(apiClient.request).toHaveBeenCalledWith({ | ||
method: 'GET', | ||
path: '/v3/grants/id123/attachments/attach123', | ||
queryParams: { | ||
messageId: 'message123', | ||
}, | ||
overrides: { | ||
apiUri: 'https://test.api.nylas.com', | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('download', () => { | ||
it('should call apiClient.requestRaw with the correct params for downloading an attachment', async () => { | ||
await attachments.download({ | ||
identifier: 'id123', | ||
attachmentId: 'attach123', | ||
queryParams: { | ||
messageId: 'message123', | ||
}, | ||
overrides: { | ||
apiUri: 'https://test.api.nylas.com', | ||
}, | ||
}); | ||
|
||
expect(apiClient.requestRaw).toHaveBeenCalledWith({ | ||
method: 'GET', | ||
path: '/v3/grants/id123/attachments/attach123/download', | ||
queryParams: { | ||
messageId: 'message123', | ||
}, | ||
overrides: { | ||
apiUri: 'https://test.api.nylas.com', | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |