1 | import axios, { AxiosRequestConfig } from 'axios';
|
---|
2 | import { auth } from 'src/lib/firebase';
|
---|
3 | // config
|
---|
4 | import { HOST_API } from 'src/config-global';
|
---|
5 |
|
---|
6 | // ----------------------------------------------------------------------
|
---|
7 |
|
---|
8 | const axiosInstance = axios.create({ baseURL: HOST_API });
|
---|
9 |
|
---|
10 | axiosInstance.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 |
|
---|
27 | axiosInstance.interceptors.response.use(
|
---|
28 | (res) => res,
|
---|
29 | (error) => Promise.reject((error.response && error.response.data) || 'Something went wrong')
|
---|
30 | );
|
---|
31 |
|
---|
32 | export default axiosInstance;
|
---|
33 |
|
---|
34 | // ----------------------------------------------------------------------
|
---|
35 |
|
---|
36 | export 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 |
|
---|
46 | export const endpoints = {
|
---|
47 | invoice: '/api/invoices',
|
---|
48 | customer: '/api/customers',
|
---|
49 | tenant: '/api/tenant',
|
---|
50 | service: '/api/services',
|
---|
51 | employee: '/api/employees',
|
---|
52 | };
|
---|