import { isFormData , isBlob , isArrayBuffer } from "../functions/util" ; import { createQueryPath } from "../functions/create-query-path" ; export class HttpRequest { constructor ( public body , public headers , public host , isHttps , public method , public path , public params , public queryParams , public responseType , public scheme , fullPath ? ) { if ( ! responseType ) this . responseType = 'json' ; this . isHttps = isHttps ; if ( fullPath ) this . fullPath = fullPath ; } private _isHttps : boolean ; private _fullPath : string ; get isHttps ( ) { return this . _isHttps ; } set isHttps ( value ) { this . _isHttps = value ; if ( value ) this . fullPath = this . fullPath . replace ( "http:" , "https:" ) ; } get fullPath ( ) { return this . _fullPath ? this . _fullPath : `${ this . host }/${ this . path }${ createQueryPath ( { params : this . params , queryParams : this . queryParams } ) }` ; } set fullPath ( value ) { this . _fullPath = value ; } getContentType ( ) { if ( this . body === null ) { return null ; } if ( isFormData ( this . body ) ) { return null ; } if ( isBlob ( this . body ) ) { return this . body . type || null ; } if ( isArrayBuffer ( this . body ) ) { return null ; } if ( typeof this . body === 'string' ) { return 'text/plain' ; } if ( typeof this . body === 'object' || typeof this . body === 'number' || Array . isArray ( this . body ) ) { return 'application/json' ; } return null ; } serializeBody ( ) { if ( this . body === null ) { return null ; } if ( isArrayBuffer ( this . body ) || isBlob ( this . body ) || isFormData ( this . body ) || typeof this . body === 'string' ) { return this . body ; } if ( this . method == 'PATCH' ) this . body = this . convertPatchBody ( this . body ) ; if ( typeof this . body === 'object' || typeof this . body === 'boolean' || Array . isArray ( this . body ) ) { return JSON . stringify ( this . body ) ; } if ( this . body ) return String ( this . body ) ; return '' ; } private convertPatchBody ( object ) { let patchBody = [ ] ; for ( var col in object ) { patchBody . push ( { 'op' : 'replace' , 'path' : '/' + col , 'value' : object [ col ] } ) ; } return patchBody ; } }