import { HttpHeaders , HttpParams } from '@angular/common/http' ; import { BOUNDARY_PREFIX_SUFFIX } from '../constants' ; import { Types } from './types' ; export const Http = { mergeHttpHeaders ( ... values ) { let headers = new HttpHeaders ( ) ; values . forEach ( ( value ) => { if ( value instanceof HttpHeaders ) { value . keys ( ) . forEach ( ( key ) => { headers = ( ( value as HttpHeaders ) . getAll ( key ) || [ ] ) . reduce ( ( acc , v ) => acc . append ( key , v ) , headers ) ; } ) ; } else if ( Types . isPlainObject ( value ) ) { Object . entries ( value ) . forEach ( ( [ key , value ] ) => { headers = ( Array . isArray ( value ) ? value : [ value ] ) . reduce ( ( acc , v ) => acc . append ( key , v ) , headers ) ; } ) ; } } ) ; return headers ; } , mergeHttpParams ( ... values ) { let params = new HttpParams ( ) ; values . forEach ( ( value ) => { if ( value instanceof HttpParams ) { value . keys ( ) . forEach ( ( key ) => { params = ( ( value as HttpParams ) . getAll ( key ) || [ ] ) . reduce ( ( acc , v ) => acc . append ( key , v ) , params ) ; } ) ; } else if ( Types . isPlainObject ( value ) ) { Object . entries ( value ) . forEach ( ( [ key , value ] ) => { params = ( Array . isArray ( value ) ? value : [ value ] ) . reduce ( ( acc , v ) => acc . append ( key , v ) , params ) ; } ) ; } } ) ; return params ; } , splitHttpParams ( params , keys ) { let other = new HttpParams ( ) ; params . keys ( ) . forEach ( ( key ) => { if ( keys . includes ( key ) ) { other = ( params . getAll ( key ) || [ ] ) . reduce ( ( acc , v ) => acc . append ( key , v ) , other ) ; params = params . delete ( key ) ; } } ) ; return [ params , other ] ; } , withoutHttpParams ( params , keys ) { return keys . reduce ( ( acc , key ) => acc . delete ( key ) , params ) ; } , resolveHeaderKey ( headers , options ) { if ( headers instanceof HttpHeaders ) { return headers . keys ( ) . find ( ( k ) => options . indexOf ( k ) !== - 1 ) ; } else if ( Types . isPlainObject ( headers ) ) { return Object . keys ( headers ) . find ( ( k ) => options . indexOf ( k ) !== - 1 ) ; } return undefined ; } , headerValue ( header ) { let res = header . split ( ';' ) [ 0 ] . trim ( ) ; res = res . split ( ':' ) [ 1 ] . trim ( ) ; return res ; } , parseResponseStatus ( statusLine ) { const status = statusLine . split ( ' ' ) [ 0 ] ; const code = parseInt ( statusLine . split ( ' ' ) [ 1 ] , 10 ) ; const message = statusLine . split ( ' ' ) [ 2 ] ; return { status , code , message } ; } , boundaryDelimiter ( contentType ) { const contentTypeParts = contentType . split ( ';' ) ; if ( contentTypeParts . length === 2 ) { const boundary = contentType . split ( ';' ) [ 1 ] . trim ( ) ; const boundaryDelimiter = BOUNDARY_PREFIX_SUFFIX + boundary . split ( '=' ) [ 1 ] ; return boundaryDelimiter ; } else { return '' ; } } , boundaryEnd ( boundaryDelimiter ) { if ( ! boundaryDelimiter . length ) { return '' ; } const boundaryEnd = boundaryDelimiter + BOUNDARY_PREFIX_SUFFIX ; return boundaryEnd ; } , } ;