UNPKG

@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.

37 lines (36 loc) 1.99 kB
/** * Utility functions for API interfacing. * -------------------------------------- * These functions are designed to facilitate interaction with an API client */ import { AxiosError, type AxiosResponse } from 'axios'; import type { AnyPromiseFunction } from '../@types/global'; /** * 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. */ export declare const isAxiosResponse: <TResponse>(response?: TResponse | AxiosResponse<TResponse, any> | undefined) => response is AxiosResponse<TResponse, any>; /** * 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. */ export declare const isAxiosError: (error: unknown) => error is AxiosError<unknown, any>; /** * 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. */ export declare const fixGeneratedClient: <T>(original: T) => T; /** * 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. */ export declare const processAxiosPromise: <TFunc extends AnyPromiseFunction>(func: TFunc) => Promise<ReturnType<TFunc>>;