@@ -3,29 +3,42 @@ const defaultOptions = {
3
3
redirect : 'manual' ,
4
4
} ;
5
5
6
+ interface RequestOptions {
7
+ parseJson ?: boolean ;
8
+ }
9
+
6
10
function options ( custom : any = { } ) {
7
11
return Object . assign ( { } , defaultOptions , custom ) ;
8
12
}
9
13
10
- function checkStatus ( response : Response ) : Promise < any > {
14
+ function checkStatus ( response : Response , requestOptions : RequestOptions = { parseJson : true } ) : Promise < any > {
15
+ const { parseJson = true } = requestOptions ;
16
+
11
17
if ( response . status === 204 ) {
12
18
return Promise . resolve ( { } ) ;
13
- } else if ( response . status >= 200 && response . status < 300 ) {
19
+ }
20
+
21
+ if ( response . status >= 200 && response . status < 300 ) {
14
22
const contentType = response . headers . get ( 'Content-Type' ) ;
15
- if ( contentType && contentType . includes ( 'application/json' ) ) {
23
+ if ( parseJson && contentType && contentType . includes ( 'application/json' ) ) {
16
24
return response . json ( ) ;
17
25
}
18
26
return Promise . resolve ( response ) ;
19
- } else if ( response . status === 400 ) {
27
+ }
28
+
29
+ if ( response . status === 400 ) {
20
30
return response . json ( ) . then ( ( data : any ) => Promise . reject ( { errorData : data } ) ) ;
21
- } else if ( response . type === 'opaqueredirect' ) {
31
+ }
32
+
33
+ if ( response . type === 'opaqueredirect' ) {
22
34
window . location . reload ( ) ;
23
35
}
24
36
25
37
console . error ( response ) ;
26
38
return Promise . reject ( response ) ;
27
39
}
28
40
41
+
29
42
function getHeaders ( etag : any ) {
30
43
const headers : any = { 'Content-Type' : 'application/json' } ;
31
44
@@ -70,12 +83,17 @@ class Server {
70
83
* @param {Object } data
71
84
* @return {Promise }
72
85
*/
73
- post ( url : any , data : any , etag ?: string ) {
86
+ post (
87
+ url : any ,
88
+ data : any ,
89
+ etag ?: string ,
90
+ requestOptions : RequestOptions = { parseJson : true }
91
+ ) {
74
92
return fetch ( url , options ( {
75
93
method : 'POST' ,
76
94
headers : getHeaders ( etag ) ,
77
95
body : data ? JSON . stringify ( data ) : null ,
78
- } ) ) . then ( checkStatus ) ;
96
+ } ) ) . then ( ( response ) => checkStatus ( response , requestOptions ) ) ;
79
97
}
80
98
81
99
/**
0 commit comments