gtsoft-snail-job-admin/src/service/request/index.ts

95 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-03-08 17:59:45 +08:00
import { BACKEND_ERROR_CODE, createFlatRequest, createRequest } from '@sa/axios';
import { localStg } from '@/utils/storage';
2024-03-21 10:57:53 +08:00
import { getServiceBaseURL } from '@/utils/service';
2024-03-08 17:59:45 +08:00
2024-03-21 10:57:53 +08:00
const isHttpProxy = import.meta.env.DEV && import.meta.env.VITE_HTTP_PROXY === 'Y';
const { baseURL, otherBaseURL } = getServiceBaseURL(import.meta.env, isHttpProxy);
2024-03-08 17:59:45 +08:00
export const request = createFlatRequest<App.Service.Response>(
{
2024-03-21 10:57:53 +08:00
baseURL,
timeout: 6000
2024-03-08 17:59:45 +08:00
},
{
async onRequest(config) {
const { headers } = config;
// set token
const token = localStg.get('token');
const namespaceId = localStg.get('namespaceId');
// const Authorization = token ? `Bearer ${token}` : null;
headers['EASY-RETRY-AUTH'] = token;
headers['EASY-RETRY-NAMESPACE-ID'] = namespaceId;
Object.assign(headers, { 'EASY-RETRY-AUTH': token, 'EASY-RETRY-NAMESPACE-ID': namespaceId });
2024-03-08 17:59:45 +08:00
return config;
},
isBackendSuccess(response) {
// when the backend response code is "0000", it means the request is success
// you can change this logic by yourself
return response.data.status === 1;
2024-03-08 17:59:45 +08:00
},
async onBackendFail(_response) {
2024-03-21 10:57:53 +08:00
// when the backend response code is not "0000", it means the request is fail
// for example: the token is expired, refresh token and retry request
2024-03-08 17:59:45 +08:00
},
transformBackendResponse(response) {
return response.data.data;
},
onError(error) {
// when the request is fail, you can show error message
let message = error.message;
// show backend error message
if (error.code === BACKEND_ERROR_CODE) {
message = error.response?.data?.message || message;
2024-03-08 17:59:45 +08:00
}
window.$message?.error(message);
}
}
);
export const demoRequest = createRequest<App.Service.DemoResponse>(
{
2024-03-21 10:57:53 +08:00
baseURL: otherBaseURL.demo
2024-03-08 17:59:45 +08:00
},
{
async onRequest(config) {
const { headers } = config;
// set token
const token = localStg.get('token');
const Authorization = token ? `Bearer ${token}` : null;
Object.assign(headers, { Authorization });
return config;
},
isBackendSuccess(response) {
2024-03-21 10:57:53 +08:00
// when the backend response code is "200", it means the request is success
2024-03-08 17:59:45 +08:00
// you can change this logic by yourself
return response.data.status === '200';
},
async onBackendFail(_response) {
2024-03-21 10:57:53 +08:00
// when the backend response code is not "200", it means the request is fail
// for example: the token is expired, refresh token and retry request
2024-03-08 17:59:45 +08:00
},
transformBackendResponse(response) {
return response.data.result;
},
onError(error) {
// when the request is fail, you can show error message
let message = error.message;
// show backend error message
if (error.code === BACKEND_ERROR_CODE) {
2024-03-21 10:57:53 +08:00
message = error.response?.data?.message || message;
2024-03-08 17:59:45 +08:00
}
window.$message?.error(message);
}
}
);