UNPKG

nylas

Version:

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

117 lines (116 loc) 3.62 kB
import { Messages } from './messages.js'; import { Resource } from './resource.js'; import { encodeAttachmentStreams } 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: `/v3/grants/${identifier}/drafts`, }); } /** * Return a Draft * @return The draft */ find({ identifier, draftId, overrides, }) { return super._find({ path: `/v3/grants/${identifier}/drafts/${draftId}`, overrides, }); } /** * Return a Draft * @return The draft */ async create({ identifier, requestBody, overrides, }) { const path = `/v3/grants/${identifier}/drafts`; // Use form data only if the attachment size is greater than 3mb const attachmentSize = requestBody.attachments?.reduce((total, attachment) => { return total + (attachment.size || 0); }, 0) || 0; if (attachmentSize >= 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 = `/v3/grants/${identifier}/drafts/${draftId}`; // Use form data only if the attachment size is greater than 3mb const attachmentSize = requestBody.attachments?.reduce((total, attachment) => { return total + (attachment.size || 0); }, 0) || 0; if (attachmentSize >= 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: `/v3/grants/${identifier}/drafts/${draftId}`, overrides, }); } /** * Send a Draft * @return The sent message */ send({ identifier, draftId, overrides, }) { return super._create({ path: `/v3/grants/${identifier}/drafts/${draftId}`, requestBody: {}, overrides, }); } }