@beauraines/node-helpers 4.0.4 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +2 -2
- package/src/callAPI.js +0 -147
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [4.0.5](https://github.com/beauraines/node-helpers/compare/v4.0.4...v4.0.5) (2023-07-16)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* **deps:** bump sqlite from 4.2.1 to 5.0.1 ([#54](https://github.com/beauraines/node-helpers/issues/54)) ([1066aec](https://github.com/beauraines/node-helpers/commit/1066aec108fe771c268e76975fb0151bf5f1ef40))
|
|
11
|
+
|
|
5
12
|
### [4.0.4](https://github.com/beauraines/node-helpers/compare/v4.0.1...v4.0.4) (2023-07-16)
|
|
6
13
|
|
|
7
14
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@beauraines/node-helpers",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.5",
|
|
4
4
|
"description": "Collection of node helpers",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"dayjs": "^1.11.7",
|
|
20
20
|
"node-fetch": "^2.6.7",
|
|
21
21
|
"sparkly": "^5.0.0",
|
|
22
|
-
"sqlite": "^
|
|
22
|
+
"sqlite": "^5.0.1",
|
|
23
23
|
"sqlite3": "^5.1.2"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
package/src/callAPI.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
const fetch = require('node-fetch')
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
// TODO query string builder?
|
|
5
|
-
// Other than JSON responses?
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Makes a GET request
|
|
9
|
-
*
|
|
10
|
-
* @param {string} url URL for request
|
|
11
|
-
* @param {object} [headers] Headers object
|
|
12
|
-
* @returns {Promise} JSON response
|
|
13
|
-
* @throws {Error} if response is not okay
|
|
14
|
-
*/
|
|
15
|
-
const getRequest = async (url,headers) => {
|
|
16
|
-
const response = await fetch(url,{
|
|
17
|
-
method: 'GET',
|
|
18
|
-
headers,
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
let responseBody
|
|
22
|
-
if (response.ok) {
|
|
23
|
-
responseBody = await response.json()
|
|
24
|
-
} else {
|
|
25
|
-
throw new Error(response.statusText)
|
|
26
|
-
}
|
|
27
|
-
return responseBody
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Makes a GET request
|
|
32
|
-
*
|
|
33
|
-
* @param {string} url URL for request
|
|
34
|
-
* @param {object} [headers] Headers object
|
|
35
|
-
* @returns {Promise} Blob response
|
|
36
|
-
* @throws {Error} if response is not okay
|
|
37
|
-
*/
|
|
38
|
-
const getRequestBlob = async (url,headers) => {
|
|
39
|
-
const response = await fetch(url,{
|
|
40
|
-
method: 'GET',
|
|
41
|
-
headers
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
let responseBody
|
|
45
|
-
if (response.ok) {
|
|
46
|
-
responseBody = await response.blob()
|
|
47
|
-
} else {
|
|
48
|
-
throw new Error(response.statusText)
|
|
49
|
-
}
|
|
50
|
-
return responseBody
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Makes synchronous POST request to a specified URL using Bearer Token authorization and
|
|
55
|
-
* returns the JSON response
|
|
56
|
-
*
|
|
57
|
-
* @param {string} url
|
|
58
|
-
* @param {object} requestBody
|
|
59
|
-
* @param {string} token
|
|
60
|
-
* @param {object} [headers]
|
|
61
|
-
* @returns {object}
|
|
62
|
-
* @throws {Error} if the response is not OK
|
|
63
|
-
*/
|
|
64
|
-
const postRequest = async (url,requestBody,token,headers) => {
|
|
65
|
-
const response = await fetch(url,{
|
|
66
|
-
method: 'POST',
|
|
67
|
-
headers: {
|
|
68
|
-
'Authorization': 'Bearer ' + token,
|
|
69
|
-
'Content-Type': 'application/json',
|
|
70
|
-
... headers
|
|
71
|
-
},
|
|
72
|
-
body: JSON.stringify(requestBody)
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
let responseBody
|
|
76
|
-
if (response.ok) {
|
|
77
|
-
responseBody = await response.json()
|
|
78
|
-
} else {
|
|
79
|
-
throw new Error(response.statusText)
|
|
80
|
-
}
|
|
81
|
-
return responseBody
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
/**
|
|
85
|
-
* Makes synchronous POST request to a specified URL using Bearer Token authorization and
|
|
86
|
-
* returns the JSON response
|
|
87
|
-
*
|
|
88
|
-
* @param url {string}
|
|
89
|
-
* @param requestBody {object}
|
|
90
|
-
* @param token {string}
|
|
91
|
-
* @param [headers] {object}
|
|
92
|
-
* @returns {object}
|
|
93
|
-
* @throws {Error} if the response is not OK
|
|
94
|
-
*/
|
|
95
|
-
const postRequestPaginated = async (url,requestBody,token,headers) => {
|
|
96
|
-
let responseBody = await postRequest(url,requestBody,token,headers)
|
|
97
|
-
if (responseBody.totalCount < responseBody.pageSize) {
|
|
98
|
-
return responseBody
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
let requestBodies = []
|
|
102
|
-
// TODO make these pagination properties arguments
|
|
103
|
-
let pageSize = responseBody.pageSize
|
|
104
|
-
let total = responseBody.totalCount
|
|
105
|
-
let start = responseBody.start
|
|
106
|
-
let end = responseBody.end
|
|
107
|
-
let totalPages = 1
|
|
108
|
-
while (end < total) {
|
|
109
|
-
requestBodies.push(
|
|
110
|
-
{
|
|
111
|
-
... requestBody,
|
|
112
|
-
start: start + pageSize,
|
|
113
|
-
end: end + pageSize
|
|
114
|
-
}
|
|
115
|
-
)
|
|
116
|
-
totalPages++
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
let paginatedResponseBodyPromises = []
|
|
120
|
-
for (const requestBody of requestBodies) {
|
|
121
|
-
let paginatedResponseBody = postRequest(url,requestBody,token,headers)
|
|
122
|
-
paginatedResponseBodyPromises.concat(paginatedResponseBody)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
let paginatedResponseBodies = await Promise.all(paginatedResponseBodyPromises);
|
|
126
|
-
for ( const body of paginatedResponseBodies ) {
|
|
127
|
-
responseBody.data.concat(body.data)
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// TODO update the pagination properties to show all data returned
|
|
131
|
-
responseBody.start = 1,
|
|
132
|
-
responseBody.end = end
|
|
133
|
-
responseBody.totalPages = totalPages
|
|
134
|
-
responseBody.pageSize = 500
|
|
135
|
-
if (responseBody.totalCount != responseBody.data.length) {
|
|
136
|
-
throw new Error('Response data does not equql total records')
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
return responseBody
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
module.exports = {
|
|
143
|
-
getRequest,
|
|
144
|
-
getRequestBlob,
|
|
145
|
-
postRequest,
|
|
146
|
-
postRequestPaginated
|
|
147
|
-
}
|