@map-colonies/mc-utils 3.0.0 → 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.
Files changed (122) hide show
  1. package/README.md +67 -67
  2. package/dist/arrays/index.d.ts +1 -1
  3. package/dist/arrays/index.d.ts.map +0 -0
  4. package/dist/arrays/index.js +17 -17
  5. package/dist/arrays/index.js.map +0 -0
  6. package/dist/arrays/subGroups.d.ts +8 -8
  7. package/dist/arrays/subGroups.d.ts.map +0 -0
  8. package/dist/arrays/subGroups.js +33 -138
  9. package/dist/arrays/subGroups.js.map +1 -1
  10. package/dist/communication/http/httpClient.d.ts +29 -29
  11. package/dist/communication/http/httpClient.d.ts.map +0 -0
  12. package/dist/communication/http/httpClient.js +317 -434
  13. package/dist/communication/http/httpClient.js.map +1 -1
  14. package/dist/communication/http/index.d.ts +1 -1
  15. package/dist/communication/http/index.d.ts.map +0 -0
  16. package/dist/communication/http/index.js +17 -17
  17. package/dist/communication/http/index.js.map +0 -0
  18. package/dist/communication/index.d.ts +1 -1
  19. package/dist/communication/index.d.ts.map +0 -0
  20. package/dist/communication/index.js +17 -17
  21. package/dist/communication/index.js.map +0 -0
  22. package/dist/dateTime/getUTCDate.d.ts +1 -1
  23. package/dist/dateTime/getUTCDate.d.ts.map +0 -0
  24. package/dist/dateTime/getUTCDate.js +10 -10
  25. package/dist/dateTime/getUTCDate.js.map +1 -1
  26. package/dist/dateTime/index.d.ts +1 -1
  27. package/dist/dateTime/index.d.ts.map +0 -0
  28. package/dist/dateTime/index.js +17 -17
  29. package/dist/dateTime/index.js.map +0 -0
  30. package/dist/geo/bboxUtils.d.ts +23 -23
  31. package/dist/geo/bboxUtils.d.ts.map +0 -0
  32. package/dist/geo/bboxUtils.js +89 -89
  33. package/dist/geo/bboxUtils.js.map +1 -1
  34. package/dist/geo/geoConvertor.d.ts +18 -18
  35. package/dist/geo/geoConvertor.d.ts.map +0 -0
  36. package/dist/geo/geoConvertor.js +49 -51
  37. package/dist/geo/geoConvertor.js.map +1 -1
  38. package/dist/geo/geoHash.d.ts +24 -24
  39. package/dist/geo/geoHash.d.ts.map +0 -0
  40. package/dist/geo/geoHash.js +134 -274
  41. package/dist/geo/geoHash.js.map +1 -1
  42. package/dist/geo/geoIntersection.d.ts +19 -19
  43. package/dist/geo/geoIntersection.d.ts.map +0 -0
  44. package/dist/geo/geoIntersection.js +70 -70
  45. package/dist/geo/geoIntersection.js.map +1 -1
  46. package/dist/geo/index.d.ts +8 -8
  47. package/dist/geo/index.d.ts.map +0 -0
  48. package/dist/geo/index.js +24 -24
  49. package/dist/geo/index.js.map +0 -0
  50. package/dist/geo/tileBatcher.d.ts +8 -8
  51. package/dist/geo/tileBatcher.d.ts.map +0 -0
  52. package/dist/geo/tileBatcher.js +97 -193
  53. package/dist/geo/tileBatcher.js.map +1 -1
  54. package/dist/geo/tileRanger.d.ts +46 -46
  55. package/dist/geo/tileRanger.d.ts.map +0 -0
  56. package/dist/geo/tileRanger.js +232 -383
  57. package/dist/geo/tileRanger.js.map +1 -1
  58. package/dist/geo/tiles.d.ts +82 -82
  59. package/dist/geo/tiles.d.ts.map +0 -0
  60. package/dist/geo/tiles.js +219 -246
  61. package/dist/geo/tiles.js.map +1 -1
  62. package/dist/geo/tilesGenerator.d.ts +2 -2
  63. package/dist/geo/tilesGenerator.d.ts.map +0 -0
  64. package/dist/geo/tilesGenerator.js +17 -111
  65. package/dist/geo/tilesGenerator.js.map +1 -1
  66. package/dist/index.d.ts +6 -5
  67. package/dist/index.d.ts.map +1 -1
  68. package/dist/index.js +22 -21
  69. package/dist/index.js.map +1 -1
  70. package/dist/models/enums/gdal/dataType.d.ts +16 -16
  71. package/dist/models/enums/gdal/dataType.d.ts.map +0 -0
  72. package/dist/models/enums/gdal/dataType.js +20 -20
  73. package/dist/models/enums/gdal/dataType.js.map +0 -0
  74. package/dist/models/enums/gdal/index.d.ts +2 -2
  75. package/dist/models/enums/gdal/index.d.ts.map +0 -0
  76. package/dist/models/enums/gdal/index.js +18 -18
  77. package/dist/models/enums/gdal/index.js.map +0 -0
  78. package/dist/models/enums/gdal/resamplingMethod.d.ts +16 -16
  79. package/dist/models/enums/gdal/resamplingMethod.d.ts.map +0 -0
  80. package/dist/models/enums/gdal/resamplingMethod.js +20 -20
  81. package/dist/models/enums/gdal/resamplingMethod.js.map +0 -0
  82. package/dist/models/enums/geo/index.d.ts +1 -1
  83. package/dist/models/enums/geo/index.d.ts.map +0 -0
  84. package/dist/models/enums/geo/index.js +17 -17
  85. package/dist/models/enums/geo/index.js.map +0 -0
  86. package/dist/models/enums/geo/tileOrigin.d.ts +7 -7
  87. package/dist/models/enums/geo/tileOrigin.d.ts.map +0 -0
  88. package/dist/models/enums/geo/tileOrigin.js +11 -11
  89. package/dist/models/enums/geo/tileOrigin.js.map +0 -0
  90. package/dist/models/enums/index.d.ts +2 -2
  91. package/dist/models/enums/index.d.ts.map +0 -0
  92. package/dist/models/enums/index.js +18 -18
  93. package/dist/models/enums/index.js.map +0 -0
  94. package/dist/models/index.d.ts +2 -2
  95. package/dist/models/index.d.ts.map +0 -0
  96. package/dist/models/index.js +18 -18
  97. package/dist/models/index.js.map +0 -0
  98. package/dist/models/interfaces/geo/iPoint.d.ts +7 -7
  99. package/dist/models/interfaces/geo/iPoint.d.ts.map +0 -0
  100. package/dist/models/interfaces/geo/iPoint.js +2 -2
  101. package/dist/models/interfaces/geo/iPoint.js.map +0 -0
  102. package/dist/models/interfaces/geo/iTile.d.ts +15 -15
  103. package/dist/models/interfaces/geo/iTile.d.ts.map +0 -0
  104. package/dist/models/interfaces/geo/iTile.js +2 -2
  105. package/dist/models/interfaces/geo/iTile.js.map +0 -0
  106. package/dist/models/interfaces/geo/index.d.ts +2 -2
  107. package/dist/models/interfaces/geo/index.d.ts.map +0 -0
  108. package/dist/models/interfaces/geo/index.js +18 -18
  109. package/dist/models/interfaces/geo/index.js.map +0 -0
  110. package/dist/models/interfaces/index.d.ts +1 -1
  111. package/dist/models/interfaces/index.d.ts.map +0 -0
  112. package/dist/models/interfaces/index.js +17 -17
  113. package/dist/models/interfaces/index.js.map +0 -0
  114. package/dist/utils/index.d.ts +2 -0
  115. package/dist/utils/index.d.ts.map +1 -0
  116. package/dist/utils/index.js +18 -0
  117. package/dist/utils/index.js.map +1 -0
  118. package/dist/utils/timeout.d.ts +4 -1
  119. package/dist/utils/timeout.d.ts.map +1 -1
  120. package/dist/utils/timeout.js +22 -47
  121. package/dist/utils/timeout.js.map +1 -1
  122. package/package.json +79 -79
