@tectonique/api-standards-client
Version:
Client side implementation for API Standards (response envelopes and Problem Details).
79 lines (78 loc) • 4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTypedAxiosVerbs = exports.createTypedHandleProblemDetail = exports.createTypeSafeAxios = void 0;
const axios_1 = require("axios");
const api_standards_1 = require("@tectonique/api-standards");
const ClientProblemDetails_1 = require("../ClientProblemDetails");
const { ClientSideInternalProblemDetail, ResponseNotAnEnvelopeProblemDetail } = ClientProblemDetails_1.ClientProblemDetailsCollection;
function createTypeSafeAxios(axios) {
return {
verbs: createTypedAxiosVerbs(axios),
createProblemDetailHandler: createTypedCreateProblemDetailHandler(),
handleProblemDetail: createTypedHandleProblemDetail(),
};
}
exports.createTypeSafeAxios = createTypeSafeAxios;
function createTypedCreateProblemDetailHandler() {
const handleProblemDetail = createTypedHandleProblemDetail();
return function (handler) {
return function (reason) {
return handleProblemDetail(reason, handler);
};
};
}
function createTypedHandleProblemDetail() {
return function (reason, handler) {
var _a, _b;
if (api_standards_1.ProblemDetails.isOne(reason)) {
return handler(reason);
}
else if ((0, axios_1.isAxiosError)(reason) &&
api_standards_1.ProblemDetails.isOne((_a = reason.response) === null || _a === void 0 ? void 0 : _a.data)) {
return handler((_b = reason.response) === null || _b === void 0 ? void 0 : _b.data);
}
throw reason;
};
}
exports.createTypedHandleProblemDetail = createTypedHandleProblemDetail;
function createTypedAxiosVerbs(axios) {
return {
get: (url, options) => {
return handleAxiosResponse(axios.get(url, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.config), { params: options === null || options === void 0 ? void 0 : options.query })));
},
post: (url, body, options) => {
return handleAxiosResponse(axios.post(url, body, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.config), { params: options === null || options === void 0 ? void 0 : options.query })));
},
put: (url, body, options) => {
return handleAxiosResponse(axios.put(url, body, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.config), { params: options === null || options === void 0 ? void 0 : options.query })));
},
patch: (url, body, options) => {
return handleAxiosResponse(axios.patch(url, body, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.config), { params: options === null || options === void 0 ? void 0 : options.query })));
},
delete: (url, options) => {
return handleAxiosResponse(axios.delete(url, Object.assign(Object.assign({}, options === null || options === void 0 ? void 0 : options.config), { params: options === null || options === void 0 ? void 0 : options.query })));
},
};
}
exports.createTypedAxiosVerbs = createTypedAxiosVerbs;
function handleAxiosResponse(axiosPromise) {
return axiosPromise
.then((response) => {
if (api_standards_1.ResponseEnvelopes.isOne(response.data)) {
return response.data;
}
return Promise.reject(ResponseNotAnEnvelopeProblemDetail({
payload: response.data,
}));
})
.catch((reason) => {
var _a;
if (api_standards_1.ProblemDetails.isOne((_a = reason === null || reason === void 0 ? void 0 : reason.response) === null || _a === void 0 ? void 0 : _a.data)) {
return Promise.reject(reason);
}
return Promise.reject(ClientSideInternalProblemDetail({
detail: reason === null || reason === void 0 ? void 0 : reason.message,
payload: reason,
}));
});
}