Spaces:
Runtime error
Runtime error
File size: 2,563 Bytes
13095e0 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
export type RequestModel = {
params?: object;
headers?: object;
signal?: AbortSignal;
};
export type RequestWithBodyModel = RequestModel & {
body?: object | FormData;
};
export const useFetch = () => {
const handleFetch = async (
url: string,
request: any,
signal?: AbortSignal,
) => {
const requestUrl = request?.params ? `${url}${request.params}` : url;
const requestBody = request?.body
? request.body instanceof FormData
? { ...request, body: request.body }
: { ...request, body: JSON.stringify(request.body) }
: request;
const headers = {
...(request?.headers
? request.headers
: request?.body && request.body instanceof FormData
? {}
: { 'Content-type': 'application/json' }),
};
return fetch(requestUrl, { ...requestBody, headers, signal })
.then((response) => {
if (!response.ok) throw response;
const contentType = response.headers.get('content-type');
const contentDisposition = response.headers.get('content-disposition');
const headers = response.headers;
const result =
contentType &&
(contentType?.indexOf('application/json') !== -1 ||
contentType?.indexOf('text/plain') !== -1)
? response.json()
: contentDisposition?.indexOf('attachment') !== -1
? response.blob()
: response;
return result;
})
.catch(async (err) => {
const contentType = err.headers.get('content-type');
const errResult =
contentType && contentType?.indexOf('application/problem+json') !== -1
? await err.json()
: err;
throw errResult;
});
};
return {
get: async <T>(url: string, request?: RequestModel): Promise<T> => {
return handleFetch(url, { ...request, method: 'get' });
},
post: async <T>(
url: string,
request?: RequestWithBodyModel,
): Promise<T> => {
return handleFetch(url, { ...request, method: 'post' });
},
put: async <T>(url: string, request?: RequestWithBodyModel): Promise<T> => {
return handleFetch(url, { ...request, method: 'put' });
},
patch: async <T>(
url: string,
request?: RequestWithBodyModel,
): Promise<T> => {
return handleFetch(url, { ...request, method: 'patch' });
},
delete: async <T>(url: string, request?: RequestModel): Promise<T> => {
return handleFetch(url, { ...request, method: 'delete' });
},
};
};
|