57 lines
1.3 KiB
TypeScript
57 lines
1.3 KiB
TypeScript
import { Query } from '@kevisual/query/query';
|
|
import { getConfig } from './get-config.ts';
|
|
import { QueryLoginNode, storage } from '@kevisual/query-login/node';
|
|
const config = getConfig();
|
|
export const baseURL = config?.baseURL || 'https://kevisual.cn';
|
|
export { storage };
|
|
export const getBaseURL = () => {
|
|
if (typeof config?.dev === 'undefined') {
|
|
return baseURL;
|
|
}
|
|
if (typeof config?.dev === 'string') {
|
|
if (config?.dev === 'true') {
|
|
return 'http://localhost:4002';
|
|
}
|
|
return baseURL;
|
|
}
|
|
if (config?.dev === true) {
|
|
return 'http://localhost:4002';
|
|
}
|
|
return baseURL;
|
|
};
|
|
export const query = new Query({
|
|
url: `${getBaseURL()}/api/router`,
|
|
});
|
|
|
|
query.beforeRequest = async (config) => {
|
|
if (config.headers) {
|
|
let token = process.env.KEVISUAL_TOKEN;
|
|
if (!token) {
|
|
token = await storage.getItem('token');
|
|
}
|
|
|
|
if (token) {
|
|
config.headers['Authorization'] = 'Bearer ' + token;
|
|
}
|
|
}
|
|
return config;
|
|
};
|
|
query.afterResponse = async (response, ctx) => {
|
|
return await queryLogin.run401Action(response, ctx);
|
|
};
|
|
export const queryLogin = new QueryLoginNode({
|
|
query: query as any,
|
|
onLoad: async () => {
|
|
// console.log('onLoad');
|
|
},
|
|
});
|
|
|
|
/**
|
|
*
|
|
* @param url
|
|
* @returns
|
|
*/
|
|
export const getUrl = (url: string) => {
|
|
return new URL('/api/router', url).href;
|
|
};
|