@datocms/rest-client-utils 0.1.0 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -1
- package/dist/cjs/ApiError.js +29 -4
- package/dist/cjs/ApiError.js.map +1 -1
- package/dist/cjs/request.js +13 -12
- package/dist/cjs/request.js.map +1 -1
- package/dist/esm/ApiError.d.ts +4 -2
- package/dist/esm/ApiError.js +29 -4
- package/dist/esm/ApiError.js.map +1 -1
- package/dist/esm/request.d.ts +1 -0
- package/dist/esm/request.js +13 -12
- package/dist/esm/request.js.map +1 -1
- package/dist/types/ApiError.d.ts +4 -2
- package/dist/types/request.d.ts +1 -0
- package/package.json +5 -5
- package/src/ApiError.ts +19 -5
- package/src/request.ts +13 -15
package/README.md
CHANGED
|
@@ -1 +1,17 @@
|
|
|
1
|
-
|
|
1
|
+
[](https://github.com/datocms/js-rest-api-clients/actions/workflows/node.js.yml)
|
|
2
|
+
|
|
3
|
+
## Common utilities for DatoCMS REST clients
|
|
4
|
+
|
|
5
|
+
<br /><br />
|
|
6
|
+
<a href="https://www.datocms.com/">
|
|
7
|
+
<img src="https://www.datocms.com/images/full_logo.svg" height="60">
|
|
8
|
+
</a>
|
|
9
|
+
<br /><br />
|
|
10
|
+
|
|
11
|
+
## Contributing
|
|
12
|
+
|
|
13
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/datocms/js-rest-api-clients. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
The package is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
package/dist/cjs/ApiError.js
CHANGED
|
@@ -14,6 +14,22 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
18
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
19
|
+
if (!m) return o;
|
|
20
|
+
var i = m.call(o), r, ar = [], e;
|
|
21
|
+
try {
|
|
22
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
23
|
+
}
|
|
24
|
+
catch (error) { e = { error: error }; }
|
|
25
|
+
finally {
|
|
26
|
+
try {
|
|
27
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
28
|
+
}
|
|
29
|
+
finally { if (e) throw e.error; }
|
|
30
|
+
}
|
|
31
|
+
return ar;
|
|
32
|
+
};
|
|
17
33
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
34
|
exports.ApiError = void 0;
|
|
19
35
|
function isErrorBody(body) {
|
|
@@ -109,20 +125,29 @@ var ApiError = /** @class */ (function (_super) {
|
|
|
109
125
|
enumerable: false,
|
|
110
126
|
configurable: true
|
|
111
127
|
});
|
|
112
|
-
ApiError.prototype.
|
|
128
|
+
ApiError.prototype.findError = function (codeOrCodes, filterDetails) {
|
|
113
129
|
var codes = Array.isArray(codeOrCodes) ? codeOrCodes : [codeOrCodes];
|
|
114
|
-
return this.errors.find(function (error) {
|
|
130
|
+
return this.errors.find(function (error) {
|
|
131
|
+
return codes.includes(error.attributes.code) &&
|
|
132
|
+
(!filterDetails ||
|
|
133
|
+
(typeof filterDetails === 'function'
|
|
134
|
+
? filterDetails(error.attributes.details)
|
|
135
|
+
: Object.entries(filterDetails).every(function (_a) {
|
|
136
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
137
|
+
return error.attributes.details[key] === value;
|
|
138
|
+
})));
|
|
139
|
+
});
|
|
115
140
|
};
|
|
116
141
|
Object.defineProperty(ApiError.prototype, "humanMessage", {
|
|
117
142
|
get: function () {
|
|
118
143
|
var _this = this;
|
|
119
|
-
var planUpgradeError = this.
|
|
144
|
+
var planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED');
|
|
120
145
|
if (planUpgradeError) {
|
|
121
146
|
var limit = planUpgradeError.attributes.details.limit;
|
|
122
147
|
return "".concat(humanMessageForPlanUpgradeLimit[limit], ". Please head over to your account dashboard (https://dashboard.datocms.com/) to upgrade the plan or, if no publicly available plan suits your needs, contact our Sales team (https://www.datocms.com/contact) to get a custom quote!");
|
|
123
148
|
}
|
|
124
149
|
var errors = Object.keys(humanMessageForCode)
|
|
125
|
-
.filter(function (code) { return _this.
|
|
150
|
+
.filter(function (code) { return _this.findError(code); })
|
|
126
151
|
.map(function (code) { return humanMessageForCode[code]; });
|
|
127
152
|
if (errors.length === 0) {
|
|
128
153
|
return null;
|
package/dist/cjs/ApiError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiError.js","sourceRoot":"","sources":["../../src/ApiError.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ApiError.js","sourceRoot":"","sources":["../../src/ApiError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,SAAS,WAAW,CAAC,IAAa;IAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,EAAE;QAClE,OAAO,KAAK,CAAC;KACd;IAED,IAAM,YAAY,GAAG,IAAyB,CAAC;IAE/C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE;QACrC,OAAO,KAAK,CAAC;KACd;IAED,IAAM,gBAAgB,GAAG,YAAmC,CAAC;IAE7D,IAAI,gBAAgB,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;QACtC,OAAO,KAAK,CAAC;KACd;IAED,IAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEzC,IACE,OAAO,OAAO,KAAK,QAAQ;QAC3B,OAAO,K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IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;gBACvB,OAAO,IAAI,CAAC;aACb;YAED,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;;;OAAA;IACH,eAAC;AAAD,CAAC,AA9ED,CAA8B,KAAK,GA8ElC;AA9EY,4BAAQ"}
|
package/dist/cjs/request.js
CHANGED
|
@@ -144,7 +144,7 @@ function isErrorWithCode(error) {
|
|
|
144
144
|
var requestCount = 1;
|
|
145
145
|
function request(options) {
|
|
146
146
|
return __awaiter(this, void 0, void 0, function () {
|
|
147
|
-
var requestId, preCallStack, userAgent, retryCount, logLevel, autoRetry, headers, baseUrl, body, queryString, url, _a, _b, _c, key, value, response_1, waitTimeInSecs, responseBody, _d, jobResult, error, error_1;
|
|
147
|
+
var requestId, preCallStack, userAgent, retryCount, logLevel, autoRetry, log, headers, baseUrl, body, queryString, url, _a, _b, _c, key, value, response_1, waitTimeInSecs, responseBody, _d, jobResult, error, error_1;
|
|
148
148
|
var e_1, _e;
|
|
149
149
|
return __generator(this, function (_f) {
|
|
150
150
|
switch (_f.label) {
|
|
@@ -156,6 +156,7 @@ function request(options) {
|
|
|
156
156
|
retryCount = options.retryCount || 1;
|
|
157
157
|
logLevel = options.logLevel || LogLevel.NONE;
|
|
158
158
|
autoRetry = 'autoRetry' in options ? options.autoRetry : true;
|
|
159
|
+
log = options.logFn || (function () { return true; });
|
|
159
160
|
headers = __assign({ 'content-type': 'application/json', accept: 'application/json', authorization: "Bearer ".concat(options.apiToken), 'user-agent': userAgent }, (options.extraHeaders || {}));
|
|
160
161
|
baseUrl = options.baseUrl.replace(/\/$/, '');
|
|
161
162
|
body = options.body ? JSON.stringify(options.body, null, 2) : undefined;
|
|
@@ -164,12 +165,12 @@ function request(options) {
|
|
|
164
165
|
: '';
|
|
165
166
|
url = "".concat(baseUrl).concat(options.url).concat(queryString);
|
|
166
167
|
if (logLevel >= LogLevel.BASIC) {
|
|
167
|
-
|
|
168
|
+
log("[".concat(requestId, "] ").concat(options.method, " ").concat(url));
|
|
168
169
|
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
|
|
169
170
|
try {
|
|
170
171
|
for (_a = __values(Object.entries(headers || {})), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
171
172
|
_c = __read(_b.value, 2), key = _c[0], value = _c[1];
|
|
172
|
-
|
|
173
|
+
log("[".concat(requestId, "] ").concat(key, ": ").concat(value));
|
|
173
174
|
}
|
|
174
175
|
}
|
|
175
176
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
@@ -181,7 +182,7 @@ function request(options) {
|
|
|
181
182
|
}
|
|
182
183
|
}
|
|
183
184
|
if (logLevel >= LogLevel.BODY && body) {
|
|
184
|
-
|
|
185
|
+
log("[".concat(requestId, "] ").concat(body));
|
|
185
186
|
}
|
|
186
187
|
}
|
|
187
188
|
_f.label = 1;
|
|
@@ -203,7 +204,7 @@ function request(options) {
|
|
|
203
204
|
parseInt(response_1.headers.get('X-RateLimit-Reset'), 10)
|
|
204
205
|
: retryCount;
|
|
205
206
|
if (logLevel >= LogLevel.BASIC) {
|
|
206
|
-
|
|
207
|
+
log("[".concat(requestId, "] Rate limit exceeded, waiting ").concat(waitTimeInSecs, " seconds..."));
|
|
207
208
|
}
|
|
208
209
|
return [4 /*yield*/, wait(waitTimeInSecs * 1000)];
|
|
209
210
|
case 3:
|
|
@@ -211,7 +212,7 @@ function request(options) {
|
|
|
211
212
|
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))];
|
|
212
213
|
case 4:
|
|
213
214
|
if (logLevel >= LogLevel.BASIC) {
|
|
214
|
-
|
|
215
|
+
log("[".concat(requestId, "] Status: ").concat(response_1.status, " (").concat(response_1.statusText, ")"));
|
|
215
216
|
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
|
|
216
217
|
[
|
|
217
218
|
'content-type',
|
|
@@ -219,10 +220,11 @@ function request(options) {
|
|
|
219
220
|
'x-environment',
|
|
220
221
|
'x-queue-time',
|
|
221
222
|
'x-ratelimit-remaining',
|
|
223
|
+
'x-request-id',
|
|
222
224
|
].forEach(function (key) {
|
|
223
225
|
var value = response_1.headers.get(key);
|
|
224
226
|
if (value) {
|
|
225
|
-
|
|
227
|
+
log("[".concat(requestId, "] ").concat(key, ": ").concat(value));
|
|
226
228
|
}
|
|
227
229
|
});
|
|
228
230
|
}
|
|
@@ -237,7 +239,7 @@ function request(options) {
|
|
|
237
239
|
case 7:
|
|
238
240
|
responseBody = _d;
|
|
239
241
|
if (logLevel >= LogLevel.BODY && responseBody) {
|
|
240
|
-
|
|
242
|
+
log("[".concat(requestId, "] ").concat(JSON.stringify(responseBody, null, 2)));
|
|
241
243
|
}
|
|
242
244
|
if (!(response_1.status === 202)) return [3 /*break*/, 9];
|
|
243
245
|
return [4 /*yield*/, options.fetchJobResult(responseBody.data.id)];
|
|
@@ -253,10 +255,9 @@ function request(options) {
|
|
|
253
255
|
return [2 /*return*/, responseBody];
|
|
254
256
|
}
|
|
255
257
|
error = new ApiError_1.ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, responseBody, preCallStack));
|
|
256
|
-
if (!(autoRetry &&
|
|
257
|
-
error.findErrorWithCode('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11];
|
|
258
|
+
if (!(autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11];
|
|
258
259
|
if (logLevel >= LogLevel.BASIC) {
|
|
259
|
-
|
|
260
|
+
log("[".concat(requestId, "] Data validation in progress, waiting ").concat(retryCount, " seconds..."));
|
|
260
261
|
}
|
|
261
262
|
return [4 /*yield*/, wait(retryCount * 1000)];
|
|
262
263
|
case 10:
|
|
@@ -267,7 +268,7 @@ function request(options) {
|
|
|
267
268
|
error_1 = _f.sent();
|
|
268
269
|
if (!(isErrorWithCode(error_1) && error_1.code.includes('ETIMEDOUT'))) return [3 /*break*/, 14];
|
|
269
270
|
if (logLevel >= LogLevel.BASIC) {
|
|
270
|
-
|
|
271
|
+
log("[".concat(requestId, "] Error ").concat(error_1.code, ", waiting ").concat(retryCount, " seconds..."));
|
|
271
272
|
}
|
|
272
273
|
return [4 /*yield*/, wait(retryCount * 1000)];
|
|
273
274
|
case 13:
|
package/dist/cjs/request.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAoB;AACpB,4DAAgC;AAChC,uCAA0D;AAG1D,IAAY,QASX;AATD,WAAY,QAAQ;IAClB,iBAAiB;IACjB,uCAAQ,CAAA;IACR,8DAA8D;IAC9D,yCAAS,CAAA;IACT,0EAA0E;IAC1E,uCAAQ,CAAA;IACR,4FAA4F;IAC5F,+DAAoB,CAAA;AACtB,CAAC,EATW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QASnB;
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAoB;AACpB,4DAAgC;AAChC,uCAA0D;AAG1D,IAAY,QASX;AATD,WAAY,QAAQ;IAClB,iBAAiB;IACjB,uCAAQ,CAAA;IACR,8DAA8D;IAC9D,yCAAS,CAAA;IACT,0EAA0E;IAC1E,uCAAQ,CAAA;IACR,4FAA4F;IAC5F,+DAAoB,CAAA;AACtB,CAAC,EATW,QAAQ,GAAR,gBAAQ,KAAR,gBAAQ,QASnB;AAmBD,SAAS,eAAe,CAAC,OAAgB;IACvC,IAAM,MAAM,GAAG,EAAE,CAAC;IAElB,OAAO,CAAC,OAAO,CAAC,UAAC,KAAK,EAAE,GAAG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AC,IAAG,EAAC;yBAG7D,MAAM,KAAK,CAAC;;;yBAER,CAAA,eAAe,CAAC,OAAK,CAAC,IAAI,OAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA,EAA1D,yBAA0D;oBAC5D,IAAI,QAAQ,IAAI,QAAQ,CAAC,KAAK,EAAE;wBAC9B,GAAG,CACD,WAAI,SAAS,qBAAW,OAAK,CAAC,IAAI,uBAAa,UAAU,gBAAa,CACvE,CAAC;qBACH;oBAED,qBAAM,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,EAAA;;oBAA7B,SAA6B,CAAC;oBAE9B,sBAAO,OAAO,uBAAM,OAAO,KAAE,UAAU,EAAE,UAAU,GAAG,CAAC,IAAG,EAAC;yBAG7D,MAAM,OAAK,CAAC;;;;;CAEf;AAzKD,0BAyKC"}
|
package/dist/esm/ApiError.d.ts
CHANGED
|
@@ -3,9 +3,10 @@ export declare type ErrorEntity = {
|
|
|
3
3
|
type: 'api_error';
|
|
4
4
|
attributes: {
|
|
5
5
|
code: string;
|
|
6
|
-
details: unknown
|
|
6
|
+
details: Record<string, unknown>;
|
|
7
7
|
};
|
|
8
8
|
};
|
|
9
|
+
declare type FilterFn = (details: Record<string, unknown>) => boolean;
|
|
9
10
|
export declare type ApiErrorRequest = {
|
|
10
11
|
url: string;
|
|
11
12
|
method: string;
|
|
@@ -29,6 +30,7 @@ export declare class ApiError extends Error {
|
|
|
29
30
|
preCallStack?: string;
|
|
30
31
|
constructor(initObject: ApiErrorInitObject);
|
|
31
32
|
get errors(): ErrorEntity[];
|
|
32
|
-
|
|
33
|
+
findError(codeOrCodes: string | string[], filterDetails?: Record<string, string> | FilterFn): ErrorEntity | undefined;
|
|
33
34
|
get humanMessage(): string | null;
|
|
34
35
|
}
|
|
36
|
+
export {};
|
package/dist/esm/ApiError.js
CHANGED
|
@@ -13,6 +13,22 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
13
13
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
14
|
};
|
|
15
15
|
})();
|
|
16
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
17
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
18
|
+
if (!m) return o;
|
|
19
|
+
var i = m.call(o), r, ar = [], e;
|
|
20
|
+
try {
|
|
21
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
22
|
+
}
|
|
23
|
+
catch (error) { e = { error: error }; }
|
|
24
|
+
finally {
|
|
25
|
+
try {
|
|
26
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
27
|
+
}
|
|
28
|
+
finally { if (e) throw e.error; }
|
|
29
|
+
}
|
|
30
|
+
return ar;
|
|
31
|
+
};
|
|
16
32
|
function isErrorBody(body) {
|
|
17
33
|
if (typeof body !== 'object' || body === null || !('data' in body)) {
|
|
18
34
|
return false;
|
|
@@ -106,20 +122,29 @@ var ApiError = /** @class */ (function (_super) {
|
|
|
106
122
|
enumerable: false,
|
|
107
123
|
configurable: true
|
|
108
124
|
});
|
|
109
|
-
ApiError.prototype.
|
|
125
|
+
ApiError.prototype.findError = function (codeOrCodes, filterDetails) {
|
|
110
126
|
var codes = Array.isArray(codeOrCodes) ? codeOrCodes : [codeOrCodes];
|
|
111
|
-
return this.errors.find(function (error) {
|
|
127
|
+
return this.errors.find(function (error) {
|
|
128
|
+
return codes.includes(error.attributes.code) &&
|
|
129
|
+
(!filterDetails ||
|
|
130
|
+
(typeof filterDetails === 'function'
|
|
131
|
+
? filterDetails(error.attributes.details)
|
|
132
|
+
: Object.entries(filterDetails).every(function (_a) {
|
|
133
|
+
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
134
|
+
return error.attributes.details[key] === value;
|
|
135
|
+
})));
|
|
136
|
+
});
|
|
112
137
|
};
|
|
113
138
|
Object.defineProperty(ApiError.prototype, "humanMessage", {
|
|
114
139
|
get: function () {
|
|
115
140
|
var _this = this;
|
|
116
|
-
var planUpgradeError = this.
|
|
141
|
+
var planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED');
|
|
117
142
|
if (planUpgradeError) {
|
|
118
143
|
var limit = planUpgradeError.attributes.details.limit;
|
|
119
144
|
return "".concat(humanMessageForPlanUpgradeLimit[limit], ". Please head over to your account dashboard (https://dashboard.datocms.com/) to upgrade the plan or, if no publicly available plan suits your needs, contact our Sales team (https://www.datocms.com/contact) to get a custom quote!");
|
|
120
145
|
}
|
|
121
146
|
var errors = Object.keys(humanMessageForCode)
|
|
122
|
-
.filter(function (code) { return _this.
|
|
147
|
+
.filter(function (code) { return _this.findError(code); })
|
|
123
148
|
.map(function (code) { return humanMessageForCode[code]; });
|
|
124
149
|
if (errors.length === 0) {
|
|
125
150
|
return null;
|
package/dist/esm/ApiError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ApiError.js","sourceRoot":"","sources":["../../src/ApiError.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"ApiError.js","sourceRoot":"","sources":["../../src/ApiError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,SAAS,WAAW,CAAC,IAAa;IAChC,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC,EAAE;QAClE,OAAO,KAAK,CAAC;KACd;IAED,IAAM,YAAY,GAAG,IAAyB,CAAC;IAE/C,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,YAAY,CAAC,IAAI,CAAC,EAAE;QACrC,OAAO,KAAK,CAAC;KACd;IAED,IAAM,gBAAgB,GAAG,YAAmC,CAAC;IAE7D,IAAI,gBAAgB,CAAC,IAAI,CAAC,MAAM,KAAK,CAAC,EAAE;QACtC,OAAO,KAAK,CAAC;KACd;IAED,IAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAEzC,IACE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,CAAC,EAAzB,CAAyB,CAAC,CAAC;YAE5C,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE;gBACvB,OAAO,IAAI,CAAC;aACb;YAED,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;;;OAAA;IACH,eAAC;AAAD,CAAC,AA9ED,CAA8B,KAAK,GA8ElC"}
|
package/dist/esm/request.d.ts
CHANGED
package/dist/esm/request.js
CHANGED
|
@@ -138,7 +138,7 @@ function isErrorWithCode(error) {
|
|
|
138
138
|
var requestCount = 1;
|
|
139
139
|
export function request(options) {
|
|
140
140
|
return __awaiter(this, void 0, void 0, function () {
|
|
141
|
-
var requestId, preCallStack, userAgent, retryCount, logLevel, autoRetry, headers, baseUrl, body, queryString, url, _a, _b, _c, key, value, response_1, waitTimeInSecs, responseBody, _d, jobResult, error, error_1;
|
|
141
|
+
var requestId, preCallStack, userAgent, retryCount, logLevel, autoRetry, log, headers, baseUrl, body, queryString, url, _a, _b, _c, key, value, response_1, waitTimeInSecs, responseBody, _d, jobResult, error, error_1;
|
|
142
142
|
var e_1, _e;
|
|
143
143
|
return __generator(this, function (_f) {
|
|
144
144
|
switch (_f.label) {
|
|
@@ -150,6 +150,7 @@ export function request(options) {
|
|
|
150
150
|
retryCount = options.retryCount || 1;
|
|
151
151
|
logLevel = options.logLevel || LogLevel.NONE;
|
|
152
152
|
autoRetry = 'autoRetry' in options ? options.autoRetry : true;
|
|
153
|
+
log = options.logFn || (function () { return true; });
|
|
153
154
|
headers = __assign({ 'content-type': 'application/json', accept: 'application/json', authorization: "Bearer ".concat(options.apiToken), 'user-agent': userAgent }, (options.extraHeaders || {}));
|
|
154
155
|
baseUrl = options.baseUrl.replace(/\/$/, '');
|
|
155
156
|
body = options.body ? JSON.stringify(options.body, null, 2) : undefined;
|
|
@@ -158,12 +159,12 @@ export function request(options) {
|
|
|
158
159
|
: '';
|
|
159
160
|
url = "".concat(baseUrl).concat(options.url).concat(queryString);
|
|
160
161
|
if (logLevel >= LogLevel.BASIC) {
|
|
161
|
-
|
|
162
|
+
log("[".concat(requestId, "] ").concat(options.method, " ").concat(url));
|
|
162
163
|
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
|
|
163
164
|
try {
|
|
164
165
|
for (_a = __values(Object.entries(headers || {})), _b = _a.next(); !_b.done; _b = _a.next()) {
|
|
165
166
|
_c = __read(_b.value, 2), key = _c[0], value = _c[1];
|
|
166
|
-
|
|
167
|
+
log("[".concat(requestId, "] ").concat(key, ": ").concat(value));
|
|
167
168
|
}
|
|
168
169
|
}
|
|
169
170
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
@@ -175,7 +176,7 @@ export function request(options) {
|
|
|
175
176
|
}
|
|
176
177
|
}
|
|
177
178
|
if (logLevel >= LogLevel.BODY && body) {
|
|
178
|
-
|
|
179
|
+
log("[".concat(requestId, "] ").concat(body));
|
|
179
180
|
}
|
|
180
181
|
}
|
|
181
182
|
_f.label = 1;
|
|
@@ -197,7 +198,7 @@ export function request(options) {
|
|
|
197
198
|
parseInt(response_1.headers.get('X-RateLimit-Reset'), 10)
|
|
198
199
|
: retryCount;
|
|
199
200
|
if (logLevel >= LogLevel.BASIC) {
|
|
200
|
-
|
|
201
|
+
log("[".concat(requestId, "] Rate limit exceeded, waiting ").concat(waitTimeInSecs, " seconds..."));
|
|
201
202
|
}
|
|
202
203
|
return [4 /*yield*/, wait(waitTimeInSecs * 1000)];
|
|
203
204
|
case 3:
|
|
@@ -205,7 +206,7 @@ export function request(options) {
|
|
|
205
206
|
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))];
|
|
206
207
|
case 4:
|
|
207
208
|
if (logLevel >= LogLevel.BASIC) {
|
|
208
|
-
|
|
209
|
+
log("[".concat(requestId, "] Status: ").concat(response_1.status, " (").concat(response_1.statusText, ")"));
|
|
209
210
|
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
|
|
210
211
|
[
|
|
211
212
|
'content-type',
|
|
@@ -213,10 +214,11 @@ export function request(options) {
|
|
|
213
214
|
'x-environment',
|
|
214
215
|
'x-queue-time',
|
|
215
216
|
'x-ratelimit-remaining',
|
|
217
|
+
'x-request-id',
|
|
216
218
|
].forEach(function (key) {
|
|
217
219
|
var value = response_1.headers.get(key);
|
|
218
220
|
if (value) {
|
|
219
|
-
|
|
221
|
+
log("[".concat(requestId, "] ").concat(key, ": ").concat(value));
|
|
220
222
|
}
|
|
221
223
|
});
|
|
222
224
|
}
|
|
@@ -231,7 +233,7 @@ export function request(options) {
|
|
|
231
233
|
case 7:
|
|
232
234
|
responseBody = _d;
|
|
233
235
|
if (logLevel >= LogLevel.BODY && responseBody) {
|
|
234
|
-
|
|
236
|
+
log("[".concat(requestId, "] ").concat(JSON.stringify(responseBody, null, 2)));
|
|
235
237
|
}
|
|
236
238
|
if (!(response_1.status === 202)) return [3 /*break*/, 9];
|
|
237
239
|
return [4 /*yield*/, options.fetchJobResult(responseBody.data.id)];
|
|
@@ -247,10 +249,9 @@ export function request(options) {
|
|
|
247
249
|
return [2 /*return*/, responseBody];
|
|
248
250
|
}
|
|
249
251
|
error = new ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, responseBody, preCallStack));
|
|
250
|
-
if (!(autoRetry &&
|
|
251
|
-
error.findErrorWithCode('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11];
|
|
252
|
+
if (!(autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11];
|
|
252
253
|
if (logLevel >= LogLevel.BASIC) {
|
|
253
|
-
|
|
254
|
+
log("[".concat(requestId, "] Data validation in progress, waiting ").concat(retryCount, " seconds..."));
|
|
254
255
|
}
|
|
255
256
|
return [4 /*yield*/, wait(retryCount * 1000)];
|
|
256
257
|
case 10:
|
|
@@ -261,7 +262,7 @@ export function request(options) {
|
|
|
261
262
|
error_1 = _f.sent();
|
|
262
263
|
if (!(isErrorWithCode(error_1) && error_1.code.includes('ETIMEDOUT'))) return [3 /*break*/, 14];
|
|
263
264
|
if (logLevel >= LogLevel.BASIC) {
|
|
264
|
-
|
|
265
|
+
log("[".concat(requestId, "] Error ").concat(error_1.code, ", waiting ").concat(retryCount, " seconds..."));
|
|
265
266
|
}
|
|
266
267
|
return [4 /*yield*/, wait(retryCount * 1000)];
|
|
267
268
|
case 13:
|
package/dist/esm/request.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,KAAK,MAAM,aAAa,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAsB,MAAM,YAAY,CAAC;AAG1D,MAAM,CAAN,IAAY,QASX;AATD,WAAY,QAAQ;IAClB,iBAAiB;IACjB,uCAAQ,CAAA;IACR,8DAA8D;IAC9D,yCAAS,CAAA;IACT,0EAA0E;IAC1E,uCAAQ,CAAA;IACR,4FAA4F;IAC5F,+DAAoB,CAAA;AACtB,CAAC,EATW,QAAQ,KAAR,QAAQ,QASnB;
|
|
1
|
+
{"version":3,"file":"request.js","sourceRoot":"","sources":["../../src/request.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,KAAK,MAAM,aAAa,CAAC;AAChC,OAAO,EAAE,QAAQ,EAAsB,MAAM,YAAY,CAAC;AAG1D,MAAM,CAAN,IAAY,QASX;AATD,WAAY,QAAQ;IAClB,iBAAiB;IACjB,uCAAQ,CAAA;IACR,8DAA8D;IAC9D,yCAAS,CAAA;IACT,0EAA0E;IAC1E,uCAAQ,CAAA;IACR,4FAA4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AA0C,UAAU,gBAAa,CAC/E,CAAC;qBACH;oBAED,qBAAM,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,EAAA;;oBAA7B,SAA6B,CAAC;oBAE9B,sBAAO,OAAO,uBAAM,OAAO,KAAE,UAAU,EAAE,UAAU,GAAG,CAAC,IAAG,EAAC;yBAG7D,MAAM,KAAK,CAAC;;;yBAER,CAAA,eAAe,CAAC,OAAK,CAAC,IAAI,OAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAA,EAA1D,yBAA0D;oBAC5D,IAAI,QAAQ,IAAI,QAAQ,CAAC,KAAK,EAAE;wBAC9B,GAAG,CACD,WAAI,SAAS,qBAAW,OAAK,CAAC,IAAI,uBAAa,UAAU,gBAAa,CACvE,CAAC;qBACH;oBAED,qBAAM,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,EAAA;;oBAA7B,SAA6B,CAAC;oBAE9B,sBAAO,OAAO,uBAAM,OAAO,KAAE,UAAU,EAAE,UAAU,GAAG,CAAC,IAAG,EAAC;yBAG7D,MAAM,OAAK,CAAC;;;;;CAEf"}
|
package/dist/types/ApiError.d.ts
CHANGED
|
@@ -3,9 +3,10 @@ export declare type ErrorEntity = {
|
|
|
3
3
|
type: 'api_error';
|
|
4
4
|
attributes: {
|
|
5
5
|
code: string;
|
|
6
|
-
details: unknown
|
|
6
|
+
details: Record<string, unknown>;
|
|
7
7
|
};
|
|
8
8
|
};
|
|
9
|
+
declare type FilterFn = (details: Record<string, unknown>) => boolean;
|
|
9
10
|
export declare type ApiErrorRequest = {
|
|
10
11
|
url: string;
|
|
11
12
|
method: string;
|
|
@@ -29,6 +30,7 @@ export declare class ApiError extends Error {
|
|
|
29
30
|
preCallStack?: string;
|
|
30
31
|
constructor(initObject: ApiErrorInitObject);
|
|
31
32
|
get errors(): ErrorEntity[];
|
|
32
|
-
|
|
33
|
+
findError(codeOrCodes: string | string[], filterDetails?: Record<string, string> | FilterFn): ErrorEntity | undefined;
|
|
33
34
|
get humanMessage(): string | null;
|
|
34
35
|
}
|
|
36
|
+
export {};
|
package/dist/types/request.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@datocms/rest-client-utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Utilities for DatoCMS REST API clients",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"datocms",
|
|
7
7
|
"client"
|
|
8
8
|
],
|
|
9
9
|
"author": "Stefano Verna <s.verna@datocms.com>",
|
|
10
|
-
"homepage": "https://github.com/datocms/js-
|
|
10
|
+
"homepage": "https://github.com/datocms/js-rest-api-clients/tree/main/packages/rest-client-utils#readme",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"main": "dist/cjs/index.js",
|
|
13
13
|
"module": "dist/esm/index.js",
|
|
@@ -26,19 +26,19 @@
|
|
|
26
26
|
},
|
|
27
27
|
"repository": {
|
|
28
28
|
"type": "git",
|
|
29
|
-
"url": "git+https://github.com/datocms/js-
|
|
29
|
+
"url": "git+https://github.com/datocms/js-rest-api-clients.git"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsc && tsc --project ./tsconfig.esnext.json",
|
|
33
33
|
"prebuild": "rimraf dist"
|
|
34
34
|
},
|
|
35
35
|
"bugs": {
|
|
36
|
-
"url": "https://github.com/datocms/js-
|
|
36
|
+
"url": "https://github.com/datocms/js-rest-api-clients/issues"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"async-scheduler": "^1.4.4",
|
|
40
40
|
"cross-fetch": "^3.1.5",
|
|
41
41
|
"qs": "^6.10.3"
|
|
42
42
|
},
|
|
43
|
-
"gitHead": "
|
|
43
|
+
"gitHead": "dc93ff1a234e3e95a1db409b44c3fe538afd8a1e"
|
|
44
44
|
}
|
package/src/ApiError.ts
CHANGED
|
@@ -3,11 +3,13 @@ export type ErrorEntity = {
|
|
|
3
3
|
type: 'api_error';
|
|
4
4
|
attributes: {
|
|
5
5
|
code: string;
|
|
6
|
-
details: unknown
|
|
6
|
+
details: Record<string, unknown>;
|
|
7
7
|
};
|
|
8
8
|
};
|
|
9
9
|
type ErrorBody = { data: ErrorEntity[] };
|
|
10
10
|
|
|
11
|
+
type FilterFn = (details: Record<string, unknown>) => boolean;
|
|
12
|
+
|
|
11
13
|
function isErrorBody(body: unknown): body is ErrorBody {
|
|
12
14
|
if (typeof body !== 'object' || body === null || !('data' in body)) {
|
|
13
15
|
return false;
|
|
@@ -138,13 +140,25 @@ export class ApiError extends Error {
|
|
|
138
140
|
return this.response.body.data;
|
|
139
141
|
}
|
|
140
142
|
|
|
141
|
-
|
|
143
|
+
findError(
|
|
144
|
+
codeOrCodes: string | string[],
|
|
145
|
+
filterDetails?: Record<string, string> | FilterFn,
|
|
146
|
+
) {
|
|
142
147
|
const codes = Array.isArray(codeOrCodes) ? codeOrCodes : [codeOrCodes];
|
|
143
|
-
return this.errors.find(
|
|
148
|
+
return this.errors.find(
|
|
149
|
+
(error) =>
|
|
150
|
+
codes.includes(error.attributes.code) &&
|
|
151
|
+
(!filterDetails ||
|
|
152
|
+
(typeof filterDetails === 'function'
|
|
153
|
+
? filterDetails(error.attributes.details)
|
|
154
|
+
: Object.entries(filterDetails).every(
|
|
155
|
+
([key, value]) => error.attributes.details[key] === value,
|
|
156
|
+
))),
|
|
157
|
+
);
|
|
144
158
|
}
|
|
145
159
|
|
|
146
160
|
get humanMessage() {
|
|
147
|
-
const planUpgradeError = this.
|
|
161
|
+
const planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED');
|
|
148
162
|
|
|
149
163
|
if (planUpgradeError) {
|
|
150
164
|
const { limit } = planUpgradeError.attributes.details as Record<
|
|
@@ -155,7 +169,7 @@ export class ApiError extends Error {
|
|
|
155
169
|
}
|
|
156
170
|
|
|
157
171
|
const errors = Object.keys(humanMessageForCode)
|
|
158
|
-
.filter((code) => this.
|
|
172
|
+
.filter((code) => this.findError(code))
|
|
159
173
|
.map((code) => humanMessageForCode[code]);
|
|
160
174
|
|
|
161
175
|
if (errors.length === 0) {
|
package/src/request.ts
CHANGED
|
@@ -27,6 +27,7 @@ type RequestOptions = {
|
|
|
27
27
|
queryParams?: Record<string, unknown>;
|
|
28
28
|
body?: unknown;
|
|
29
29
|
preCallStack?: string;
|
|
30
|
+
logFn?: (message: string) => void;
|
|
30
31
|
userAgent?: string;
|
|
31
32
|
};
|
|
32
33
|
|
|
@@ -113,6 +114,7 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
113
114
|
const retryCount = options.retryCount || 1;
|
|
114
115
|
const logLevel = options.logLevel || LogLevel.NONE;
|
|
115
116
|
const autoRetry = 'autoRetry' in options ? options.autoRetry : true;
|
|
117
|
+
const log = options.logFn || (() => true);
|
|
116
118
|
|
|
117
119
|
const headers = {
|
|
118
120
|
'content-type': 'application/json',
|
|
@@ -133,14 +135,14 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
133
135
|
const url = `${baseUrl}${options.url}${queryString}`;
|
|
134
136
|
|
|
135
137
|
if (logLevel >= LogLevel.BASIC) {
|
|
136
|
-
|
|
138
|
+
log(`[${requestId}] ${options.method} ${url}`);
|
|
137
139
|
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
|
|
138
140
|
for (const [key, value] of Object.entries(headers || {})) {
|
|
139
|
-
|
|
141
|
+
log(`[${requestId}] ${key}: ${value}`);
|
|
140
142
|
}
|
|
141
143
|
}
|
|
142
144
|
if (logLevel >= LogLevel.BODY && body) {
|
|
143
|
-
|
|
145
|
+
log(`[${requestId}] ${body}`);
|
|
144
146
|
}
|
|
145
147
|
}
|
|
146
148
|
|
|
@@ -172,7 +174,7 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
172
174
|
: retryCount;
|
|
173
175
|
|
|
174
176
|
if (logLevel >= LogLevel.BASIC) {
|
|
175
|
-
|
|
177
|
+
log(
|
|
176
178
|
`[${requestId}] Rate limit exceeded, waiting ${waitTimeInSecs} seconds...`,
|
|
177
179
|
);
|
|
178
180
|
}
|
|
@@ -183,9 +185,7 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
183
185
|
}
|
|
184
186
|
|
|
185
187
|
if (logLevel >= LogLevel.BASIC) {
|
|
186
|
-
|
|
187
|
-
`[${requestId}] Status: ${response.status} (${response.statusText})`,
|
|
188
|
-
);
|
|
188
|
+
log(`[${requestId}] Status: ${response.status} (${response.statusText})`);
|
|
189
189
|
if (logLevel >= LogLevel.BODY_AND_HEADERS) {
|
|
190
190
|
[
|
|
191
191
|
'content-type',
|
|
@@ -193,10 +193,11 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
193
193
|
'x-environment',
|
|
194
194
|
'x-queue-time',
|
|
195
195
|
'x-ratelimit-remaining',
|
|
196
|
+
'x-request-id',
|
|
196
197
|
].forEach((key) => {
|
|
197
198
|
const value = response.headers.get(key);
|
|
198
199
|
if (value) {
|
|
199
|
-
|
|
200
|
+
log(`[${requestId}] ${key}: ${value}`);
|
|
200
201
|
}
|
|
201
202
|
});
|
|
202
203
|
}
|
|
@@ -206,7 +207,7 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
206
207
|
response.status === 204 ? undefined : await response.json();
|
|
207
208
|
|
|
208
209
|
if (logLevel >= LogLevel.BODY && responseBody) {
|
|
209
|
-
|
|
210
|
+
log(`[${requestId}] ${JSON.stringify(responseBody, null, 2)}`);
|
|
210
211
|
}
|
|
211
212
|
|
|
212
213
|
if (response.status === 202) {
|
|
@@ -245,12 +246,9 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
245
246
|
),
|
|
246
247
|
);
|
|
247
248
|
|
|
248
|
-
if (
|
|
249
|
-
autoRetry &&
|
|
250
|
-
error.findErrorWithCode('BATCH_DATA_VALIDATION_IN_PROGRESS')
|
|
251
|
-
) {
|
|
249
|
+
if (autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS')) {
|
|
252
250
|
if (logLevel >= LogLevel.BASIC) {
|
|
253
|
-
|
|
251
|
+
log(
|
|
254
252
|
`[${requestId}] Data validation in progress, waiting ${retryCount} seconds...`,
|
|
255
253
|
);
|
|
256
254
|
}
|
|
@@ -264,7 +262,7 @@ export async function request<T>(options: RequestOptions): Promise<T> {
|
|
|
264
262
|
} catch (error) {
|
|
265
263
|
if (isErrorWithCode(error) && error.code.includes('ETIMEDOUT')) {
|
|
266
264
|
if (logLevel >= LogLevel.BASIC) {
|
|
267
|
-
|
|
265
|
+
log(
|
|
268
266
|
`[${requestId}] Error ${error.code}, waiting ${retryCount} seconds...`,
|
|
269
267
|
);
|
|
270
268
|
}
|