1
1
import { GlobalEventHandler } from './event' ;
2
+ import { merge } from 'lodash' ;
2
3
3
4
4
5
@@ -54,7 +55,7 @@ export class Ajax {
54
55
* @param requestBody body mime type, default auto derive
55
56
* @returns {Promise<any> }
56
57
*/
57
- static async send ( url : string , data : any = { } , method = 'GET' , expectedDataType = 'json' , requestBody = 'formdata' ) : Promise < any > {
58
+ static async send < T = any > ( url : string , data : any = { } , method = 'GET' , expectedDataType = 'json' , requestBody = 'formdata' , options : Partial < RequestInit > = { } ) : Promise < T > {
58
59
// for compatibility
59
60
method = method . toUpperCase ( ) ;
60
61
@@ -67,50 +68,50 @@ export class Ajax {
67
68
}
68
69
}
69
70
70
- const options : RequestInit = {
71
+ const mergedOptions : RequestInit = merge ( {
71
72
credentials : 'same-origin' ,
72
73
method,
73
74
headers : {
74
75
'Accept' : 'application/json'
75
76
} ,
76
- } ;
77
+ } , options ) ;
77
78
78
79
if ( data ) {
79
80
let mimetype : string = '' ;
80
81
switch ( requestBody . trim ( ) . toLowerCase ( ) ) {
81
82
case 'json' :
82
83
case 'application/json' :
83
84
mimetype = 'application/json' ;
84
- options . body = typeof data === 'string' ? data : JSON . stringify ( data ) ;
85
+ mergedOptions . body = typeof data === 'string' ? data : JSON . stringify ( data ) ;
85
86
break ;
86
87
case 'text' :
87
88
case 'text/plain' :
88
89
mimetype = 'text/plain' ;
89
- options . body = String ( data ) ;
90
+ mergedOptions . body = String ( data ) ;
90
91
break ;
91
92
case 'blob' :
92
93
case 'arraybuffer' :
93
94
mimetype = 'application/octet-stream' ;
94
- options . body = data ;
95
+ mergedOptions . body = data ;
95
96
break ;
96
97
default :
97
98
if ( data instanceof FormData ) {
98
- options . body = data ;
99
+ mergedOptions . body = data ;
99
100
} else {
100
101
mimetype = 'application/x-www-form-urlencoded' ;
101
- options . body = Ajax . encodeParams ( data ) ;
102
+ mergedOptions . body = Ajax . encodeParams ( data ) ;
102
103
}
103
104
}
104
105
if ( mimetype ) {
105
- ( < any > options . headers ) [ 'Content-Type' ] = mimetype ;
106
+ mergedOptions . headers [ 'Content-Type' ] = mimetype ;
106
107
}
107
108
}
108
109
109
110
// there are no typings for fetch so far
110
- GlobalEventHandler . getInstance ( ) . fire ( Ajax . GLOBAL_EVENT_AJAX_PRE_SEND , url , options ) ;
111
- const r = Ajax . checkStatus ( await self . fetch ( url , options ) ) ;
111
+ GlobalEventHandler . getInstance ( ) . fire ( Ajax . GLOBAL_EVENT_AJAX_PRE_SEND , url , mergedOptions ) ;
112
+ const r = Ajax . checkStatus ( await self . fetch ( url , mergedOptions ) ) ;
112
113
const output = Ajax . parseType ( expectedDataType , r ) ;
113
- GlobalEventHandler . getInstance ( ) . fire ( Ajax . GLOBAL_EVENT_AJAX_POST_SEND , url , options , r , output ) ;
114
+ GlobalEventHandler . getInstance ( ) . fire ( Ajax . GLOBAL_EVENT_AJAX_POST_SEND , url , mergedOptions , r , output ) ;
114
115
return output ;
115
116
}
116
117
/**
0 commit comments