@datocms/rest-client-utils
Version:
Utilities for DatoCMS REST API clients
116 lines • 4.59 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.ApiError = exports.TimeoutError = void 0;
function isErrorBody(body) {
if (typeof body !== 'object' || body === null || !('data' in body)) {
return false;
}
var bodyWithData = body;
if (!Array.isArray(bodyWithData.data)) {
return false;
}
var bodyWithDataList = bodyWithData;
if (bodyWithDataList.data.length === 0) {
return false;
}
var firstEl = bodyWithDataList.data[0];
if (typeof firstEl !== 'object' ||
firstEl === null ||
!('id' in firstEl) ||
!('type' in firstEl) ||
!('attributes' in firstEl) ||
firstEl.type !== 'api_error') {
return false;
}
return true;
}
var TimeoutError = /** @class */ (function (_super) {
__extends(TimeoutError, _super);
function TimeoutError(initObject) {
var _newTarget = this.constructor;
var _this = _super.call(this, 'API Error!') || this;
Object.setPrototypeOf(_this, _newTarget.prototype);
if ('captureStackTrace' in Error) {
Error.captureStackTrace(_this, ApiError);
}
else {
_this.stack = new Error().stack;
}
_this.request = initObject.request;
_this.preCallStack = initObject.preCallStack;
_this.message = "".concat(initObject.request.method, " ").concat(initObject.request.url, ": Timeout error");
if (_this.preCallStack) {
_this.stack += "\nCaused By:\n".concat(_this.preCallStack);
}
return _this;
}
return TimeoutError;
}(Error));
exports.TimeoutError = TimeoutError;
var ApiError = /** @class */ (function (_super) {
__extends(ApiError, _super);
function ApiError(initObject) {
var _newTarget = this.constructor;
var _this = _super.call(this, 'API Error!') || this;
Object.setPrototypeOf(_this, _newTarget.prototype);
if ('captureStackTrace' in Error) {
Error.captureStackTrace(_this, ApiError);
}
else {
_this.stack = new Error().stack;
}
_this.request = initObject.request;
_this.response = initObject.response;
_this.preCallStack = initObject.preCallStack;
var message = "".concat(initObject.request.method, " ").concat(initObject.request.url, ": ").concat(_this.response.status, " ").concat(_this.response.statusText);
if (_this.errors.length > 0) {
message += "\n\n".concat(JSON.stringify(_this.errors, null, 2));
}
_this.message = message;
if (_this.preCallStack) {
_this.stack += "\nCaused By:\n".concat(_this.preCallStack);
}
return _this;
}
Object.defineProperty(ApiError.prototype, "errors", {
get: function () {
if (!isErrorBody(this.response.body)) {
return [];
}
return this.response.body.data;
},
enumerable: false,
configurable: true
});
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) &&
(!filterDetails ||
(typeof filterDetails === 'function'
? filterDetails(error.attributes.details)
: Object.entries(filterDetails).every(function (_a) {
var key = _a[0], value = _a[1];
return error.attributes.details[key] === value;
})));
});
};
return ApiError;
}(Error));
exports.ApiError = ApiError;
//# sourceMappingURL=errors.js.map