diff --git a/src/hooks/useRestQueries.ts b/src/hooks/useRestQueries.ts new file mode 100644 index 0000000000000000000000000000000000000000..1201e5c74ff6438a529af36d11dd6d789719be31 --- /dev/null +++ b/src/hooks/useRestQueries.ts @@ -0,0 +1,20 @@ +import axios, { AxiosRequestConfig } from 'axios'; +import { ApiRequest, RequestParams } from './types'; + +function userRestQueries( + // When a request needs more config like CancelToken, etc. + config?: AxiosRequestConfig, +): Record<string, <Data>(path: string) => ApiRequest<Data>> { + return { + get: <Data>(path: string) => (params: RequestParams) => + axios.get<Data>(path, { ...config, ...params }), + post: <Data>(path: string) => (params: RequestParams) => + axios.post<Data>(path, params.body ?? {}, { ...config, ...params }), + put: <Data>(path: string) => (params: RequestParams) => + axios.put<Data>(path, params.body ?? {}, { ...config, ...params }), + delete: (path: string) => (params: RequestParams) => + axios.delete(path, { ...config, ...params }), + }; +} + +export default userRestQueries;