119 lines
3.9 KiB
TypeScript
119 lines
3.9 KiB
TypeScript
import {logResource} from "@/service";
|
|
import {getAccessToken, setAccessToken} from "@/auth/token";
|
|
import {toast} from "@/toast";
|
|
import axios, {InternalAxiosRequestConfig} from "axios";
|
|
import {
|
|
AcquisitionApi,
|
|
ActivityApi,
|
|
AuthApi,
|
|
CharacterApi,
|
|
CorporationApi,
|
|
LedgerApi,
|
|
LocationApi,
|
|
MarketApi,
|
|
MeResponse,
|
|
ReprocessingApi,
|
|
RuleBookApi,
|
|
RuleScriptApi,
|
|
TransactionApi
|
|
} from "@/generated/mammon";
|
|
|
|
export const mammonUrl = import.meta.env.VITE_MAMMON_URL;
|
|
export const mammonLoginUrl = mammonUrl + "oauth2/authorization/esi"
|
|
|
|
const mammonAxiosInstance = axios.create({
|
|
baseURL: mammonUrl,
|
|
headers: {
|
|
'Accept': 'application/json',
|
|
"Content-Type": "application/json",
|
|
},
|
|
})
|
|
logResource(mammonAxiosInstance)
|
|
|
|
const credentialedClient = axios.create({
|
|
baseURL: mammonUrl,
|
|
withCredentials: true,
|
|
})
|
|
logResource(credentialedClient)
|
|
|
|
mammonAxiosInstance.interceptors.request.use(config => {
|
|
const token = getAccessToken();
|
|
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
return config;
|
|
})
|
|
|
|
const authApi = new AuthApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
const credentialedAuthApi = new AuthApi(undefined, mammonUrl, credentialedClient);
|
|
|
|
let onAuthExpired: () => void = () => {};
|
|
|
|
export const setOnAuthExpired = (callback: () => void): void => {
|
|
onAuthExpired = callback;
|
|
}
|
|
|
|
let refreshing: Promise<string | null> | null = null;
|
|
|
|
export const refreshAccessToken = (): Promise<string | null> => {
|
|
if (!refreshing) {
|
|
refreshing = credentialedAuthApi.refresh()
|
|
.then(response => {
|
|
setAccessToken(response.data.accessToken);
|
|
return response.data.accessToken;
|
|
})
|
|
.catch(() => {
|
|
setAccessToken(null);
|
|
return null;
|
|
})
|
|
.finally(() => {
|
|
refreshing = null;
|
|
});
|
|
}
|
|
return refreshing;
|
|
}
|
|
|
|
mammonAxiosInstance.interceptors.response.use(response => response, async error => {
|
|
const original = error.config as (InternalAxiosRequestConfig & { _retried?: boolean }) | undefined;
|
|
|
|
if (error.response?.status === 401 && original && !original._retried) {
|
|
original._retried = true;
|
|
|
|
const token = await refreshAccessToken();
|
|
|
|
if (token) {
|
|
original.headers.Authorization = `Bearer ${token}`;
|
|
return mammonAxiosInstance(original);
|
|
}
|
|
onAuthExpired();
|
|
}
|
|
|
|
const status = error.response?.status;
|
|
|
|
if (status !== 401 && status !== 403) {
|
|
toast.error('Something went wrong');
|
|
}
|
|
return Promise.reject(error);
|
|
})
|
|
|
|
export const fetchMe = (): Promise<MeResponse> =>
|
|
authApi.me().then(response => response.data);
|
|
|
|
export const addCharacter = (): Promise<void> =>
|
|
authApi.addCharacter({withCredentials: true}).then(() => undefined);
|
|
|
|
export const postLogout = (): Promise<void> =>
|
|
credentialedAuthApi.logout().then(() => undefined);
|
|
|
|
export const ledgerApi = new LedgerApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const transactionApi = new TransactionApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const characterApi = new CharacterApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const corporationApi = new CorporationApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const ruleBookApi = new RuleBookApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const ruleScriptApi = new RuleScriptApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const activityApi = new ActivityApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const acquisitionApi = new AcquisitionApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const marketApi = new MarketApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const reprocessingApi = new ReprocessingApi(undefined, mammonUrl, mammonAxiosInstance);
|
|
export const locationApi = new LocationApi(undefined, mammonUrl, mammonAxiosInstance); |