DS
Давай с другой стороны. Если ты сейчас токен положишь в обычную переменную, что у тебя отвалится?
Size: a a a
DS
m
export const TOKEN_NAME = 'jwt';
const API_ROOT = 'https://conduit.productionready.io/api';
type Request = <T>(method: string, url: string, data?: unknown) => Promise<T>;
const request: Request = async (method, url, data) => {
const token = JSON.parse(String(localStorage.getItem(TOKEN_NAME)));
const headers = {
'Content-Type': 'application/json',
};
const options: RequestInit = {
headers: token ? { ...headers, Authorization: `Token ${token}` } : headers,
method: method.toUpperCase(),
body: data ? JSON.stringify(data) : null,
};
const response = await fetch(`${API_ROOT}${url}`, options);
if (response.ok) {
return response.json();
}
throw await response.json();
};
export const get = <T>(url: string): Promise<T> => request<T>('get', url);
export const post = <T = void>(url: string, body?: unknown): Promise<T> =>
request<T>('post', url, body);
export const put = <T = void>(url: string, body: unknown): Promise<T> =>
request<T>('put', url, body);
export const del = <T = void>(url: string): Promise<T> =>
request<T>('delete', url);
m
m
AO
AO
AO
AO
AO
🚀🚀
const instance = axios.create({
baseURL: "https://api.example.com",
});
$token.watch((token) => {
instance.defaults.headers.common['Authorization'] = token;
});🚀🚀
🚀🚀
m
m
🚀🚀
🚀🚀
m
let instance = null token.watch(token => instance = someLibrary({token: token}))🚀🚀
🚀🚀