-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.ts
70 lines (59 loc) · 2.21 KB
/
list.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
import { getPrintFormat, initSmsServiceWithServicePlanId, printFullResponse } from '../../config';
import { Sms, PageResult } from '@sinch/sdk-core';
const populateBatchesList = (
batchListPage: PageResult<Sms.SendSMSResponse>,
fullBatchesList: Sms.SendSMSResponse[],
batchesList: string[],
) => {
fullBatchesList.push(...batchListPage.data);
batchListPage.data.map((batch) => {
batchesList.push(`Batch ID: ${batch.id} - Type: ${batch.type} - From: ${batch.from}`);
});
};
(async () => {
console.log('***************');
console.log('* ListBatches *');
console.log('***************');
const oneWeekAgo = new Date();
oneWeekAgo.setDate(oneWeekAgo.getDate() - 7);
oneWeekAgo.setHours(0, 0, 0, 0);
const requestData: Sms.ListBatchesRequestData= {
start_date: oneWeekAgo,
page_size: 2,
};
const smsService = initSmsServiceWithServicePlanId();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await smsService.batches.list(requestData);
const fullBatchesList: Sms.SendSMSResponse[] = [];
const batchesList: string[] = [];
// Loop on all the pages to get all the batches
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateBatchesList(response, fullBatchesList, batchesList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(batchesList.length > 0
? 'List of batches: ' + JSON.stringify(batchesList, null, 2)
: 'Sorry, no batches were found.');
} else {
printFullResponse(fullBatchesList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const batch of smsService.batches.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`Batch ID: ${batch.id} - Type: ${batch.type} - From: ${batch.from}`);
} else {
console.log(batch);
}
}
})();