create-packer 1.25.0 → 1.25.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { forEach, size } from 'lodash-es'
|
|
2
2
|
import { stringify } from 'qs'
|
|
3
3
|
|
|
4
4
|
type fetchConfigType = Required<Parameters<typeof fetch>>[1]
|
|
@@ -9,8 +9,8 @@ interface configType {
|
|
|
9
9
|
'Content-Type'?: string
|
|
10
10
|
[key: string]: string | undefined
|
|
11
11
|
}
|
|
12
|
-
onError?: (e: Response) => void
|
|
13
12
|
}
|
|
13
|
+
type onErrorType = (e: Response) => void
|
|
14
14
|
|
|
15
15
|
async function resFormatter(response: Response) {
|
|
16
16
|
const contentType = response.headers.get('Content-Type')
|
|
@@ -28,25 +28,36 @@ async function resFormatter(response: Response) {
|
|
|
28
28
|
|
|
29
29
|
class Request {
|
|
30
30
|
config: configType
|
|
31
|
+
interceptors: {
|
|
32
|
+
error: onErrorType[]
|
|
33
|
+
}
|
|
31
34
|
constructor(config: configType) {
|
|
32
35
|
this.config = {
|
|
33
|
-
onError: noop,
|
|
34
36
|
...config,
|
|
35
37
|
headers: {
|
|
36
38
|
'Content-Type': 'application/json',
|
|
37
39
|
...config.headers
|
|
38
40
|
}
|
|
39
41
|
}
|
|
42
|
+
this.interceptors = {
|
|
43
|
+
error: []
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
onError(callback: onErrorType) {
|
|
47
|
+
this.interceptors.error.push(callback)
|
|
40
48
|
}
|
|
41
49
|
async request(url: string, config?: fetchConfigType) {
|
|
42
50
|
try {
|
|
43
51
|
const response = await fetch(this.config.baseURL + url, {
|
|
44
52
|
headers: this.config.headers as never,
|
|
53
|
+
redirect: 'manual',
|
|
45
54
|
...config
|
|
46
55
|
})
|
|
47
56
|
if (response.status !== 200) {
|
|
48
57
|
const errMsg = `服务器错误状态:${response.status}`
|
|
49
|
-
this.
|
|
58
|
+
forEach(this.interceptors.error, onError => {
|
|
59
|
+
onError(response)
|
|
60
|
+
})
|
|
50
61
|
throw new Error(errMsg)
|
|
51
62
|
}
|
|
52
63
|
return resFormatter(response)
|