-
Notifications
You must be signed in to change notification settings - Fork 180
/
proactiveConnect.ts
303 lines (275 loc) · 10.7 KB
/
proactiveConnect.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
import { AuthenticationType, Client } from '@vonage/server-client';
import { HTTPMethods, VetchOptions } from '@vonage/vetch';
import {
FindListItemParams,
FindListParams,
ImportFileResponse,
List,
ListItem,
ListItemPageResponse,
ListItemResponse,
ListPageResponse,
ListResponse,
} from './types';
import pick from 'lodash.pick';
import { readFileSync, writeFileSync } from 'fs';
import FormData from 'form-data';
const apiListToList = (list: ListResponse): List => {
delete list?._links;
return Client.transformers.camelCaseObjectKeys(list, true) as List;
};
const apiItemToItem = <T>(item: ListItemResponse<T>): ListItem<T> => {
delete item?._links;
// do not deep transform as the data property has to remain the same
return Client.transformers.camelCaseObjectKeys(item) as ListItem<T>;
};
/**
* Represents an API client for interacting with the Vonage Proactive Connect
* API. This client provides methods for managing lists, list items, and
* performing import/export operations.
*/
export class ProactiveConnect extends Client {
public FORM_BOUNDARY = '-------------------------Vonage-Node_SDK';
protected authType = AuthenticationType.JWT;
public static LIST_WRITE_KEYS = [
'name',
'description',
'tags',
'attributes',
'datasource',
];
/**
* Retrieves all lists available in the Vonage Proactive Connect API.
*
* @param {FindListParams} params - Optional parameters for pagination and filtering.
* @return {AsyncGenerator<List, void, undefined>} An async generator that yields lists as they are retrieved.
*
* @throws {Error} If there's an issue with the API request.
*/
async *findAllLists(
params: FindListParams = {},
): AsyncGenerator<List, void & List, undefined> {
let next = null;
params.page = params?.page || 1;
do {
const resp = await this.sendGetRequest<ListPageResponse>(
`${this.config.proactiveHost}/v0.1/bulk/lists`,
Client.transformers.snakeCaseObjectKeys(params, true),
);
yield* resp?.data?._embedded?.lists.map(apiListToList);
next = resp?.data?._links?.next;
params.page++;
} while (next);
}
/**
* Creates a new list in the Vonage Proactive Connect API.
*
* @param {List} list - The list object representing the list to be created.
* @return {Promise<List>} A promise that resolves to the newly created list.
*
* @throws {Error} If there's an issue with the API request or the list creation fails.
*/
async createList(list: List): Promise<List> {
const resp = await this.sendPostRequest<ListResponse>(
`${this.config.proactiveHost}/v0.1/bulk/lists`,
pick(
Client.transformers.snakeCaseObjectKeys(list, true),
ProactiveConnect.LIST_WRITE_KEYS,
),
);
return apiListToList(resp.data);
}
/**
* Retrieves a list by its unique identifier from the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list to retrieve.
* @return {Promise<List>} A promise that resolves to the retrieved list.
*
* @throws {Error} If there's an issue with the API request or the list retrieval fails.
*/
async getListById(listId: string): Promise<List> {
const resp = await this.sendGetRequest<ListResponse>(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}`,
);
return apiListToList(resp.data);
}
async updateList(listId: string, list: List): Promise<List> {
const resp = await this.sendPutRequest<ListResponse>(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}`,
pick(
Client.transformers.snakeCaseObjectKeys(list, true),
ProactiveConnect.LIST_WRITE_KEYS,
),
);
return apiListToList(resp.data);
}
/**
* Updates an existing list in the Vonage Proactive Connect API by its unique identifier.
*
* @param {string} listId - The unique identifier of the list to update.
* @return {Promise<List>} A promise that resolves to the updated list.
*
* @throws {Error} If there's an issue with the API request or the list update fails.
*/
async deleteList(listId: string): Promise<true> {
await this.sendDeleteRequest(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}`,
);
return true;
}
/**
* Clears all data from an existing list in the Vonage Proactive Connect API by its unique identifier.
*
* @param {string} listId - The unique identifier of the list to clear.
* @return {Promise<true>} A promise that resolves to `true` if the list is successfully cleared.
*
* @throws {Error} If there's an issue with the API request or the list clearing fails.
*/
async clearList(listId: string): Promise<true> {
await this.sendPostRequest(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/clear`,
);
return true;
}
/**
* Retrieves all list items from a specific list in the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list to retrieve items from.
* @param {FindListItemParams} params - Optional parameters for pagination and filtering.
* @return {AsyncGenerator<ListItem, void, undefined>} An async generator that yields list items as they are retrieved.
*
* @throws {Error} If there's an issue with the API request.
*/
async *findAllListItems<T>(
listId: string,
params: FindListItemParams = {},
): AsyncGenerator<ListItem<T>, void & ListItem<T>, undefined> {
let next = null;
params.page = params?.page || 1;
do {
const resp = await this.sendGetRequest<ListItemPageResponse<T>>(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/items`,
Client.transformers.snakeCaseObjectKeys(params, true),
);
yield* resp?.data?._embedded?.items.map(apiItemToItem<T>);
next = resp?.data?._links?.next;
params.page++;
} while (next);
}
/**
* Creates a new list item in a specific list of the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list to add the item to.
* @param {ListItem} item - The list item object representing the item to be created.
* @return {Promise<ListItem>} A promise that resolves to the newly created list item.
*
* @throws {Error} If there's an issue with the API request or the list item creation fails.
*/
async createListItem<T>(
listId: string,
item: ListItem<T>,
): Promise<ListItem<T>> {
const resp = await this.sendPostRequest<ListItemResponse<T>>(
`${this.config.proactiveHost}/v0.1/bulk/lists`,
{
data: item.data,
},
);
return apiItemToItem<T>(resp.data);
}
/**
* Retrieves a specific list item by its unique identifier from a list in the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list containing the item.
* @param {string} itemId - The unique identifier of the list item to retrieve.
* @return {Promise<ListItem>} A promise that resolves to the retrieved list item.
*
* @throws {Error} If there's an issue with the API request or the list item retrieval fails.
*/
async getListItemById<T>(
listId: string,
itemId: string,
): Promise<ListItem<T>> {
const resp = await this.sendGetRequest<ListItemResponse<T>>(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/items/${itemId}`,
);
return apiItemToItem<T>(resp.data);
}
/**
* Updates an existing list item in a specific list of the Vonage Proactive Connect API by its unique identifier.
*
* @param {string} listId - The unique identifier of the list containing the item.
* @param {string} itemId - The unique identifier of the list item to update.
* @param {ListItem} item - The list item object representing the updated item.
* @return {Promise<ListItem>} A promise that resolves to the updated list item.
*
* @throws {Error} If there's an issue with the API request or the list item update fails.
*/
async updateListItem<T>(
listId: string,
itemId: string,
item: ListItem<T>,
): Promise<ListItem<T>> {
const resp = await this.sendPutRequest<ListItemResponse<T>>(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/items/${itemId}`,
{
data: item.data,
},
);
return apiItemToItem<T>(resp.data);
}
/**
* Deletes a specific list item by its unique identifier from a list in the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list containing the item to delete.
* @param {string} itemId - The unique identifier of the list item to delete.
* @return {Promise<true>} A promise that resolves to `true` if the list item is successfully deleted.
*
* @throws {Error} If there's an issue with the API request or the list item deletion fails.
*/
async deleteListItem(listId: string, itemId: string): Promise<true> {
await this.sendDeleteRequest(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/items/${itemId}`,
);
return true;
}
/**
* Downloads a file containing all list items from a specific list in the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list to download items from.
* @param {string} file - The path to the file where the list items will be saved.
* @return {Promise<true>} A promise that resolves to `true` if the list items are successfully downloaded and saved to the file.
*
* @throws {Error} If there's an issue with the API request or the file download fails.
*/
async downloadListItems(listId: string, file: string): Promise<true> {
const resp = await this.sendGetRequest<string>(
`${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/items/download`,
);
writeFileSync(file, resp.data);
return true;
}
/**
* Imports list items from a CSV file into a specific list in the Vonage Proactive Connect API.
*
* @param {string} listId - The unique identifier of the list to import items into.
* @param {string} file - The path to the CSV file containing the list items to import.
* @return {Promise<ImportFileResponse>} A promise that resolves to an import response containing details on the number of items inserted, updated, and deleted.
*
* @throws {Error} If there's an issue with the API request or the import process fails.
*/
async importListItems(
listId: string,
file: string,
): Promise<ImportFileResponse> {
const itemsForm = new FormData();
itemsForm.setBoundary(this.FORM_BOUNDARY);
itemsForm.append('file', readFileSync(file));
const resp = await this.sendRequest<ImportFileResponse>({
url: `${this.config.proactiveHost}/v0.1/bulk/lists/${listId}/items/download`,
data: itemsForm.toString(),
method: HTTPMethods.POST,
} as VetchOptions);
return resp.data;
}
}