@datocms/rest-client-utils
Advanced tools
Comparing version
@@ -133,5 +133,5 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var requestId, fetchFn, preCallStack, userAgent, retryCount, logLevel, autoRetry, log, headers, baseUrl, body, queryString, url, _i, _a, _b, key, value, requestPromise_1, timeoutId, response_1, responseContentType, invalidContentType, waitTimeInSecs, responseBody, _c, jobResult, error, error_1; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var requestId, fetchFn, preCallStack, userAgent, retryCount, logLevel, autoRetry, log, headers, baseUrl, body, queryString, url, _i, _a, _b, key, value, requestPromise_1, timeoutId, response, responseContentType, invalidContentType, waitTimeInSecs, _c, _d, key, value, responseBody, _e, jobResult, error, error_1; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
@@ -175,5 +175,5 @@ requestId = requestCount; | ||
} | ||
_d.label = 1; | ||
_f.label = 1; | ||
case 1: | ||
_d.trys.push([1, 12, , 16]); | ||
_f.trys.push([1, 12, , 16]); | ||
requestPromise_1 = (0, makeCancelablePromise_1.makeCancelablePromise)(fetchFn(url, { | ||
@@ -189,19 +189,19 @@ method: options.method, | ||
case 2: | ||
response_1 = _d.sent(); | ||
response = _f.sent(); | ||
clearTimeout(timeoutId); | ||
responseContentType = response_1.headers.get('Content-Type'); | ||
responseContentType = response.headers.get('Content-Type'); | ||
invalidContentType = responseContentType && !responseContentType.includes('application/json'); | ||
if (!(response_1.status === 429 || invalidContentType)) return [3 /*break*/, 4]; | ||
if (!(response.status === 429 || invalidContentType)) return [3 /*break*/, 4]; | ||
if (!autoRetry || (invalidContentType && options.method !== 'GET')) { | ||
throw new errors_1.ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, undefined, preCallStack)); | ||
throw new errors_1.ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response, undefined, preCallStack)); | ||
} | ||
waitTimeInSecs = response_1.headers.has('X-RateLimit-Reset') | ||
? parseInt(response_1.headers.get('X-RateLimit-Reset'), 10) | ||
waitTimeInSecs = response.headers.has('X-RateLimit-Reset') | ||
? parseInt(response.headers.get('X-RateLimit-Reset'), 10) | ||
: retryCount; | ||
if (logLevel >= LogLevel.BASIC) { | ||
if (response_1.status === 429) { | ||
if (response.status === 429) { | ||
log("[".concat(requestId, "] Rate limit exceeded, wait ").concat(waitTimeInSecs, " seconds then retry...")); | ||
} | ||
else { | ||
log("[".concat(requestId, "] Invalid response content type \"").concat(responseContentType, "\" (status ").concat(response_1.status, "). Wait ").concat(waitTimeInSecs, " seconds then retry...")); | ||
log("[".concat(requestId, "] Invalid response content type \"").concat(responseContentType, "\" (status ").concat(response.status, "). Wait ").concat(waitTimeInSecs, " seconds then retry...")); | ||
} | ||
@@ -211,9 +211,9 @@ } | ||
case 3: | ||
_d.sent(); | ||
_f.sent(); | ||
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))]; | ||
case 4: | ||
if (logLevel >= LogLevel.BASIC) { | ||
log("[".concat(requestId, "] Status: ").concat(response_1.status, " (").concat(response_1.statusText, ")")); | ||
log("[".concat(requestId, "] Status: ").concat(response.status, " (").concat(response.statusText, ")")); | ||
if (logLevel >= LogLevel.BODY_AND_HEADERS) { | ||
[ | ||
for (_c = 0, _d = [ | ||
'x-api-version', | ||
@@ -225,26 +225,27 @@ 'x-environment', | ||
'cf-ray', | ||
].forEach(function (key) { | ||
var value = response_1.headers.get(key); | ||
]; _c < _d.length; _c++) { | ||
key = _d[_c]; | ||
value = response.headers.get(key); | ||
if (value) { | ||
log("[".concat(requestId, "] ").concat(key, ": ").concat(value)); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
if (!(response_1.status === 204)) return [3 /*break*/, 5]; | ||
_c = undefined; | ||
if (!(response.status === 204)) return [3 /*break*/, 5]; | ||
_e = undefined; | ||
return [3 /*break*/, 7]; | ||
case 5: return [4 /*yield*/, response_1.json()]; | ||
case 5: return [4 /*yield*/, response.json()]; | ||
case 6: | ||
_c = _d.sent(); | ||
_d.label = 7; | ||
_e = _f.sent(); | ||
_f.label = 7; | ||
case 7: | ||
responseBody = _c; | ||
responseBody = _e; | ||
if (logLevel >= LogLevel.BODY && responseBody) { | ||
log("[".concat(requestId, "] ").concat(JSON.stringify(responseBody, null, 2))); | ||
} | ||
if (!(response_1.status === 202)) return [3 /*break*/, 9]; | ||
if (!(response.status === 202)) return [3 /*break*/, 9]; | ||
return [4 /*yield*/, options.fetchJobResult(responseBody.data.id)]; | ||
case 8: | ||
jobResult = _d.sent(); | ||
jobResult = _f.sent(); | ||
if (jobResult.status < 200 || jobResult.status >= 300) { | ||
@@ -254,8 +255,8 @@ throw new errors_1.ApiError(buildApiErrorInitObjectFromJobResult(options.method, url, headers, options.body, jobResult.status, jobResult.payload, preCallStack)); | ||
responseBody = jobResult.payload; | ||
_d.label = 9; | ||
_f.label = 9; | ||
case 9: | ||
if (response_1.status >= 200 && response_1.status < 300) { | ||
if (response.status >= 200 && response.status < 300) { | ||
return [2 /*return*/, responseBody]; | ||
} | ||
error = new errors_1.ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, responseBody, preCallStack)); | ||
error = new errors_1.ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response, responseBody, preCallStack)); | ||
if (!(autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11]; | ||
@@ -267,7 +268,7 @@ if (logLevel >= LogLevel.BASIC) { | ||
case 10: | ||
_d.sent(); | ||
_f.sent(); | ||
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))]; | ||
case 11: throw error; | ||
case 12: | ||
error_1 = _d.sent(); | ||
error_1 = _f.sent(); | ||
if (!(error_1 instanceof makeCancelablePromise_1.CanceledPromiseError || | ||
@@ -281,3 +282,3 @@ (isErrorWithCode(error_1) && error_1.code.includes('ETIMEDOUT')))) return [3 /*break*/, 15]; | ||
case 13: | ||
_d.sent(); | ||
_f.sent(); | ||
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))]; | ||
@@ -284,0 +285,0 @@ case 14: throw new errors_1.TimeoutError(buildTimeoutErrorInitObject(options.method, url, headers, options.body, preCallStack)); |
@@ -130,5 +130,5 @@ /// <reference lib="dom" /> | ||
return __awaiter(this, void 0, void 0, function () { | ||
var requestId, fetchFn, preCallStack, userAgent, retryCount, logLevel, autoRetry, log, headers, baseUrl, body, queryString, url, _i, _a, _b, key, value, requestPromise_1, timeoutId, response_1, responseContentType, invalidContentType, waitTimeInSecs, responseBody, _c, jobResult, error, error_1; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var requestId, fetchFn, preCallStack, userAgent, retryCount, logLevel, autoRetry, log, headers, baseUrl, body, queryString, url, _i, _a, _b, key, value, requestPromise_1, timeoutId, response, responseContentType, invalidContentType, waitTimeInSecs, _c, _d, key, value, responseBody, _e, jobResult, error, error_1; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
@@ -172,5 +172,5 @@ requestId = requestCount; | ||
} | ||
_d.label = 1; | ||
_f.label = 1; | ||
case 1: | ||
_d.trys.push([1, 12, , 16]); | ||
_f.trys.push([1, 12, , 16]); | ||
requestPromise_1 = makeCancelablePromise(fetchFn(url, { | ||
@@ -186,19 +186,19 @@ method: options.method, | ||
case 2: | ||
response_1 = _d.sent(); | ||
response = _f.sent(); | ||
clearTimeout(timeoutId); | ||
responseContentType = response_1.headers.get('Content-Type'); | ||
responseContentType = response.headers.get('Content-Type'); | ||
invalidContentType = responseContentType && !responseContentType.includes('application/json'); | ||
if (!(response_1.status === 429 || invalidContentType)) return [3 /*break*/, 4]; | ||
if (!(response.status === 429 || invalidContentType)) return [3 /*break*/, 4]; | ||
if (!autoRetry || (invalidContentType && options.method !== 'GET')) { | ||
throw new ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, undefined, preCallStack)); | ||
throw new ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response, undefined, preCallStack)); | ||
} | ||
waitTimeInSecs = response_1.headers.has('X-RateLimit-Reset') | ||
? parseInt(response_1.headers.get('X-RateLimit-Reset'), 10) | ||
waitTimeInSecs = response.headers.has('X-RateLimit-Reset') | ||
? parseInt(response.headers.get('X-RateLimit-Reset'), 10) | ||
: retryCount; | ||
if (logLevel >= LogLevel.BASIC) { | ||
if (response_1.status === 429) { | ||
if (response.status === 429) { | ||
log("[".concat(requestId, "] Rate limit exceeded, wait ").concat(waitTimeInSecs, " seconds then retry...")); | ||
} | ||
else { | ||
log("[".concat(requestId, "] Invalid response content type \"").concat(responseContentType, "\" (status ").concat(response_1.status, "). Wait ").concat(waitTimeInSecs, " seconds then retry...")); | ||
log("[".concat(requestId, "] Invalid response content type \"").concat(responseContentType, "\" (status ").concat(response.status, "). Wait ").concat(waitTimeInSecs, " seconds then retry...")); | ||
} | ||
@@ -208,9 +208,9 @@ } | ||
case 3: | ||
_d.sent(); | ||
_f.sent(); | ||
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))]; | ||
case 4: | ||
if (logLevel >= LogLevel.BASIC) { | ||
log("[".concat(requestId, "] Status: ").concat(response_1.status, " (").concat(response_1.statusText, ")")); | ||
log("[".concat(requestId, "] Status: ").concat(response.status, " (").concat(response.statusText, ")")); | ||
if (logLevel >= LogLevel.BODY_AND_HEADERS) { | ||
[ | ||
for (_c = 0, _d = [ | ||
'x-api-version', | ||
@@ -222,26 +222,27 @@ 'x-environment', | ||
'cf-ray', | ||
].forEach(function (key) { | ||
var value = response_1.headers.get(key); | ||
]; _c < _d.length; _c++) { | ||
key = _d[_c]; | ||
value = response.headers.get(key); | ||
if (value) { | ||
log("[".concat(requestId, "] ").concat(key, ": ").concat(value)); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
if (!(response_1.status === 204)) return [3 /*break*/, 5]; | ||
_c = undefined; | ||
if (!(response.status === 204)) return [3 /*break*/, 5]; | ||
_e = undefined; | ||
return [3 /*break*/, 7]; | ||
case 5: return [4 /*yield*/, response_1.json()]; | ||
case 5: return [4 /*yield*/, response.json()]; | ||
case 6: | ||
_c = _d.sent(); | ||
_d.label = 7; | ||
_e = _f.sent(); | ||
_f.label = 7; | ||
case 7: | ||
responseBody = _c; | ||
responseBody = _e; | ||
if (logLevel >= LogLevel.BODY && responseBody) { | ||
log("[".concat(requestId, "] ").concat(JSON.stringify(responseBody, null, 2))); | ||
} | ||
if (!(response_1.status === 202)) return [3 /*break*/, 9]; | ||
if (!(response.status === 202)) return [3 /*break*/, 9]; | ||
return [4 /*yield*/, options.fetchJobResult(responseBody.data.id)]; | ||
case 8: | ||
jobResult = _d.sent(); | ||
jobResult = _f.sent(); | ||
if (jobResult.status < 200 || jobResult.status >= 300) { | ||
@@ -251,8 +252,8 @@ throw new ApiError(buildApiErrorInitObjectFromJobResult(options.method, url, headers, options.body, jobResult.status, jobResult.payload, preCallStack)); | ||
responseBody = jobResult.payload; | ||
_d.label = 9; | ||
_f.label = 9; | ||
case 9: | ||
if (response_1.status >= 200 && response_1.status < 300) { | ||
if (response.status >= 200 && response.status < 300) { | ||
return [2 /*return*/, responseBody]; | ||
} | ||
error = new ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, responseBody, preCallStack)); | ||
error = new ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response, responseBody, preCallStack)); | ||
if (!(autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11]; | ||
@@ -264,7 +265,7 @@ if (logLevel >= LogLevel.BASIC) { | ||
case 10: | ||
_d.sent(); | ||
_f.sent(); | ||
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))]; | ||
case 11: throw error; | ||
case 12: | ||
error_1 = _d.sent(); | ||
error_1 = _f.sent(); | ||
if (!(error_1 instanceof CanceledPromiseError || | ||
@@ -278,3 +279,3 @@ (isErrorWithCode(error_1) && error_1.code.includes('ETIMEDOUT')))) return [3 /*break*/, 15]; | ||
case 13: | ||
_d.sent(); | ||
_f.sent(); | ||
return [2 /*return*/, request(__assign(__assign({}, options), { retryCount: retryCount + 1 }))]; | ||
@@ -281,0 +282,0 @@ case 14: throw new TimeoutError(buildTimeoutErrorInitObject(options.method, url, headers, options.body, preCallStack)); |
{ | ||
"name": "@datocms/rest-client-utils", | ||
"version": "3.1.4", | ||
"version": "3.1.5", | ||
"description": "Utilities for DatoCMS REST API clients", | ||
@@ -41,3 +41,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "cad1c1fcae257e50798c58f430a3ff966030078b", | ||
"gitHead": "889c19a9678129b7b8883d0ee5ee0b281faf4568", | ||
"devDependencies": { | ||
@@ -44,0 +44,0 @@ "@types/qs": "^6.9.7" |
@@ -33,2 +33,11 @@ /// <reference lib="dom" /> | ||
type Job = { | ||
type: 'job'; | ||
id: string; | ||
}; | ||
type JobResponse = { | ||
data: Job; | ||
}; | ||
type RequestOptions = { | ||
@@ -264,3 +273,3 @@ baseUrl: string; | ||
if (logLevel >= LogLevel.BODY_AND_HEADERS) { | ||
[ | ||
for (const key of [ | ||
'x-api-version', | ||
@@ -272,3 +281,3 @@ 'x-environment', | ||
'cf-ray', | ||
].forEach((key) => { | ||
]) { | ||
const value = response.headers.get(key); | ||
@@ -278,3 +287,3 @@ if (value) { | ||
} | ||
}); | ||
} | ||
} | ||
@@ -291,3 +300,5 @@ } | ||
if (response.status === 202) { | ||
const jobResult = await options.fetchJobResult(responseBody.data.id); | ||
const jobResult = await options.fetchJobResult( | ||
(responseBody as JobResponse).data.id, | ||
); | ||
@@ -312,3 +323,3 @@ if (jobResult.status < 200 || jobResult.status >= 300) { | ||
if (response.status >= 200 && response.status < 300) { | ||
return responseBody; | ||
return responseBody as T; | ||
} | ||
@@ -315,0 +326,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
191779
0.12%3268
0.34%