-
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.
Add support for attachments api (#505)
* Attachments api * Added download method for returning stream * lint * PR Review request updates
- Loading branch information
1 parent
38f075c
commit f9f18e5
Showing
7 changed files
with
345 additions
and
39 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,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; |
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,100 @@ | ||
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. | ||
*/ | ||
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, | ||
}); | ||
} | ||
|
||
/** | ||
* Download the attachment data | ||
* | ||
* This method returns a NodeJS.ReadableStream which can be used to stream the attachment data. | ||
* This is particularly useful for handling large attachments efficiently, as it avoids loading | ||
* the entire file into memory. The stream can be piped to a file stream or used in any other way | ||
* that Node.js streams are typically used. | ||
* | ||
* @param identifier Grant ID or email account to query | ||
* @param attachmentId The id of the attachment to download. | ||
* @param queryParams The query parameters to include in the request | ||
* @returns {NodeJS.ReadableStream} The ReadableStream containing the file data. | ||
*/ | ||
public download({ | ||
identifier, | ||
attachmentId, | ||
queryParams, | ||
overrides, | ||
}: DownloadAttachmentParams & Overrides): Promise<NodeJS.ReadableStream> { | ||
return this._getStream({ | ||
path: `/v3/grants/${identifier}/attachments/${attachmentId}/download`, | ||
queryParams, | ||
overrides, | ||
}); | ||
} | ||
|
||
/** | ||
* Download the attachment as a byte array | ||
* @param identifier Grant ID or email account to query | ||
* @param attachmentId The id of the attachment to download. | ||
* @param queryParams The query parameters to include in the request | ||
* @return The raw file data | ||
*/ | ||
public downloadBytes({ | ||
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
Oops, something went wrong.