getta 0.4.6 → 1.0.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 (104) hide show
  1. package/README.md +165 -7
  2. package/dist/main/index.mjs +621 -0
  3. package/dist/main/index.mjs.map +1 -0
  4. package/dist/tsconfig.build.tsbuildinfo +1 -0
  5. package/{lib → dist}/types/constants.d.ts +2 -2
  6. package/dist/types/constants.d.ts.map +1 -0
  7. package/dist/types/helpers/buildEndpoint/index.d.ts +3 -0
  8. package/dist/types/helpers/buildEndpoint/index.d.ts.map +1 -0
  9. package/{lib/types/helpers/build-endpoint → dist/types/helpers/buildEndpoint}/types.d.ts +2 -2
  10. package/dist/types/helpers/buildEndpoint/types.d.ts.map +1 -0
  11. package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts +2 -0
  12. package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts.map +1 -0
  13. package/dist/types/helpers/delay/index.d.ts +2 -0
  14. package/{lib → dist}/types/helpers/delay/index.d.ts.map +1 -1
  15. package/dist/types/helpers/getResponseGroup/index.d.ts +2 -0
  16. package/dist/types/helpers/getResponseGroup/index.d.ts.map +1 -0
  17. package/dist/types/helpers/isCacheabilityValid/index.d.ts +3 -0
  18. package/dist/types/helpers/isCacheabilityValid/index.d.ts.map +1 -0
  19. package/dist/types/index.d.ts +5 -0
  20. package/dist/types/index.d.ts.map +1 -0
  21. package/{lib → dist}/types/main.d.ts +12 -12
  22. package/dist/types/main.d.ts.map +1 -0
  23. package/dist/types/types.d.ts +161 -0
  24. package/dist/types/types.d.ts.map +1 -0
  25. package/package.json +113 -145
  26. package/src/{__tests__ → __testUtils__}/data/136-7317.json +1 -1
  27. package/src/{__tests__ → __testUtils__}/data/180-1387.json +1 -1
  28. package/src/{__tests__ → __testUtils__}/data/183-3905.json +1 -1
  29. package/src/{__tests__ → __testUtils__}/data/202-3315.json +1 -1
  30. package/src/__testUtils__/data/index.ts +23 -0
  31. package/src/__testUtils__/helpers/index.ts +49 -0
  32. package/src/constants.ts +28 -28
  33. package/src/helpers/buildEndpoint/index.ts +34 -0
  34. package/src/helpers/{build-endpoint → buildEndpoint}/types.ts +2 -2
  35. package/src/helpers/defaultPathTemplateCallback/index.test.ts +11 -0
  36. package/src/helpers/defaultPathTemplateCallback/index.ts +17 -0
  37. package/src/helpers/delay/index.ts +2 -2
  38. package/src/helpers/getResponseGroup/index.ts +25 -0
  39. package/src/helpers/isCacheabilityValid/index.ts +6 -0
  40. package/src/index.ts +4 -4
  41. package/src/main.test.ts +283 -411
  42. package/src/main.ts +238 -241
  43. package/src/types.ts +114 -16
  44. package/tsconfig.build.json +14 -0
  45. package/tsconfig.json +9 -0
  46. package/CHANGELOG.md +0 -244
  47. package/jest.setup.js +0 -10
  48. package/lib/browser/index.js +0 -2
  49. package/lib/browser/index.js.map +0 -1
  50. package/lib/browser/production.analysis.txt +0 -91
  51. package/lib/main/constants.js +0 -97
  52. package/lib/main/helpers/build-endpoint/index.js +0 -38
  53. package/lib/main/helpers/build-endpoint/types.js +0 -1
  54. package/lib/main/helpers/default-path-template-callback/index.js +0 -16
  55. package/lib/main/helpers/delay/index.js +0 -12
  56. package/lib/main/helpers/get-response-group/index.js +0 -27
  57. package/lib/main/helpers/is-cacheability-valid/index.js +0 -13
  58. package/lib/main/index.js +0 -62
  59. package/lib/main/main.js +0 -580
  60. package/lib/main/types.js +0 -1
  61. package/lib/module/constants.js +0 -49
  62. package/lib/module/helpers/build-endpoint/index.js +0 -28
  63. package/lib/module/helpers/build-endpoint/types.js +0 -0
  64. package/lib/module/helpers/default-path-template-callback/index.js +0 -9
  65. package/lib/module/helpers/delay/index.js +0 -4
  66. package/lib/module/helpers/get-response-group/index.js +0 -19
  67. package/lib/module/helpers/is-cacheability-valid/index.js +0 -6
  68. package/lib/module/index.js +0 -4
  69. package/lib/module/main.js +0 -576
  70. package/lib/module/types.js +0 -0
  71. package/lib/types/__tests__/data/index.d.ts +0 -17
  72. package/lib/types/__tests__/data/index.d.ts.map +0 -1
  73. package/lib/types/__tests__/helpers/index.d.ts +0 -29
  74. package/lib/types/__tests__/helpers/index.d.ts.map +0 -1
  75. package/lib/types/__tests__/types.d.ts +0 -13
  76. package/lib/types/__tests__/types.d.ts.map +0 -1
  77. package/lib/types/constants.d.ts.map +0 -1
  78. package/lib/types/helpers/build-endpoint/index.d.ts +0 -3
  79. package/lib/types/helpers/build-endpoint/index.d.ts.map +0 -1
  80. package/lib/types/helpers/build-endpoint/types.d.ts.map +0 -1
  81. package/lib/types/helpers/default-path-template-callback/index.d.ts +0 -3
  82. package/lib/types/helpers/default-path-template-callback/index.d.ts.map +0 -1
  83. package/lib/types/helpers/default-path-template-callback/index.test.d.ts +0 -2
  84. package/lib/types/helpers/default-path-template-callback/index.test.d.ts.map +0 -1
  85. package/lib/types/helpers/delay/index.d.ts +0 -2
  86. package/lib/types/helpers/get-response-group/index.d.ts +0 -2
  87. package/lib/types/helpers/get-response-group/index.d.ts.map +0 -1
  88. package/lib/types/helpers/is-cacheability-valid/index.d.ts +0 -3
  89. package/lib/types/helpers/is-cacheability-valid/index.d.ts.map +0 -1
  90. package/lib/types/index.d.ts +0 -5
  91. package/lib/types/index.d.ts.map +0 -1
  92. package/lib/types/main.d.ts.map +0 -1
  93. package/lib/types/main.test.d.ts +0 -2
  94. package/lib/types/main.test.d.ts.map +0 -1
  95. package/lib/types/types.d.ts +0 -69
  96. package/lib/types/types.d.ts.map +0 -1
  97. package/src/__tests__/data/index.ts +0 -19
  98. package/src/__tests__/helpers/index.ts +0 -61
  99. package/src/__tests__/types.ts +0 -14
  100. package/src/helpers/build-endpoint/index.ts +0 -34
  101. package/src/helpers/default-path-template-callback/index.test.ts +0 -11
  102. package/src/helpers/default-path-template-callback/index.ts +0 -16
  103. package/src/helpers/get-response-group/index.ts +0 -22
  104. package/src/helpers/is-cacheability-valid/index.ts +0 -6
