-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlist.ts
101 lines (89 loc) · 2.97 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
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
import { Fax, PageResult } from '@sinch/sdk-core';
import { getPrintFormat, initFaxService, printFullResponse } from '../../config';
const populateFaxesList = (
faxPage: PageResult<Fax.Fax>,
fullFaxesList: Fax.Fax[],
faxesList: string[],
) => {
fullFaxesList.push(...faxPage.data);
faxPage.data.map((fax) => {
faxesList.push(`Fax ID: '${fax.id}' - Created at: '${fax.createTime}' - Status: '${fax.status}'`);
});
};
(async () => {
console.log('************');
console.log('* getFaxes *');
console.log('************');
const today = new Date();
const previousMonth = (today.getUTCMonth() - 1) % 12;
const lastMonth = new Date().setMonth(previousMonth);
// Example of createTime filter
// - fetch last month's faxes (with Date objects)
// => Output = ?createTime>=2024-04-24T11:33:44Z&createTime<=2024-05-24T11:33:44Z
// createTimeFilter: {
// from: new Date(lastMonth),
// to: new Date(),
// }
// - fetch last month's faxes (with DateFormat objets)
// => Output = ?createTime>=2024-04-24&createTime<=2024-05-24
// createTimeFilter: {
// from: {
// date: new Date(lastMonth),
// unit: 'day',
// },
// to: {
// date: new Date(),
// unit: 'day',
// },
// }
const requestData: Fax.ListFaxesRequestData = {
pageSize: 10,
createTimeRange: {
from: {
date: new Date(lastMonth),
unit: 'day',
},
to: {
date: new Date(),
unit: 'day',
},
},
};
const faxService = initFaxService();
// ----------------------------------------------
// Method 1: Fetch the data page by page manually
// ----------------------------------------------
let response = await faxService.faxes.list(requestData);
// Init data structure to hold the response content
const fullFaxesList: Fax.Fax[] = [];
// Init data structure to hold the response content for pretty print
const faxesList: string[] = [];
// Loop on all the pages to get all the faxes
let reachedEndOfPages = false;
while (!reachedEndOfPages) {
populateFaxesList(response, fullFaxesList, faxesList);
if (response.hasNextPage) {
response = await response.nextPage();
} else {
reachedEndOfPages = true;
}
}
const printFormat = getPrintFormat(process.argv);
if (printFormat === 'pretty') {
console.log(faxesList.length > 0
? `List of faxes:\n${faxesList.join('\n')}`
: 'Sorry, no faxes were found');
} else {
printFullResponse(fullFaxesList);
}
// ---------------------------------------------------------------------
// Method 2: Use the iterator and fetch data on more pages automatically
// ---------------------------------------------------------------------
for await (const fax of faxService.faxes.list(requestData)) {
if (printFormat === 'pretty') {
console.log(`Fax ID: '${fax.id}' - Created at: '${fax.createTime}' - Status: '${fax.status}'`);
} else {
console.log(fax);
}
}
})();