@datocms/rest-client-utils
Advanced tools
Comparing version 0.1.2 to 0.1.4
@@ -17,2 +17,18 @@ "use strict"; | ||
})(); | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -113,5 +129,14 @@ exports.ApiError = void 0; | ||
}); | ||
ApiError.prototype.findErrorWithCode = function (codeOrCodes) { | ||
ApiError.prototype.findError = function (codeOrCodes, filterDetails) { | ||
var codes = Array.isArray(codeOrCodes) ? codeOrCodes : [codeOrCodes]; | ||
return this.errors.find(function (error) { return codes.includes(error.attributes.code); }); | ||
return this.errors.find(function (error) { | ||
return codes.includes(error.attributes.code) && | ||
(!filterDetails || | ||
(typeof filterDetails === 'function' | ||
? filterDetails(error.attributes.details) | ||
: Object.entries(filterDetails).every(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return error.attributes.details[key] === value; | ||
}))); | ||
}); | ||
}; | ||
@@ -121,3 +146,3 @@ Object.defineProperty(ApiError.prototype, "humanMessage", { | ||
var _this = this; | ||
var planUpgradeError = this.findErrorWithCode('PLAN_UPGRADE_REQUIRED'); | ||
var planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED'); | ||
if (planUpgradeError) { | ||
@@ -128,3 +153,3 @@ var limit = planUpgradeError.attributes.details.limit; | ||
var errors = Object.keys(humanMessageForCode) | ||
.filter(function (code) { return _this.findErrorWithCode(code); }) | ||
.filter(function (code) { return _this.findError(code); }) | ||
.map(function (code) { return humanMessageForCode[code]; }); | ||
@@ -131,0 +156,0 @@ if (errors.length === 0) { |
@@ -256,4 +256,3 @@ "use strict"; | ||
error = new ApiError_1.ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, responseBody, preCallStack)); | ||
if (!(autoRetry && | ||
error.findErrorWithCode('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11]; | ||
if (!(autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11]; | ||
if (logLevel >= LogLevel.BASIC) { | ||
@@ -260,0 +259,0 @@ console.log("[".concat(requestId, "] Data validation in progress, waiting ").concat(retryCount, " seconds...")); |
@@ -6,5 +6,6 @@ export declare type ErrorEntity = { | ||
code: string; | ||
details: unknown; | ||
details: Record<string, unknown>; | ||
}; | ||
}; | ||
declare type FilterFn = (details: Record<string, unknown>) => boolean; | ||
export declare type ApiErrorRequest = { | ||
@@ -33,4 +34,5 @@ url: string; | ||
get errors(): ErrorEntity[]; | ||
findErrorWithCode(codeOrCodes: any): ErrorEntity | undefined; | ||
findError(codeOrCodes: string | string[], filterDetails?: Record<string, string> | FilterFn): ErrorEntity | undefined; | ||
get humanMessage(): string | null; | ||
} | ||
export {}; |
@@ -16,2 +16,18 @@ var __extends = (this && this.__extends) || (function () { | ||
})(); | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
function isErrorBody(body) { | ||
@@ -110,5 +126,14 @@ if (typeof body !== 'object' || body === null || !('data' in body)) { | ||
}); | ||
ApiError.prototype.findErrorWithCode = function (codeOrCodes) { | ||
ApiError.prototype.findError = function (codeOrCodes, filterDetails) { | ||
var codes = Array.isArray(codeOrCodes) ? codeOrCodes : [codeOrCodes]; | ||
return this.errors.find(function (error) { return codes.includes(error.attributes.code); }); | ||
return this.errors.find(function (error) { | ||
return codes.includes(error.attributes.code) && | ||
(!filterDetails || | ||
(typeof filterDetails === 'function' | ||
? filterDetails(error.attributes.details) | ||
: Object.entries(filterDetails).every(function (_a) { | ||
var _b = __read(_a, 2), key = _b[0], value = _b[1]; | ||
return error.attributes.details[key] === value; | ||
}))); | ||
}); | ||
}; | ||
@@ -118,3 +143,3 @@ Object.defineProperty(ApiError.prototype, "humanMessage", { | ||
var _this = this; | ||
var planUpgradeError = this.findErrorWithCode('PLAN_UPGRADE_REQUIRED'); | ||
var planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED'); | ||
if (planUpgradeError) { | ||
@@ -125,3 +150,3 @@ var limit = planUpgradeError.attributes.details.limit; | ||
var errors = Object.keys(humanMessageForCode) | ||
.filter(function (code) { return _this.findErrorWithCode(code); }) | ||
.filter(function (code) { return _this.findError(code); }) | ||
.map(function (code) { return humanMessageForCode[code]; }); | ||
@@ -128,0 +153,0 @@ if (errors.length === 0) { |
@@ -250,4 +250,3 @@ var __assign = (this && this.__assign) || function () { | ||
error = new ApiError(buildApiErrorInitObject(options.method, url, headers, options.body, response_1, responseBody, preCallStack)); | ||
if (!(autoRetry && | ||
error.findErrorWithCode('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11]; | ||
if (!(autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS'))) return [3 /*break*/, 11]; | ||
if (logLevel >= LogLevel.BASIC) { | ||
@@ -254,0 +253,0 @@ console.log("[".concat(requestId, "] Data validation in progress, waiting ").concat(retryCount, " seconds...")); |
@@ -6,5 +6,6 @@ export declare type ErrorEntity = { | ||
code: string; | ||
details: unknown; | ||
details: Record<string, unknown>; | ||
}; | ||
}; | ||
declare type FilterFn = (details: Record<string, unknown>) => boolean; | ||
export declare type ApiErrorRequest = { | ||
@@ -33,4 +34,5 @@ url: string; | ||
get errors(): ErrorEntity[]; | ||
findErrorWithCode(codeOrCodes: any): ErrorEntity | undefined; | ||
findError(codeOrCodes: string | string[], filterDetails?: Record<string, string> | FilterFn): ErrorEntity | undefined; | ||
get humanMessage(): string | null; | ||
} | ||
export {}; |
{ | ||
"name": "@datocms/rest-client-utils", | ||
"version": "0.1.2", | ||
"version": "0.1.4", | ||
"description": "Utilities for DatoCMS REST API clients", | ||
@@ -43,3 +43,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "9edc89423c21752d61d2b776cb11821b5334c83b" | ||
"gitHead": "a766f60fee9258542328e4a28b93eeac8d31cded" | ||
} |
@@ -6,3 +6,3 @@ export type ErrorEntity = { | ||
code: string; | ||
details: unknown; | ||
details: Record<string, unknown>; | ||
}; | ||
@@ -12,2 +12,4 @@ }; | ||
type FilterFn = (details: Record<string, unknown>) => boolean; | ||
function isErrorBody(body: unknown): body is ErrorBody { | ||
@@ -143,9 +145,21 @@ if (typeof body !== 'object' || body === null || !('data' in body)) { | ||
findErrorWithCode(codeOrCodes) { | ||
findError( | ||
codeOrCodes: string | string[], | ||
filterDetails?: Record<string, string> | FilterFn, | ||
) { | ||
const codes = Array.isArray(codeOrCodes) ? codeOrCodes : [codeOrCodes]; | ||
return this.errors.find((error) => codes.includes(error.attributes.code)); | ||
return this.errors.find( | ||
(error) => | ||
codes.includes(error.attributes.code) && | ||
(!filterDetails || | ||
(typeof filterDetails === 'function' | ||
? filterDetails(error.attributes.details) | ||
: Object.entries(filterDetails).every( | ||
([key, value]) => error.attributes.details[key] === value, | ||
))), | ||
); | ||
} | ||
get humanMessage() { | ||
const planUpgradeError = this.findErrorWithCode('PLAN_UPGRADE_REQUIRED'); | ||
const planUpgradeError = this.findError('PLAN_UPGRADE_REQUIRED'); | ||
@@ -161,3 +175,3 @@ if (planUpgradeError) { | ||
const errors = Object.keys(humanMessageForCode) | ||
.filter((code) => this.findErrorWithCode(code)) | ||
.filter((code) => this.findError(code)) | ||
.map((code) => humanMessageForCode[code]); | ||
@@ -164,0 +178,0 @@ |
@@ -248,6 +248,3 @@ import qs from 'qs'; | ||
if ( | ||
autoRetry && | ||
error.findErrorWithCode('BATCH_DATA_VALIDATION_IN_PROGRESS') | ||
) { | ||
if (autoRetry && error.findError('BATCH_DATA_VALIDATION_IN_PROGRESS')) { | ||
if (logLevel >= LogLevel.BASIC) { | ||
@@ -254,0 +251,0 @@ console.log( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
160780
2834