-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.ts
66 lines (55 loc) · 2.12 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
import { Conversation, PageResult } from '@sinch/sdk-core';
import { getPrintFormat, initConversationService, printFullResponse } from '../../config';
const populateContactsList = (
contactPage: PageResult<Conversation.Contact>,
contactList: Conversation.Contact[],
contactDetailsList: string[],
) => {
contactPage.data?.map((contact: Conversation.Contact) => {
contactList.push(contact);
contactDetailsList.push(`${contact.id} - ${contact.display_name}`);
});
};
(async () => {
console.log('************************');
console.log('* Contact_ListContacts *');
console.log('************************');
const requestData: Conversation.ListContactsRequestData = {
page_size: 2,
};
const conversationService = initConversationService();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await conversationService.contact.list(requestData);
const contactList: Conversation.Contact[] = [];
const contactDetailsList: string[] = [];
// Loop on all the pages to get all the active numbers
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateContactsList(response, contactList, contactDetailsList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(contactDetailsList.length > 0
? 'List of contacts:\n' + contactDetailsList.join('\n')
: 'Sorry, no contacts were found.');
} else {
printFullResponse(contactList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const contact of conversationService.contact.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`${contact.id} - ${contact.display_name}`);
} else {
console.log(contact);
}
}
})();