You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tailchat/server/admin/src/client/request.ts

30 lines
575 B
TypeScript

import axios from 'axios';
import { authStorageKey } from './auth';
import _set from 'lodash/set';
/**
* 创建请求实例
*/
function createRequest() {
const ins = axios.create({
baseURL: '/admin/api',
});
ins.interceptors.request.use(async (val) => {
try {
const { token } = JSON.parse(
window.localStorage.getItem(authStorageKey) ?? '{}'
);
_set(val, ['headers', 'Authorization'], `Bearer ${token}`);
return val;
} catch (err) {
throw err;
}
});
return ins;
}
export const request = createRequest();