39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import axios, { AxiosInstance } from 'axios';
|
|
|
|
const backend_url = process.env.NEXT_PUBLIC_STRAPI_URL;
|
|
|
|
export class HttpInstanceFactory {
|
|
private static baseInstance: AxiosInstance | null = null;
|
|
private static authorizedInstance: AxiosInstance | null = null;
|
|
|
|
public static getBaseInstance(): AxiosInstance {
|
|
if (this.baseInstance) return this.baseInstance;
|
|
this.baseInstance = axios.create({
|
|
baseURL: backend_url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'ngrok-skip-browser-warning': 'true',
|
|
},
|
|
});
|
|
return this.baseInstance;
|
|
}
|
|
|
|
public static getAuthorizedInstance(jwt?: string): AxiosInstance {
|
|
if (this.authorizedInstance) return this.authorizedInstance;
|
|
if (jwt) return this.updateAuthorizedInstance(jwt);
|
|
return this.getBaseInstance();
|
|
}
|
|
|
|
public static updateAuthorizedInstance(jwt: string) {
|
|
this.authorizedInstance = axios.create({
|
|
baseURL: backend_url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'ngrok-skip-browser-warning': 'true',
|
|
Authorization: `Bearer ${jwt}`,
|
|
},
|
|
});
|
|
return this.authorizedInstance;
|
|
}
|
|
}
|