-
Notifications
You must be signed in to change notification settings - Fork 180
/
meetings.ts
780 lines (715 loc) · 23.6 KB
/
meetings.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
import { XMLParser } from 'fast-xml-parser';
import FormData from 'form-data';
import { AuthenticationType, Client } from '@vonage/server-client';
import { HTTPMethods, VetchError, VetchOptions } from '@vonage/vetch';
import pick from 'lodash.pick';
import {
MeetingRoomPageResponse,
MeetingRoomResponse,
RecordingResponse,
RecordingResponsePage,
ThemeResponse,
UrlResponse,
MeetingRoomParams,
MeetingRoom,
Recording,
DialInNumber,
Theme,
} from './types';
import { LogoType } from './enums';
import { existsSync, readFileSync } from 'fs';
type MeetingError = {
status: number;
name: string;
message: string;
errors: Array<{ code: string; message: string }>;
};
/**
* Converts an API response for a meeting room to the SDK format.
*
* @param {MeetingRoomResponse} room - The meeting room response from the API.
* @return {MeetingRoom} A meeting room object in the SDK format.
*/
const apiRoomToSdk = (room: MeetingRoomResponse): MeetingRoom => {
const links = room._links;
delete room._links;
const sdkRoom = {
...Client.transformers.camelCaseObjectKeys(room, true),
hostUrl: links?.host_url?.href,
guestUrl: links?.guest_url?.href,
};
return sdkRoom as MeetingRoom;
};
/**
* Converts an API response for a recording to the SDK format.
*
* @param {RecordingResponse} recording - The recording response from the API.
* @return {Recording} A recording object in the SDK format.
*/
const apiRecordingToSdk = (recording: RecordingResponse): Recording =>
({
...Client.transformers.camelCaseObjectKeys(recording),
url: recording?._links?.url?.href,
}) as Recording;
/**
* Client class to interact with the Meetings API to create and manage meeting rooms.
*
* @remarks
* This client is only available as a standalone client. It cannot be
* instantiated from the server-sdk package.
*
* @example
* Create a standalone Meetings Client
*
* ```ts
* import { Meetings } from '@vonage/meetings';
*
* const meetingsClient = new Meetings({
* apiKey: VONAGE_API_KEY,
* apiSecret: VONAGE_API_SECRET,
* applicationId: VONAGE_APPLICATION_ID,
* privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH
* });
* ```
*/
export class Meetings extends Client {
authType = AuthenticationType.JWT;
/**
* Used to keep the form boundary consistent when uploading files
*/
FORM_BOUNDARY = '-------------------------Vonage-Node_SDK';
/**
* List of properties from the room that can be written
*/
ROOM_WRITE_KEYS = [
'display_name',
'metadata',
'type',
'expires_at',
'recording_options.auto_record',
'recording_options.record_only_owner',
'expire_after_use',
'theme_id',
'join_approval_level',
'initial_join_options.microphone_state',
'callback_urls.rooms_callback_url',
'callback_urls.sessions_callback_url',
'callback_urls.recordings_callback_url',
'available_features.is_recording_available',
'available_features.is_chat_available',
'available_features.is_whiteboard_available',
];
/**
* List of properties from the theme that can be written
*/
THEME_WRITE_KEYS = [
'theme_name',
'main_color',
'brand_text',
'short_company_url',
];
/**
* Retrieves a list of meeting rooms until there are no more pages
*
* @param {MeetingRoomParams} params - Optional parameters for pagination.
* @return {AsyncGenerator<MeetingRoom, void, undefined>} An async generator of meeting rooms.
*
* @example
* Generate a list of meeting rooms
*
* ```ts
*
* for await (const room of meetingsClient.getRooms()) {
* console.log(`Room ${room.id} has ${room.participants} participants`);
* console.log(`Room ${room.id} has ${room.sessions} sessions`);
* console.log(`Room ${room.id} has ${room.recordings} recordings`);
* console.log(`Room ${room.id} has ${room.members} members`);
* }
* ```
*/
async *getRooms(
params: MeetingRoomParams = {},
): AsyncGenerator<MeetingRoom, void & MeetingRoom, undefined> {
let startId = params?.startId || null;
do {
if (startId) {
params.startId = startId;
}
const resp = await this.getRoomPage(params);
const rooms = (resp._embedded || []).map(apiRoomToSdk);
yield* rooms;
const nextLink = resp._links?.next?.href;
startId = null;
if (nextLink) {
const nextLinkUrl = new URL(nextLink);
startId = nextLinkUrl.searchParams.get('start_id');
}
} while (startId);
}
/**
* Retrieves a page of meeting rooms based on the provided parameters.
*
* @param {MeetingRoomParams} params - Optional parameters for pagination.
* @return {Promise<MeetingRoomPageResponse>} A promise that resolves to a page of meeting rooms.
*
* @example
* Get a page of meeting rooms
*
* ```ts
* const resp = await meetingsClient.getRoomPage();
* console.log(`There are ${resp.totalItems} meeting rooms`);
* console.log(`There are ${resp.pageSize} meeting rooms per page`);
* ```
*
* @example
* Get a specific page of meeting rooms
*
* ```ts
* const resp = await meetingsClient.getRoomPage({pageSize: 10, pageNumber: 2});
* console.log(`There are ${resp.totalItems} meeting rooms`);
* console.log(`There are ${resp.pageSize} meeting rooms per page`);
* ```
*
*/
async getRoomPage(
params: MeetingRoomParams = {},
): Promise<MeetingRoomPageResponse> {
const resp = await this.sendGetRequest<MeetingRoomPageResponse>(
`${this.config.meetingsHost}/v1/meetings/rooms`,
Client.transformers.snakeCaseObjectKeys(params),
);
return resp.data;
}
/**
* Retrieves a meeting room by its ID.
*
* @param {string} roomId - The ID of the meeting room to retrieve.
* @return {Promise<MeetingRoom>} A promise that resolves to the meeting room.
*
* @example
* Get a meeting room by ID
*
* ```ts
* const room = await meetingsClient.getRoom('my-room-id');
* console.log(`Room ${room.id} has ${room.participants} participants`);
* console.log(`Room ${room.id} has ${room.members} members`);
* console.log(`Room ${room.id} has ${room.sessions} sessions`);
* console.log(`Room ${room.id} has ${room.recordings} recordings`);
* ```
*/
async getRoom(roomId: string): Promise<MeetingRoom> {
const resp = await this.sendGetRequest<MeetingRoomResponse>(
`${this.config.meetingsHost}/v1/meetings/rooms/${roomId}`,
);
return apiRoomToSdk(resp.data);
}
/**
* Creates a new meeting room.
*
* @param {MeetingRoom} room - The meeting room object to create.
* @return {Promise<MeetingRoom>} A promise that resolves to the created meeting room.
*
* @example
* Create a new meeting room
*
* ```ts
* const room = await meetingsClient.createRoom({
* displayName: 'My Room',
* metadata: {
* my_data: 'my_value'
* }
* });
* console.log(`Created room with ID ${room.id}`);
* ```
*/
async createRoom(room: MeetingRoom): Promise<MeetingRoom> {
const resp = await this.sendPostRequest<MeetingRoomResponse>(
`${this.config.meetingsHost}/v1/meetings/rooms`,
pick(
Client.transformers.snakeCaseObjectKeys(room, true),
this.ROOM_WRITE_KEYS,
),
);
return apiRoomToSdk(resp.data);
}
/**
* Updates an existing meeting room.
*
* @param {string} roomId - The ID of the meeting room to update.
* @param {MeetingRoom} room - The meeting room object with updated information.
* @return {Promise<MeetingRoom>} A promise that resolves to the updated meeting room.
*
* @example
* Update a meeting room
*
* ```ts
* const room = await meetingsClient.updateRoom('my-room-id', {
* displayName: 'My Room',
* metadata: {
* my_data: 'my_value'
* }
* });
* console.log(`Updated room with ID ${room.id}`);
* ```
*/
async updateRoom(roomId: string, room: MeetingRoom): Promise<MeetingRoom> {
const resp = await this.sendPatchRequest<MeetingRoomResponse>(
`${this.config.meetingsHost}/v1/meetings/rooms/${roomId}`,
{
update_options: pick(
Client.transformers.snakeCaseObjectKeys(room, true),
this.ROOM_WRITE_KEYS,
),
},
);
return apiRoomToSdk(resp.data);
}
/**
* Retrieves a recording by its ID.
*
* @param {string} recordingId - The ID of the recording to retrieve.
* @return {Promise<Recording>} A promise that resolves to the recording.
*
* @example
* Get a recording by ID
*
* ```ts
* const recording = await meetingsClient.getRecording('my-recording-id');
* console.log(`Recording ${recording.id} started at ${recording.startedAt}`);
* console.log(`Recording ${recording.id} ended at ${recording.endedAt}`);
* ```
*/
async getRecording(recordingId: string): Promise<Recording> {
const resp = await this.sendGetRequest<RecordingResponse>(
`${this.config.meetingsHost}/v1/meetings/recordings/${recordingId}`,
);
return apiRecordingToSdk(resp.data);
}
/**
* Retrieves recordings associated with a session by its ID until there are no
* more recordings
*
* @remarks
* All the recordings will be returned in one request. There could be a large
* number of recordings.
*
* @param {string} sessionId - The ID of the session for which to retrieve recordings.
* @return {AsyncGenerator<Recording, void, undefined>} An async generator that yields recordings associated with the session.
*
* @example
* Get recordings for a session
*
* ```ts
* for await (const recording of meetingsClient.getSessionRecordings('my-session-id')) {
* console.log(`Recording ${recording.id} started at ${recording.startedAt}`);
* console.log(`Recording ${recording.id} ended at ${recording.endedAt}`);
* }
* ```
*/
async *getSessionRecordings(
sessionId: string,
): AsyncGenerator<Recording, void & Recording, undefined> {
const resp = await this.sendGetRequest<RecordingResponsePage>(
`${this.config.meetingsHost}/v1/meetings/sessions/${sessionId}`,
);
const recordings = (resp.data?._embedded?.recordings || []).map(
apiRecordingToSdk,
);
yield* recordings;
}
/**
* Deletes a recording by its ID.
*
* @param {string} recordingId - The ID of the recording to delete.
* @return {Promise<void>} A promise that resolves when the recording is successfully deleted.
*
* @example
* Delete a recording by ID
*
* ```ts
* await meetingsClient.deleteRecording('my-recording-id');
* console.log(`Recording with ID ${recordingId} has been deleted`);
* ```
*/
async deleteRecording(recordingId: string): Promise<void> {
await this.sendDeleteRequest(
`${this.config.meetingsHost}/v1/meetings/recordings/${recordingId}`,
);
}
/**
* Retrieves a list of dial-in numbers.
*
* @remarks
* All numbers will be returned in one request
*
* @return {AsyncGenerator<DialInNumber, void, undefined>} An asynchronous generator that yields dial-in numbers.
*
* @example
* Get a list of dial-in numbers
*
* ```ts
* for await (const number of meetingsClient.getDialInNumbers()) {
* console.log(`Dial-in number ${number.number} is in ${number.country}`);
* }
* ```
*/
async *getDialInNumbers(): AsyncGenerator<
DialInNumber,
void & DialInNumber,
undefined
> {
const resp = await this.sendGetRequest<DialInNumber[]>(
`${this.config.meetingsHost}/v1/meetings/dial-in-numbers`,
);
const numbers = (resp.data || []).map(
(number: DialInNumber): DialInNumber =>
Client.transformers.camelCaseObjectKeys(number, false) as DialInNumber,
);
yield* numbers;
}
/**
* Retrieves a list of themes.
*
* @remarks
* All themes are returned in one request
*
* @return {AsyncGenerator<Theme, void, undefined>} An asynchronous generator that yields themes.
*
* @example
* Get a list of getThemes
* ```ts
* for await (const theme of meetingsClient.getThemes()) {
* console.log(`Theme ${theme.themeName} has ID ${theme.id}`);
* }
* ```
*/
async *getThemes(): AsyncGenerator<Theme, void & Theme, undefined> {
const resp = await this.sendGetRequest<ThemeResponse[]>(
`${this.config.meetingsHost}/v1/meetings/themes`,
);
const themes = (resp.data || []).map(
(theme: ThemeResponse): Theme =>
Client.transformers.camelCaseObjectKeys(theme, false) as Theme,
);
yield* themes;
}
/**
* Retrieves a theme by its theme ID.
*
* @param {string} themeId - The ID of the theme to retrieve.
* @return {Promise<Theme>} A Promise that resolves to the retrieved theme.
*
* @example
* Get a theme by ID
*
* ```ts
* const theme = await meetingsClient.getTheme('my-theme-id');
* console.log(`Theme ${theme.themeName} has ID ${theme.id}`);
* ```
*/
async getTheme(themeId: string): Promise<Theme> {
const resp = await this.sendGetRequest<ThemeResponse>(
`${this.config.meetingsHost}/v1/meetings/themes/${themeId}`,
);
return Client.transformers.camelCaseObjectKeys(resp.data) as Theme;
}
/**
* Deletes a theme by its theme ID.
*
* @param {string} themeId - The ID of the theme to delete.
* @param {boolean} [force=false] - Whether to force the deletion even if it's associated with rooms.
* @return {Promise<void>} A Promise that resolves when the theme is successfully deleted.
*
* @example
* Delete a theme by ID
*
* ```ts
* await meetingsClient.deleteTheme('my-theme-id');
* console.log(`Theme with ID ${themeId} has been deleted`);
* ```
*/
async deleteTheme(themeId: string, force: boolean = false): Promise<void> {
await this.sendRequest({
url: `${this.config.meetingsHost}/v1/meetings/themes/${themeId}`,
method: HTTPMethods.DELETE,
...(force ? { params: { force: 'true' } } : {}),
} as VetchOptions);
}
/**
* Creates a new theme with the provided theme details.
*
* @param {Theme} theme - The theme details to create.
* @return {Promise<Theme>} A Promise that resolves with the created theme.
*
* @example
* Create a new themes
*
* ```ts
* const theme = await meetingsClient.createTheme({
* themeName: 'My Theme',
* mainColor: '#C0FFEE',
* brandText: 'My Brand',
* shortCompanyUrl: 'my-brand'
* });
* console.log(`Created theme with ID ${theme.id}`);
* ```
*/
async createTheme(theme: Theme): Promise<Theme> {
const resp = await this.sendPostRequest<ThemeResponse>(
`${this.config.meetingsHost}/v1/meetings/themes`,
pick(
Client.transformers.snakeCaseObjectKeys(theme, true),
this.THEME_WRITE_KEYS,
),
);
return Client.transformers.camelCaseObjectKeys(resp.data) as Theme;
}
/**
* Updates an existing theme with the provided theme details.
*
* @param {string} themeId - The ID of the theme to update.
* @param {Theme} theme - The updated theme details.
* @return {Promise<Theme>} A Promise that resolves with the updated theme.
*
* @example
* Update a theme
*
* ```ts
* const theme = await meetingsClient.updateTheme('my-theme-id', {
* themeName: 'My Theme',
* mainColor: '#C0FFEE',
* brandText: 'My Brand',
* shortCompanyUrl: 'my-brand'
* });
* console.log(`Updated theme with ID ${theme.id}`);
* ```
*/
async updateTheme(themeId: string, theme: Theme): Promise<Theme> {
const resp = await this.sendPatchRequest<ThemeResponse>(
`${this.config.meetingsHost}/v1/meetings/themes/${themeId}`,
{
update_details: pick(
Client.transformers.snakeCaseObjectKeys(theme, true),
this.THEME_WRITE_KEYS,
),
},
);
return Client.transformers.camelCaseObjectKeys(resp.data) as Theme;
}
/**
* Retrieves a list of meeting rooms associated with a specific theme. This will
* keep calling the API until there are no more pages
*
* @param {string} themeId - The ID of the theme for which to retrieve meeting rooms.
* @param {MeetingRoomParams} params - Optional parameters to filter and paginate the results.
* @return {AsyncGenerator<MeetingRoom, void, undefined>} An async generator that yields meeting rooms associated with the theme.
*
* @example
* Get meeting rooms for a theme
*
* ```ts
* for await (const room of meetingsClient.getRoomsForTheme('my-theme-id')) {
* console.log(`Room ${room.id} has ${room.participants} participants`);
* console.log(`Room ${room.id} has ${room.sessions} sessions`);
* console.log(`Room ${room.id} has ${room.recordings} recordings`);
* console.log(`Room ${room.id} has ${room.members} members`);
* }
* ```
*
*
*/
async *getRoomsForTheme(
themeId: string,
params: MeetingRoomParams = {},
): AsyncGenerator<MeetingRoom, void & MeetingRoom, undefined> {
let startId = params?.startId || null;
do {
if (startId) {
params.startId = startId;
}
const resp = await this.getRoomsForThemePage(themeId, params);
const rooms = (resp._embedded || []).map(apiRoomToSdk);
yield* rooms;
const nextLink = resp._links?.next?.href;
startId = null;
if (nextLink) {
const nextLinkUrl = new URL(nextLink);
startId = nextLinkUrl.searchParams.get('start_id');
}
} while (startId);
}
/**
* Retrieves a page of meeting rooms associated with a specific theme.
*
* @param {string} themeId - The ID of the theme for which to retrieve meeting rooms.
* @param {MeetingRoomParams} params - Optional parameters to filter and paginate the results.
* @return {Promise<MeetingRoomPageResponse>} A promise that resolves to a page of meeting rooms associated with the theme.
*
* @example
* Get a page of meeting rooms for a theme
*
* ```ts
* const resp = await meetingsClient.getRoomsForThemePage('my-theme-id');
* console.log(`There are ${resp.totalItems} meeting rooms`);
* console.log(`There are ${resp.pageSize} meeting rooms per page`);
* ```
*
* @example
* Get a specific page of meeting rooms for a theme
*
* ```ts
* const resp = await meetingsClient.getRoomsForThemePage('my-theme-id', {pageSize: 10, pageNumber: 2});
* console.log(`There are ${resp.totalItems} meeting rooms`);
* console.log(`There are ${resp.pageSize} meeting rooms per page`);
* ```
*/
async getRoomsForThemePage(
themeId: string,
params: MeetingRoomParams = {},
): Promise<MeetingRoomPageResponse> {
const resp = await this.sendGetRequest<MeetingRoomPageResponse>(
`${this.config.meetingsHost}/v1/meetings/themes/${themeId}/rooms`,
Client.transformers.snakeCaseObjectKeys(params),
);
return resp.data;
}
/**
* Sets the default theme for the application.
*
* @param {string} themeId - The ID of the theme to set as the default theme.
* @return {Promise<true>} A promise that resolves to `true` when the default theme is set successfully.
*
* @example
* Set the default theme
*
* ```ts
* await meetingsClient.setDefaultTheme('my-theme-id');
* console.log(`Default theme has been set`);
* ```
*/
async setDefaultTheme(themeId: string): Promise<true> {
await this.sendPatchRequest<ThemeResponse>(
`${this.config.meetingsHost}/v1/meetings/applications`,
{
update_details: {
default_theme_id: themeId,
},
},
);
return true;
}
/**
* Uploads an icon (logo) to a theme.
*
* @remarks
* To add icons and logos to a theme, they first need to be uploaded to the
* Meetings API AWS bucket, and then paired with the respective theme. You
* should also ensure that your Logos and Favicons adhere to the Image.
*
* @see Uploading Icons and Logos {@link https://developer.vonage.com/en/meetings/guides/upload-icons-logo}
*
* @param {string} themeId - The ID of the theme to which the icon will be attached.
* @param {LogoType} logo - The type of logo to upload (e.g., LogoType.WHITE, LogoType.COLORED).
* @param {string} logoFile - The file path of the logo to upload.
* @return {Promise<true>} A promise that resolves to `true` when the icon is uploaded and attached to the theme successfully.
* @throws {Error} If the file specified by `logoFile` does not exist or if the upload fails.
*
* @example
* Upload an icon to a theme
*
* ```ts
* await meetingsClient.uploadIcon('my-theme-id', LogoType.WHITE, '/path/to/white-logo.png');
* console.log(`Icon has been uploaded`);
* ```
*/
async uploadIcon(
themeId: string,
logo: LogoType,
logoFile: string,
): Promise<true> {
if (!existsSync(logoFile)) {
throw new Error(`File ${logoFile} does not exist`);
}
const urlResponse = await this._getIconUploadUrl(logo);
await this._uploadToAws(urlResponse, logoFile);
let message = 'FATAL ERROR';
try {
await this.sendPutRequest<ThemeResponse>(
`${this.config.meetingsHost}/v1/meetings/themes/${themeId}/finalizeLogos`,
{
keys: [urlResponse.fields.key],
},
);
return true;
} catch (apiError: VetchError | unknown) {
if (apiError instanceof Error) {
message = apiError.message;
}
if (apiError instanceof VetchError) {
const errorJson = (await apiError?.response?.json()) as MeetingError;
const errors = errorJson?.errors || [];
message = errors[0]?.code || 'FATAL ERROR';
}
}
throw new Error(`Could not attach image to theme: ${message}`);
}
/**
* Uploads a file to an AWS S3 bucket using the provided URL and fields.
*
* @param {UrlResponse} urlResponse - The URL and fields required for the AWS S3 upload.
* @param {string} logoFile - The file path of the file to upload.
* @return {Promise<true>} A promise that resolves to `true` when the file is successfully uploaded to AWS S3.
* @throws {Error} If the upload fails or encounters an error.
*/
protected async _uploadToAws(
{ url, fields }: UrlResponse,
logoFile: string,
): Promise<true> {
const awsForm = new FormData();
awsForm.setBoundary(this.FORM_BOUNDARY);
for (const [key, value] of Object.entries(fields)) {
awsForm.append(key, value);
}
awsForm.append('file', readFileSync(logoFile));
let message;
try {
await this.sendRequest({
url: url,
data: awsForm.toString(),
method: HTTPMethods.POST,
} as VetchOptions);
return true;
} catch (apiError: VetchError | unknown) {
if (apiError instanceof Error) {
message = apiError.message;
}
if (apiError instanceof VetchError) {
const errorResponse
= ((await apiError?.response?.text()) as string) || '';
const parser = new XMLParser();
const output = parser.parse(errorResponse);
message = output?.Error?.Message;
}
}
throw new Error(`Failed to upload to AWS: ${message}`);
}
/**
* Retrieves the URL for uploading an icon (logo) to the specified LogoType.
*
* @param {LogoType} logo - The LogoType for which the upload URL is requested.
* @return {Promise<UrlResponse>} A promise that resolves to the URLResponse containing the upload URL and fields.
* @throws {Error} If the upload URL retrieval fails or encounters an error.
*/
protected async _getIconUploadUrl(logo: LogoType): Promise<UrlResponse> {
const resp = await this.sendGetRequest<UrlResponse[]>(
`${this.config.meetingsHost}/v1/meetings/themes/logos-upload-urls`,
);
const matchingUrl = (resp.data || []).filter(
({ fields }) => fields.logoType === logo,
);
if (!matchingUrl) {
throw new Error(`Cannot find url to upload ${logo}`);
}
return matchingUrl[0];
}
}