53 lines
1.3 KiB
TypeScript
53 lines
1.3 KiB
TypeScript
|
|
export interface Params {
|
|
baseUrl: string;
|
|
beforeRequest?: (url: string, options: RequestInit) => Promise<RequestInit>;
|
|
afterRequest?: (
|
|
url: string,
|
|
options: RequestInit,
|
|
response: Response
|
|
) => Promise<boolean>;
|
|
}
|
|
export const customFetch = (
|
|
params: Params,
|
|
auth?: string,
|
|
showorg?: string
|
|
) => {
|
|
return async function newFetch(url: string, options: RequestInit = {}) {
|
|
const newRequestObject = await params?.beforeRequest?.(url, options);
|
|
const fetchRequest = await fetch(params.baseUrl + url, {
|
|
credentials: 'include',
|
|
...(newRequestObject || options),
|
|
headers: {
|
|
...(auth ? { auth } : {}),
|
|
...(showorg ? { showorg } : {}),
|
|
...(options.body instanceof FormData
|
|
? {}
|
|
: { 'Content-Type': 'application/json' }),
|
|
Accept: 'application/json',
|
|
...options?.headers,
|
|
},
|
|
// @ts-ignore
|
|
...(!options.next && options.cache !== 'force-cache'
|
|
? { cache: options.cache || 'no-store' }
|
|
: {}),
|
|
});
|
|
|
|
if (
|
|
!params?.afterRequest ||
|
|
(await params?.afterRequest?.(url, options, fetchRequest))
|
|
) {
|
|
return fetchRequest;
|
|
}
|
|
|
|
// @ts-ignore
|
|
return new Promise((res) => {}) as Response;
|
|
};
|
|
};
|
|
|
|
export const fetchBackend = customFetch({
|
|
get baseUrl() {
|
|
return process.env.BACKEND_URL!;
|
|
},
|
|
});
|