nylas 7.3.0 → 7.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Drafts = void 0;
4
4
  const messages_js_1 = require("./messages.js");
5
5
  const resource_js_1 = require("./resource.js");
6
+ const utils_js_1 = require("../utils.js");
6
7
  /**
7
8
  * Nylas Drafts API
8
9
  *
@@ -34,11 +35,11 @@ class Drafts extends resource_js_1.Resource {
34
35
  * Return a Draft
35
36
  * @return The draft
36
37
  */
37
- create({ identifier, requestBody, overrides, }) {
38
+ async create({ identifier, requestBody, overrides, }) {
38
39
  const path = `/v3/grants/${identifier}/drafts`;
39
40
  // Use form data only if the attachment size is greater than 3mb
40
- const attachmentSize = requestBody.attachments?.reduce(function (_, attachment) {
41
- return attachment.size || 0;
41
+ const attachmentSize = requestBody.attachments?.reduce((total, attachment) => {
42
+ return total + (attachment.size || 0);
42
43
  }, 0) || 0;
43
44
  if (attachmentSize >= messages_js_1.Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) {
44
45
  const form = messages_js_1.Messages._buildFormRequest(requestBody);
@@ -49,6 +50,13 @@ class Drafts extends resource_js_1.Resource {
49
50
  overrides,
50
51
  });
51
52
  }
53
+ else if (requestBody.attachments) {
54
+ const processedAttachments = await (0, utils_js_1.encodeAttachmentStreams)(requestBody.attachments);
55
+ requestBody = {
56
+ ...requestBody,
57
+ attachments: processedAttachments,
58
+ };
59
+ }
52
60
  return super._create({
53
61
  path,
54
62
  requestBody,
@@ -59,11 +67,11 @@ class Drafts extends resource_js_1.Resource {
59
67
  * Update a Draft
60
68
  * @return The updated draft
61
69
  */
62
- update({ identifier, draftId, requestBody, overrides, }) {
70
+ async update({ identifier, draftId, requestBody, overrides, }) {
63
71
  const path = `/v3/grants/${identifier}/drafts/${draftId}`;
64
72
  // Use form data only if the attachment size is greater than 3mb
65
- const attachmentSize = requestBody.attachments?.reduce(function (_, attachment) {
66
- return attachment.size || 0;
73
+ const attachmentSize = requestBody.attachments?.reduce((total, attachment) => {
74
+ return total + (attachment.size || 0);
67
75
  }, 0) || 0;
68
76
  if (attachmentSize >= messages_js_1.Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) {
69
77
  const form = messages_js_1.Messages._buildFormRequest(requestBody);
@@ -74,6 +82,13 @@ class Drafts extends resource_js_1.Resource {
74
82
  overrides,
75
83
  });
76
84
  }
85
+ else if (requestBody.attachments) {
86
+ const processedAttachments = await (0, utils_js_1.encodeAttachmentStreams)(requestBody.attachments);
87
+ requestBody = {
88
+ ...requestBody,
89
+ attachments: processedAttachments,
90
+ };
91
+ }
77
92
  return super._update({
78
93
  path,
79
94
  requestBody,
@@ -62,7 +62,7 @@ class Messages extends resource_js_1.Resource {
62
62
  * Send an email
63
63
  * @return The sent message
64
64
  */
65
- send({ identifier, requestBody, overrides, }) {
65
+ async send({ identifier, requestBody, overrides, }) {
66
66
  const path = `/v3/grants/${identifier}/messages/send`;
67
67
  const requestOptions = {
68
68
  method: 'POST',
@@ -70,14 +70,23 @@ class Messages extends resource_js_1.Resource {
70
70
  overrides,
71
71
  };
72
72
  // Use form data only if the attachment size is greater than 3mb
73
- const attachmentSize = requestBody.attachments?.reduce(function (_, attachment) {
74
- return attachment.size || 0;
73
+ const attachmentSize = requestBody.attachments?.reduce((total, attachment) => {
74
+ return total + (attachment.size || 0);
75
75
  }, 0) || 0;
76
76
  if (attachmentSize >= Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) {
77
77
  requestOptions.form = Messages._buildFormRequest(requestBody);
78
78
  }
79
79
  else {
80
- requestOptions.body = requestBody;
80
+ if (requestBody.attachments) {
81
+ const processedAttachments = await (0, utils_js_1.encodeAttachmentStreams)(requestBody.attachments);
82
+ requestOptions.body = {
83
+ ...requestBody,
84
+ attachments: processedAttachments,
85
+ };
86
+ }
87
+ else {
88
+ requestOptions.body = requestBody;
89
+ }
81
90
  }
82
91
  return this.apiClient.request(requestOptions);
83
92
  }
@@ -111,6 +120,18 @@ class Messages extends resource_js_1.Resource {
111
120
  overrides,
112
121
  });
113
122
  }
123
+ /**
124
+ * Remove extra information from a list of messages
125
+ * @return The list of cleaned messages
126
+ */
127
+ cleanMessages({ identifier, requestBody, overrides, }) {
128
+ return this.apiClient.request({
129
+ method: 'PUT',
130
+ path: `/v3/grants/${identifier}/messages/clean`,
131
+ body: requestBody,
132
+ overrides,
133
+ });
134
+ }
114
135
  static _buildFormRequest(requestBody) {
115
136
  let form;
116
137
  // FormData imports are funky, cjs needs to use .default, es6 doesn't
package/lib/cjs/utils.js CHANGED
@@ -1,10 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.objKeysToSnakeCase = exports.objKeysToCamelCase = exports.createFileRequestBuilder = void 0;
3
+ exports.objKeysToSnakeCase = exports.objKeysToCamelCase = exports.encodeAttachmentStreams = exports.createFileRequestBuilder = void 0;
4
4
  const change_case_1 = require("change-case");
5
5
  const fs = require("fs");
6
6
  const path = require("path");
7
7
  const mime = require("mime-types");
8
+ const stream_1 = require("stream");
8
9
  function createFileRequestBuilder(filePath) {
9
10
  const stats = fs.statSync(filePath);
10
11
  const filename = path.basename(filePath);
@@ -18,6 +19,40 @@ function createFileRequestBuilder(filePath) {
18
19
  };
19
20
  }
20
21
  exports.createFileRequestBuilder = createFileRequestBuilder;
22
+ /**
23
+ * Converts a ReadableStream to a base64 encoded string.
24
+ * @param stream The ReadableStream containing the binary data.
25
+ * @returns The stream base64 encoded to a string.
26
+ */
27
+ function streamToBase64(stream) {
28
+ return new Promise((resolve, reject) => {
29
+ const chunks = [];
30
+ stream.on('data', (chunk) => {
31
+ chunks.push(chunk);
32
+ });
33
+ stream.on('end', () => {
34
+ const base64 = Buffer.concat(chunks).toString('base64');
35
+ resolve(base64);
36
+ });
37
+ stream.on('error', err => {
38
+ reject(err);
39
+ });
40
+ });
41
+ }
42
+ /**
43
+ * Encodes the content of each attachment stream to base64.
44
+ * @param attachments The attachments to encode.
45
+ * @returns The attachments with their content encoded to base64.
46
+ */
47
+ async function encodeAttachmentStreams(attachments) {
48
+ return await Promise.all(attachments.map(async (attachment) => {
49
+ const base64EncodedContent = attachment.content instanceof stream_1.Readable
50
+ ? await streamToBase64(attachment.content)
51
+ : attachment.content;
52
+ return { ...attachment, content: base64EncodedContent }; // Replace the stream with its base64 string
53
+ }));
54
+ }
55
+ exports.encodeAttachmentStreams = encodeAttachmentStreams;
21
56
  /**
22
57
  * Applies the casing function and ensures numeric parts are preceded by underscores in snake_case.
23
58
  * @param casingFunction The original casing function.
@@ -2,4 +2,4 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.SDK_VERSION = void 0;
4
4
  // This file is generated by scripts/exportVersion.js
5
- exports.SDK_VERSION = '7.3.0';
5
+ exports.SDK_VERSION = '7.4.0';
@@ -1,5 +1,6 @@
1
1
  import { Messages } from './messages.js';
2
2
  import { Resource } from './resource.js';
3
+ import { encodeAttachmentStreams } from '../utils.js';
3
4
  /**
4
5
  * Nylas Drafts API
5
6
  *
@@ -31,11 +32,11 @@ export class Drafts extends Resource {
31
32
  * Return a Draft
32
33
  * @return The draft
33
34
  */
34
- create({ identifier, requestBody, overrides, }) {
35
+ async create({ identifier, requestBody, overrides, }) {
35
36
  const path = `/v3/grants/${identifier}/drafts`;
36
37
  // Use form data only if the attachment size is greater than 3mb
37
- const attachmentSize = requestBody.attachments?.reduce(function (_, attachment) {
38
- return attachment.size || 0;
38
+ const attachmentSize = requestBody.attachments?.reduce((total, attachment) => {
39
+ return total + (attachment.size || 0);
39
40
  }, 0) || 0;
40
41
  if (attachmentSize >= Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) {
41
42
  const form = Messages._buildFormRequest(requestBody);
@@ -46,6 +47,13 @@ export class Drafts extends Resource {
46
47
  overrides,
47
48
  });
48
49
  }
50
+ else if (requestBody.attachments) {
51
+ const processedAttachments = await encodeAttachmentStreams(requestBody.attachments);
52
+ requestBody = {
53
+ ...requestBody,
54
+ attachments: processedAttachments,
55
+ };
56
+ }
49
57
  return super._create({
50
58
  path,
51
59
  requestBody,
@@ -56,11 +64,11 @@ export class Drafts extends Resource {
56
64
  * Update a Draft
57
65
  * @return The updated draft
58
66
  */
59
- update({ identifier, draftId, requestBody, overrides, }) {
67
+ async update({ identifier, draftId, requestBody, overrides, }) {
60
68
  const path = `/v3/grants/${identifier}/drafts/${draftId}`;
61
69
  // Use form data only if the attachment size is greater than 3mb
62
- const attachmentSize = requestBody.attachments?.reduce(function (_, attachment) {
63
- return attachment.size || 0;
70
+ const attachmentSize = requestBody.attachments?.reduce((total, attachment) => {
71
+ return total + (attachment.size || 0);
64
72
  }, 0) || 0;
65
73
  if (attachmentSize >= Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) {
66
74
  const form = Messages._buildFormRequest(requestBody);
@@ -71,6 +79,13 @@ export class Drafts extends Resource {
71
79
  overrides,
72
80
  });
73
81
  }
82
+ else if (requestBody.attachments) {
83
+ const processedAttachments = await encodeAttachmentStreams(requestBody.attachments);
84
+ requestBody = {
85
+ ...requestBody,
86
+ attachments: processedAttachments,
87
+ };
88
+ }
74
89
  return super._update({
75
90
  path,
76
91
  requestBody,
@@ -1,6 +1,6 @@
1
1
  import { Resource } from './resource.js';
2
2
  import * as FormData from 'form-data';
3
- import { objKeysToSnakeCase } from '../utils.js';
3
+ import { encodeAttachmentStreams, objKeysToSnakeCase } from '../utils.js';
4
4
  import { SmartCompose } from './smartCompose.js';
5
5
  /**
6
6
  * Nylas Messages API
@@ -59,7 +59,7 @@ export class Messages extends Resource {
59
59
  * Send an email
60
60
  * @return The sent message
61
61
  */
62
- send({ identifier, requestBody, overrides, }) {
62
+ async send({ identifier, requestBody, overrides, }) {
63
63
  const path = `/v3/grants/${identifier}/messages/send`;
64
64
  const requestOptions = {
65
65
  method: 'POST',
@@ -67,14 +67,23 @@ export class Messages extends Resource {
67
67
  overrides,
68
68
  };
69
69
  // Use form data only if the attachment size is greater than 3mb
70
- const attachmentSize = requestBody.attachments?.reduce(function (_, attachment) {
71
- return attachment.size || 0;
70
+ const attachmentSize = requestBody.attachments?.reduce((total, attachment) => {
71
+ return total + (attachment.size || 0);
72
72
  }, 0) || 0;
73
73
  if (attachmentSize >= Messages.MAXIMUM_JSON_ATTACHMENT_SIZE) {
74
74
  requestOptions.form = Messages._buildFormRequest(requestBody);
75
75
  }
76
76
  else {
77
- requestOptions.body = requestBody;
77
+ if (requestBody.attachments) {
78
+ const processedAttachments = await encodeAttachmentStreams(requestBody.attachments);
79
+ requestOptions.body = {
80
+ ...requestBody,
81
+ attachments: processedAttachments,
82
+ };
83
+ }
84
+ else {
85
+ requestOptions.body = requestBody;
86
+ }
78
87
  }
79
88
  return this.apiClient.request(requestOptions);
80
89
  }
@@ -108,6 +117,18 @@ export class Messages extends Resource {
108
117
  overrides,
109
118
  });
110
119
  }
120
+ /**
121
+ * Remove extra information from a list of messages
122
+ * @return The list of cleaned messages
123
+ */
124
+ cleanMessages({ identifier, requestBody, overrides, }) {
125
+ return this.apiClient.request({
126
+ method: 'PUT',
127
+ path: `/v3/grants/${identifier}/messages/clean`,
128
+ body: requestBody,
129
+ overrides,
130
+ });
131
+ }
111
132
  static _buildFormRequest(requestBody) {
112
133
  let form;
113
134
  // FormData imports are funky, cjs needs to use .default, es6 doesn't
package/lib/esm/utils.js CHANGED
@@ -2,6 +2,7 @@ import { camelCase, snakeCase } from 'change-case';
2
2
  import * as fs from 'fs';
3
3
  import * as path from 'path';
4
4
  import * as mime from 'mime-types';
5
+ import { Readable } from 'stream';
5
6
  export function createFileRequestBuilder(filePath) {
6
7
  const stats = fs.statSync(filePath);
7
8
  const filename = path.basename(filePath);
@@ -14,6 +15,39 @@ export function createFileRequestBuilder(filePath) {
14
15
  size: stats.size,
15
16
  };
16
17
  }
18
+ /**
19
+ * Converts a ReadableStream to a base64 encoded string.
20
+ * @param stream The ReadableStream containing the binary data.
21
+ * @returns The stream base64 encoded to a string.
22
+ */
23
+ function streamToBase64(stream) {
24
+ return new Promise((resolve, reject) => {
25
+ const chunks = [];
26
+ stream.on('data', (chunk) => {
27
+ chunks.push(chunk);
28
+ });
29
+ stream.on('end', () => {
30
+ const base64 = Buffer.concat(chunks).toString('base64');
31
+ resolve(base64);
32
+ });
33
+ stream.on('error', err => {
34
+ reject(err);
35
+ });
36
+ });
37
+ }
38
+ /**
39
+ * Encodes the content of each attachment stream to base64.
40
+ * @param attachments The attachments to encode.
41
+ * @returns The attachments with their content encoded to base64.
42
+ */
43
+ export async function encodeAttachmentStreams(attachments) {
44
+ return await Promise.all(attachments.map(async (attachment) => {
45
+ const base64EncodedContent = attachment.content instanceof Readable
46
+ ? await streamToBase64(attachment.content)
47
+ : attachment.content;
48
+ return { ...attachment, content: base64EncodedContent }; // Replace the stream with its base64 string
49
+ }));
50
+ }
17
51
  /**
18
52
  * Applies the casing function and ensures numeric parts are preceded by underscores in snake_case.
19
53
  * @param casingFunction The original casing function.
@@ -1,2 +1,2 @@
1
1
  // This file is generated by scripts/exportVersion.js
2
- export const SDK_VERSION = '7.3.0';
2
+ export const SDK_VERSION = '7.4.0';
@@ -34,8 +34,9 @@ interface BaseAttachment {
34
34
  export interface CreateAttachmentRequest extends BaseAttachment {
35
35
  /**
36
36
  * Content of the attachment.
37
+ * It can either be a readable stream or a base64 encoded string.
37
38
  */
38
- content: NodeJS.ReadableStream;
39
+ content: NodeJS.ReadableStream | string;
39
40
  }
40
41
  /**
41
42
  * Interface of an attachment object from Nylas.
@@ -158,6 +158,10 @@ export interface CodeExchangeResponse {
158
158
  * Currently always Bearer.
159
159
  */
160
160
  tokenType?: string;
161
+ /**
162
+ * The provider that the code was exchanged with.
163
+ */
164
+ provider?: Provider;
161
165
  }
162
166
  /**
163
167
  * Interface representing the object used to set parameters for detecting a provider.
@@ -273,3 +273,41 @@ export interface FindMessageQueryParams {
273
273
  */
274
274
  fields?: MessageFields;
275
275
  }
276
+ /**
277
+ * Interface representing the request to clean a message.
278
+ */
279
+ export interface CleanMessagesRequest {
280
+ /**
281
+ * IDs of the email messages to clean.
282
+ */
283
+ messageId: string[];
284
+ /**
285
+ * If true, removes link-related tags (<a>) from the email message while keeping the text.
286
+ */
287
+ ignoreLinks?: boolean;
288
+ /**
289
+ * If true, removes images from the email message.
290
+ */
291
+ ignoreImages?: boolean;
292
+ /**
293
+ * If true, converts images in the email message to Markdown.
294
+ */
295
+ imagesAsMarkdown?: boolean;
296
+ /**
297
+ * If true, removes table-related tags (<table>, <th>, <td>, <tr>) from the email message while keeping rows.
298
+ */
299
+ ignoreTables?: boolean;
300
+ /**
301
+ * If true, removes phrases such as "Best" and "Regards" in the email message signature.
302
+ */
303
+ removeConclusionPhrases?: boolean;
304
+ }
305
+ /**
306
+ * Interface representing the response after cleaning a message.
307
+ */
308
+ export interface CleanMessagesResponse extends Message {
309
+ /**
310
+ * The cleaned HTML message body.
311
+ */
312
+ conversation: string;
313
+ }
@@ -1,5 +1,5 @@
1
1
  import { AsyncListResponse, Resource } from './resource.js';
2
- import { FindMessageQueryParams, ListMessagesQueryParams, Message, ScheduledMessage, ScheduledMessagesList, StopScheduledMessageResponse, UpdateMessageRequest } from '../models/messages.js';
2
+ import { CleanMessagesRequest, CleanMessagesResponse, FindMessageQueryParams, ListMessagesQueryParams, Message, ScheduledMessage, ScheduledMessagesList, StopScheduledMessageResponse, UpdateMessageRequest } from '../models/messages.js';
3
3
  import { Overrides } from '../config.js';
4
4
  import { NylasBaseResponse, NylasListResponse, NylasResponse } from '../models/response.js';
5
5
  import { CreateDraftRequest, SendMessageRequest, UpdateDraftRequest } from '../models/drafts.js';
@@ -77,6 +77,15 @@ export interface FindScheduledMessageParams {
77
77
  * @property scheduleId The id of the scheduled message to destroy.
78
78
  */
79
79
  export type StopScheduledMessageParams = FindScheduledMessageParams;
80
+ /**
81
+ * The parameters for the {@link Messages.cleanMessages} method
82
+ * @property identifier The identifier of the grant to act upon
83
+ * @property requestBody The values to clean the message with
84
+ */
85
+ export interface CleanMessagesParams {
86
+ identifier: string;
87
+ requestBody: CleanMessagesRequest;
88
+ }
80
89
  /**
81
90
  * Nylas Messages API
82
91
  *
@@ -126,5 +135,10 @@ export declare class Messages extends Resource {
126
135
  * @return The confirmation of the stopped scheduled message
127
136
  */
128
137
  stopScheduledMessage({ identifier, scheduleId, overrides, }: StopScheduledMessageParams & Overrides): Promise<NylasResponse<StopScheduledMessageResponse>>;
138
+ /**
139
+ * Remove extra information from a list of messages
140
+ * @return The list of cleaned messages
141
+ */
142
+ cleanMessages({ identifier, requestBody, overrides, }: CleanMessagesParams & Overrides): Promise<NylasListResponse<CleanMessagesResponse>>;
129
143
  static _buildFormRequest(requestBody: CreateDraftRequest | UpdateDraftRequest | SendMessageRequest): FormData;
130
144
  }
@@ -1,5 +1,11 @@
1
1
  import { CreateAttachmentRequest } from './models/attachments.js';
2
2
  export declare function createFileRequestBuilder(filePath: string): CreateAttachmentRequest;
3
+ /**
4
+ * Encodes the content of each attachment stream to base64.
5
+ * @param attachments The attachments to encode.
6
+ * @returns The attachments with their content encoded to base64.
7
+ */
8
+ export declare function encodeAttachmentStreams(attachments: CreateAttachmentRequest[]): Promise<CreateAttachmentRequest[]>;
3
9
  /**
4
10
  * A utility function that recursively converts all keys in an object to camelCase.
5
11
  * @param obj The object to convert
@@ -1 +1 @@
1
- export declare const SDK_VERSION = "7.3.0";
1
+ export declare const SDK_VERSION = "7.4.0";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nylas",
3
- "version": "7.3.0",
3
+ "version": "7.4.0",
4
4
  "description": "A NodeJS wrapper for the Nylas REST API for email, contacts, and calendar.",
5
5
  "main": "lib/cjs/nylas.js",
6
6
  "types": "lib/types/nylas.d.ts",