File size: 881 Bytes
743dad3
 
 
 
1e075e6
743dad3
 
 
 
 
 
1e075e6
743dad3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
31
32
33
34
35
36
import axios from "axios";
import MY_TOKEN_KEY from "./get-cookie-name";

export const api = axios.create({
  baseURL: (typeof window !== 'undefined' ? (process.env.NEXT_PUBLIC_APP_API_URL as string) : undefined) || `/api`,
  headers: {
    cache: "no-store",
  },
});

export const apiServer = axios.create({
  baseURL: (process.env.NEXT_APP_API_URL as string) || `http://localhost:3000/api`,
  headers: {
    cache: "no-store",
  },
});
api.interceptors.request.use(
  async (config) => {
    // get the token from cookies
    const cookie_name = MY_TOKEN_KEY();
    const token = document.cookie
      .split("; ")
      .find((row) => row.startsWith(`${cookie_name}=`))
      ?.split("=")[1];
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  (error) => {
    // Handle the error
    return Promise.reject(error);
  }
);