1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Request, { config, requestConfig, response } from './request'
const test = new Request()
test.setConfig((config: config) => { /* 设置全局配置 */
config.baseUrl = 'http://www.aaa.cn'
config.header = {
a: 1,
b: 2
}
return config
})
test.interceptor.request((config: requestConfig, cancel: Function) => { /* 请求之前拦截器 */
config.header = {
...config.header,
a: 1
}
/*
if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
}
*/
return config
})
test.interceptor.response((response: response) => { /* 请求之后拦截器 */
return response
}, (response: response) => { /* 对响应错误做点什么 */
return response
})
const http = new Request()
http.setConfig((config: config) => { /* 设置全局配置 */
config.baseUrl = 'http://www.bbb.cn' /* 根域名不同 */
config.header = {
a: 1,
b: 2
}
return config
})
http.interceptor.request((config: requestConfig, cancel: Function) => { /* 请求之前拦截器 */
config.header = {
...config.header,
b: 1
}
/*
if (!token) { // 如果token不存在,调用cancel 会取消本次请求,但是该函数的catch() 仍会执行
cancel('token 不存在') // 接收一个参数,会传给catch((err) => {}) err.errMsg === 'token 不存在'
}
*/
return config
})
http.interceptor.response((response: response) => { /* 请求之后拦截器 */
return response
}, (response: response) => { /* 对响应错误做点什么 */
return response
})
export {
http,
test
}