50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { AxiosInstance } from 'axios';
|
|
import { getIsJwtToken, getIsJwtTokenExpire } from '@Tools/utility';
|
|
import Header from './_APITool/Header';
|
|
import { createAxios } from './_APITool';
|
|
import { CreatUserAPI } from './user-apis';
|
|
import { UserAPIProps } from './user-apis/types';
|
|
import { APIParams } from './types';
|
|
|
|
class API {
|
|
private axios: AxiosInstance;
|
|
|
|
public baseHeader: Header;
|
|
|
|
public baseToken: string;
|
|
|
|
public user: UserAPIProps;
|
|
|
|
constructor({ host }: APIParams) {
|
|
this.axios = createAxios({ host });
|
|
this.baseHeader = new Header();
|
|
this.baseToken = '';
|
|
this.user = CreatUserAPI({ axios: this.axios, header: this.baseHeader });
|
|
}
|
|
|
|
updateAccessToken = (token: string): void => {
|
|
this.baseHeader.updateToken(token);
|
|
this.baseToken = token;
|
|
};
|
|
|
|
removeAccessToken = (): void => {
|
|
this.baseHeader.removeToken();
|
|
this.baseToken = '';
|
|
};
|
|
|
|
checkAccessTokenValid = async (): Promise<boolean> => {
|
|
const isJwtToken = await getIsJwtToken(this.baseToken);
|
|
return isJwtToken;
|
|
};
|
|
|
|
checkAccessTokenExpired = async (expiredTime = 0): Promise<boolean> => {
|
|
const isJwtToken = await this.checkAccessTokenValid();
|
|
if (isJwtToken) {
|
|
return getIsJwtTokenExpire(this.baseToken, expiredTime);
|
|
}
|
|
return true;
|
|
};
|
|
}
|
|
|
|
export default API;
|