core-services-sdk 1.3.4 → 1.3.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/package.json +1 -1
- package/src/http/http.js +111 -52
- package/src/index.js +8 -3
package/package.json
CHANGED
package/src/http/http.js
CHANGED
|
@@ -10,10 +10,22 @@ const JSON_HEADER = {
|
|
|
10
10
|
'Content-Type': 'application/json',
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
/**
|
|
14
|
+
* Checks if the HTTP status is considered successful (2xx).
|
|
15
|
+
*
|
|
16
|
+
* @param {Response} res - The fetch response object.
|
|
17
|
+
* @returns {boolean} `true` if status code is between 200-299.
|
|
18
|
+
*/
|
|
13
19
|
const isOkStatus = ({ status }) =>
|
|
14
|
-
// Range of response OK
|
|
15
20
|
status >= httpStatus.OK && status < httpStatus.MULTIPLE_CHOICES
|
|
16
21
|
|
|
22
|
+
/**
|
|
23
|
+
* Verifies response status and throws a structured HttpError if not OK.
|
|
24
|
+
*
|
|
25
|
+
* @param {Response} res - The fetch response object.
|
|
26
|
+
* @throws {HttpError} When the response status is not OK.
|
|
27
|
+
* @returns {Response} The response if status is OK.
|
|
28
|
+
*/
|
|
17
29
|
const checkStatus = async (res) => {
|
|
18
30
|
if (!isOkStatus(res)) {
|
|
19
31
|
const text = await res.text()
|
|
@@ -30,63 +42,95 @@ const checkStatus = async (res) => {
|
|
|
30
42
|
return res
|
|
31
43
|
}
|
|
32
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Reads the raw text from a fetch response.
|
|
47
|
+
*
|
|
48
|
+
* @param {Response} response - The fetch response.
|
|
49
|
+
* @returns {Promise<string>} The text body.
|
|
50
|
+
*/
|
|
33
51
|
const getTextResponse = async (response) => {
|
|
34
|
-
|
|
35
|
-
return text
|
|
52
|
+
return await response.text()
|
|
36
53
|
}
|
|
37
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Attempts to parse a JSON string.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} responseText - A raw JSON string.
|
|
59
|
+
* @returns {Object} Parsed JSON object.
|
|
60
|
+
* @throws {SyntaxError} If the string is not valid JSON.
|
|
61
|
+
*/
|
|
38
62
|
const tryConvertJsonResponse = (responseText) => {
|
|
39
63
|
try {
|
|
40
|
-
|
|
41
|
-
return obj
|
|
64
|
+
return JSON.parse(responseText)
|
|
42
65
|
} catch (error) {
|
|
43
66
|
error.responseText = responseText
|
|
44
67
|
throw error
|
|
45
68
|
}
|
|
46
69
|
}
|
|
47
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Attempts to extract a JSON object from a fetch response.
|
|
73
|
+
*
|
|
74
|
+
* @param {Response} response - The fetch response.
|
|
75
|
+
* @returns {Promise<Object|string>} Parsed object or raw string on failure.
|
|
76
|
+
*/
|
|
48
77
|
const tryGetJsonResponse = async (response) => {
|
|
49
78
|
let jsonText
|
|
50
79
|
try {
|
|
51
|
-
jsonText = getTextResponse(response)
|
|
52
|
-
|
|
53
|
-
return obj
|
|
80
|
+
jsonText = await getTextResponse(response)
|
|
81
|
+
return tryConvertJsonResponse(jsonText)
|
|
54
82
|
} catch (error) {
|
|
55
|
-
if (!jsonText)
|
|
56
|
-
throw error
|
|
57
|
-
}
|
|
83
|
+
if (!jsonText) throw error
|
|
58
84
|
return jsonText
|
|
59
85
|
}
|
|
60
86
|
}
|
|
61
87
|
|
|
88
|
+
/**
|
|
89
|
+
* Attempts to extract an XML object from a fetch response.
|
|
90
|
+
*
|
|
91
|
+
* @param {Response} response - The fetch response.
|
|
92
|
+
* @returns {Promise<Object|string>} Parsed XML object or raw string.
|
|
93
|
+
*/
|
|
62
94
|
const tryGetXmlResponse = async (response) => {
|
|
63
95
|
let xmlText
|
|
64
96
|
try {
|
|
65
97
|
xmlText = await getTextResponse(response)
|
|
66
|
-
|
|
67
|
-
return xml
|
|
98
|
+
return await parseStringPromise(xmlText)
|
|
68
99
|
} catch (error) {
|
|
69
|
-
if (!xmlText)
|
|
70
|
-
throw error
|
|
71
|
-
}
|
|
100
|
+
if (!xmlText) throw error
|
|
72
101
|
return xmlText
|
|
73
102
|
}
|
|
74
103
|
}
|
|
75
104
|
|
|
105
|
+
/**
|
|
106
|
+
* Extracts and parses the fetch response body based on expected type.
|
|
107
|
+
*
|
|
108
|
+
* @param {Response} response - The fetch response.
|
|
109
|
+
* @param {string} responseType - The expected response type ('json', 'xml', 'text').
|
|
110
|
+
* @returns {Promise<any>} The parsed response payload.
|
|
111
|
+
*/
|
|
76
112
|
const getResponsePayload = async (response, responseType) => {
|
|
77
113
|
switch (responseType) {
|
|
78
114
|
case ResponseType.json:
|
|
79
115
|
return tryGetJsonResponse(response)
|
|
80
|
-
|
|
81
116
|
case ResponseType.xml:
|
|
82
117
|
return tryGetXmlResponse(response)
|
|
83
|
-
|
|
84
118
|
default:
|
|
85
119
|
case ResponseType.text:
|
|
86
120
|
return getTextResponse(response)
|
|
87
121
|
}
|
|
88
122
|
}
|
|
89
123
|
|
|
124
|
+
/**
|
|
125
|
+
* Sends an HTTP GET request.
|
|
126
|
+
*
|
|
127
|
+
* @param {Object} params
|
|
128
|
+
* @param {string} params.url - The target URL.
|
|
129
|
+
* @param {Object} [params.headers] - Optional custom headers.
|
|
130
|
+
* @param {string} [params.credentials='include'] - Credential mode.
|
|
131
|
+
* @param {string} [params.expectedType='json'] - Response type.
|
|
132
|
+
* @returns {Promise<any>} Parsed response data.
|
|
133
|
+
*/
|
|
90
134
|
export const get = async ({
|
|
91
135
|
url,
|
|
92
136
|
headers = {},
|
|
@@ -95,18 +139,24 @@ export const get = async ({
|
|
|
95
139
|
}) => {
|
|
96
140
|
const response = await fetch(url, {
|
|
97
141
|
method: HTTP_METHODS.GET,
|
|
98
|
-
headers: {
|
|
99
|
-
...JSON_HEADER,
|
|
100
|
-
...headers,
|
|
101
|
-
},
|
|
142
|
+
headers: { ...JSON_HEADER, ...headers },
|
|
102
143
|
...(credentials ? { credentials } : {}),
|
|
103
144
|
})
|
|
104
|
-
|
|
105
145
|
await checkStatus(response)
|
|
106
|
-
|
|
107
|
-
return data
|
|
146
|
+
return await getResponsePayload(response, expectedType)
|
|
108
147
|
}
|
|
109
148
|
|
|
149
|
+
/**
|
|
150
|
+
* Sends an HTTP POST request.
|
|
151
|
+
*
|
|
152
|
+
* @param {Object} params
|
|
153
|
+
* @param {string} params.url - The target URL.
|
|
154
|
+
* @param {Object} params.body - Body data to send.
|
|
155
|
+
* @param {Object} [params.headers] - Optional custom headers.
|
|
156
|
+
* @param {string} [params.credentials='include'] - Credential mode.
|
|
157
|
+
* @param {string} [params.expectedType='json'] - Response type.
|
|
158
|
+
* @returns {Promise<any>} Parsed response data.
|
|
159
|
+
*/
|
|
110
160
|
export const post = async ({
|
|
111
161
|
url,
|
|
112
162
|
body,
|
|
@@ -116,18 +166,20 @@ export const post = async ({
|
|
|
116
166
|
}) => {
|
|
117
167
|
const response = await fetch(url, {
|
|
118
168
|
method: HTTP_METHODS.POST,
|
|
119
|
-
headers: {
|
|
120
|
-
...JSON_HEADER,
|
|
121
|
-
...headers,
|
|
122
|
-
},
|
|
169
|
+
headers: { ...JSON_HEADER, ...headers },
|
|
123
170
|
body: JSON.stringify(body),
|
|
124
171
|
...(credentials ? { credentials } : {}),
|
|
125
172
|
})
|
|
126
173
|
await checkStatus(response)
|
|
127
|
-
|
|
128
|
-
return data
|
|
174
|
+
return await getResponsePayload(response, expectedType)
|
|
129
175
|
}
|
|
130
176
|
|
|
177
|
+
/**
|
|
178
|
+
* Sends an HTTP PUT request.
|
|
179
|
+
*
|
|
180
|
+
* @param {Object} params - Same as `post`.
|
|
181
|
+
* @returns {Promise<any>} Parsed response data.
|
|
182
|
+
*/
|
|
131
183
|
export const put = async ({
|
|
132
184
|
url,
|
|
133
185
|
body,
|
|
@@ -137,19 +189,20 @@ export const put = async ({
|
|
|
137
189
|
}) => {
|
|
138
190
|
const response = await fetch(url, {
|
|
139
191
|
method: HTTP_METHODS.PUT,
|
|
140
|
-
headers: {
|
|
141
|
-
...JSON_HEADER,
|
|
142
|
-
...headers,
|
|
143
|
-
},
|
|
192
|
+
headers: { ...JSON_HEADER, ...headers },
|
|
144
193
|
body: JSON.stringify(body),
|
|
145
194
|
...(credentials ? { credentials } : {}),
|
|
146
195
|
})
|
|
147
|
-
|
|
148
196
|
await checkStatus(response)
|
|
149
|
-
|
|
150
|
-
return data
|
|
197
|
+
return await getResponsePayload(response, expectedType)
|
|
151
198
|
}
|
|
152
199
|
|
|
200
|
+
/**
|
|
201
|
+
* Sends an HTTP PATCH request.
|
|
202
|
+
*
|
|
203
|
+
* @param {Object} params - Same as `post`.
|
|
204
|
+
* @returns {Promise<any>} Parsed response data.
|
|
205
|
+
*/
|
|
153
206
|
export const patch = async ({
|
|
154
207
|
url,
|
|
155
208
|
body,
|
|
@@ -159,19 +212,20 @@ export const patch = async ({
|
|
|
159
212
|
}) => {
|
|
160
213
|
const response = await fetch(url, {
|
|
161
214
|
method: HTTP_METHODS.PATCH,
|
|
162
|
-
headers: {
|
|
163
|
-
...JSON_HEADER,
|
|
164
|
-
...headers,
|
|
165
|
-
},
|
|
215
|
+
headers: { ...JSON_HEADER, ...headers },
|
|
166
216
|
body: JSON.stringify(body),
|
|
167
217
|
...(credentials ? { credentials } : {}),
|
|
168
218
|
})
|
|
169
|
-
|
|
170
219
|
await checkStatus(response)
|
|
171
|
-
|
|
172
|
-
return data
|
|
220
|
+
return await getResponsePayload(response, expectedType)
|
|
173
221
|
}
|
|
174
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Sends an HTTP DELETE request.
|
|
225
|
+
*
|
|
226
|
+
* @param {Object} params - Same as `post`, but body is optional.
|
|
227
|
+
* @returns {Promise<any>} Parsed response data.
|
|
228
|
+
*/
|
|
175
229
|
export const deleteApi = async ({
|
|
176
230
|
url,
|
|
177
231
|
body,
|
|
@@ -181,19 +235,24 @@ export const deleteApi = async ({
|
|
|
181
235
|
}) => {
|
|
182
236
|
const response = await fetch(url, {
|
|
183
237
|
method: HTTP_METHODS.DELETE,
|
|
184
|
-
headers: {
|
|
185
|
-
...JSON_HEADER,
|
|
186
|
-
...headers,
|
|
187
|
-
},
|
|
238
|
+
headers: { ...JSON_HEADER, ...headers },
|
|
188
239
|
...(body ? { body: JSON.stringify(body) } : {}),
|
|
189
240
|
...(credentials ? { credentials } : {}),
|
|
190
241
|
})
|
|
191
|
-
|
|
192
242
|
await checkStatus(response)
|
|
193
|
-
|
|
194
|
-
return data
|
|
243
|
+
return await getResponsePayload(response, expectedType)
|
|
195
244
|
}
|
|
196
245
|
|
|
246
|
+
/**
|
|
247
|
+
* Consolidated HTTP client with method shortcuts.
|
|
248
|
+
*
|
|
249
|
+
* @namespace http
|
|
250
|
+
* @property {Function} get - HTTP GET method.
|
|
251
|
+
* @property {Function} post - HTTP POST method.
|
|
252
|
+
* @property {Function} put - HTTP PUT method.
|
|
253
|
+
* @property {Function} patch - HTTP PATCH method.
|
|
254
|
+
* @property {Function} deleteApi - HTTP DELETE method.
|
|
255
|
+
*/
|
|
197
256
|
export const http = {
|
|
198
257
|
get,
|
|
199
258
|
put,
|
package/src/index.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
export * from './core/index.js'
|
|
2
|
-
export * from './crypto/index.js'
|
|
3
|
-
export * from './logger/index.js'
|
|
4
1
|
export * from './fastify/index.js'
|
|
5
2
|
export * from './mongodb/index.js'
|
|
3
|
+
export * from './crypto/crypto.js'
|
|
6
4
|
export * from './rabbit-mq/index.js'
|
|
5
|
+
export * from './logger/get-logger.js'
|
|
6
|
+
export * from './core/regex-utils.js'
|
|
7
7
|
export * as http from './http/http.js'
|
|
8
8
|
export * from './http/responseType.js'
|
|
9
|
+
export * from './crypto/encryption.js'
|
|
10
|
+
export * from './core/otp-generators.js'
|
|
11
|
+
export * from './core/sanitize-objects.js'
|
|
12
|
+
export * from './core/normalize-to-array.js'
|
|
9
13
|
export { initMailer } from './mailer/index.js'
|
|
14
|
+
export * from './core/combine-unique-arrays.js'
|
|
10
15
|
export { HttpError } from './http/HttpError.js'
|
|
11
16
|
export { Mailer } from './mailer/mailer.service.js'
|
|
12
17
|
export { TransportFactory } from './mailer/transport.factory.js'
|