import * as qs from 'qs' ; import * as url from 'url' ; import * as path from 'path' ; import zlib = require ( 'zlib' ) ; import { IRequestQueryParams , IHttpClientResponse } from './Interfaces' ; param { string } resource param { string } baseUrl param { IRequestOptions } options return { string } export function getUrl ( resource , baseUrl ? , queryParams ? ) { const pathApi = path . posix || path ; let requestUrl = '' ; if ( ! baseUrl ) { requestUrl = resource ; } else if ( ! resource ) { requestUrl = baseUrl ; } else { const base = url . parse ( baseUrl ) ; const resultantUrl = url . parse ( resource ) ; resultantUrl . protocol = resultantUrl . protocol || base . protocol ; resultantUrl . auth = resultantUrl . auth || base . auth ; resultantUrl . host = resultantUrl . host || base . host ; resultantUrl . pathname = pathApi . resolve ( base . pathname , resultantUrl . pathname ) ; if ( ! resultantUrl . pathname . endsWith ( '/' ) && resource . endsWith ( '/' ) ) { resultantUrl . pathname += '/' ; } requestUrl = url . format ( resultantUrl ) ; } return queryParams ? getUrlWithParsedQueryParams ( requestUrl , queryParams ) : requestUrl ; } param { string } requestUrl param { IRequestQueryParams } queryParams return { string } function getUrlWithParsedQueryParams ( requestUrl , queryParams ) { const url = requestUrl . replace ( /\?$/g , '' ) ; const parsedQueryParams = qs . stringify ( queryParams . params , buildParamsStringifyOptions ( queryParams ) ) ; return `${ url }${ parsedQueryParams }` ; } param { IRequestQueryParams } queryParams return { object } function buildParamsStringifyOptions ( queryParams ) { let options = { addQueryPrefix : true , delimiter : ( queryParams . options || { } ) . separator || '&' , allowDots : ( queryParams . options || { } ) . shouldAllowDots || false , arrayFormat : ( queryParams . options || { } ) . arrayFormat || 'repeat' , encodeValuesOnly : ( queryParams . options || { } ) . shouldOnlyEncodeValues || true } ; return options ; } param { Buffer } buffer param { string } charset return { Promise < string > } export async function decompressGzippedContent ( buffer , charset ? ) { return new Promise < string > ( async ( resolve , reject ) => { zlib . gunzip ( buffer , function ( error , buffer ) { if ( error ) { reject ( error ) ; } resolve ( buffer . toString ( charset || 'utf-8' ) ) ; } ) ; } ) ; } param { string } bypass return { RegExp } export function buildProxyBypassRegexFromEnv ( bypass ) { try { return new RegExp ( bypass , 'i' ) ; } catch ( err ) { if ( err instanceof SyntaxError && ( bypass || "" ) . startsWith ( "*" ) ) { let wildcardEscaped = bypass . replace ( '*' , '(.*)' ) ; return new RegExp ( wildcardEscaped , 'i' ) ; } throw err ; } } param { IHttpClientResponse } response return { string } export function obtainContentCharset ( response ) { const nodeSupportedEncodings = [ 'ascii' , 'utf8' , 'utf16le' , 'ucs2' , 'base64' , 'binary' , 'hex' ] ; const contentType = response . message . headers [ 'content-type' ] || '' ; const matches = contentType . match ( /charset=([^;,\r\n]+)/i ) ; return ( matches && matches [ 1 ] && nodeSupportedEncodings . indexOf ( matches [ 1 ] ) != - 1 ) ? matches [ 1 ] : 'utf-8' ; }