UNPKG

@tectonique/api-standards-client

Version:

Client side implementation for API Standards (response envelopes and Problem Details).

73 lines (72 loc) 3.58 kB
import { isAxiosError } from "axios"; import { ProblemDetails, ResponseEnvelopes } from "@tectonique/api-standards"; import { ClientProblemDetailsCollection } from "../ClientProblemDetails"; const { ClientSideInternalProblemDetail, ResponseNotAnEnvelopeProblemDetail } = ClientProblemDetailsCollection; export function createTypeSafeAxios(axios) { return { verbs: createTypedAxiosVerbs(axios), createProblemDetailHandler: createTypedCreateProblemDetailHandler(), handleProblemDetail: createTypedHandleProblemDetail(), }; } function createTypedCreateProblemDetailHandler() { const handleProblemDetail = createTypedHandleProblemDetail(); return function (handler) { return function (reason) { return handleProblemDetail(reason, handler); }; }; } export function createTypedHandleProblemDetail() { return function (reason, handler) { var _a, _b; if (ProblemDetails.isOne(reason)) { return handler(reason); } else if (isAxiosError(reason) && 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; }; } export 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 }))); }, }; } function handleAxiosResponse(axiosPromise) { return axiosPromise .then((response) => { if (ResponseEnvelopes.isOne(response.data)) { return response.data; } return Promise.reject(ResponseNotAnEnvelopeProblemDetail({ payload: response.data, })); }) .catch((reason) => { var _a; if (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, })); }); }