@@ -2,42 +2,44 @@ import 'whatwg-fetch';
2
2
import { stringify } from 'qs' ;
3
3
// 本工具类只能用于接收和处理json数据,其他数据请使用fetch原生;
4
4
5
- const noop = ( ) => { } ;
5
+ const noop = ( ) => { } ;
6
6
7
7
let globalCallbacks = {
8
8
onStart : noop ,
9
9
onComplete : noop ,
10
10
onSuccess : noop ,
11
11
onError : noop ,
12
- onSuccessFilter : result => result
12
+ onSuccessFilter : result => result ,
13
13
} ;
14
14
let globalConifg = { } ;
15
- export const config = ( {
16
- method = 'GET' ,
17
- headers,
18
- credentials = 'omit' ,
19
- body,
20
- onStart = noop ,
21
- onComplete = noop ,
22
- onError = noop ,
23
- onSuccessFilter = result => result ,
24
- onSuccess = noop
25
- } = { } ) => {
15
+ export const config = (
16
+ {
17
+ method = 'GET' ,
18
+ headers,
19
+ credentials = 'omit' ,
20
+ body,
21
+ onStart = noop ,
22
+ onComplete = noop ,
23
+ onError = noop ,
24
+ onSuccessFilter = result => result ,
25
+ onSuccess = noop ,
26
+ } = { }
27
+ ) => {
26
28
globalCallbacks = {
27
29
onStart,
28
30
onComplete,
29
31
onSuccess,
30
32
onError,
31
- onSuccessFilter
33
+ onSuccessFilter,
32
34
} ;
33
35
globalConifg = {
34
36
method,
35
37
headers,
36
38
body,
37
- credentials
39
+ credentials,
38
40
} ;
39
41
} ;
40
- const catchCallbackFn = fn => ( params ) => {
42
+ const catchCallbackFn = fn => params => {
41
43
try {
42
44
fn ( params ) ;
43
45
} catch ( error ) {
@@ -46,57 +48,61 @@ const catchCallbackFn = fn => (params) => {
46
48
throw error ;
47
49
}
48
50
} ;
49
- const request = (
50
- {
51
- headers = { } ,
52
- credentials = 'omit'
53
- } = { } ) => ( urlWithMethod , params = { } ) => {
54
- let [ method , url ] = urlWithMethod . split ( ' ' ) ; // eslint-disable-line
55
- const { onStart, onComplete, onSuccess, onError, onSuccessFilter } = globalCallbacks ;
56
- const assignHeaders = globalConifg . headers ? {
51
+ const request = ( { headers = { } , credentials = 'omit' } = { } ) => ( urlWithMethod , params = { } ) => {
52
+ let [ method , url ] = urlWithMethod . split ( ' ' ) ; // eslint-disable-line
53
+ const { onStart, onComplete, onSuccess, onError, onSuccessFilter } = globalCallbacks ;
54
+ const assignHeaders = globalConifg . headers
55
+ ? {
57
56
...globalConifg . headers ,
58
- ...headers
59
- } : headers ;
60
- const assignBody = globalConifg . body ? {
57
+ ...headers ,
58
+ }
59
+ : headers ;
60
+ let assignBody = globalConifg . body
61
+ ? {
61
62
...globalConifg . body ,
62
- ...params
63
- } : params ;
64
- const options = {
65
- ... globalConifg ,
66
- headers : assignHeaders ,
67
- method ,
68
- credentials
69
- } ;
70
- assignBody = JSON . stringify ( JSON . parse ( JSON . stringify ( assignBody ) ) ) ;
71
- if ( Object . keys ( assignBody ) . length ) {
72
- if ( method . toUpperCase ( ) !== 'GET' ) {
73
- options . body = assignBody ;
74
- } else {
75
- url += `? ${ stringify ( assignBody ) } ` ;
76
- }
63
+ ...params ,
64
+ }
65
+ : params ;
66
+ const options = {
67
+ ... globalConifg ,
68
+ headers : assignHeaders ,
69
+ method ,
70
+ credentials ,
71
+ } ;
72
+ assignBody = JSON . stringify ( JSON . parse ( JSON . stringify ( assignBody ) ) ) ;
73
+ if ( Object . keys ( assignBody ) . length ) {
74
+ if ( method . toUpperCase ( ) !== 'GET' ) {
75
+ options . body = assignBody ;
76
+ } else {
77
+ url += `? ${ stringify ( assignBody ) } ` ;
77
78
}
78
- const promise = new Promise ( ( resolve , reject ) => {
79
- onStart ( ) ;
80
- fetch ( url , options ) . then ( ( response ) => {
79
+ }
80
+ const promise = new Promise ( ( resolve , reject ) => {
81
+ onStart ( ) ;
82
+ fetch ( url , options ) . then (
83
+ response => {
81
84
if ( response . ok ) {
82
- response . json ( ) . then ( ( result ) => {
83
- const filterResult = onSuccessFilter ( result ) ;
84
- if ( Object . prototype . toString . call ( filterResult ) === '[object Error]' ) {
85
- onComplete ( filterResult ) ;
86
- onError ( filterResult ) ;
87
- reject ( filterResult ) ;
88
- } else {
89
- onSuccess ( filterResult ) ;
90
- onComplete ( null , filterResult ) ;
91
- resolve ( filterResult ) ;
85
+ response . json ( ) . then (
86
+ result => {
87
+ const filterResult = onSuccessFilter ( result ) ;
88
+ if ( Object . prototype . toString . call ( filterResult ) === '[object Error]' ) {
89
+ onComplete ( filterResult ) ;
90
+ onError ( filterResult ) ;
91
+ reject ( filterResult ) ;
92
+ } else {
93
+ onSuccess ( filterResult ) ;
94
+ onComplete ( null , filterResult ) ;
95
+ resolve ( filterResult ) ;
96
+ }
97
+ } ,
98
+ error => {
99
+ const e = new Error ( '服务器返回数据错误' ) ;
100
+ onComplete ( e ) ;
101
+ onError ( e ) ;
102
+ reject ( e ) ;
103
+ throw new Error ( error ) ;
92
104
}
93
- } , ( error ) => {
94
- const e = new Error ( '服务器返回数据错误' ) ;
95
- onComplete ( e ) ;
96
- onError ( e ) ;
97
- reject ( e ) ;
98
- throw new Error ( error ) ;
99
- } ) ;
105
+ ) ;
100
106
} else {
101
107
console . log ( response ) ;
102
108
const error = new Error ( response . statusText ) ;
@@ -105,33 +111,38 @@ const request = (
105
111
onComplete ( error ) ;
106
112
reject ( error ) ;
107
113
}
108
- } , ( error ) => {
114
+ } ,
115
+ error => {
109
116
onError ( error ) ;
110
117
onComplete ( error ) ;
111
118
reject ( error ) ;
112
- } ) ;
119
+ }
120
+ ) ;
121
+ } ) ;
122
+ promise . success = fn => {
123
+ promise . then ( result => {
124
+ if ( typeof fn === 'function' ) { catchCallbackFn ( fn ) ( result ) ; }
125
+ } ) ;
126
+ return promise ;
127
+ } ;
128
+ promise . error = fn => {
129
+ promise . then ( null , error => {
130
+ if ( typeof fn === 'function' ) { catchCallbackFn ( fn ) ( error ) ; }
113
131
} ) ;
114
- promise . success = ( fn ) => {
115
- promise . then ( ( result ) => {
116
- if ( typeof fn === 'function' ) catchCallbackFn ( fn ) ( result ) ;
117
- } ) ;
118
- return promise ;
119
- } ;
120
- promise . error = ( fn ) => {
121
- promise . then ( null , ( error ) => {
122
- if ( typeof fn === 'function' ) catchCallbackFn ( fn ) ( error ) ;
123
- } ) ;
124
- return promise ;
125
- } ;
126
- promise . complete = ( fn ) => {
127
- fn = ( typeof fn === 'function' ) ? catchCallbackFn ( fn ) : noop ;
128
- promise . then ( ( result ) => {
132
+ return promise ;
133
+ } ;
134
+ promise . complete = fn => {
135
+ fn = typeof fn === 'function' ? catchCallbackFn ( fn ) : noop ;
136
+ promise . then (
137
+ result => {
129
138
fn ( null , result ) ;
130
- } , ( error ) => {
139
+ } ,
140
+ error => {
131
141
fn ( error ) ;
132
- } ) ;
133
- return promise ;
134
- } ;
142
+ }
143
+ ) ;
135
144
return promise ;
136
145
} ;
146
+ return promise ;
147
+ } ;
137
148
export default request ;
0 commit comments