-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.ts
71 lines (58 loc) · 2.38 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, initNumbersService, printFullResponse } from '../../config';
import { Numbers, PageResult } from '@sinch/sdk-core';
const populateActiveNumbersList = (
activeNumbersPage: PageResult<Numbers.ActiveNumber>,
activeNumbersList: Numbers.ActiveNumber[],
phoneNumbersList: (string | undefined)[],
) => {
activeNumbersPage.data?.map((activeNumber: Numbers.ActiveNumber) => {
activeNumbersList.push(activeNumber);
phoneNumbersList.push(`${activeNumber.phoneNumber} - Voice Configuration: ${activeNumber.voiceConfiguration?.type}`);
});
};
(async () => {
console.log('***********************************');
console.log('* NumberService_ListActiveNumbers *');
console.log('***********************************');
const requestData: Numbers.ListActiveNumbersRequestData = {
regionCode: 'US',
type: 'LOCAL',
capability: 'SMS',
pageSize: 2,
};
const numbersService = initNumbersService();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await numbersService.list(requestData);
const activeNumbersList: Numbers.ActiveNumber[] = [];
const phoneNumbersList: (string | undefined)[] = [];
// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateActiveNumbersList(response, activeNumbersList, phoneNumbersList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(phoneNumbersList.length > 0
? 'List of active numbers: ' + JSON.stringify(phoneNumbersList, null, 2)
: 'Sorry, no active numbers were found.');
} else {
printFullResponse(activeNumbersList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const activeNumber of numbersService.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${activeNumber.phoneNumber} - Voice Configuration: ${activeNumber.voiceConfiguration?.type}`);
} else {
console.log(activeNumber);
}
}
})();