nylas
Version:
A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.
109 lines (108 loc) • 4.56 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Attachments = void 0;
const utils_js_1 = require("../utils.js");
const resource_js_1 = require("./resource.js");
const node_stream_1 = require("node:stream");
/**
* Nylas Attachments API
*
* The Nylas Attachments API allows you to retrieve metadata and download attachments.
*/
class Attachments extends resource_js_1.Resource {
/**
* Returns an attachment by ID.
* @return The Attachment metadata
*/
find({ identifier, attachmentId, queryParams, overrides, }) {
return super._find({
path: (0, utils_js_1.makePathParams)('/v3/grants/{identifier}/attachments/{attachmentId}', { identifier, attachmentId }),
queryParams,
overrides,
});
}
/**
* Download the attachment data
*
* This method returns a Web 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. In Node.js, convert it with Readable.fromWeb() when a
* NodeJS.ReadableStream is required.
*
* @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 {ReadableStream<Uint8Array>} The ReadableStream containing the file data.
*/
download({ identifier, attachmentId, queryParams, overrides, }) {
return this._getStream({
path: (0, utils_js_1.makePathParams)('/v3/grants/{identifier}/attachments/{attachmentId}/download', { identifier, attachmentId }),
queryParams,
overrides,
});
}
/**
* Download the attachment data as a Node.js readable stream.
*
* This is a Node.js convenience wrapper around {@link Attachments.download}. Use
* {@link Attachments.download} directly in Fetch-native runtimes, such as Cloudflare Workers,
* where Web ReadableStreams are the standard stream primitive.
*
* @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 Node.js readable stream containing the file data.
*/
async downloadNodeStream({ identifier, attachmentId, queryParams, overrides, }) {
const stream = await this.download({
identifier,
attachmentId,
queryParams,
overrides,
});
return node_stream_1.Readable.fromWeb(stream);
}
/**
* 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
*/
downloadBytes({ identifier, attachmentId, queryParams, overrides, }) {
return super._getRaw({
path: (0, utils_js_1.makePathParams)('/v3/grants/{identifier}/attachments/{attachmentId}/download', { identifier, attachmentId }),
queryParams,
overrides,
});
}
/**
* Create a resumable upload session for a large attachment (up to 150 MB).
* Upload bytes to the returned `url`, then call {@link Attachments.completeUploadSession}.
*
* @see https://developer.nylas.com/docs/v3/email/send-large-attachments/
* @return Session details including `attachmentId` and pre-signed `url`
*/
createUploadSession({ identifier, requestBody, overrides, }) {
return super._create({
path: (0, utils_js_1.makePathParams)('/v3/grants/{identifier}/attachment-uploads', {
identifier,
}),
requestBody,
overrides,
});
}
/**
* Complete an upload session after the file has been uploaded to the pre-signed URL.
*
* @see https://developer.nylas.com/docs/v3/email/send-large-attachments/
*/
completeUploadSession({ identifier, attachmentId, overrides, }) {
return super._create({
path: (0, utils_js_1.makePathParams)('/v3/grants/{identifier}/attachment-uploads/{attachmentId}/complete', { identifier, attachmentId }),
requestBody: {},
overrides,
});
}
}
exports.Attachments = Attachments;