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.
- package/README.md +165 -7
- package/dist/main/index.mjs +621 -0
- package/dist/main/index.mjs.map +1 -0
- package/dist/tsconfig.build.tsbuildinfo +1 -0
- package/{lib → dist}/types/constants.d.ts +2 -2
- package/dist/types/constants.d.ts.map +1 -0
- package/dist/types/helpers/buildEndpoint/index.d.ts +3 -0
- package/dist/types/helpers/buildEndpoint/index.d.ts.map +1 -0
- package/{lib/types/helpers/build-endpoint → dist/types/helpers/buildEndpoint}/types.d.ts +2 -2
- package/dist/types/helpers/buildEndpoint/types.d.ts.map +1 -0
- package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts +2 -0
- package/dist/types/helpers/defaultPathTemplateCallback/index.d.ts.map +1 -0
- package/dist/types/helpers/delay/index.d.ts +2 -0
- package/{lib → dist}/types/helpers/delay/index.d.ts.map +1 -1
- package/dist/types/helpers/getResponseGroup/index.d.ts +2 -0
- package/dist/types/helpers/getResponseGroup/index.d.ts.map +1 -0
- package/dist/types/helpers/isCacheabilityValid/index.d.ts +3 -0
- package/dist/types/helpers/isCacheabilityValid/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/index.d.ts.map +1 -0
- package/{lib → dist}/types/main.d.ts +12 -12
- package/dist/types/main.d.ts.map +1 -0
- package/dist/types/types.d.ts +161 -0
- package/dist/types/types.d.ts.map +1 -0
- package/package.json +113 -145
- package/src/{__tests__ → __testUtils__}/data/136-7317.json +1 -1
- package/src/{__tests__ → __testUtils__}/data/180-1387.json +1 -1
- package/src/{__tests__ → __testUtils__}/data/183-3905.json +1 -1
- package/src/{__tests__ → __testUtils__}/data/202-3315.json +1 -1
- package/src/__testUtils__/data/index.ts +23 -0
- package/src/__testUtils__/helpers/index.ts +49 -0
- package/src/constants.ts +28 -28
- package/src/helpers/buildEndpoint/index.ts +34 -0
- package/src/helpers/{build-endpoint → buildEndpoint}/types.ts +2 -2
- package/src/helpers/defaultPathTemplateCallback/index.test.ts +11 -0
- package/src/helpers/defaultPathTemplateCallback/index.ts +17 -0
- package/src/helpers/delay/index.ts +2 -2
- package/src/helpers/getResponseGroup/index.ts +25 -0
- package/src/helpers/isCacheabilityValid/index.ts +6 -0
- package/src/index.ts +4 -4
- package/src/main.test.ts +283 -411
- package/src/main.ts +238 -241
- package/src/types.ts +114 -16
- package/tsconfig.build.json +14 -0
- package/tsconfig.json +9 -0
- package/CHANGELOG.md +0 -244
- package/jest.setup.js +0 -10
- package/lib/browser/index.js +0 -2
- package/lib/browser/index.js.map +0 -1
- package/lib/browser/production.analysis.txt +0 -91
- package/lib/main/constants.js +0 -97
- package/lib/main/helpers/build-endpoint/index.js +0 -38
- package/lib/main/helpers/build-endpoint/types.js +0 -1
- package/lib/main/helpers/default-path-template-callback/index.js +0 -16
- package/lib/main/helpers/delay/index.js +0 -12
- package/lib/main/helpers/get-response-group/index.js +0 -27
- package/lib/main/helpers/is-cacheability-valid/index.js +0 -13
- package/lib/main/index.js +0 -62
- package/lib/main/main.js +0 -580
- package/lib/main/types.js +0 -1
- package/lib/module/constants.js +0 -49
- package/lib/module/helpers/build-endpoint/index.js +0 -28
- package/lib/module/helpers/build-endpoint/types.js +0 -0
- package/lib/module/helpers/default-path-template-callback/index.js +0 -9
- package/lib/module/helpers/delay/index.js +0 -4
- package/lib/module/helpers/get-response-group/index.js +0 -19
- package/lib/module/helpers/is-cacheability-valid/index.js +0 -6
- package/lib/module/index.js +0 -4
- package/lib/module/main.js +0 -576
- package/lib/module/types.js +0 -0
- package/lib/types/__tests__/data/index.d.ts +0 -17
- package/lib/types/__tests__/data/index.d.ts.map +0 -1
- package/lib/types/__tests__/helpers/index.d.ts +0 -29
- package/lib/types/__tests__/helpers/index.d.ts.map +0 -1
- package/lib/types/__tests__/types.d.ts +0 -13
- package/lib/types/__tests__/types.d.ts.map +0 -1
- package/lib/types/constants.d.ts.map +0 -1
- package/lib/types/helpers/build-endpoint/index.d.ts +0 -3
- package/lib/types/helpers/build-endpoint/index.d.ts.map +0 -1
- package/lib/types/helpers/build-endpoint/types.d.ts.map +0 -1
- package/lib/types/helpers/default-path-template-callback/index.d.ts +0 -3
- package/lib/types/helpers/default-path-template-callback/index.d.ts.map +0 -1
- package/lib/types/helpers/default-path-template-callback/index.test.d.ts +0 -2
- package/lib/types/helpers/default-path-template-callback/index.test.d.ts.map +0 -1
- package/lib/types/helpers/delay/index.d.ts +0 -2
- package/lib/types/helpers/get-response-group/index.d.ts +0 -2
- package/lib/types/helpers/get-response-group/index.d.ts.map +0 -1
- package/lib/types/helpers/is-cacheability-valid/index.d.ts +0 -3
- package/lib/types/helpers/is-cacheability-valid/index.d.ts.map +0 -1
- package/lib/types/index.d.ts +0 -5
- package/lib/types/index.d.ts.map +0 -1
- package/lib/types/main.d.ts.map +0 -1
- package/lib/types/main.test.d.ts +0 -2
- package/lib/types/main.test.d.ts.map +0 -1
- package/lib/types/types.d.ts +0 -69
- package/lib/types/types.d.ts.map +0 -1
- package/src/__tests__/data/index.ts +0 -19
- package/src/__tests__/helpers/index.ts +0 -61
- package/src/__tests__/types.ts +0 -14
- package/src/helpers/build-endpoint/index.ts +0 -34
- package/src/helpers/default-path-template-callback/index.test.ts +0 -11
- package/src/helpers/default-path-template-callback/index.ts +0 -16
- package/src/helpers/get-response-group/index.ts +0 -22
- package/src/helpers/is-cacheability-valid/index.ts +0 -6
package/lib/main/main.js
DELETED
|
@@ -1,580 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
-
|
|
5
|
-
Object.defineProperty(exports, "__esModule", {
|
|
6
|
-
value: true
|
|
7
|
-
});
|
|
8
|
-
exports.createRestClient = exports.Getta = void 0;
|
|
9
|
-
|
|
10
|
-
require("core-js/modules/es.promise.js");
|
|
11
|
-
|
|
12
|
-
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
13
|
-
|
|
14
|
-
var _merge2 = _interopRequireDefault(require("lodash/merge"));
|
|
15
|
-
|
|
16
|
-
var _castArray2 = _interopRequireDefault(require("lodash/castArray"));
|
|
17
|
-
|
|
18
|
-
var _md = _interopRequireDefault(require("md5"));
|
|
19
|
-
|
|
20
|
-
var _constants = require("./constants");
|
|
21
|
-
|
|
22
|
-
var _buildEndpoint = _interopRequireDefault(require("./helpers/build-endpoint"));
|
|
23
|
-
|
|
24
|
-
var _defaultPathTemplateCallback = _interopRequireDefault(require("./helpers/default-path-template-callback"));
|
|
25
|
-
|
|
26
|
-
var _delay = _interopRequireDefault(require("./helpers/delay"));
|
|
27
|
-
|
|
28
|
-
var _getResponseGroup = _interopRequireDefault(require("./helpers/get-response-group"));
|
|
29
|
-
|
|
30
|
-
var _isCacheabilityValid = _interopRequireDefault(require("./helpers/is-cacheability-valid"));
|
|
31
|
-
|
|
32
|
-
class Getta {
|
|
33
|
-
constructor(options) {
|
|
34
|
-
(0, _defineProperty2.default)(this, "_basePath", void 0);
|
|
35
|
-
(0, _defineProperty2.default)(this, "_bodyParser", void 0);
|
|
36
|
-
(0, _defineProperty2.default)(this, "_cache", void 0);
|
|
37
|
-
(0, _defineProperty2.default)(this, "_conditionalRequestsEnabled", void 0);
|
|
38
|
-
(0, _defineProperty2.default)(this, "_fetchTimeout", void 0);
|
|
39
|
-
(0, _defineProperty2.default)(this, "_headers", void 0);
|
|
40
|
-
(0, _defineProperty2.default)(this, "_log", void 0);
|
|
41
|
-
(0, _defineProperty2.default)(this, "_maxRedirects", void 0);
|
|
42
|
-
(0, _defineProperty2.default)(this, "_maxRetries", void 0);
|
|
43
|
-
(0, _defineProperty2.default)(this, "_optionalPathTemplateRegExp", void 0);
|
|
44
|
-
(0, _defineProperty2.default)(this, "_pathTemplateCallback", void 0);
|
|
45
|
-
(0, _defineProperty2.default)(this, "_pathTemplateRegExp", void 0);
|
|
46
|
-
(0, _defineProperty2.default)(this, "_performance", void 0);
|
|
47
|
-
(0, _defineProperty2.default)(this, "_queryParams", void 0);
|
|
48
|
-
(0, _defineProperty2.default)(this, "_rateLimitCount", 0);
|
|
49
|
-
(0, _defineProperty2.default)(this, "_rateLimitedRequestQueue", []);
|
|
50
|
-
(0, _defineProperty2.default)(this, "_rateLimitPerSecond", void 0);
|
|
51
|
-
(0, _defineProperty2.default)(this, "_rateLimitTimer", null);
|
|
52
|
-
(0, _defineProperty2.default)(this, "_requestRetryWait", void 0);
|
|
53
|
-
(0, _defineProperty2.default)(this, "_requestTracker", {
|
|
54
|
-
active: [],
|
|
55
|
-
pending: new Map()
|
|
56
|
-
});
|
|
57
|
-
(0, _defineProperty2.default)(this, "_streamReader", void 0);
|
|
58
|
-
const {
|
|
59
|
-
basePath,
|
|
60
|
-
bodyParser = _constants.DEFAULT_BODY_PARSER,
|
|
61
|
-
cache,
|
|
62
|
-
enableConditionalRequests = true,
|
|
63
|
-
fetchTimeout = _constants.DEFAULT_FETCH_TIMEOUT,
|
|
64
|
-
headers,
|
|
65
|
-
log,
|
|
66
|
-
maxRedirects = _constants.DEFAULT_MAX_REDIRECTS,
|
|
67
|
-
maxRetries = _constants.DEFAULT_MAX_RETRIES,
|
|
68
|
-
optionalPathTemplateRegExp = _constants.OPTIONAL_PATH_TEMPLATE_REGEX,
|
|
69
|
-
pathTemplateCallback = _defaultPathTemplateCallback.default,
|
|
70
|
-
pathTemplateRegExp = _constants.DEFAULT_PATH_TEMPLATE_REGEX,
|
|
71
|
-
performance,
|
|
72
|
-
queryParams = {},
|
|
73
|
-
rateLimitPerSecond = _constants.DEFAULT_RATE_LIMIT,
|
|
74
|
-
requestRetryWait = _constants.DEFAULT_REQUEST_RETRY_WAIT,
|
|
75
|
-
streamReader = _constants.JSON_FORMAT
|
|
76
|
-
} = options;
|
|
77
|
-
|
|
78
|
-
if (!basePath) {
|
|
79
|
-
throw new Error(_constants.MISSING_BASE_PATH_ERROR);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
this._basePath = basePath;
|
|
83
|
-
this._bodyParser = bodyParser;
|
|
84
|
-
this._cache = cache;
|
|
85
|
-
this._conditionalRequestsEnabled = enableConditionalRequests;
|
|
86
|
-
this._fetchTimeout = fetchTimeout;
|
|
87
|
-
this._headers = { ..._constants.DEFAULT_HEADERS,
|
|
88
|
-
...(headers || {})
|
|
89
|
-
};
|
|
90
|
-
this._log = log;
|
|
91
|
-
this._maxRedirects = maxRedirects;
|
|
92
|
-
this._maxRetries = maxRetries;
|
|
93
|
-
this._optionalPathTemplateRegExp = optionalPathTemplateRegExp;
|
|
94
|
-
this._pathTemplateCallback = pathTemplateCallback;
|
|
95
|
-
this._pathTemplateRegExp = pathTemplateRegExp;
|
|
96
|
-
this._performance = performance;
|
|
97
|
-
this._queryParams = queryParams;
|
|
98
|
-
this._rateLimitPerSecond = rateLimitPerSecond;
|
|
99
|
-
this._requestRetryWait = requestRetryWait;
|
|
100
|
-
this._streamReader = streamReader;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
get cache() {
|
|
104
|
-
return this._cache;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
createShortcut(name, path, {
|
|
108
|
-
method,
|
|
109
|
-
...otherOptions
|
|
110
|
-
}) {
|
|
111
|
-
if (!_constants.FETCH_METHODS.includes(method)) {
|
|
112
|
-
throw new Error(`${_constants.INVALID_FETCH_METHOD_ERROR} ${method}`);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
this[name] = async ({
|
|
116
|
-
method: requestMethod,
|
|
117
|
-
...otherOptionOverrides
|
|
118
|
-
} = {}, context) => this[requestMethod !== null && requestMethod !== void 0 ? requestMethod : method](path, (0, _merge2.default)({}, otherOptions, otherOptionOverrides), context);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
async delete(path, options = {}, context) {
|
|
122
|
-
return this._delete(path, options, context);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
async get(path, options = {}, context) {
|
|
126
|
-
return this._get(path, options, context);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
async post(path, options, context) {
|
|
130
|
-
return this._request(path, { ...options,
|
|
131
|
-
method: _constants.POST_METHOD
|
|
132
|
-
}, context);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
async put(path, options, context) {
|
|
136
|
-
return this._request(path, { ...options,
|
|
137
|
-
method: _constants.PUT_METHOD
|
|
138
|
-
}, context);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
_addRequestToRateLimitedQueue(endpoint, options, context) {
|
|
142
|
-
return new Promise(resolve => {
|
|
143
|
-
this._rateLimitedRequestQueue.push([resolve, endpoint, options, context]);
|
|
144
|
-
});
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async _cacheEntryDelete(requestHash) {
|
|
148
|
-
if (!this._cache) return false;
|
|
149
|
-
|
|
150
|
-
try {
|
|
151
|
-
return await this._cache.delete(requestHash);
|
|
152
|
-
} catch (errors) {
|
|
153
|
-
return Promise.reject(errors);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
async _cacheEntryGet(requestHash) {
|
|
158
|
-
if (!this._cache) return undefined;
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
return await this._cache.get(requestHash);
|
|
162
|
-
} catch (errors) {
|
|
163
|
-
return Promise.reject(errors);
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
async _cacheEntryHas(requestHash) {
|
|
168
|
-
if (!this._cache) return false;
|
|
169
|
-
|
|
170
|
-
try {
|
|
171
|
-
return await this._cache.has(requestHash);
|
|
172
|
-
} catch (error) {
|
|
173
|
-
return false;
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
async _cacheEntrySet(requestHash, data, cacheHeaders) {
|
|
178
|
-
if (!this._cache) return undefined;
|
|
179
|
-
|
|
180
|
-
try {
|
|
181
|
-
return await this._cache.set(requestHash, data, {
|
|
182
|
-
cacheHeaders
|
|
183
|
-
});
|
|
184
|
-
} catch (error) {
|
|
185
|
-
return Promise.reject(error);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async _delete(path, {
|
|
190
|
-
headers = {},
|
|
191
|
-
pathTemplateData,
|
|
192
|
-
queryParams = {},
|
|
193
|
-
...rest
|
|
194
|
-
}, context) {
|
|
195
|
-
const endpoint = (0, _buildEndpoint.default)(this._basePath, path, {
|
|
196
|
-
optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
|
|
197
|
-
pathTemplateCallback: this._pathTemplateCallback,
|
|
198
|
-
pathTemplateData,
|
|
199
|
-
pathTemplateRegExp: this._pathTemplateRegExp,
|
|
200
|
-
queryParams: { ...this._queryParams,
|
|
201
|
-
...queryParams
|
|
202
|
-
}
|
|
203
|
-
});
|
|
204
|
-
const requestHash = (0, _md.default)(endpoint);
|
|
205
|
-
const cacheability = await this._cacheEntryHas(requestHash);
|
|
206
|
-
|
|
207
|
-
if (cacheability) {
|
|
208
|
-
this._cacheEntryDelete(requestHash);
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
return this._fetch(endpoint, {
|
|
212
|
-
headers: { ...this._headers,
|
|
213
|
-
...headers
|
|
214
|
-
},
|
|
215
|
-
method: _constants.DELETE_METHOD,
|
|
216
|
-
...rest
|
|
217
|
-
}, context);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
async _fetch(endpoint, options, context = {}) {
|
|
221
|
-
context.startTime = this._performance.now();
|
|
222
|
-
|
|
223
|
-
try {
|
|
224
|
-
const {
|
|
225
|
-
redirects,
|
|
226
|
-
retries,
|
|
227
|
-
...rest
|
|
228
|
-
} = options;
|
|
229
|
-
return await new Promise(async (resolve, reject) => {
|
|
230
|
-
const fetchTimer = setTimeout(() => {
|
|
231
|
-
reject(new Error(`${_constants.FETCH_TIMEOUT_ERROR} ${this._fetchTimeout}ms.`));
|
|
232
|
-
}, this._fetchTimeout);
|
|
233
|
-
|
|
234
|
-
this._rateLimit();
|
|
235
|
-
|
|
236
|
-
if (!(this._rateLimitCount < this._rateLimitPerSecond)) {
|
|
237
|
-
clearTimeout(fetchTimer);
|
|
238
|
-
resolve(await this._addRequestToRateLimitedQueue(endpoint, options, context));
|
|
239
|
-
return;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
if (!redirects && !retries) {
|
|
243
|
-
var _this$_log;
|
|
244
|
-
|
|
245
|
-
(_this$_log = this._log) === null || _this$_log === void 0 ? void 0 : _this$_log.call(this, _constants.REQUEST_SENT, {
|
|
246
|
-
context: {
|
|
247
|
-
redirects,
|
|
248
|
-
retries,
|
|
249
|
-
url: endpoint,
|
|
250
|
-
...rest,
|
|
251
|
-
...context
|
|
252
|
-
},
|
|
253
|
-
stats: {
|
|
254
|
-
startTime: context.startTime
|
|
255
|
-
}
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const res = await fetch(endpoint, rest);
|
|
260
|
-
clearTimeout(fetchTimer);
|
|
261
|
-
const {
|
|
262
|
-
headers,
|
|
263
|
-
status
|
|
264
|
-
} = res;
|
|
265
|
-
const responseGroup = (0, _getResponseGroup.default)(status);
|
|
266
|
-
|
|
267
|
-
if (responseGroup === _constants.REDIRECTION_REPSONSE && headers.get(_constants.LOCATION_HEADER)) {
|
|
268
|
-
resolve(await this._fetchRedirectHandler(res, headers.get(_constants.LOCATION_HEADER), {
|
|
269
|
-
redirects,
|
|
270
|
-
status,
|
|
271
|
-
...rest
|
|
272
|
-
}, context));
|
|
273
|
-
return;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
if (responseGroup === _constants.SERVER_ERROR_REPSONSE) {
|
|
277
|
-
resolve(await this._fetchRetryHandler(res, endpoint, {
|
|
278
|
-
retries,
|
|
279
|
-
...rest
|
|
280
|
-
}, context));
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
const fetchRes = res;
|
|
285
|
-
|
|
286
|
-
try {
|
|
287
|
-
fetchRes.data = res.body ? this._bodyParser(await res[this._streamReader]()) : undefined;
|
|
288
|
-
|
|
289
|
-
this._logResponse(fetchRes, endpoint, options, context);
|
|
290
|
-
|
|
291
|
-
resolve(fetchRes);
|
|
292
|
-
} catch (e) {
|
|
293
|
-
reject([e, new Error(`Unable to ${rest.method} ${endpoint} due to previous error`)]);
|
|
294
|
-
}
|
|
295
|
-
});
|
|
296
|
-
} catch (error) {
|
|
297
|
-
const fetchRes = {
|
|
298
|
-
errors: (0, _castArray2.default)(error)
|
|
299
|
-
};
|
|
300
|
-
|
|
301
|
-
this._logResponse(fetchRes, endpoint, options, context);
|
|
302
|
-
|
|
303
|
-
return fetchRes;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
async _fetchRedirectHandler(res, endpoint, options, context) {
|
|
308
|
-
const {
|
|
309
|
-
method,
|
|
310
|
-
redirects = 1,
|
|
311
|
-
status,
|
|
312
|
-
...rest
|
|
313
|
-
} = options;
|
|
314
|
-
|
|
315
|
-
if (redirects === this._maxRedirects) {
|
|
316
|
-
const fetchRes = res;
|
|
317
|
-
fetchRes.errors = [new Error(`${_constants.MAX_REDIRECTS_EXCEEDED_ERROR} ${this._maxRedirects}.`)];
|
|
318
|
-
|
|
319
|
-
this._logResponse(fetchRes, endpoint, options, context);
|
|
320
|
-
|
|
321
|
-
return fetchRes;
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
const redirectMethod = status === 303 ? _constants.GET_METHOD : method;
|
|
325
|
-
return this._fetch(endpoint, {
|
|
326
|
-
method: redirectMethod,
|
|
327
|
-
redirects: redirects + 1,
|
|
328
|
-
...rest
|
|
329
|
-
});
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
async _fetchRetryHandler(res, endpoint, options, context) {
|
|
333
|
-
const {
|
|
334
|
-
retries = 1,
|
|
335
|
-
...rest
|
|
336
|
-
} = options;
|
|
337
|
-
|
|
338
|
-
if (retries === this._maxRetries) {
|
|
339
|
-
const fetchRes = res;
|
|
340
|
-
fetchRes.errors = [new Error(`${_constants.MAX_RETRIES_EXCEEDED_ERROR} ${this._maxRetries}.`)];
|
|
341
|
-
|
|
342
|
-
this._logResponse(fetchRes, endpoint, options, context);
|
|
343
|
-
|
|
344
|
-
return fetchRes;
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
await (0, _delay.default)(this._requestRetryWait);
|
|
348
|
-
return this._fetch(endpoint, {
|
|
349
|
-
retries: retries + 1,
|
|
350
|
-
...rest
|
|
351
|
-
});
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
async _get(path, {
|
|
355
|
-
headers = {},
|
|
356
|
-
pathTemplateData,
|
|
357
|
-
queryParams = {}
|
|
358
|
-
}, context) {
|
|
359
|
-
const endpoint = (0, _buildEndpoint.default)(this._basePath, path, {
|
|
360
|
-
optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
|
|
361
|
-
pathTemplateCallback: this._pathTemplateCallback,
|
|
362
|
-
pathTemplateData,
|
|
363
|
-
pathTemplateRegExp: this._pathTemplateRegExp,
|
|
364
|
-
queryParams: { ...this._queryParams,
|
|
365
|
-
...queryParams
|
|
366
|
-
}
|
|
367
|
-
});
|
|
368
|
-
const requestHash = (0, _md.default)(endpoint);
|
|
369
|
-
const cacheability = await this._cacheEntryHas(requestHash);
|
|
370
|
-
|
|
371
|
-
if (cacheability) {
|
|
372
|
-
if ((0, _isCacheabilityValid.default)(cacheability)) {
|
|
373
|
-
return {
|
|
374
|
-
data: await this._cacheEntryGet(requestHash),
|
|
375
|
-
headers: new Headers({
|
|
376
|
-
"cache-control": cacheability.printCacheControl()
|
|
377
|
-
})
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
if (this._conditionalRequestsEnabled) {
|
|
382
|
-
var _cacheability$metadat, _cacheability$metadat2;
|
|
383
|
-
|
|
384
|
-
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;
|
|
385
|
-
if (etag) headers[_constants.IF_NONE_MATCH_HEADER] = etag;
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
const pendingRequest = this._trackRequest(requestHash);
|
|
390
|
-
|
|
391
|
-
if (pendingRequest) return pendingRequest;
|
|
392
|
-
return this._getResolve(requestHash, await this._fetch(endpoint, {
|
|
393
|
-
headers: { ...this._headers,
|
|
394
|
-
...headers
|
|
395
|
-
},
|
|
396
|
-
method: _constants.GET_METHOD
|
|
397
|
-
}, context));
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
async _getResolve(requestHash, res) {
|
|
401
|
-
const {
|
|
402
|
-
data,
|
|
403
|
-
headers,
|
|
404
|
-
status
|
|
405
|
-
} = res;
|
|
406
|
-
|
|
407
|
-
if (status === _constants.NOT_FOUND_STATUS_CODE) {
|
|
408
|
-
this._cacheEntryDelete(requestHash);
|
|
409
|
-
|
|
410
|
-
if (!res.errors) {
|
|
411
|
-
res.errors = [];
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
res.errors.push(new Error(_constants.RESOURCE_NOT_FOUND_ERROR));
|
|
415
|
-
} else if (status === _constants.NOT_MODIFIED_STATUS_CODE && headers) {
|
|
416
|
-
const cachedData = await this._cacheEntryGet(requestHash);
|
|
417
|
-
|
|
418
|
-
if (cachedData) {
|
|
419
|
-
this._cacheEntrySet(requestHash, cachedData, {
|
|
420
|
-
cacheControl: headers.get(_constants.CACHE_CONTROL_HEADER) || undefined,
|
|
421
|
-
etag: headers.get(_constants.ETAG_HEADER) || undefined
|
|
422
|
-
});
|
|
423
|
-
|
|
424
|
-
res.data = cachedData;
|
|
425
|
-
}
|
|
426
|
-
} else if (data && headers) {
|
|
427
|
-
this._cacheEntrySet(requestHash, data, {
|
|
428
|
-
cacheControl: headers.get(_constants.CACHE_CONTROL_HEADER) || undefined,
|
|
429
|
-
etag: headers.get(_constants.ETAG_HEADER) || undefined
|
|
430
|
-
});
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
this._resolvePendingRequests(requestHash, res);
|
|
434
|
-
|
|
435
|
-
this._requestTracker.active = this._requestTracker.active.filter(value => value !== requestHash);
|
|
436
|
-
return res;
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
_logResponse(res, endpoint, options, context) {
|
|
440
|
-
var _this$_log2;
|
|
441
|
-
|
|
442
|
-
const {
|
|
443
|
-
data,
|
|
444
|
-
errors,
|
|
445
|
-
headers,
|
|
446
|
-
status
|
|
447
|
-
} = res;
|
|
448
|
-
const {
|
|
449
|
-
redirects,
|
|
450
|
-
retries
|
|
451
|
-
} = options;
|
|
452
|
-
const {
|
|
453
|
-
startTime,
|
|
454
|
-
...otherContext
|
|
455
|
-
} = context;
|
|
456
|
-
|
|
457
|
-
const endTime = this._performance.now();
|
|
458
|
-
|
|
459
|
-
const duration = endTime - startTime;
|
|
460
|
-
(_this$_log2 = this._log) === null || _this$_log2 === void 0 ? void 0 : _this$_log2.call(this, _constants.RESPONSE_RECEIVED, {
|
|
461
|
-
context: {
|
|
462
|
-
body: data ? {
|
|
463
|
-
data
|
|
464
|
-
} : {
|
|
465
|
-
errors: errors !== null && errors !== void 0 ? errors : []
|
|
466
|
-
},
|
|
467
|
-
headers,
|
|
468
|
-
method: options.method,
|
|
469
|
-
redirects,
|
|
470
|
-
retries,
|
|
471
|
-
status,
|
|
472
|
-
url: endpoint,
|
|
473
|
-
...otherContext
|
|
474
|
-
},
|
|
475
|
-
stats: {
|
|
476
|
-
duration,
|
|
477
|
-
endTime,
|
|
478
|
-
startTime
|
|
479
|
-
}
|
|
480
|
-
});
|
|
481
|
-
}
|
|
482
|
-
|
|
483
|
-
_rateLimit() {
|
|
484
|
-
if (!this._rateLimitTimer) {
|
|
485
|
-
this._rateLimitTimer = setTimeout(() => {
|
|
486
|
-
this._rateLimitTimer = null;
|
|
487
|
-
this._rateLimitCount = 0;
|
|
488
|
-
|
|
489
|
-
if (this._rateLimitedRequestQueue.length) {
|
|
490
|
-
this._releaseRateLimitedRequestQueue();
|
|
491
|
-
}
|
|
492
|
-
}, 1000);
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
this._rateLimitCount += 1;
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
_releaseRateLimitedRequestQueue() {
|
|
499
|
-
this._rateLimitedRequestQueue.forEach(async ([resolve, endpoint, options, context]) => {
|
|
500
|
-
resolve(await this._fetch(endpoint, options, context));
|
|
501
|
-
});
|
|
502
|
-
|
|
503
|
-
this._rateLimitedRequestQueue = [];
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
async _request(path, {
|
|
507
|
-
body,
|
|
508
|
-
headers,
|
|
509
|
-
method,
|
|
510
|
-
pathTemplateData,
|
|
511
|
-
queryParams,
|
|
512
|
-
...rest
|
|
513
|
-
}, context) {
|
|
514
|
-
const endpoint = (0, _buildEndpoint.default)(this._basePath, path, {
|
|
515
|
-
optionalPathTemplateRegExp: this._optionalPathTemplateRegExp,
|
|
516
|
-
pathTemplateCallback: this._pathTemplateCallback,
|
|
517
|
-
pathTemplateData,
|
|
518
|
-
pathTemplateRegExp: this._pathTemplateRegExp,
|
|
519
|
-
queryParams: { ...this._queryParams,
|
|
520
|
-
...queryParams
|
|
521
|
-
}
|
|
522
|
-
});
|
|
523
|
-
return this._fetch(endpoint, {
|
|
524
|
-
body,
|
|
525
|
-
headers: { ...this._headers,
|
|
526
|
-
...headers
|
|
527
|
-
},
|
|
528
|
-
method,
|
|
529
|
-
...rest
|
|
530
|
-
}, context);
|
|
531
|
-
}
|
|
532
|
-
|
|
533
|
-
_resolvePendingRequests(requestHash, responseData) {
|
|
534
|
-
const pendingRequests = this._requestTracker.pending.get(requestHash);
|
|
535
|
-
|
|
536
|
-
if (!pendingRequests) return;
|
|
537
|
-
pendingRequests.forEach(({
|
|
538
|
-
resolve
|
|
539
|
-
}) => {
|
|
540
|
-
resolve(responseData);
|
|
541
|
-
});
|
|
542
|
-
|
|
543
|
-
this._requestTracker.pending.delete(requestHash);
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
_setPendingRequest(requestHash, resolver) {
|
|
547
|
-
let pending = this._requestTracker.pending.get(requestHash);
|
|
548
|
-
|
|
549
|
-
if (!pending) pending = [];
|
|
550
|
-
pending.push(resolver);
|
|
551
|
-
|
|
552
|
-
this._requestTracker.pending.set(requestHash, pending);
|
|
553
|
-
}
|
|
554
|
-
|
|
555
|
-
_trackRequest(requestHash) {
|
|
556
|
-
if (this._requestTracker.active.includes(requestHash)) {
|
|
557
|
-
return new Promise(resolve => {
|
|
558
|
-
this._setPendingRequest(requestHash, {
|
|
559
|
-
resolve
|
|
560
|
-
});
|
|
561
|
-
});
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
this._requestTracker.active.push(requestHash);
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
}
|
|
568
|
-
|
|
569
|
-
exports.Getta = Getta;
|
|
570
|
-
|
|
571
|
-
const createRestClient = (options, shortcuts) => {
|
|
572
|
-
const getta = new Getta(options);
|
|
573
|
-
if (!shortcuts) return getta;
|
|
574
|
-
Object.keys(shortcuts).forEach(key => {
|
|
575
|
-
getta.createShortcut(key, ...shortcuts[key]);
|
|
576
|
-
});
|
|
577
|
-
return getta;
|
|
578
|
-
};
|
|
579
|
-
|
|
580
|
-
exports.createRestClient = createRestClient;
|
package/lib/main/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";
|
package/lib/module/constants.js
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
export const ARRAY_BUFFER_FORMAT = "arrayBuffer";
|
|
2
|
-
export const BLOB_FORMAT = "blob";
|
|
3
|
-
export const FORM_DATA_FORMAT = "formData";
|
|
4
|
-
export const JSON_FORMAT = "json";
|
|
5
|
-
export const TEXT_FORMAT = "text";
|
|
6
|
-
export const STREAM_READERS = {
|
|
7
|
-
ARRAY_BUFFER_FORMAT,
|
|
8
|
-
BLOB_FORMAT,
|
|
9
|
-
FORM_DATA_FORMAT,
|
|
10
|
-
JSON_FORMAT,
|
|
11
|
-
TEXT_FORMAT
|
|
12
|
-
};
|
|
13
|
-
export const DEFAULT_BODY_PARSER = body => body;
|
|
14
|
-
export const DEFAULT_FETCH_TIMEOUT = 5000;
|
|
15
|
-
export const DEFAULT_HEADERS = {
|
|
16
|
-
"content-type": "application/json"
|
|
17
|
-
};
|
|
18
|
-
export const DEFAULT_MAX_REDIRECTS = 5;
|
|
19
|
-
export const DEFAULT_MAX_RETRIES = 3;
|
|
20
|
-
export const DEFAULT_PATH_TEMPLATE_REGEX = /({type})|({id})|({id,\+})|({brief\|standard})/g;
|
|
21
|
-
export const OPTIONAL_PATH_TEMPLATE_REGEX = /({[a-zA-Z0-9_]+\?})/g;
|
|
22
|
-
export const DEFAULT_RATE_LIMIT = 50;
|
|
23
|
-
export const DEFAULT_REQUEST_RETRY_WAIT = 100;
|
|
24
|
-
export const MISSING_BASE_PATH_ERROR = `Getta expected to receive 'basePath' in the constructor options,
|
|
25
|
-
but recevied undefined.`;
|
|
26
|
-
export const MAX_REDIRECTS_EXCEEDED_ERROR = "The request exceeded the maximum number of redirects, which is";
|
|
27
|
-
export const MAX_RETRIES_EXCEEDED_ERROR = "The request exceeded the maximum number of retries, which is";
|
|
28
|
-
export const INVALID_FETCH_METHOD_ERROR = "Getta expected to receive 'get', 'post', 'put' or 'delete', but received";
|
|
29
|
-
export const RESOURCE_NOT_FOUND_ERROR = "The requested resource could not been found.";
|
|
30
|
-
export const FETCH_TIMEOUT_ERROR = "The request timed out. Getta did not get a response within";
|
|
31
|
-
export const GET_METHOD = "get";
|
|
32
|
-
export const POST_METHOD = "post";
|
|
33
|
-
export const PUT_METHOD = "put";
|
|
34
|
-
export const DELETE_METHOD = "delete";
|
|
35
|
-
export const FETCH_METHODS = [GET_METHOD, POST_METHOD, PUT_METHOD, DELETE_METHOD];
|
|
36
|
-
export const INFORMATION_REPSONSE = "information";
|
|
37
|
-
export const SUCCESSFUL_REPSONSE = "successful";
|
|
38
|
-
export const REDIRECTION_REPSONSE = "redirection";
|
|
39
|
-
export const CLIENT_ERROR_REPSONSE = "clientError";
|
|
40
|
-
export const SERVER_ERROR_REPSONSE = "serverError";
|
|
41
|
-
export const NOT_MODIFIED_STATUS_CODE = 304;
|
|
42
|
-
export const NOT_FOUND_STATUS_CODE = 404;
|
|
43
|
-
export const COOKIE_HEADER = "Cookie";
|
|
44
|
-
export const ETAG_HEADER = "ETag";
|
|
45
|
-
export const LOCATION_HEADER = "Location";
|
|
46
|
-
export const IF_NONE_MATCH_HEADER = "If-None-Match";
|
|
47
|
-
export const CACHE_CONTROL_HEADER = "Cache-Control";
|
|
48
|
-
export const REQUEST_SENT = "request_sent";
|
|
49
|
-
export const RESPONSE_RECEIVED = "response_received";
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import queryString from "query-string";
|
|
2
|
-
export default function buildEndpoint(basePath, path, {
|
|
3
|
-
optionalPathTemplateRegExp,
|
|
4
|
-
pathTemplateCallback,
|
|
5
|
-
pathTemplateData,
|
|
6
|
-
pathTemplateRegExp,
|
|
7
|
-
queryParams
|
|
8
|
-
}) {
|
|
9
|
-
const pathJoiner = basePath.endsWith("/") || path.startsWith("/") ? "" : "/";
|
|
10
|
-
let endpoint = `${basePath}${pathJoiner}${path}`;
|
|
11
|
-
|
|
12
|
-
if (pathTemplateData) {
|
|
13
|
-
endpoint = pathTemplateCallback(endpoint, pathTemplateData, pathTemplateRegExp);
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
endpoint = endpoint.replace(optionalPathTemplateRegExp, "");
|
|
17
|
-
|
|
18
|
-
if (endpoint.endsWith("/")) {
|
|
19
|
-
endpoint = endpoint.substring(0, endpoint.length - 1);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
if (queryParams && Object.keys(queryParams).length) {
|
|
23
|
-
const queryJoin = queryString.extract(endpoint) ? "&" : "?";
|
|
24
|
-
endpoint = `${endpoint}${queryJoin}${queryString.stringify(queryParams)}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return endpoint;
|
|
28
|
-
}
|
|
File without changes
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
export default function defaultPathTemplateCallback(pathTemplate, data, pathTemplateRegExp) {
|
|
2
|
-
const dataKeys = Object.keys(data);
|
|
3
|
-
return pathTemplate.replace(pathTemplateRegExp, match => {
|
|
4
|
-
return dataKeys.reduce((value, key) => {
|
|
5
|
-
if (match.includes(key)) return data[key];
|
|
6
|
-
return value;
|
|
7
|
-
}, "");
|
|
8
|
-
});
|
|
9
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { CLIENT_ERROR_REPSONSE, INFORMATION_REPSONSE, REDIRECTION_REPSONSE, SERVER_ERROR_REPSONSE, SUCCESSFUL_REPSONSE } from "../../constants";
|
|
2
|
-
export default function getResponseGroup(status) {
|
|
3
|
-
switch (true) {
|
|
4
|
-
case status < 200:
|
|
5
|
-
return INFORMATION_REPSONSE;
|
|
6
|
-
|
|
7
|
-
case status < 300:
|
|
8
|
-
return SUCCESSFUL_REPSONSE;
|
|
9
|
-
|
|
10
|
-
case status < 400:
|
|
11
|
-
return REDIRECTION_REPSONSE;
|
|
12
|
-
|
|
13
|
-
case status < 500:
|
|
14
|
-
return CLIENT_ERROR_REPSONSE;
|
|
15
|
-
|
|
16
|
-
default:
|
|
17
|
-
return SERVER_ERROR_REPSONSE;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export default function isCacheabilityValid(cacheability) {
|
|
2
|
-
var _cacheability$metadat, _cacheability$metadat2, _cacheability$metadat3;
|
|
3
|
-
|
|
4
|
-
const noCache = (_cacheability$metadat = cacheability === null || cacheability === void 0 ? void 0 : (_cacheability$metadat2 = cacheability.metadata) === null || _cacheability$metadat2 === void 0 ? void 0 : (_cacheability$metadat3 = _cacheability$metadat2.cacheControl) === null || _cacheability$metadat3 === void 0 ? void 0 : _cacheability$metadat3.noCache) !== null && _cacheability$metadat !== void 0 ? _cacheability$metadat : false;
|
|
5
|
-
return !noCache && cacheability.checkTTL();
|
|
6
|
-
}
|