@@ -25,20 +25,36 @@ export type AjaxHeaders = {
25
25
authorization ?: string ;
26
26
[ 'api-version' ] ?: string ;
27
27
} ;
28
+ interface GetParams {
29
+ method : 'GET' | 'DELETE' ;
30
+ data ?: UrlParams ;
31
+ }
32
+ export interface JsonParams {
33
+ method : 'POST' | 'PUT' ;
34
+ data ?: Dict < Serializable > ;
35
+ dataType ?: 'JSON' ;
36
+ }
37
+
38
+ interface TextParams {
39
+ method : 'POST' | 'PUT' ;
40
+ data : string ;
41
+ contentType ?: string ;
42
+ dataType : 'TEXT' ;
43
+ }
44
+ interface FormParams {
45
+ method : 'POST' | 'PUT' ;
46
+ data : FormData ;
47
+ dataType : 'FORM' ;
48
+ }
49
+
50
+ export type AjaxParams = GetParams | JsonParams | TextParams | FormParams ;
28
51
export type Ajax = {
29
52
url : string ;
30
53
headers ?: AjaxHeaders ;
31
54
progress ?: ProgressCbs ;
32
55
timeout ?: number ; // todo: implement
33
56
stack : string ;
34
- } & (
35
- | { method : 'GET' | 'DELETE' ; data ?: UrlParams }
36
- | { method : 'POST' }
37
- | { method : 'POST' | 'PUT' ; data : Dict < Serializable > ; dataType : 'JSON' }
38
- | { method : 'POST' | 'PUT' ; contentType ?: string ; data : string ; dataType : 'TEXT' }
39
- | { method : 'POST' | 'PUT' ; data : FormData ; dataType : 'FORM' }
40
- | { method : never ; data : never ; contentType : never }
41
- ) ;
57
+ } & AjaxParams ;
42
58
type RawAjaxErr = {
43
59
readyState : number ;
44
60
responseText ?: string ;
@@ -157,7 +173,7 @@ export class Api {
157
173
headersInit . push ( [ 'Content-Type' , req . contentType ] ) ;
158
174
}
159
175
} else {
160
- body = req . data ; // todo: form data content-type?
176
+ body = req . data as FormData ; // todo: form data content-type?
161
177
}
162
178
}
163
179
}
@@ -291,13 +307,26 @@ export class Api {
291
307
let data : BodyInit | undefined = formattedData ;
292
308
const headersInit : Dict < string > = req . headers ?? { } ;
293
309
294
- if ( req . method === 'PUT' || req . method === 'POST' ) {
295
- if ( 'data' in req && typeof req . data !== 'undefined' ) {
296
- data = req . dataType === 'JSON' ? JSON . stringify ( req . data ) : req . data ;
297
-
298
- if ( req . dataType === 'TEXT' && typeof req . contentType === 'string' ) {
299
- headersInit [ 'Content-Type' ] = req . contentType ;
300
- }
310
+ if ( ( req . method === 'PUT' || req . method === 'POST' ) && 'data' in req ) {
311
+ switch ( req . dataType ) {
312
+ case 'JSON' :
313
+ // req.data is Dict<Serializable>
314
+ data = JSON . stringify ( req . data ) ;
315
+ headersInit [ 'Content-Type' ] = 'application/json' ;
316
+ break ;
317
+ case 'TEXT' :
318
+ // req.data is string
319
+ data = req . data ;
320
+ if ( req . contentType ) {
321
+ headersInit [ 'Content-Type' ] = req . contentType ;
322
+ }
323
+ break ;
324
+ case 'FORM' :
325
+ // req.data is FormData
326
+ data = req . data ;
327
+ break ;
328
+ default :
329
+ break ;
301
330
}
302
331
}
303
332
const apiReq : JQuery . AjaxSettings < ApiCallContext > = {
@@ -385,12 +414,7 @@ export class Api {
385
414
) : Promise < FetchResult < T , RT > > {
386
415
progress = progress || ( { } as ProgressCbs ) ;
387
416
let formattedData : FormData | string | undefined ;
388
- let dataPart :
389
- | { method : 'GET' }
390
- | { method : 'POST' | 'PUT' ; data : Dict < Serializable > ; dataType : 'JSON' }
391
- | { method : 'POST' | 'PUT' ; data : string ; dataType : 'TEXT' }
392
- | { method : 'POST' | 'PUT' ; data : FormData ; dataType : 'FORM' } ;
393
- dataPart = { method : 'GET' } ;
417
+ let dataPart : AjaxParams = { method : 'GET' } ;
394
418
if ( values ) {
395
419
if ( values . fmt === 'JSON' ) {
396
420
dataPart = { method : values . method ?? 'POST' , data : values . data , dataType : 'JSON' } ;
0 commit comments