Skip to content
Snippets Groups Projects
useRestQueries.ts 830 B
Newer Older
  • Learn to ignore specific revisions
  • 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;