@@ -25,20 +25,36 @@ export type AjaxHeaders = {
2525 authorization ?: string ;
2626 [ 'api-version' ] ?: string ;
2727} ;
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 ;
2851export type Ajax = {
2952 url : string ;
3053 headers ?: AjaxHeaders ;
3154 progress ?: ProgressCbs ;
3255 timeout ?: number ; // todo: implement
3356 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 ;
4258type RawAjaxErr = {
4359 readyState : number ;
4460 responseText ?: string ;
@@ -157,7 +173,7 @@ export class Api {
157173 headersInit . push ( [ 'Content-Type' , req . contentType ] ) ;
158174 }
159175 } else {
160- body = req . data ; // todo: form data content-type?
176+ body = req . data as FormData ; // todo: form data content-type?
161177 }
162178 }
163179 }
@@ -291,13 +307,26 @@ export class Api {
291307 let data : BodyInit | undefined = formattedData ;
292308 const headersInit : Dict < string > = req . headers ?? { } ;
293309
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 ;
301330 }
302331 }
303332 const apiReq : JQuery . AjaxSettings < ApiCallContext > = {
@@ -385,12 +414,7 @@ export class Api {
385414 ) : Promise < FetchResult < T , RT > > {
386415 progress = progress || ( { } as ProgressCbs ) ;
387416 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' } ;
394418 if ( values ) {
395419 if ( values . fmt === 'JSON' ) {
396420 dataPart = { method : values . method ?? 'POST' , data : values . data , dataType : 'JSON' } ;
0 commit comments