@rocketmakers/api-swr
Version:
Rocketmakers front-end library for parsing a generated Typescript API client into a set of configurable React hooks for fetching and mutating data.
68 lines (67 loc) • 3.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processAxiosPromise = exports.fixGeneratedClient = exports.isAxiosError = exports.isAxiosResponse = void 0;
const tslib_1 = require("tslib");
/**
* Utility functions for API interfacing.
* --------------------------------------
* These functions are designed to facilitate interaction with an API client
*/
const axios_1 = require("axios");
/**
* Checks whether an API response object is an Axios response.
* @param {AxiosResponse<TResponse> | TResponse} response - The API response object to be checked.
* @returns {boolean} - A boolean indicating whether the API response object is an Axios response.
* @template TResponse - The type of data returned by the Axios response.
*/
const isAxiosResponse = (response) => {
if (!response)
return false;
const castResponse = response;
return 'status' in castResponse && 'statusText' in castResponse;
};
exports.isAxiosResponse = isAxiosResponse;
/**
* Checks if the provided error is an AxiosError.
* @param {unknown} error - The error to be checked.
* @returns {boolean} Returns true if the error is an AxiosError, false otherwise.
*/
const isAxiosError = (error) => {
return !!(error === null || error === void 0 ? void 0 : error.isAxiosError);
};
exports.isAxiosError = isAxiosError;
/**
* Iterates over an OpenAPI TypeScript controller and corrects the scoping issues by adding an initial arrow function to each class method.
* @param original - The original OpenAPI TypeScript controller to be fixed.
* @returns A new OpenAPI TypeScript controller with fixed scoping issues.
* @typeparam T - The type of the OpenAPI TypeScript controller to be fixed.
*/
const fixGeneratedClient = (original) => {
const keys = new Set([...Object.getOwnPropertyNames(Object.getPrototypeOf(original)), ...Object.getOwnPropertyNames(original)]);
return Array.from(keys).reduce((client, func) => {
if (func !== 'constructor') {
// eslint-disable-next-line no-param-reassign
client[func] = (...args) => original[func](...args);
}
return client;
}, {});
};
exports.fixGeneratedClient = fixGeneratedClient;
/**
* Processes the data from an axios API response.
* @async
* @template TFunc - The function to execute and process.
* @param {TFunc} func - The function to execute.
* @returns {Promise<ReturnType<TFunc>>} - The response from the API.
* @throws {Error} - If the API response status is 400 or higher, an error with the response is thrown.
*/
const processAxiosPromise = (func) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const response = yield func();
if ((0, exports.isAxiosResponse)(response)) {
if (response.status >= 400) {
throw new axios_1.AxiosError(response.statusText, response.status.toString(), response.config, response.request, response);
}
}
return response;
});
exports.processAxiosPromise = processAxiosPromise;