@@ -1,4 +0,0 @@
1
- export * from "./constants";
2
- export { default as defaultPathTemplateCallback } from "./helpers/default-path-template-callback";
3
- export { Getta, createRestClient } from "./main";
4
- export * from "./types";
@@ -1,576 +0,0 @@
1
- import _defineProperty from "@babel/runtime/helpers/defineProperty";
2
- import _merge from "lodash/merge";
3
- import _castArray from "lodash/castArray";
4
- import "core-js/modules/es.promise.js";
5
- import md5 from "md5";
6
- import { CACHE_CONTROL_HEADER, DEFAULT_BODY_PARSER, DEFAULT_FETCH_TIMEOUT, DEFAULT_HEADERS, DEFAULT_MAX_REDIRECTS, DEFAULT_MAX_RETRIES, DEFAULT_PATH_TEMPLATE_REGEX, DEFAULT_RATE_LIMIT, DEFAULT_REQUEST_RETRY_WAIT, DELETE_METHOD, ETAG_HEADER, FETCH_METHODS, FETCH_TIMEOUT_ERROR, GET_METHOD, IF_NONE_MATCH_HEADER, INVALID_FETCH_METHOD_ERROR, JSON_FORMAT, LOCATION_HEADER, MAX_REDIRECTS_EXCEEDED_ERROR, MAX_RETRIES_EXCEEDED_ERROR, MISSING_BASE_PATH_ERROR, NOT_FOUND_STATUS_CODE, NOT_MODIFIED_STATUS_CODE, OPTIONAL_PATH_TEMPLATE_REGEX, POST_METHOD, PUT_METHOD, REDIRECTION_REPSONSE, REQUEST_SENT, RESOURCE_NOT_FOUND_ERROR, RESPONSE_RECEIVED, SERVER_ERROR_REPSONSE } from "./constants";
7
- import buildEndpoint from "./helpers/build-endpoint";
8
- import defaultPathTemplateCallback from "./helpers/default-path-template-callback";
9
- import delay from "./helpers/delay";
10
- import getResponseGroup from "./helpers/get-response-group";
11
- import isCacheabilityValid from "./helpers/is-cacheability-valid";
12
- export class Getta {
13
- constructor(options) {
14
- _defineProperty(this, "_basePath", void 0);
15
-
16
- _defineProperty(this, "_bodyParser", void 0);
17
-
18
- _defineProperty(this, "_cache", void 0);
19
-
20
- _defineProperty(this, "_conditionalRequestsEnabled", void 0);
21
-
22
- _defineProperty(this, "_fetchTimeout", void 0);
23
-
24
- _defineProperty(this, "_headers", void 0);
25
-
26
- _defineProperty(this, "_log", void 0);
27
-
28
- _defineProperty(this, "_maxRedirects", void 0);
29
-
30
- _defineProperty(this, "_maxRetries", void 0);
31
-
32
- _defineProperty(this, "_optionalPathTemplateRegExp", void 0);
33
-
34
- _defineProperty(this, "_pathTemplateCallback", void 0);
35
-
36
- _defineProperty(this, "_pathTemplateRegExp", void 0);
37
-
38
- _defineProperty(this, "_performance", void 0);
39
-
40
- _defineProperty(this, "_queryParams", void 0);
41
-
42
- _defineProperty(this, "_rateLimitCount", 0);
43
-
44
- _defineProperty(this, "_rateLimitedRequestQueue", []);
45
-
46
- _defineProperty(this, "_rateLimitPerSecond", void 0);
47
-
48
- _defineProperty(this, "_rateLimitTimer", null);
49
-
50
- _defineProperty(this, "_requestRetryWait", void 0);
51
-
52
- _defineProperty(this, "_requestTracker", {
53
- active: [],
54
- pending: new Map()
55
- });
56
-
57
- _defineProperty(this, "_streamReader", void 0);
58
-
59
- const {
60
- basePath,
61
- bodyParser = DEFAULT_BODY_PARSER,
62
- cache,
63
- enableConditionalRequests = true,
64
- fetchTimeout = DEFAULT_FETCH_TIMEOUT,
65
- headers,
66
- log,
67
- maxRedirects = DEFAULT_MAX_REDIRECTS,
68
- maxRetries = DEFAULT_MAX_RETRIES,
69
- optionalPathTemplateRegExp = OPTIONAL_PATH_TEMPLATE_REGEX,
70
- pathTemplateCallback = defaultPathTemplateCallback,
71
- pathTemplateRegExp = DEFAULT_PATH_TEMPLATE_REGEX,
72
- performance,
73
- queryParams = {},
74
- rateLimitPerSecond = DEFAULT_RATE_LIMIT,
75
- requestRetryWait = DEFAULT_REQUEST_RETRY_WAIT,
76
- streamReader = JSON_FORMAT
77
- } = options;
78
-
79
- if (!basePath) {
80
- throw new Error(MISSING_BASE_PATH_ERROR);
81
- }
82
-
83
- this._basePath = basePath;
84
- this._bodyParser = bodyParser;
85
- this._cache = cache;
86
- this._conditionalRequestsEnabled = enableConditionalRequests;
87
- this._fetchTimeout = fetchTimeout;
88
- this._headers = { ...DEFAULT_HEADERS,
89
- ...(headers || {})
90
- };
91
- this._log = log;
92
- this._maxRedirects = maxRedirects;
93
- this._maxRetries = maxRetries;
94
- this._optionalPathTemplateRegExp = optionalPathTemplateRegExp;
95
- this._pathTemplateCallback = pathTemplateCallback;
96
- this._pathTemplateRegExp = pathTemplateRegExp;
97
- this._performance = performance;
98
- this._queryParams = queryParams;
99
- this._rateLimitPerSecond = rateLimitPerSecond;
100
- this._requestRetryWait = requestRetryWait;
101
- this._streamReader = streamReader;
102
- }
103
-
104
- get cache() {
105
- return this._cache;
106
- }
107
-
108
- createShortcut(name, path, {
109
- method,
110
- ...otherOptions
111
- }) {
112
- if (!FETCH_METHODS.includes(method)) {
113
- throw new Error(`${INVALID_FETCH_METHOD_ERROR} ${method}`);
114
- }
115
-
116
- this[name] = async ({
117
- method: requestMethod,
118
- ...otherOptionOverrides
119
- } = {}, context) => this[requestMethod !== null && requestMethod !== void 0 ? requestMethod : method](path, _merge({}, otherOptions, otherOptionOverrides), context);
120
- }
121
-
122
- async delete(path, options = {}, context) {
123
- return this._delete(path, options, context);
124
- }
125
-
126
- async get(path, options = {}, context) {
127
- return this._get(path, options, context);
128
- }
129
-
130
- async post(path, options, context) {
131
- return this._request(path, { ...options,
132
- method: POST_METHOD
133
- }, context);
134
- }
135
-
136
- async put(path, options, context) {
137
- return this._request(path, { ...options,
138
- method: PUT_METHOD
139
- }, context);
140
- }
141
-
142
- _addRequestToRateLimitedQueue(endpoint, options, context) {
143
- return new Promise(resolve => {
144
- this._rateLimitedRequestQueue.push([resolve, endpoint, options, context]);
145
- });
146
- }
147
-
148
- async _cacheEntryDelete(requestHash) {
149
- if (!this._cache) return false;
150
-
151
- try {
152
- return await this._cache.delete(requestHash);
153
- } catch (errors) {
154
- return Promise.reject(errors);
155
- }
156
- }
157
-
158
- async _cacheEntryGet(requestHash) {
159
- if (!this._cache) return undefined;
160
-
161
- try {
162
- return await this._cache.get(requestHash);
163
- } catch (errors) {
164
- return Promise.reject(errors);
165
- }
166
- }
167
-
168
- async _cacheEntryHas(requestHash) {
169
- if (!this._cache) return false;
170
-
171
- try {
172
- return await this._cache.has(requestHash);
173
- } catch (error) {
174
- return false;
175
- }
176
- }
177
-
178
- async _cacheEntrySet(requestHash, data, cacheHeaders) {
179
- if (!this._cache) return undefined;
180
-
181
- try {
182
- return await this._cache.set(requestHash, data, {
183
- cacheHeaders
184
- });
185
- } catch (error) {
186
- return Promise.reject(error);
187
- }
188
- }
189
-
190
- async _delete(path, {
191
- headers = {},
192
- pathTemplateData,
193
- queryParams = {},
194
- ...rest
195
- }, context) {
196
- const endpoint = buildEndpoint(this._basePath, path, {
197
- optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
198
- pathTemplateCallback: this._pathTemplateCallback,
199
- pathTemplateData,
200
- pathTemplateRegExp: this._pathTemplateRegExp,
201
- queryParams: { ...this._queryParams,
202
- ...queryParams
203
- }
204
- });
205
- const requestHash = md5(endpoint);
206
- const cacheability = await this._cacheEntryHas(requestHash);
207
-
208
- if (cacheability) {
209
- this._cacheEntryDelete(requestHash);
210
- }
211
-
212
- return this._fetch(endpoint, {
213
- headers: { ...this._headers,
214
- ...headers
215
- },
216
- method: DELETE_METHOD,
217
- ...rest
218
- }, context);
219
- }
220
-
221
- async _fetch(endpoint, options, context = {}) {
222
- context.startTime = this._performance.now();
223
-
224
- try {
225
- const {
226
- redirects,
227
- retries,
228
- ...rest
229
- } = options;
230
- return await new Promise(async (resolve, reject) => {
231
- const fetchTimer = setTimeout(() => {
232
- reject(new Error(`${FETCH_TIMEOUT_ERROR} ${this._fetchTimeout}ms.`));
233
- }, this._fetchTimeout);
234
-
235
- this._rateLimit();
236
-
237
- if (!(this._rateLimitCount < this._rateLimitPerSecond)) {
238
- clearTimeout(fetchTimer);
239
- resolve(await this._addRequestToRateLimitedQueue(endpoint, options, context));
240
- return;
241
- }
242
-
243
- if (!redirects && !retries) {
244
- var _this$_log;
245
-
246
- (_this$_log = this._log) === null || _this$_log === void 0 ? void 0 : _this$_log.call(this, REQUEST_SENT, {
247
- context: {
248
- redirects,
249
- retries,
250
- url: endpoint,
251
- ...rest,
252
- ...context
253
- },
254
- stats: {
255
- startTime: context.startTime
256
- }
257
- });
258
- }
259
-
260
- const res = await fetch(endpoint, rest);
261
- clearTimeout(fetchTimer);
262
- const {
263
- headers,
264
- status
265
- } = res;
266
- const responseGroup = getResponseGroup(status);
267
-
268
- if (responseGroup === REDIRECTION_REPSONSE && headers.get(LOCATION_HEADER)) {
269
- resolve(await this._fetchRedirectHandler(res, headers.get(LOCATION_HEADER), {
270
- redirects,
271
- status,
272
- ...rest
273
- }, context));
274
- return;
275
- }
276
-
277
- if (responseGroup === SERVER_ERROR_REPSONSE) {
278
- resolve(await this._fetchRetryHandler(res, endpoint, {
279
- retries,
280
- ...rest
281
- }, context));
282
- return;
283
- }
284
-
285
- const fetchRes = res;
286
-
287
- try {
288
- fetchRes.data = res.body ? this._bodyParser(await res[this._streamReader]()) : undefined;
289
-
290
- this._logResponse(fetchRes, endpoint, options, context);
291
-
292
- resolve(fetchRes);
293
- } catch (e) {
294
- reject([e, new Error(`Unable to ${rest.method} ${endpoint} due to previous error`)]);
295
- }
296
- });
297
- } catch (error) {
298
- const fetchRes = {
299
- errors: _castArray(error)
300
- };
301
-
302
- this._logResponse(fetchRes, endpoint, options, context);
303
-
304
- return fetchRes;
305
- }
306
- }
307
-
308
- async _fetchRedirectHandler(res, endpoint, options, context) {
309
- const {
310
- method,
311
- redirects = 1,
312
- status,
313
- ...rest
314
- } = options;
315
-
316
- if (redirects === this._maxRedirects) {
317
- const fetchRes = res;
318
- fetchRes.errors = [new Error(`${MAX_REDIRECTS_EXCEEDED_ERROR} ${this._maxRedirects}.`)];
319
-
320
- this._logResponse(fetchRes, endpoint, options, context);
321
-
322
- return fetchRes;
323
- }
324
-
325
- const redirectMethod = status === 303 ? GET_METHOD : method;
326
- return this._fetch(endpoint, {
327
- method: redirectMethod,
328
- redirects: redirects + 1,
329
- ...rest
330
- });
331
- }
332
-
333
- async _fetchRetryHandler(res, endpoint, options, context) {
334
- const {
335
- retries = 1,
336
- ...rest
337
- } = options;
338
-
339
- if (retries === this._maxRetries) {
340
- const fetchRes = res;
341
- fetchRes.errors = [new Error(`${MAX_RETRIES_EXCEEDED_ERROR} ${this._maxRetries}.`)];
342
-
343
- this._logResponse(fetchRes, endpoint, options, context);
344
-
345
- return fetchRes;
346
- }
347
-
348
- await delay(this._requestRetryWait);
349
- return this._fetch(endpoint, {
350
- retries: retries + 1,
351
- ...rest
352
- });
353
- }
354
-
355
- async _get(path, {
356
- headers = {},
357
- pathTemplateData,
358
- queryParams = {}
359
- }, context) {
360
- const endpoint = buildEndpoint(this._basePath, path, {
361
- optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
362
- pathTemplateCallback: this._pathTemplateCallback,
363
- pathTemplateData,
364
- pathTemplateRegExp: this._pathTemplateRegExp,
365
- queryParams: { ...this._queryParams,
366
- ...queryParams
367
- }
368
- });
369
- const requestHash = md5(endpoint);
370
- const cacheability = await this._cacheEntryHas(requestHash);
371
-
372
- if (cacheability) {
373
- if (isCacheabilityValid(cacheability)) {
374
- return {
375
- data: await this._cacheEntryGet(requestHash),
376
- headers: new Headers({
377
- "cache-control": cacheability.printCacheControl()
378
- })
379
- };
380
- }
381
-
382
- if (this._conditionalRequestsEnabled) {
383
- var _cacheability$metadat, _cacheability$metadat2;
384
-
385
- const etag = (_cacheability$metadat = cacheability === null || cacheability === void 0 ? void 0 : (_cacheability$metadat2 = cacheability.metadata) === null || _cacheability$metadat2 === void 0 ? void 0 : _cacheability$metadat2.etag) !== null && _cacheability$metadat !== void 0 ? _cacheability$metadat : null;
386
- if (etag) headers[IF_NONE_MATCH_HEADER] = etag;
387
- }
388
- }
389
-
390
- const pendingRequest = this._trackRequest(requestHash);
391
-
392
- if (pendingRequest) return pendingRequest;
393
- return this._getResolve(requestHash, await this._fetch(endpoint, {
394
- headers: { ...this._headers,
395
- ...headers
396
- },
397
- method: GET_METHOD
398
- }, context));
399
- }
400
-
401
- async _getResolve(requestHash, res) {
402
- const {
403
- data,
404
- headers,
405
- status
406
- } = res;
407
-
408
- if (status === NOT_FOUND_STATUS_CODE) {
409
- this._cacheEntryDelete(requestHash);
410
-
411
- if (!res.errors) {
412
- res.errors = [];
413
- }
414
-
415
- res.errors.push(new Error(RESOURCE_NOT_FOUND_ERROR));
416
- } else if (status === NOT_MODIFIED_STATUS_CODE && headers) {
417
- const cachedData = await this._cacheEntryGet(requestHash);
418
-
419
- if (cachedData) {
420
- this._cacheEntrySet(requestHash, cachedData, {
421
- cacheControl: headers.get(CACHE_CONTROL_HEADER) || undefined,
422
- etag: headers.get(ETAG_HEADER) || undefined
423
- });
424
-
425
- res.data = cachedData;
426
- }
427
- } else if (data && headers) {
428
- this._cacheEntrySet(requestHash, data, {
429
- cacheControl: headers.get(CACHE_CONTROL_HEADER) || undefined,
430
- etag: headers.get(ETAG_HEADER) || undefined
431
- });
432
- }
433
-
434
- this._resolvePendingRequests(requestHash, res);
435
-
436
- this._requestTracker.active = this._requestTracker.active.filter(value => value !== requestHash);
437
- return res;
438
- }
439
-
440
- _logResponse(res, endpoint, options, context) {
441
- var _this$_log2;
442
-
443
- const {
444
- data,
445
- errors,
446
- headers,
447
- status
448
- } = res;
449
- const {
450
- redirects,
451
- retries
452
- } = options;
453
- const {
454
- startTime,
455
- ...otherContext
456
- } = context;
457
-
458
- const endTime = this._performance.now();
459
-
460
- const duration = endTime - startTime;
461
- (_this$_log2 = this._log) === null || _this$_log2 === void 0 ? void 0 : _this$_log2.call(this, RESPONSE_RECEIVED, {
462
- context: {
463
- body: data ? {
464
- data
465
- } : {
466
- errors: errors !== null && errors !== void 0 ? errors : []
467
- },
468
- headers,
469
- method: options.method,
470
- redirects,
471
- retries,
472
- status,
473
- url: endpoint,
474
- ...otherContext
475
- },
476
- stats: {
477
- duration,
478
- endTime,
479
- startTime
480
- }
481
- });
482
- }
483
-
484
- _rateLimit() {
485
- if (!this._rateLimitTimer) {
486
- this._rateLimitTimer = setTimeout(() => {
487
- this._rateLimitTimer = null;
488
- this._rateLimitCount = 0;
489
-
490
- if (this._rateLimitedRequestQueue.length) {
491
- this._releaseRateLimitedRequestQueue();
492
- }
493
- }, 1000);
494
- }
495
-
496
- this._rateLimitCount += 1;
497
- }
498
-
499
- _releaseRateLimitedRequestQueue() {
500
- this._rateLimitedRequestQueue.forEach(async ([resolve, endpoint, options, context]) => {
501
- resolve(await this._fetch(endpoint, options, context));
502
- });
503
-
504
- this._rateLimitedRequestQueue = [];
505
- }
506
-
507
- async _request(path, {
508
- body,
509
- headers,
510
- method,
511
- pathTemplateData,
512
- queryParams,
513
- ...rest
514
- }, context) {
515
- const endpoint = buildEndpoint(this._basePath, path, {
516
- optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
517
- pathTemplateCallback: this._pathTemplateCallback,
518
- pathTemplateData,
519
- pathTemplateRegExp: this._pathTemplateRegExp,
520
- queryParams: { ...this._queryParams,
521
- ...queryParams
522
- }
523
- });
524
- return this._fetch(endpoint, {
525
- body,
526
- headers: { ...this._headers,
527
- ...headers
528
- },
529
- method,
530
- ...rest
531
- }, context);
532
- }
533
-
534
- _resolvePendingRequests(requestHash, responseData) {
535
- const pendingRequests = this._requestTracker.pending.get(requestHash);
536
-
537
- if (!pendingRequests) return;
538
- pendingRequests.forEach(({
539
- resolve
540
- }) => {
541
- resolve(responseData);
542
- });
543
-
544
- this._requestTracker.pending.delete(requestHash);
545
- }
546
-
547
- _setPendingRequest(requestHash, resolver) {
548
- let pending = this._requestTracker.pending.get(requestHash);
549
-
550
- if (!pending) pending = [];
551
- pending.push(resolver);
552
-
553
- this._requestTracker.pending.set(requestHash, pending);
554
- }
555
-
556
- _trackRequest(requestHash) {
557
- if (this._requestTracker.active.includes(requestHash)) {
558
- return new Promise(resolve => {
559
- this._setPendingRequest(requestHash, {
560
- resolve
561
- });
562
- });
563
- }
564
-
565
- this._requestTracker.active.push(requestHash);
566
- }
567
-
568
- }
569
- export const createRestClient = (options, shortcuts) => {
570
- const getta = new Getta(options);
571
- if (!shortcuts) return getta;
572
- Object.keys(shortcuts).forEach(key => {
573
- getta.createShortcut(key, ...shortcuts[key]);
574
- });
575
- return getta;
576
- };
File without changes
@@ -1,17 +0,0 @@
1
- export declare const PRD_136_7317: {
2
- body: any;
3
- url: string;
4
- };
5
- export declare const PRD_180_1387: {
6
- body: any;
7
- url: string;
8
- };
9
- export declare const PRD_183_3905: {
10
- body: any;
11
- url: string;
12
- };
13
- export declare const PRD_202_3315: {
14
- body: any;
15
- url: string;
16
- };
17
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/__tests__/data/index.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY;;;CAGxB,CAAC;AAEF,eAAO,MAAM,YAAY;;;CAGxB,CAAC;AAEF,eAAO,MAAM,YAAY;;;CAGxB,CAAC;AAEF,eAAO,MAAM,YAAY;;;CAGxB,CAAC"}
@@ -1,29 +0,0 @@
1
- import Cachemap from "@cachemap/core";
2
- import { PlainObject } from "@repodog/types";
3
- import { RequestOptions } from "../../types";
4
- import { MockRequestCallbackParams, TearDownTestParams } from "../types";
5
- export declare const basePath = "https://tesco.com";
6
- export declare const defaultPath = "/direct/rest/content/catalog/{type}/{id,+}?format={brief|standard}";
7
- export declare const defaultPathTemplateData: {
8
- "brief|standard": string;
9
- "id,+": string;
10
- type: string;
11
- };
12
- export declare const defaultPayload = "{ payload: true }";
13
- export declare const idPathTemplateData: {
14
- "id,+": string;
15
- };
16
- export declare const pathTemplateDataWithoutID: {
17
- "brief|standard": string;
18
- type: string;
19
- };
20
- export declare const defaultEtag = "33a64df551425fcc55e4d42a148795d9f25f89d4";
21
- export declare const defaultHeaders: {
22
- "cache-control": string;
23
- "content-type": string;
24
- etag: string;
25
- };
26
- export declare function getCache(): Cachemap;
27
- export declare function mockRequest(path: string, body: PlainObject, { headers, pathTemplateData, queryParams }: RequestOptions | undefined, callback: (options: MockRequestCallbackParams) => void): void;
28
- export declare function tearDownTest({ fetchMock, restClient }: TearDownTestParams): Promise<void>;
29
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/__tests__/helpers/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAK7C,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,yBAAyB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEzE,eAAO,MAAM,QAAQ,sBAAsB,CAAC;AAE5C,eAAO,MAAM,WAAW,uEAAuE,CAAC;AAEhG,eAAO,MAAM,uBAAuB;;;;CAAwE,CAAC;AAE7G,eAAO,MAAM,cAAc,sBAAsB,CAAC;AAElD,eAAO,MAAM,kBAAkB;;CAAyB,CAAC;AAEzD,eAAO,MAAM,yBAAyB;;;CAAoD,CAAC;AAE3F,eAAO,MAAM,WAAW,6CAA6C,CAAC;AAEtE,eAAO,MAAM,cAAc;;;;CAI1B,CAAC;AAEF,wBAAgB,QAAQ,aAMvB;AAED,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,EACjB,EAAE,OAAY,EAAE,gBAAgB,EAAE,WAAW,EAAE,4BAAqB,EACpE,QAAQ,EAAE,CAAC,OAAO,EAAE,yBAAyB,KAAK,IAAI,QAavD;AAED,wBAAsB,YAAY,CAAC,EAAE,SAAS,EAAE,UAAU,EAAE,EAAE,kBAAkB,iBAG/E"}
@@ -1,13 +0,0 @@
1
- import { StringObject } from "@repodog/types";
2
- import { FetchMockStatic } from "fetch-mock";
3
- import { Getta } from "../main";
4
- export interface TearDownTestParams {
5
- fetchMock: FetchMockStatic;
6
- restClient: Getta;
7
- }
8
- export interface MockRequestCallbackParams {
9
- body?: BodyInit;
10
- endpoint: string;
11
- headers: StringObject;
12
- }
13
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/__tests__/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAEhC,MAAM,WAAW,kBAAkB;IACjC,SAAS,EAAE,eAAe,CAAC;IAC3B,UAAU,EAAE,KAAK,CAAC;CACnB;AAED,MAAM,WAAW,yBAAyB;IACxC,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,YAAY,CAAC;CACvB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/constants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAE7C,eAAO,MAAM,mBAAmB,eAAyB,CAAC;AAC1D,eAAO,MAAM,WAAW,QAAkB,CAAC;AAC3C,eAAO,MAAM,gBAAgB,YAAsB,CAAC;AACpD,eAAO,MAAM,WAAW,QAAkB,CAAC;AAC3C,eAAO,MAAM,WAAW,QAAkB,CAAC;AAE3C,eAAO,MAAM,cAAc;;;;;;CAM1B,CAAC;AAEF,eAAO,MAAM,mBAAmB,SAAU,WAAW,gBAAS,CAAC;AAC/D,eAAO,MAAM,qBAAqB,MAAgB,CAAC;AACnD,eAAO,MAAM,eAAe;;CAAyC,CAAC;AACtE,eAAO,MAAM,qBAAqB,GAAa,CAAC;AAChD,eAAO,MAAM,mBAAmB,GAAa,CAAC;AAC9C,eAAO,MAAM,2BAA2B,QAAmD,CAAC;AAC5F,eAAO,MAAM,4BAA4B,QAAyB,CAAC;AACnE,eAAO,MAAM,kBAAkB,KAAK,CAAC;AACrC,eAAO,MAAM,0BAA0B,MAAM,CAAC;AAE9C,eAAO,MAAM,uBAAuB,gGACV,CAAC;AAE3B,eAAO,MAAM,4BAA4B,mEAAmE,CAAC;AAE7G,eAAO,MAAM,0BAA0B,iEAAiE,CAAC;AAEzG,eAAO,MAAM,0BAA0B,6EAA6E,CAAC;AAErH,eAAO,MAAM,wBAAwB,iDAAiD,CAAC;AAEvF,eAAO,MAAM,mBAAmB,+DAA+D,CAAC;AAEhG,eAAO,MAAM,UAAU,OAAiB,CAAC;AACzC,eAAO,MAAM,WAAW,QAAkB,CAAC;AAC3C,eAAO,MAAM,UAAU,OAAiB,CAAC;AACzC,eAAO,MAAM,aAAa,UAAoB,CAAC;AAE/C,eAAO,MAAM,aAAa,uCAAuD,CAAC;AAElF,eAAO,MAAM,oBAAoB,eAAyB,CAAC;AAC3D,eAAO,MAAM,mBAAmB,cAAwB,CAAC;AACzD,eAAO,MAAM,oBAAoB,eAAyB,CAAC;AAC3D,eAAO,MAAM,qBAAqB,eAAyB,CAAC;AAC5D,eAAO,MAAM,qBAAqB,eAAyB,CAAC;AAE5D,eAAO,MAAM,wBAAwB,KAAe,CAAC;AACrD,eAAO,MAAM,qBAAqB,KAAe,CAAC;AAElD,eAAO,MAAM,aAAa,UAAoB,CAAC;AAC/C,eAAO,MAAM,WAAW,QAAkB,CAAC;AAC3C,eAAO,MAAM,eAAe,YAAsB,CAAC;AACnD,eAAO,MAAM,oBAAoB,iBAA2B,CAAC;AAC7D,eAAO,MAAM,oBAAoB,iBAA2B,CAAC;AAE7D,eAAO,MAAM,YAAY,gBAA0B,CAAC;AACpD,eAAO,MAAM,iBAAiB,qBAA+B,CAAC"}
@@ -1,3 +0,0 @@
1
- import { BuildEndpointOptions } from "./types";
2
- export default function buildEndpoint(basePath: string, path: string, { optionalPathTemplateRegExp, pathTemplateCallback, pathTemplateData, pathTemplateRegExp, queryParams, }: BuildEndpointOptions): string;
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/helpers/build-endpoint/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAE/C,MAAM,CAAC,OAAO,UAAU,aAAa,CACnC,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,MAAM,EACZ,EACE,0BAA0B,EAC1B,oBAAoB,EACpB,gBAAgB,EAChB,kBAAkB,EAClB,WAAW,GACZ,EAAE,oBAAoB,UAqBxB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../../src/helpers/build-endpoint/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAEnE,MAAM,WAAW,oBAAqB,SAAQ,IAAI,CAAC,cAAc,EAAE,SAAS,CAAC;IAC3E,0BAA0B,EAAE,MAAM,CAAC;IACnC,oBAAoB,EAAE,oBAAoB,CAAC;IAC3C,kBAAkB,EAAE,MAAM,CAAC;CAC5B"}
@@ -1,3 +0,0 @@
1
- import { StringObject } from "@repodog/types";
2
- export default function defaultPathTemplateCallback(pathTemplate: string, data: StringObject, pathTemplateRegExp: RegExp): string;
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/helpers/default-path-template-callback/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C,MAAM,CAAC,OAAO,UAAU,2BAA2B,CACjD,YAAY,EAAE,MAAM,EACpB,IAAI,EAAE,YAAY,EAClB,kBAAkB,EAAE,MAAM,UAU3B"}
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=index.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../../../src/helpers/default-path-template-callback/index.test.ts"],"names":[],"mappings":""}
@@ -1,2 +0,0 @@
1
- export default function delay(ms: number): Promise<unknown>;
2
- //# sourceMappingURL=index.d.ts.map