Skip to content
Snippets Groups Projects
Select Git revision
  • b5ed2531320ab313ed30c4c6c511c0a896d29027
  • master default protected
  • dev
3 results

useRestQueries.ts

Blame
  • useRestQueries.ts 828 B
    import axios, { AxiosRequestConfig } from 'axios';
    import { ApiRequest, RequestParams } from './types';
    
    function useRestQueries(
      // 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 useRestQueries;