@map-colonies/mc-utils 2.0.2 → 3.1.0
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/README.md +67 -67
- package/dist/arrays/index.d.ts +1 -1
- package/dist/arrays/index.d.ts.map +0 -0
- package/dist/arrays/index.js +17 -17
- package/dist/arrays/index.js.map +0 -0
- package/dist/arrays/subGroups.d.ts +8 -8
- package/dist/arrays/subGroups.d.ts.map +0 -0
- package/dist/arrays/subGroups.js +33 -138
- package/dist/arrays/subGroups.js.map +1 -1
- package/dist/communication/http/httpClient.d.ts +29 -29
- package/dist/communication/http/httpClient.d.ts.map +0 -0
- package/dist/communication/http/httpClient.js +317 -434
- package/dist/communication/http/httpClient.js.map +1 -1
- package/dist/communication/http/index.d.ts +1 -1
- package/dist/communication/http/index.d.ts.map +0 -0
- package/dist/communication/http/index.js +17 -17
- package/dist/communication/http/index.js.map +0 -0
- package/dist/communication/index.d.ts +1 -1
- package/dist/communication/index.d.ts.map +0 -0
- package/dist/communication/index.js +17 -17
- package/dist/communication/index.js.map +0 -0
- package/dist/dateTime/getUTCDate.d.ts +1 -1
- package/dist/dateTime/getUTCDate.d.ts.map +0 -0
- package/dist/dateTime/getUTCDate.js +10 -10
- package/dist/dateTime/getUTCDate.js.map +1 -1
- package/dist/dateTime/index.d.ts +1 -1
- package/dist/dateTime/index.d.ts.map +0 -0
- package/dist/dateTime/index.js +17 -17
- package/dist/dateTime/index.js.map +0 -0
- package/dist/geo/bboxUtils.d.ts +23 -23
- package/dist/geo/bboxUtils.d.ts.map +0 -0
- package/dist/geo/bboxUtils.js +89 -89
- package/dist/geo/bboxUtils.js.map +1 -1
- package/dist/geo/geoConvertor.d.ts +18 -18
- package/dist/geo/geoConvertor.d.ts.map +0 -0
- package/dist/geo/geoConvertor.js +49 -51
- package/dist/geo/geoConvertor.js.map +1 -1
- package/dist/geo/geoHash.d.ts +24 -24
- package/dist/geo/geoHash.d.ts.map +1 -1
- package/dist/geo/geoHash.js +134 -246
- package/dist/geo/geoHash.js.map +1 -1
- package/dist/geo/geoIntersection.d.ts +19 -19
- package/dist/geo/geoIntersection.d.ts.map +0 -0
- package/dist/geo/geoIntersection.js +70 -70
- package/dist/geo/geoIntersection.js.map +1 -1
- package/dist/geo/index.d.ts +8 -8
- package/dist/geo/index.d.ts.map +0 -0
- package/dist/geo/index.js +24 -24
- package/dist/geo/index.js.map +0 -0
- package/dist/geo/tileBatcher.d.ts +8 -8
- package/dist/geo/tileBatcher.d.ts.map +1 -1
- package/dist/geo/tileBatcher.js +97 -167
- package/dist/geo/tileBatcher.js.map +1 -1
- package/dist/geo/tileRanger.d.ts +46 -46
- package/dist/geo/tileRanger.d.ts.map +1 -1
- package/dist/geo/tileRanger.js +232 -327
- package/dist/geo/tileRanger.js.map +1 -1
- package/dist/geo/tiles.d.ts +82 -82
- package/dist/geo/tiles.d.ts.map +0 -0
- package/dist/geo/tiles.js +219 -246
- package/dist/geo/tiles.js.map +1 -1
- package/dist/geo/tilesGenerator.d.ts +2 -2
- package/dist/geo/tilesGenerator.d.ts.map +1 -1
- package/dist/geo/tilesGenerator.js +17 -94
- package/dist/geo/tilesGenerator.js.map +1 -1
- package/dist/index.d.ts +6 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -21
- package/dist/index.js.map +1 -1
- package/dist/models/enums/gdal/dataType.d.ts +16 -16
- package/dist/models/enums/gdal/dataType.d.ts.map +0 -0
- package/dist/models/enums/gdal/dataType.js +20 -20
- package/dist/models/enums/gdal/dataType.js.map +0 -0
- package/dist/models/enums/gdal/index.d.ts +2 -2
- package/dist/models/enums/gdal/index.d.ts.map +0 -0
- package/dist/models/enums/gdal/index.js +18 -18
- package/dist/models/enums/gdal/index.js.map +0 -0
- package/dist/models/enums/gdal/resamplingMethod.d.ts +16 -16
- package/dist/models/enums/gdal/resamplingMethod.d.ts.map +0 -0
- package/dist/models/enums/gdal/resamplingMethod.js +20 -20
- package/dist/models/enums/gdal/resamplingMethod.js.map +0 -0
- package/dist/models/enums/geo/index.d.ts +1 -1
- package/dist/models/enums/geo/index.d.ts.map +0 -0
- package/dist/models/enums/geo/index.js +17 -17
- package/dist/models/enums/geo/index.js.map +0 -0
- package/dist/models/enums/geo/tileOrigin.d.ts +7 -7
- package/dist/models/enums/geo/tileOrigin.d.ts.map +0 -0
- package/dist/models/enums/geo/tileOrigin.js +11 -11
- package/dist/models/enums/geo/tileOrigin.js.map +0 -0
- package/dist/models/enums/index.d.ts +2 -2
- package/dist/models/enums/index.d.ts.map +0 -0
- package/dist/models/enums/index.js +18 -18
- package/dist/models/enums/index.js.map +0 -0
- package/dist/models/index.d.ts +2 -2
- package/dist/models/index.d.ts.map +0 -0
- package/dist/models/index.js +18 -18
- package/dist/models/index.js.map +0 -0
- package/dist/models/interfaces/geo/iPoint.d.ts +7 -7
- package/dist/models/interfaces/geo/iPoint.d.ts.map +0 -0
- package/dist/models/interfaces/geo/iPoint.js +2 -2
- package/dist/models/interfaces/geo/iPoint.js.map +0 -0
- package/dist/models/interfaces/geo/iTile.d.ts +15 -15
- package/dist/models/interfaces/geo/iTile.d.ts.map +0 -0
- package/dist/models/interfaces/geo/iTile.js +2 -2
- package/dist/models/interfaces/geo/iTile.js.map +0 -0
- package/dist/models/interfaces/geo/index.d.ts +2 -2
- package/dist/models/interfaces/geo/index.d.ts.map +0 -0
- package/dist/models/interfaces/geo/index.js +18 -18
- package/dist/models/interfaces/geo/index.js.map +0 -0
- package/dist/models/interfaces/index.d.ts +1 -1
- package/dist/models/interfaces/index.d.ts.map +0 -0
- package/dist/models/interfaces/index.js +17 -17
- package/dist/models/interfaces/index.js.map +0 -0
- package/dist/utils/index.d.ts +2 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +18 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/timeout.d.ts +5 -0
- package/dist/utils/timeout.d.ts.map +1 -0
- package/dist/utils/timeout.js +23 -0
- package/dist/utils/timeout.js.map +1 -0
- package/package.json +79 -79
|
@@ -1,435 +1,318 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
};
|
|
13
|
-
var
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
};
|
|
319
|
-
HttpClient.prototype.getRequestConfig = function (retryConfig, queryParams, auth, headers) {
|
|
320
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
321
|
-
var reqConfig = retryConfig ? __assign(__assign({}, this.axiosOptions), { 'axios-retry': retryConfig }) : __assign({}, this.axiosOptions);
|
|
322
|
-
if (queryParams !== undefined) {
|
|
323
|
-
reqConfig.params = reqConfig.params !== undefined ? __assign(__assign({}, reqConfig.params), queryParams) : queryParams;
|
|
324
|
-
}
|
|
325
|
-
if (auth !== undefined) {
|
|
326
|
-
reqConfig.auth = auth;
|
|
327
|
-
}
|
|
328
|
-
if (headers !== undefined) {
|
|
329
|
-
reqConfig.headers = reqConfig.headers !== undefined ? __assign(__assign({}, reqConfig.headers), headers) : headers;
|
|
330
|
-
}
|
|
331
|
-
return reqConfig;
|
|
332
|
-
};
|
|
333
|
-
HttpClient.prototype.wrapError = function (url, err, body) {
|
|
334
|
-
var _a;
|
|
335
|
-
var message = (0, lodash_1.get)(err, 'response.data.message', undefined);
|
|
336
|
-
switch ((_a = err.response) === null || _a === void 0 ? void 0 : _a.status) {
|
|
337
|
-
case http_status_codes_1.default.BAD_REQUEST:
|
|
338
|
-
if (!this.disableDebugLogs) {
|
|
339
|
-
this.logger.debug({
|
|
340
|
-
err: err,
|
|
341
|
-
url: url,
|
|
342
|
-
body: body,
|
|
343
|
-
targetService: this.targetService,
|
|
344
|
-
msg: "invalid request error recieved from service ".concat(this.targetService, "."),
|
|
345
|
-
msgError: err.message,
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
return new error_types_1.BadRequestError(err, message);
|
|
349
|
-
case http_status_codes_1.default.NOT_FOUND:
|
|
350
|
-
if (!this.disableDebugLogs) {
|
|
351
|
-
this.logger.debug({
|
|
352
|
-
err: err,
|
|
353
|
-
url: url,
|
|
354
|
-
body: body,
|
|
355
|
-
targetService: this.targetService,
|
|
356
|
-
msg: "not found error recieved from service ".concat(this.targetService, "."),
|
|
357
|
-
msgError: err.message,
|
|
358
|
-
});
|
|
359
|
-
}
|
|
360
|
-
return new error_types_1.NotFoundError(err, message);
|
|
361
|
-
case http_status_codes_1.default.CONFLICT:
|
|
362
|
-
if (!this.disableDebugLogs) {
|
|
363
|
-
this.logger.debug({
|
|
364
|
-
err: err,
|
|
365
|
-
url: url,
|
|
366
|
-
body: body,
|
|
367
|
-
targetService: this.targetService,
|
|
368
|
-
msg: "conflict error recieved from service ".concat(this.targetService, "."),
|
|
369
|
-
msgError: err.message,
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
return new error_types_1.ConflictError(err, message);
|
|
373
|
-
case http_status_codes_1.default.FORBIDDEN:
|
|
374
|
-
if (!this.disableDebugLogs) {
|
|
375
|
-
this.logger.debug({
|
|
376
|
-
err: err,
|
|
377
|
-
url: url,
|
|
378
|
-
body: body,
|
|
379
|
-
targetService: this.targetService,
|
|
380
|
-
msg: "forbidden error recieved from service ".concat(this.targetService, "."),
|
|
381
|
-
msgError: err.message,
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
throw new error_types_1.ForbiddenError(err, message);
|
|
385
|
-
case http_status_codes_1.default.UNAUTHORIZED:
|
|
386
|
-
if (!this.disableDebugLogs) {
|
|
387
|
-
this.logger.debug({
|
|
388
|
-
err: err,
|
|
389
|
-
url: url,
|
|
390
|
-
body: body,
|
|
391
|
-
targetService: this.targetService,
|
|
392
|
-
msg: "unauthorized error recieved from service ".concat(this.targetService, "."),
|
|
393
|
-
msgError: err.message,
|
|
394
|
-
});
|
|
395
|
-
}
|
|
396
|
-
throw new error_types_1.UnauthorizedError(err, message);
|
|
397
|
-
default:
|
|
398
|
-
this.logger.error({
|
|
399
|
-
err: err,
|
|
400
|
-
url: url,
|
|
401
|
-
body: body,
|
|
402
|
-
targetService: this.targetService,
|
|
403
|
-
msg: "Internal Server Error recieved from service ".concat(this.targetService, "."),
|
|
404
|
-
msgError: err.message,
|
|
405
|
-
});
|
|
406
|
-
return new error_types_1.InternalServerError(err);
|
|
407
|
-
}
|
|
408
|
-
};
|
|
409
|
-
HttpClient.prototype.parseConfig = function (config) {
|
|
410
|
-
var retries = config.attempts - 1;
|
|
411
|
-
if (retries < 0) {
|
|
412
|
-
throw new Error('invalid retry configuration: attempts must be positive');
|
|
413
|
-
}
|
|
414
|
-
var delay;
|
|
415
|
-
if (config.delay === 'exponential') {
|
|
416
|
-
delay = axios_retry_1.exponentialDelay;
|
|
417
|
-
}
|
|
418
|
-
else if (typeof config.delay === 'number') {
|
|
419
|
-
delay = function () {
|
|
420
|
-
return config.delay;
|
|
421
|
-
};
|
|
422
|
-
}
|
|
423
|
-
else {
|
|
424
|
-
throw new Error('invalid retry configuration: delay must be "exponential" or number');
|
|
425
|
-
}
|
|
426
|
-
return {
|
|
427
|
-
retries: retries,
|
|
428
|
-
retryDelay: delay,
|
|
429
|
-
shouldResetTimeout: config.shouldResetTimeout,
|
|
430
|
-
};
|
|
431
|
-
};
|
|
432
|
-
return HttpClient;
|
|
433
|
-
}());
|
|
434
|
-
exports.HttpClient = HttpClient;
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.HttpClient = void 0;
|
|
30
|
+
const axios_1 = __importDefault(require("axios"));
|
|
31
|
+
const http_status_codes_1 = __importDefault(require("http-status-codes"));
|
|
32
|
+
const axios_retry_1 = __importStar(require("axios-retry"));
|
|
33
|
+
const lodash_1 = require("lodash");
|
|
34
|
+
const error_types_1 = require("@map-colonies/error-types");
|
|
35
|
+
class HttpClient {
|
|
36
|
+
logger;
|
|
37
|
+
baseUrl;
|
|
38
|
+
targetService;
|
|
39
|
+
retryConfig;
|
|
40
|
+
disableDebugLogs;
|
|
41
|
+
axiosOptions = {};
|
|
42
|
+
axiosClient;
|
|
43
|
+
constructor(logger, baseUrl, targetService = '', retryConfig, disableDebugLogs = false) {
|
|
44
|
+
this.logger = logger;
|
|
45
|
+
this.baseUrl = baseUrl;
|
|
46
|
+
this.targetService = targetService;
|
|
47
|
+
this.retryConfig = retryConfig;
|
|
48
|
+
this.disableDebugLogs = disableDebugLogs;
|
|
49
|
+
this.axiosClient = axios_1.default.create();
|
|
50
|
+
this.axiosOptions.baseURL = baseUrl;
|
|
51
|
+
const axiosRetryConfig = retryConfig
|
|
52
|
+
? this.parseConfig(retryConfig)
|
|
53
|
+
: {
|
|
54
|
+
retries: 0,
|
|
55
|
+
};
|
|
56
|
+
const delayFunc = axiosRetryConfig.retryDelay ?? (() => 0);
|
|
57
|
+
axiosRetryConfig.retryDelay = (retryCount, error) => {
|
|
58
|
+
this.logger.error({
|
|
59
|
+
err: error,
|
|
60
|
+
retries: retryCount,
|
|
61
|
+
targetService: this.targetService,
|
|
62
|
+
msg: `error from ${this.targetService}.`,
|
|
63
|
+
msgError: error.message,
|
|
64
|
+
});
|
|
65
|
+
return delayFunc(retryCount, error);
|
|
66
|
+
};
|
|
67
|
+
(0, axios_retry_1.default)(this.axiosClient, axiosRetryConfig);
|
|
68
|
+
}
|
|
69
|
+
async get(url, queryParams, retryConfig, auth, headers) {
|
|
70
|
+
try {
|
|
71
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
72
|
+
if (!this.disableDebugLogs) {
|
|
73
|
+
this.logger.debug({
|
|
74
|
+
reqConfig,
|
|
75
|
+
url,
|
|
76
|
+
targetService: this.targetService,
|
|
77
|
+
msg: `Send GET message to ${this.targetService}.`,
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
const res = await this.axiosClient.get(url, reqConfig);
|
|
81
|
+
return res.data;
|
|
82
|
+
}
|
|
83
|
+
catch (err) {
|
|
84
|
+
const error = this.wrapError(url, err);
|
|
85
|
+
throw error;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
async post(url, body, queryParams, retryConfig, auth, headers) {
|
|
89
|
+
try {
|
|
90
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
91
|
+
if (!this.disableDebugLogs) {
|
|
92
|
+
this.logger.debug({
|
|
93
|
+
reqConfig,
|
|
94
|
+
url,
|
|
95
|
+
body,
|
|
96
|
+
targetService: this.targetService,
|
|
97
|
+
msg: `Send POST message to ${this.targetService}.`,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const res = await this.axiosClient.post(url, body, reqConfig);
|
|
101
|
+
return res.data;
|
|
102
|
+
}
|
|
103
|
+
catch (err) {
|
|
104
|
+
const error = this.wrapError(url, err, body);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async put(url, body, queryParams, retryConfig, auth, headers) {
|
|
109
|
+
try {
|
|
110
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
111
|
+
if (!this.disableDebugLogs) {
|
|
112
|
+
this.logger.debug({
|
|
113
|
+
reqConfig,
|
|
114
|
+
url,
|
|
115
|
+
body,
|
|
116
|
+
targetService: this.targetService,
|
|
117
|
+
msg: `Send PUT message to ${this.targetService}.`,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const res = await this.axiosClient.put(url, body, reqConfig);
|
|
121
|
+
return res.data;
|
|
122
|
+
}
|
|
123
|
+
catch (err) {
|
|
124
|
+
const error = this.wrapError(url, err, body);
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
async delete(url, queryParams, retryConfig, auth, headers) {
|
|
129
|
+
try {
|
|
130
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
131
|
+
if (!this.disableDebugLogs) {
|
|
132
|
+
this.logger.debug({
|
|
133
|
+
reqConfig,
|
|
134
|
+
url,
|
|
135
|
+
targetService: this.targetService,
|
|
136
|
+
msg: `Send DELTE message to ${this.targetService}.`,
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const res = await this.axiosClient.delete(url, reqConfig);
|
|
140
|
+
return res.data;
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
const error = this.wrapError(url, err);
|
|
144
|
+
throw error;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
async head(url, queryParams, retryConfig, auth, headers) {
|
|
148
|
+
try {
|
|
149
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
150
|
+
if (!this.disableDebugLogs) {
|
|
151
|
+
this.logger.debug({
|
|
152
|
+
reqConfig,
|
|
153
|
+
url,
|
|
154
|
+
targetService: this.targetService,
|
|
155
|
+
msg: `Send HEAD message to ${this.targetService}.`,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const res = await this.axiosClient.head(url, reqConfig);
|
|
159
|
+
return res.data;
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
const error = this.wrapError(url, err);
|
|
163
|
+
throw error;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
async options(url, queryParams, retryConfig, auth, headers) {
|
|
167
|
+
try {
|
|
168
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
169
|
+
if (!this.disableDebugLogs) {
|
|
170
|
+
this.logger.debug({
|
|
171
|
+
reqConfig,
|
|
172
|
+
url,
|
|
173
|
+
targetService: this.targetService,
|
|
174
|
+
msg: `Send OPTIONS message to ${this.targetService}.`,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
const res = await this.axiosClient.options(url, reqConfig);
|
|
178
|
+
return res.data;
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
const error = this.wrapError(url, err);
|
|
182
|
+
throw error;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async patch(url, body, queryParams, retryConfig, auth, headers) {
|
|
186
|
+
try {
|
|
187
|
+
const reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
|
|
188
|
+
if (!this.disableDebugLogs) {
|
|
189
|
+
this.logger.debug({
|
|
190
|
+
reqConfig,
|
|
191
|
+
url,
|
|
192
|
+
targetService: this.targetService,
|
|
193
|
+
msg: `Send PATCH message to ${this.targetService}.`,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
const res = await this.axiosClient.patch(url, body, reqConfig);
|
|
197
|
+
return res.data;
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
const error = this.wrapError(url, err);
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
getRequestConfig(retryConfig, queryParams, auth, headers) {
|
|
205
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
206
|
+
const reqConfig = retryConfig ? { ...this.axiosOptions, 'axios-retry': retryConfig } : { ...this.axiosOptions };
|
|
207
|
+
if (queryParams !== undefined) {
|
|
208
|
+
reqConfig.params = reqConfig.params !== undefined ? { ...reqConfig.params, ...queryParams } : queryParams;
|
|
209
|
+
}
|
|
210
|
+
if (auth !== undefined) {
|
|
211
|
+
reqConfig.auth = auth;
|
|
212
|
+
}
|
|
213
|
+
if (headers !== undefined) {
|
|
214
|
+
reqConfig.headers = reqConfig.headers !== undefined ? { ...reqConfig.headers, ...headers } : headers;
|
|
215
|
+
}
|
|
216
|
+
return reqConfig;
|
|
217
|
+
}
|
|
218
|
+
wrapError(url, err, body) {
|
|
219
|
+
const message = (0, lodash_1.get)(err, 'response.data.message', undefined);
|
|
220
|
+
switch (err.response?.status) {
|
|
221
|
+
case http_status_codes_1.default.BAD_REQUEST:
|
|
222
|
+
if (!this.disableDebugLogs) {
|
|
223
|
+
this.logger.debug({
|
|
224
|
+
err,
|
|
225
|
+
url,
|
|
226
|
+
body,
|
|
227
|
+
targetService: this.targetService,
|
|
228
|
+
msg: `invalid request error recieved from service ${this.targetService}.`,
|
|
229
|
+
msgError: err.message,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
return new error_types_1.BadRequestError(err, message);
|
|
233
|
+
case http_status_codes_1.default.NOT_FOUND:
|
|
234
|
+
if (!this.disableDebugLogs) {
|
|
235
|
+
this.logger.debug({
|
|
236
|
+
err,
|
|
237
|
+
url,
|
|
238
|
+
body,
|
|
239
|
+
targetService: this.targetService,
|
|
240
|
+
msg: `not found error recieved from service ${this.targetService}.`,
|
|
241
|
+
msgError: err.message,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return new error_types_1.NotFoundError(err, message);
|
|
245
|
+
case http_status_codes_1.default.CONFLICT:
|
|
246
|
+
if (!this.disableDebugLogs) {
|
|
247
|
+
this.logger.debug({
|
|
248
|
+
err,
|
|
249
|
+
url,
|
|
250
|
+
body,
|
|
251
|
+
targetService: this.targetService,
|
|
252
|
+
msg: `conflict error recieved from service ${this.targetService}.`,
|
|
253
|
+
msgError: err.message,
|
|
254
|
+
});
|
|
255
|
+
}
|
|
256
|
+
return new error_types_1.ConflictError(err, message);
|
|
257
|
+
case http_status_codes_1.default.FORBIDDEN:
|
|
258
|
+
if (!this.disableDebugLogs) {
|
|
259
|
+
this.logger.debug({
|
|
260
|
+
err,
|
|
261
|
+
url,
|
|
262
|
+
body,
|
|
263
|
+
targetService: this.targetService,
|
|
264
|
+
msg: `forbidden error recieved from service ${this.targetService}.`,
|
|
265
|
+
msgError: err.message,
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
throw new error_types_1.ForbiddenError(err, message);
|
|
269
|
+
case http_status_codes_1.default.UNAUTHORIZED:
|
|
270
|
+
if (!this.disableDebugLogs) {
|
|
271
|
+
this.logger.debug({
|
|
272
|
+
err,
|
|
273
|
+
url,
|
|
274
|
+
body,
|
|
275
|
+
targetService: this.targetService,
|
|
276
|
+
msg: `unauthorized error recieved from service ${this.targetService}.`,
|
|
277
|
+
msgError: err.message,
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
throw new error_types_1.UnauthorizedError(err, message);
|
|
281
|
+
default:
|
|
282
|
+
this.logger.error({
|
|
283
|
+
err,
|
|
284
|
+
url,
|
|
285
|
+
body,
|
|
286
|
+
targetService: this.targetService,
|
|
287
|
+
msg: `Internal Server Error recieved from service ${this.targetService}.`,
|
|
288
|
+
msgError: err.message,
|
|
289
|
+
});
|
|
290
|
+
return new error_types_1.InternalServerError(err);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
parseConfig(config) {
|
|
294
|
+
const retries = config.attempts - 1;
|
|
295
|
+
if (retries < 0) {
|
|
296
|
+
throw new Error('invalid retry configuration: attempts must be positive');
|
|
297
|
+
}
|
|
298
|
+
let delay;
|
|
299
|
+
if (config.delay === 'exponential') {
|
|
300
|
+
delay = axios_retry_1.exponentialDelay;
|
|
301
|
+
}
|
|
302
|
+
else if (typeof config.delay === 'number') {
|
|
303
|
+
delay = () => {
|
|
304
|
+
return config.delay;
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
else {
|
|
308
|
+
throw new Error('invalid retry configuration: delay must be "exponential" or number');
|
|
309
|
+
}
|
|
310
|
+
return {
|
|
311
|
+
retries: retries,
|
|
312
|
+
retryDelay: delay,
|
|
313
|
+
shouldResetTimeout: config.shouldResetTimeout,
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
exports.HttpClient = HttpClient;
|
|
435
318
|
//# sourceMappingURL=httpClient.js.map
|