front-hub/src/stores/makeRequest.ts
2023-04-13 19:48:17 +03:00

97 lines
2.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import axios, { AxiosResponse } from "axios";
import { create } from "zustand";
import { persist } from "zustand/middleware";
type Token = string;
interface AuthStore {
token: Token;
makeRequest: <TRequest = unknown, TResponse = unknown>(props: FirstRequest<TRequest>) => Promise<TResponse>;
clearToken: () => void;
}
interface FirstRequest<T> {
method?: string;
url: string;
body?: T;
useToken?: boolean;
contentType?: boolean;
signal?: AbortSignal;
withCredentials?: boolean;
}
export const authStore = create<AuthStore>()(
persist(
(set, get) => ({
token: "",
makeRequest: <TRequest, TResponse>(props: FirstRequest<TRequest>): Promise<TResponse> => {
const newProps = { ...props, HC: (newToken: Token) => set({ token: newToken }), token: get().token };
return makeRequest<TRequest, TResponse>(newProps);
},
clearToken: () => set({ token: "" }),
}),
{
name: "token",
}
)
);
interface MakeRequest<T> extends FirstRequest<T> {
HC: (newToken: Token) => void;
token: Token;
}
async function makeRequest<TRequest, TResponse>({
method = "post",
url,
body,
useToken = true,
contentType = false,
HC,
token,
signal,
withCredentials,
}: MakeRequest<TRequest>) {
//В случае 401 рефреш должен попробовать вызваться 1 раз
let headers: any = {};
if (useToken) headers["Authorization"] = token;
if (contentType) headers["Content-Type"] = "*/*";
try {
const response = await axios<TRequest, AxiosResponse<TResponse & { accessToken?: string; }>>(
{ url, method, headers, data: body, signal, withCredentials }
);
if (response.data?.accessToken) {
HC(response.data.accessToken);
}
return response.data;
} catch (error: any) {
if (error?.response?.status === 401 && !withCredentials) {
const refreshResponse = await refresh();
if (refreshResponse.data?.accessToken) HC(refreshResponse.data.accessToken);
headers["Authorization"] = refreshResponse.data.accessToken;
const response = await axios<TRequest, AxiosResponse<TResponse>>(
{ url, method, headers, data: body, signal }
);
return response.data;
}
throw error;
}
}
function refresh() {
return axios<never, AxiosResponse<{ accessToken: string; }>>("https://admin.pena.digital/auth/refresh", {
headers: {
Authorization: localStorage.getItem("AT"),
"Content-Type": "application/json",
},
});
}