create-fetch-hooks
Version:
A simple and powerful custom React hook (useApi) that simplifies API requests with automatic loading and error states. Ideal for clean and maintainable data fetching in React components.
22 lines (21 loc) • 898 B
TypeScript
import { FetchError } from "./types/FetchError";
export type UsePostOptions<Response> = {
onSuccess?: (res: Response) => void;
onError?: (error: FetchError) => void;
onResponseGot?: (url: string, endpoint: string, responseCode: number | string | undefined) => void;
convertToFormData?: boolean;
removeIfValueIsNullOrUndefined?: boolean;
headers?: Record<string, string>;
accessTokenLocalStorageKey?: string;
callRefreshToken?: () => {
on: number[];
body: Record<string, string>;
endpoint: string;
saveAccessTokenFromResponse?: (res: any) => void;
};
};
export declare function usePost<PostDataType, ResponseType>(baseApiUrl: string, url: string, options?: UsePostOptions<ResponseType>): {
error: FetchError | undefined;
loading: boolean;
postData: (dataToPost: PostDataType, id?: number | string) => Promise<void>;
};