source: src/utils/axios.ts@ 057453c

main
Last change on this file since 057453c was 057453c, checked in by Naum Shapkarovski <naumshapkarovski@…>, 5 weeks ago

feat: implement employees

  • Property mode set to 100644
File size: 1.4 KB
Line 
1import axios, { AxiosRequestConfig } from 'axios';
2import { auth } from 'src/lib/firebase';
3// config
4import { HOST_API } from 'src/config-global';
5
6// ----------------------------------------------------------------------
7
8const axiosInstance = axios.create({ baseURL: HOST_API });
9
10axiosInstance.interceptors.request.use(
11 async (config) => {
12 try {
13 const currentUser = auth.currentUser;
14 if (currentUser) {
15 const token = await currentUser.getIdToken();
16 // eslint-disable-next-line no-param-reassign
17 config.headers.Authorization = `Bearer ${token}`;
18 }
19 return config;
20 } catch (error) {
21 return Promise.reject(error);
22 }
23 },
24 (error) => Promise.reject(error)
25);
26
27axiosInstance.interceptors.response.use(
28 (res) => res,
29 (error) => Promise.reject((error.response && error.response.data) || 'Something went wrong')
30);
31
32export default axiosInstance;
33
34// ----------------------------------------------------------------------
35
36export const fetcher = async <T = any>(args: string | [string, AxiosRequestConfig]): Promise<T> => {
37 const [url, config] = Array.isArray(args) ? args : [args];
38
39 const res = await axiosInstance.get(url, { ...config });
40
41 return res.data;
42};
43
44// ----------------------------------------------------------------------
45
46export const endpoints = {
47 invoice: '/api/invoices',
48 customer: '/api/customers',
49 tenant: '/api/tenant',
50 service: '/api/services',
51 employee: '/api/employees',
52};
Note: See TracBrowser for help on using the repository browser.