@contentgrid/problem-details
Version:
RFC9547 Problem Details types and helpers
38 lines (36 loc) • 1.16 kB
JavaScript
async function fromResponse(response) {
var _a, _b, _c;
if (response.ok) {
return null;
}
if (((_a = response.headers.get("content-type")) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== "application/problem+json") {
return {
status: response.status,
title: response.statusText
};
}
const data = await response.json();
(_b = data.status) !== null && _b !== void 0 ? _b : data.status = response.status;
(_c = data.title) !== null && _c !== void 0 ? _c : data.title = response.statusText;
if (data.type === null || data.type === "about:blank") {
delete data.type;
}
return data;
}
class ProblemDetailError extends Error {
constructor(problemDetail) {
super(problemDetail.title);
this.problemDetail = problemDetail;
this.name = 'ProblemDetailError';
Object.setPrototypeOf(this, new.target.prototype);
}
}
async function checkResponse(response) {
const problem = await fromResponse(response);
if (problem !== null) {
throw new ProblemDetailError(problem);
}
return response;
}
export { ProblemDetailError, checkResponse, fromResponse };
//# sourceMappingURL=index.mjs.map