import { validateUrl , getType , addQueryStringToUrl , convertObjectValueToString , urlEncodeFormData } from "../utils/index" ; import { AnyObj } from "../api/index.d" ; const params = { url : "" , method : "GET" , dataType : "json" , responseType : "text" , header : { "content-type" : "application/json" } , success : ( { } ) => { } , fail : ( ) => { } , complete : ( { } ) => { } , } ; export function request ( args = { } ) { const options = Object . assign ( { } , params , args ) ; let url = options . url ; const method = options . method . toUpperCase ( ) ; if ( validateUrl ( url ) === false ) { console . error ( "request" , options , `invalid url "${ url }"` ) ; return undefined ; } let headers = convertObjectValueToString ( options . header ) ; headers = Object . keys ( headers ) . reduce ( ( pre , cur ) => { if ( cur . toLowerCase ( ) === "content-type" ) { pre [ cur . toLowerCase ( ) ] = headers [ cur ] ; } else { pre [ cur ] = headers [ cur ] ; } return pre ; } , { } ) ; let responseType = "text" ; if ( options . responseType ) { responseType = options . responseType . toLowerCase ( ) ; } let body = options . data ; if ( method === "GET" || method === "HEAD" ) { url = addQueryStringToUrl ( url , body ) ; } else if ( typeof body !== "string" && getType ( body ) !== "ArrayBuffer" ) { if ( headers [ "content-type" ] . indexOf ( "application/x-www-form-urlencoded" ) > - 1 ) { body = urlEncodeFormData ( body , true ) ; } else if ( headers [ "content-type" ] . indexOf ( "application/json" ) > - 1 || typeof body === "object" ) { body = JSON . stringify ( body ) ; } else { body = body . toString ( ) ; } } const result = { } ; return fetch ( url , { method , headers , body } ) . then ( ( response ) => { result . statusCode = response . status ; result . header = { } ; response . headers . forEach ( ( val , key ) => { result . header [ key ] = val ; } ) ; if ( response . ok === false ) { throw response ; } if ( responseType === "arraybuffer" ) { return response . arrayBuffer ( ) ; } if ( options . dataType === "json" ) { return response . json ( ) ; } if ( responseType === "text" ) { return response . text ( ) ; } return Promise . resolve ( ) ; } ) . then ( ( data ) => { result . data = data ; options . success ( result ) ; options . complete ( result ) ; return result ; } ) ; }