Initial commit: Tapalka monorepo (bot, front, strapi)
This commit is contained in:
38
CMyTapper/robucks-front/utils/HttpInstanceFactory.ts
Normal file
38
CMyTapper/robucks-front/utils/HttpInstanceFactory.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user