@woogie0303/easyfetch
Version:
#### [한국어 버전](./README_KO.md)
50 lines (42 loc) • 2.12 kB
text/typescript
declare const METHOD_WITHOUT_BODY: readonly ["get", "delete"];
declare const METHOD_WITH_BODY: readonly ["patch", "put", "post"];
interface NextFetchRequestConfig {
revalidate?: number | false;
tags?: string[];
}
interface RequestInitWithNextConfig extends globalThis.RequestInit {
next?: NextFetchRequestConfig | undefined;
}
type EasyFetchResponse<T> = Omit<Awaited<ReturnType<typeof fetch>>, keyof Body | 'clone' | 'url'> & {
body: T;
config: EasyFetchRequestType;
};
type EasyFetchRequestType = [
string | URL,
RequestInitWithNextConfig | undefined
];
interface InterceptorCallbackType<T extends 'request' | 'response'> {
(onFulfilled?: (arg: T extends 'request' ? EasyFetchRequestType : EasyFetchResponse<any>) => T extends 'request' ? Promise<EasyFetchRequestType> | EasyFetchRequestType : Promise<EasyFetchResponse<any>> | EasyFetchResponse<any>, onRejected?: (err: any) => any): void;
}
declare class EasyFetch {
#private;
constructor(defaultConfig?: EasyFetchDefaultConfig);
request<T>(request: RequestInfo | URL, requestInit?: EasyFetchRequestType[1]): Promise<EasyFetchResponse<T>>;
get interceptor(): {
request: InterceptorCallbackType<"request">;
response: InterceptorCallbackType<"response">;
};
}
type MethodWithBodyType = (typeof METHOD_WITH_BODY)[number];
type MethodWithoutBodyType = (typeof METHOD_WITHOUT_BODY)[number];
type MethodType = MethodWithBodyType | MethodWithoutBodyType;
type MethodFunction<T> = T extends MethodWithBodyType ? <P>(url: string | URL, reqBody?: object, reqConfig?: Omit<RequestInitWithNextConfig, 'method' | 'body'>) => Promise<EasyFetchResponse<P>> : <P>(url: string | URL, reqConfig?: Omit<RequestInitWithNextConfig, 'method'>) => Promise<EasyFetchResponse<P>>;
type EasyFetchWithAPIMethodsType = EasyFetch & {
[K in MethodType]: MethodFunction<K>;
};
interface EasyFetchDefaultConfig {
baseUrl?: string | URL;
headers?: HeadersInit;
}
declare const easyFetch: (defaultConfig?: EasyFetchDefaultConfig) => EasyFetchWithAPIMethodsType;
export { type EasyFetchResponse, easyFetch };