import axios, { AxiosInstance } from 'axios' import { getCookieByName } from '../utils' const headers: { [key: string]: string } = { Accept: 'application/vnd.api+json', 'Content-Type': 'application/vnd.api+json', } const isWindowDefined = typeof window !== 'undefined' const authGuestToken = isWindowDefined ? localStorage.getItem('auth_guest_token') : '' if (isWindowDefined && authGuestToken) { headers['Authorization-Guest'] = authGuestToken } export const setAxiosHeader = (key: string, value: string | number) => { headers[key] = `${value}` } interface IPublicRequest extends AxiosInstance { setBaseUrl: (baseUrl: string) => void; setGuestToken: (guestToken: string) => void; } export const publicRequest: IPublicRequest = axios.create({ baseURL: 'https://www.ticketfairy.com/api', headers, withCredentials: true, }) as IPublicRequest publicRequest.interceptors.request.use(config => { if (getCookieByName('X-TF-ECOMMERCE')) { config.headers['X-TF-ECOMMERCE'] = getCookieByName('X-TF-ECOMMERCE') } return config }) publicRequest.setBaseUrl = (baseUrl: string) => (publicRequest.defaults.baseURL = baseUrl + '/api') publicRequest.setGuestToken = (guestToken: string) => (publicRequest.defaults.headers.common['Authorization-Guest'] = guestToken) export const setBaseUrl = (baseUrl: string) => { publicRequest.setBaseUrl(baseUrl) }