@@ -1,435 +1,318 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- var desc = Object.getOwnPropertyDescriptor(m, k);
16
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
- desc = { enumerable: true, get: function() { return m[k]; } };
18
- }
19
- Object.defineProperty(o, k2, desc);
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
- Object.defineProperty(o, "default", { enumerable: true, value: v });
26
- }) : function(o, v) {
27
- o["default"] = v;
28
- });
29
- var __importStar = (this && this.__importStar) || function (mod) {
30
- if (mod && mod.__esModule) return mod;
31
- var result = {};
32
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
- __setModuleDefault(result, mod);
34
- return result;
35
- };
36
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
- return new (P || (P = Promise))(function (resolve, reject) {
39
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
- step((generator = generator.apply(thisArg, _arguments || [])).next());
43
- });
44
- };
45
- var __generator = (this && this.__generator) || function (thisArg, body) {
46
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
- function verb(n) { return function (v) { return step([n, v]); }; }
49
- function step(op) {
50
- if (f) throw new TypeError("Generator is already executing.");
51
- while (_) try {
52
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53
- if (y = 0, t) op = [op[0] & 2, t.value];
54
- switch (op[0]) {
55
- case 0: case 1: t = op; break;
56
- case 4: _.label++; return { value: op[1], done: false };
57
- case 5: _.label++; y = op[1]; op = [0]; continue;
58
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
- default:
60
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
- if (t[2]) _.ops.pop();
65
- _.trys.pop(); continue;
66
- }
67
- op = body.call(thisArg, _);
68
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
- }
71
- };
72
- var __importDefault = (this && this.__importDefault) || function (mod) {
73
- return (mod && mod.__esModule) ? mod : { "default": mod };
74
- };
75
- Object.defineProperty(exports, "__esModule", { value: true });
76
- exports.HttpClient = void 0;
77
- var axios_1 = __importDefault(require("axios"));
78
- var http_status_codes_1 = __importDefault(require("http-status-codes"));
79
- var axios_retry_1 = __importStar(require("axios-retry"));
80
- var lodash_1 = require("lodash");
81
- var error_types_1 = require("@map-colonies/error-types");
82
- var HttpClient = /** @class */ (function () {
83
- function HttpClient(logger, baseUrl, targetService, retryConfig, disableDebugLogs) {
84
- if (targetService === void 0) { targetService = ''; }
85
- if (disableDebugLogs === void 0) { disableDebugLogs = false; }
86
- var _this = this;
87
- var _a;
88
- this.logger = logger;
89
- this.baseUrl = baseUrl;
90
- this.targetService = targetService;
91
- this.retryConfig = retryConfig;
92
- this.disableDebugLogs = disableDebugLogs;
93
- this.axiosOptions = {};
94
- this.axiosClient = axios_1.default.create();
95
- this.axiosOptions.baseURL = baseUrl;
96
- var axiosRetryConfig = retryConfig
97
- ? this.parseConfig(retryConfig)
98
- : {
99
- retries: 0,
100
- };
101
- var delayFunc = (_a = axiosRetryConfig.retryDelay) !== null && _a !== void 0 ? _a : (function () { return 0; });
102
- axiosRetryConfig.retryDelay = function (retryCount, error) {
103
- _this.logger.error({
104
- err: error,
105
- retries: retryCount,
106
- targetService: _this.targetService,
107
- msg: "error from ".concat(_this.targetService, "."),
108
- msgError: error.message,
109
- });
110
- return delayFunc(retryCount, error);
111
- };
112
- (0, axios_retry_1.default)(this.axiosClient, axiosRetryConfig);
113
- }
114
- HttpClient.prototype.get = function (url, queryParams, retryConfig, auth, headers) {
115
- return __awaiter(this, void 0, void 0, function () {
116
- var reqConfig, res, err_1, error;
117
- return __generator(this, function (_a) {
118
- switch (_a.label) {
119
- case 0:
120
- _a.trys.push([0, 2, , 3]);
121
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
122
- if (!this.disableDebugLogs) {
123
- this.logger.debug({
124
- reqConfig: reqConfig,
125
- url: url,
126
- targetService: this.targetService,
127
- msg: "Send GET message to ".concat(this.targetService, "."),
128
- });
129
- }
130
- return [4 /*yield*/, this.axiosClient.get(url, reqConfig)];
131
- case 1:
132
- res = _a.sent();
133
- return [2 /*return*/, res.data];
134
- case 2:
135
- err_1 = _a.sent();
136
- error = this.wrapError(url, err_1);
137
- throw error;
138
- case 3: return [2 /*return*/];
139
- }
140
- });
141
- });
142
- };
143
- HttpClient.prototype.post = function (url, body, queryParams, retryConfig, auth, headers) {
144
- return __awaiter(this, void 0, void 0, function () {
145
- var reqConfig, res, err_2, error;
146
- return __generator(this, function (_a) {
147
- switch (_a.label) {
148
- case 0:
149
- _a.trys.push([0, 2, , 3]);
150
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
151
- if (!this.disableDebugLogs) {
152
- this.logger.debug({
153
- reqConfig: reqConfig,
154
- url: url,
155
- body: body,
156
- targetService: this.targetService,
157
- msg: "Send POST message to ".concat(this.targetService, "."),
158
- });
159
- }
160
- return [4 /*yield*/, this.axiosClient.post(url, body, reqConfig)];
161
- case 1:
162
- res = _a.sent();
163
- return [2 /*return*/, res.data];
164
- case 2:
165
- err_2 = _a.sent();
166
- error = this.wrapError(url, err_2, body);
167
- throw error;
168
- case 3: return [2 /*return*/];
169
- }
170
- });
171
- });
172
- };
173
- HttpClient.prototype.put = function (url, body, queryParams, retryConfig, auth, headers) {
174
- return __awaiter(this, void 0, void 0, function () {
175
- var reqConfig, res, err_3, error;
176
- return __generator(this, function (_a) {
177
- switch (_a.label) {
178
- case 0:
179
- _a.trys.push([0, 2, , 3]);
180
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
181
- if (!this.disableDebugLogs) {
182
- this.logger.debug({
183
- reqConfig: reqConfig,
184
- url: url,
185
- body: body,
186
- targetService: this.targetService,
187
- msg: "Send PUT message to ".concat(this.targetService, "."),
188
- });
189
- }
190
- return [4 /*yield*/, this.axiosClient.put(url, body, reqConfig)];
191
- case 1:
192
- res = _a.sent();
193
- return [2 /*return*/, res.data];
194
- case 2:
195
- err_3 = _a.sent();
196
- error = this.wrapError(url, err_3, body);
197
- throw error;
198
- case 3: return [2 /*return*/];
199
- }
200
- });
201
- });
202
- };
203
- HttpClient.prototype.delete = function (url, queryParams, retryConfig, auth, headers) {
204
- return __awaiter(this, void 0, void 0, function () {
205
- var reqConfig, res, err_4, error;
206
- return __generator(this, function (_a) {
207
- switch (_a.label) {
208
- case 0:
209
- _a.trys.push([0, 2, , 3]);
210
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
211
- if (!this.disableDebugLogs) {
212
- this.logger.debug({
213
- reqConfig: reqConfig,
214
- url: url,
215
- targetService: this.targetService,
216
- msg: "Send DELTE message to ".concat(this.targetService, "."),
217
- });
218
- }
219
- return [4 /*yield*/, this.axiosClient.delete(url, reqConfig)];
220
- case 1:
221
- res = _a.sent();
222
- return [2 /*return*/, res.data];
223
- case 2:
224
- err_4 = _a.sent();
225
- error = this.wrapError(url, err_4);
226
- throw error;
227
- case 3: return [2 /*return*/];
228
- }
229
- });
230
- });
231
- };
232
- HttpClient.prototype.head = function (url, queryParams, retryConfig, auth, headers) {
233
- return __awaiter(this, void 0, void 0, function () {
234
- var reqConfig, res, err_5, error;
235
- return __generator(this, function (_a) {
236
- switch (_a.label) {
237
- case 0:
238
- _a.trys.push([0, 2, , 3]);
239
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
240
- if (!this.disableDebugLogs) {
241
- this.logger.debug({
242
- reqConfig: reqConfig,
243
- url: url,
244
- targetService: this.targetService,
245
- msg: "Send HEAD message to ".concat(this.targetService, "."),
246
- });
247
- }
248
- return [4 /*yield*/, this.axiosClient.head(url, reqConfig)];
249
- case 1:
250
- res = _a.sent();
251
- return [2 /*return*/, res.data];
252
- case 2:
253
- err_5 = _a.sent();
254
- error = this.wrapError(url, err_5);
255
- throw error;
256
- case 3: return [2 /*return*/];
257
- }
258
- });
259
- });
260
- };
261
- HttpClient.prototype.options = function (url, queryParams, retryConfig, auth, headers) {
262
- return __awaiter(this, void 0, void 0, function () {
263
- var reqConfig, res, err_6, error;
264
- return __generator(this, function (_a) {
265
- switch (_a.label) {
266
- case 0:
267
- _a.trys.push([0, 2, , 3]);
268
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
269
- if (!this.disableDebugLogs) {
270
- this.logger.debug({
271
- reqConfig: reqConfig,
272
- url: url,
273
- targetService: this.targetService,
274
- msg: "Send OPTIONS message to ".concat(this.targetService, "."),
275
- });
276
- }
277
- return [4 /*yield*/, this.axiosClient.options(url, reqConfig)];
278
- case 1:
279
- res = _a.sent();
280
- return [2 /*return*/, res.data];
281
- case 2:
282
- err_6 = _a.sent();
283
- error = this.wrapError(url, err_6);
284
- throw error;
285
- case 3: return [2 /*return*/];
286
- }
287
- });
288
- });
289
- };
290
- HttpClient.prototype.patch = function (url, body, queryParams, retryConfig, auth, headers) {
291
- return __awaiter(this, void 0, void 0, function () {
292
- var reqConfig, res, err_7, error;
293
- return __generator(this, function (_a) {
294
- switch (_a.label) {
295
- case 0:
296
- _a.trys.push([0, 2, , 3]);
297
- reqConfig = this.getRequestConfig(retryConfig, queryParams, auth, headers);
298
- if (!this.disableDebugLogs) {
299
- this.logger.debug({
300
- reqConfig: reqConfig,
301
- url: url,
302
- targetService: this.targetService,
303
- msg: "Send PATCH message to ".concat(this.targetService, "."),
304
- });
305
- }
306
- return [4 /*yield*/, this.axiosClient.patch(url, body, reqConfig)];
307
- case 1:
308
- res = _a.sent();
309
- return [2 /*return*/, res.data];
310
- case 2:
311
- err_7 = _a.sent();
312
- error = this.wrapError(url, err_7);
313
- throw error;
314
- case 3: return [2 /*return*/];
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