UNPKG

nylas

Version:

A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.

128 lines (127 loc) 3.97 kB
import { Messages } from './messages.js'; import { Resource } from './resource.js'; import { encodeAttachmentStreams, calculateTotalPayloadSize, } from '../utils.js'; import { makePathParams } from '../utils.js'; /** * Nylas Drafts API * * The Nylas Drafts API allows you to list, find, update, delete, and send drafts on user accounts. */ export class Drafts extends Resource { /** * Return all Drafts * @return A list of drafts */ list({ identifier, queryParams, overrides, }) { return super._list({ queryParams, overrides, path: makePathParams('/v3/grants/{identifier}/drafts', { identifier }), }); } /** * Return a Draft * @return The draft */ find({ identifier, draftId, overrides, }) { return super._find({ path: makePathParams('/v3/grants/{identifier}/drafts/{draftId}', { identifier, draftId, }), overrides, }); } /** * Return a Draft * @return The draft */ async create({ identifier, requestBody, overrides, }) { const path = makePathParams('/v3/grants/{identifier}/drafts', { identifier, }); // Use form data if the total payload size (body + attachments) is greater than 3mb const totalPayloadSize = calculateTotalPayloadSize(requestBody); if (totalPayloadSize >= Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) { const form = Messages._buildFormRequest(requestBody); return this.apiClient.request({ method: 'POST', path, form, overrides, }); } else if (requestBody.attachments) { const processedAttachments = await encodeAttachmentStreams(requestBody.attachments); requestBody = { ...requestBody, attachments: processedAttachments, }; } return super._create({ path, requestBody, overrides, }); } /** * Update a Draft * @return The updated draft */ async update({ identifier, draftId, requestBody, overrides, }) { const path = makePathParams('/v3/grants/{identifier}/drafts/{draftId}', { identifier, draftId, }); // Use form data if the total payload size (body + attachments) is greater than 3mb const totalPayloadSize = calculateTotalPayloadSize(requestBody); if (totalPayloadSize >= Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) { const form = Messages._buildFormRequest(requestBody); return this.apiClient.request({ method: 'PUT', path, form, overrides, }); } else if (requestBody.attachments) { const processedAttachments = await encodeAttachmentStreams(requestBody.attachments); requestBody = { ...requestBody, attachments: processedAttachments, }; } return super._update({ path, requestBody, overrides, }); } /** * Delete a Draft * @return The deleted draft */ destroy({ identifier, draftId, overrides, }) { return super._destroy({ path: makePathParams('/v3/grants/{identifier}/drafts/{draftId}', { identifier, draftId, }), overrides, }); } /** * Send a Draft * @return The sent message */ send({ identifier, draftId, overrides, }) { return super._create({ path: makePathParams('/v3/grants/{identifier}/drafts/{draftId}', { identifier, draftId, }), requestBody: {}, overrides, }); } }