-
Notifications
You must be signed in to change notification settings - Fork 118
/
messages.ts
339 lines (311 loc) · 8.86 KB
/
messages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { AsyncListResponse, Resource } from './resource.js';
import {
CleanMessagesRequest,
CleanMessagesResponse,
FindMessageQueryParams,
ListMessagesQueryParams,
Message,
ScheduledMessage,
ScheduledMessagesList,
StopScheduledMessageResponse,
UpdateMessageRequest,
} from '../models/messages.js';
import { Overrides } from '../config.js';
import {
NylasBaseResponse,
NylasListResponse,
NylasResponse,
} from '../models/response.js';
import {
CreateDraftRequest,
SendMessageRequest,
UpdateDraftRequest,
} from '../models/drafts.js';
import * as FormData from 'form-data';
import { encodeAttachmentStreams, objKeysToSnakeCase } from '../utils.js';
import { SmartCompose } from './smartCompose.js';
import APIClient, { RequestOptionsParams } from '../apiClient.js';
/**
* The parameters for the {@link Messages.list} method
* @property identifier The identifier of the grant to act upon
* @property queryParams The query parameters to include in the request
*/
export interface ListMessagesParams {
identifier: string;
queryParams?: ListMessagesQueryParams;
}
/**
* The parameters for the {@link Messages.find} method
* @property identifier The identifier of the grant to act upon
* @property messageId The id of the message to retrieve.
* @property queryParams The query parameters to include in the request
*/
export interface FindMessageParams {
identifier: string;
messageId: string;
queryParams?: FindMessageQueryParams;
}
/**
* The parameters for the {@link Messages.update} method
* @property identifier The identifier of the grant to act upon
* @property messageId The id of the message to update
* @property requestBody The values to update the message with
*/
export interface UpdateMessageParams {
identifier: string;
messageId: string;
requestBody: UpdateMessageRequest;
}
/**
* The parameters for the {@link Messages.destroy} method
* @property identifier The identifier of the grant to act upon
* @property messageId The id of the message to delete
*/
export interface DestroyMessageParams {
identifier: string;
messageId: string;
}
/**
* The parameters for the {@link Messages.send} method
* @property identifier The identifier of the grant to act upon
* @property requestBody The message to send
*/
export interface SendMessageParams {
identifier: string;
requestBody: SendMessageRequest;
}
/**
* The parameters for the {@link Messages.listScheduledMessages} method
* @property identifier The identifier of the grant to act upon
*/
export interface ListScheduledMessagesParams {
identifier: string;
}
/**
* The parameters for the {@link Messages.findScheduledMessage} method
* @property identifier The identifier of the grant to act upon
* @property scheduleId The id of the scheduled message to retrieve.
*/
export interface FindScheduledMessageParams {
identifier: string;
scheduleId: string;
}
/**
* The parameters for the {@link Messages.stopScheduledMessage} method
* @property identifier The identifier of the grant to act upon
* @property scheduleId The id of the scheduled message to destroy.
*/
export type StopScheduledMessageParams = FindScheduledMessageParams;
/**
* The parameters for the {@link Messages.cleanMessages} method
* @property identifier The identifier of the grant to act upon
* @property requestBody The values to clean the message with
*/
export interface CleanMessagesParams {
identifier: string;
requestBody: CleanMessagesRequest;
}
/**
* Nylas Messages API
*
* The Nylas Messages API allows you to list, find, update, delete, schedule, and send messages on user accounts.
*/
export class Messages extends Resource {
public smartCompose: SmartCompose;
// The maximum size of an attachment that can be sent using json
static MAXIMUM_JSON_ATTACHMENT_SIZE = 3 * 1024 * 1024;
constructor(apiClient: APIClient) {
super(apiClient);
this.smartCompose = new SmartCompose(apiClient);
}
/**
* Return all Messages
* @return A list of messages
*/
public list({
identifier,
queryParams,
overrides,
}: ListMessagesParams & Overrides): AsyncListResponse<
NylasListResponse<Message>
> {
return super._list<NylasListResponse<Message>>({
queryParams,
overrides,
path: `/v3/grants/${identifier}/messages`,
});
}
/**
* Return a Message
* @return The message
*/
public find({
identifier,
messageId,
overrides,
queryParams,
}: FindMessageParams & Overrides): Promise<NylasResponse<Message>> {
return super._find({
path: `/v3/grants/${identifier}/messages/${messageId}`,
overrides,
queryParams,
});
}
/**
* Update a Message
* @return The updated message
*/
public update({
identifier,
messageId,
requestBody,
overrides,
}: UpdateMessageParams & Overrides): Promise<NylasResponse<Message>> {
return super._update({
path: `/v3/grants/${identifier}/messages/${messageId}`,
requestBody,
overrides,
});
}
/**
* Delete a Message
* @return The deleted message
*/
public destroy({
identifier,
messageId,
overrides,
}: DestroyMessageParams & Overrides): Promise<NylasBaseResponse> {
return super._destroy({
path: `/v3/grants/${identifier}/messages/${messageId}`,
overrides,
});
}
/**
* Send an email
* @return The sent message
*/
public async send({
identifier,
requestBody,
overrides,
}: SendMessageParams & Overrides): Promise<NylasResponse<Message>> {
const path = `/v3/grants/${identifier}/messages/send`;
const requestOptions: RequestOptionsParams = {
method: 'POST',
path,
overrides,
};
// 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) {
requestOptions.form = Messages._buildFormRequest(requestBody);
} else {
if (requestBody.attachments) {
const processedAttachments = await encodeAttachmentStreams(
requestBody.attachments
);
requestOptions.body = {
...requestBody,
attachments: processedAttachments,
};
} else {
requestOptions.body = requestBody;
}
}
return this.apiClient.request(requestOptions);
}
/**
* Retrieve your scheduled messages
* @return A list of scheduled messages
*/
public listScheduledMessages({
identifier,
overrides,
}: ListScheduledMessagesParams & Overrides): Promise<
NylasResponse<ScheduledMessagesList>
> {
return super._find({
path: `/v3/grants/${identifier}/messages/schedules`,
overrides,
});
}
/**
* Retrieve a scheduled message
* @return The scheduled message
*/
public findScheduledMessage({
identifier,
scheduleId,
overrides,
}: FindScheduledMessageParams & Overrides): Promise<
NylasResponse<ScheduledMessage>
> {
return super._find({
path: `/v3/grants/${identifier}/messages/schedules/${scheduleId}`,
overrides,
});
}
/**
* Stop a scheduled message
* @return The confirmation of the stopped scheduled message
*/
public stopScheduledMessage({
identifier,
scheduleId,
overrides,
}: StopScheduledMessageParams & Overrides): Promise<
NylasResponse<StopScheduledMessageResponse>
> {
return super._destroy({
path: `/v3/grants/${identifier}/messages/schedules/${scheduleId}`,
overrides,
});
}
/**
* Remove extra information from a list of messages
* @return The list of cleaned messages
*/
public cleanMessages({
identifier,
requestBody,
overrides,
}: CleanMessagesParams & Overrides): Promise<
NylasListResponse<CleanMessagesResponse>
> {
return this.apiClient.request<NylasListResponse<CleanMessagesResponse>>({
method: 'PUT',
path: `/v3/grants/${identifier}/messages/clean`,
body: requestBody,
overrides,
});
}
static _buildFormRequest(
requestBody: CreateDraftRequest | UpdateDraftRequest | SendMessageRequest
): FormData {
let form: FormData;
// FormData imports are funky, cjs needs to use .default, es6 doesn't
if (typeof (FormData as any).default !== 'undefined') {
form = new (FormData as any).default();
} else {
form = new FormData();
}
// Split out the message payload from the attachments
const messagePayload = {
...requestBody,
attachments: undefined,
};
form.append('message', JSON.stringify(objKeysToSnakeCase(messagePayload)));
// Add a separate form field for each attachment
requestBody.attachments?.forEach((attachment, index) => {
form.append(`file${index}`, attachment.content, {
filename: attachment.filename,
contentType: attachment.contentType,
});
});
return form;
}
}