File size: 1,221 Bytes
d629cd4
 
 
f81fa54
d629cd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8268e91
d629cd4
8268e91
d629cd4
8268e91
d629cd4
8268e91
d629cd4
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import axios from 'axios';

const client = axios.create({
  baseURL: process.env.NEXT_PUBLIC_API_BASE_URL || '',
  timeout: 10000,
});

client.interceptors.request.use((config) => {
  if (typeof window !== 'undefined') {
    const token = localStorage.getItem('token');
    if (token && config.headers) {
      config.headers.Authorization = `Bearer ${token}`;
    }
  }
  return config;
});

client.interceptors.response.use(undefined, (error) => Promise.reject(error));

export const api = {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  get: (...args: Parameters<typeof client.get>) => client.get(...args).then((response) => response.data as any),
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  post: (...args: Parameters<typeof client.post>) => client.post(...args).then((response) => response.data as any),
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  put: (...args: Parameters<typeof client.put>) => client.put(...args).then((response) => response.data as any),
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  delete: (...args: Parameters<typeof client.delete>) => client.delete(...args).then((response) => response.data as any),
};