feat: add handler error if API unauthorize
This commit is contained in:
parent
c73f642b93
commit
6b9b3f94b6
|
@ -75,6 +75,7 @@
|
|||
"semver": "7.3.5",
|
||||
"style-loader": "1.3.0",
|
||||
"superagent": "^6.1.0",
|
||||
"superagent-intercept": "^0.1.2",
|
||||
"terser-webpack-plugin": "4.2.3",
|
||||
"ts-pnp": "1.2.0",
|
||||
"url-loader": "4.1.1",
|
||||
|
|
|
@ -1,31 +1,49 @@
|
|||
import superagent from "superagent";
|
||||
import superagentIntercept from 'superagent-intercept';
|
||||
import {appConfig} from "../config/app";
|
||||
import {TokenUtil} from "./token";
|
||||
import {attachSuperagentLogger} from "./http_logger";
|
||||
|
||||
let authIntercept = superagentIntercept((err, res) => {
|
||||
if (res && res.status === 401) {
|
||||
TokenUtil.clearAccessToken();
|
||||
TokenUtil.persistToken();
|
||||
window.location.href = "/login";
|
||||
}
|
||||
});
|
||||
|
||||
export const http = {
|
||||
get: (url, opts = {}) => {
|
||||
let req = superagent.get(appConfig.apiUrl + url);
|
||||
let req = superagent.get(appConfig.apiUrl + url)
|
||||
.use(authIntercept)
|
||||
.use(attachSuperagentLogger);
|
||||
if (TokenUtil.accessToken) {
|
||||
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
||||
}
|
||||
return req;
|
||||
},
|
||||
post: (url, opts) => {
|
||||
let req = superagent.post(appConfig.apiUrl + url);
|
||||
let req = superagent.post(appConfig.apiUrl + url)
|
||||
.use(authIntercept)
|
||||
.use(attachSuperagentLogger);
|
||||
if (TokenUtil.accessToken) {
|
||||
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
||||
}
|
||||
return req;
|
||||
},
|
||||
put: (url, opts) => {
|
||||
let req = superagent.put(appConfig.apiUrl + url);
|
||||
let req = superagent.put(appConfig.apiUrl + url)
|
||||
.use(authIntercept)
|
||||
.use(attachSuperagentLogger);
|
||||
if (TokenUtil.accessToken) {
|
||||
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
||||
}
|
||||
return req;
|
||||
},
|
||||
del: (url, opts) => {
|
||||
let req = superagent.del(appConfig.apiUrl + url);
|
||||
let req = superagent.del(appConfig.apiUrl + url)
|
||||
.use(authIntercept)
|
||||
.use(attachSuperagentLogger);
|
||||
if (TokenUtil.accessToken) {
|
||||
req = req.set('Authorization', 'Bearer ' + TokenUtil.accessToken);
|
||||
}
|
||||
|
@ -34,13 +52,17 @@ export const http = {
|
|||
upload: (file) => {
|
||||
const request = superagent
|
||||
.post(appConfig.apiUrl + '/files')
|
||||
.attach('file', file);
|
||||
.attach('file', file)
|
||||
.use(authIntercept)
|
||||
.use(attachSuperagentLogger);
|
||||
|
||||
return request;
|
||||
},
|
||||
uploadAntd: (args) => {
|
||||
const file = args.file;
|
||||
const request = http.upload(file);
|
||||
const request = http.upload(file)
|
||||
.use(authIntercept)
|
||||
.use(attachSuperagentLogger);
|
||||
request
|
||||
.on('progress', event => {
|
||||
args.onProgress(event);
|
||||
|
|
17
src/utils/http_logger.js
Normal file
17
src/utils/http_logger.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
export function attachSuperagentLogger(req) {
|
||||
const callback = req.callback;
|
||||
console.log('%s %s %s',
|
||||
req.method.padEnd('delete'.length, " "),
|
||||
req.url,
|
||||
'(pending)'
|
||||
);
|
||||
|
||||
req.callback = function (err, res) {
|
||||
console.log('%s %s %s',
|
||||
req.method.padEnd('delete'.length, " "),
|
||||
req.url,
|
||||
res ? res.status : '-'
|
||||
);
|
||||
callback.call(req, err, res);
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue
Block a user