import { v4 as uuidv4 } from "uuid" ; import { HttpOperationResponse } from "../httpOperationResponse" ; import { RestError } from "../restError" ; import { WebResourceLike } from "../webResource" ; import { Constants } from "./constants" ; /**
 * A constant that indicates whether the environment is node.js or browser based.
 */ export const isNode = typeof process !== "undefined" && ! ! process . version && ! ! process . versions && ! ! process . versions . node ; param { object } urlToCheck return { boolean } export function urlIsHTTPS ( urlToCheck ) { return urlToCheck . protocol . toLowerCase ( ) === Constants . HTTPS ; } param { string } uri return { string } export function encodeUri ( uri ) { return encodeURIComponent ( uri ) . replace ( /!/g , "%21" ) . replace ( /"/g , "%27" ) . replace ( /\(/g , "%28" ) . replace ( /\)/g , "%29" ) . replace ( /\*/g , "%2A" ) ; } param { HttpOperationResponse } response return { object } export function stripResponse ( response ) { const strippedResponse = { } ; strippedResponse . body = response . bodyAsText ; strippedResponse . headers = response . headers ; strippedResponse . status = response . status ; return strippedResponse ; } param { WebResource } request return { WebResource } export function stripRequest ( request ) { const strippedRequest = request . clone ( ) ; if ( strippedRequest . headers ) { strippedRequest . headers . remove ( "authorization" ) ; } return strippedRequest ; } param { string } uuid return { boolean } export function isValidUuid ( uuid ) { const validUuidRegex = new RegExp ( "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$" , "ig" ) ; return validUuidRegex . test ( uuid ) ; } param { object } obj return { any [ ] } export function objectValues ( obj ) { const result = [ ] ; if ( obj && obj instanceof Object ) { for ( const key in obj ) { if ( obj . hasOwnProperty ( key ) ) { result . push ( ( < any > obj ) [ key ] ) ; } } } else { throw new Error ( `The provided object ${ JSON . stringify ( obj , undefined , 2 ) } is not a valid object that can be ` + `enumerated to provide its values as an array.` ) ; } return result ; } return { string } export function generateUuid ( ) { return uuidv4 ( ) ; } param { Array } promiseFactories param { any } kickstart return export function executePromisesSequentially ( promiseFactories , kickstart ) { let result = Promise . resolve ( kickstart ) ; promiseFactories . forEach ( ( promiseFactory ) => { result = result . then ( promiseFactory ) ; } ) ; return result ; } param { object } source param { object } target returns { object } export function mergeObjects ( source , target ) { Object . keys ( source ) . forEach ( ( key ) => { target [ key ] = source [ key ] ; } ) ; return target ; } param { number } t param { T } value returns { Promise < T > } export function delay < T > ( t , value ? ) { return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( value ) , t ) ) ; } /**
 * Service callback that is returned for REST requests initiated by the service client.
 */ export interface ServiceCallback < TResult > { param { Error | RestError | null } err param { TResult } result param { WebResourceLike } request param { HttpOperationResponse } response ( err , result ? , request ? , response ? ) : void ; } param { Promise < any > } promise returns { Function } deprecated export function promiseToCallback ( promise ) { if ( typeof promise . then !== "function" ) { throw new Error ( "The provided input is not a Promise." ) ; } return ( cb ) : void => { promise . then ( ( data ) => { cb ( undefined , data ) ; } , ( err ) => { cb ( err ) ; } ) ; } ; } param { Promise < HttpOperationResponse > } promise returns { Function } export function promiseToServiceCallback < T > ( promise ) { if ( typeof promise . then !== "function" ) { throw new Error ( "The provided input is not a Promise." ) ; } return ( cb ) : void => { promise . then ( ( data ) => { process . nextTick ( cb , undefined , data . parsedBody as T , data . request , data ) ; } , ( err ) => { process . nextTick ( cb , err ) ; } ) ; } ; } export function prepareXMLRootList ( obj , elementName ) { if ( ! Array . isArray ( obj ) ) { obj = [ obj ] ; } return { [ elementName ] : obj } ; } param { object } targetCtor param { Array < object > } sourceCtors export function applyMixins ( targetCtor , sourceCtors ) { sourceCtors . forEach ( ( sourceCtors ) => { Object . getOwnPropertyNames ( sourceCtors . prototype ) . forEach ( ( name ) => { targetCtor . prototype [ name ] = sourceCtors . prototype [ name ] ; } ) ; } ) ; } const validateISODuration = /^(-|\+)?P(?:([-+]?[0-9,.]*)Y)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)W)?(?:([-+]?[0-9,.]*)D)?(?:T(?:([-+]?[0-9,.]*)H)?(?:([-+]?[0-9,.]*)M)?(?:([-+]?[0-9,.]*)S)?)?$/ ; param { string } value return { boolean } export function isDuration ( value ) { return validateISODuration . test ( value ) ; } param { string | undefined } value param { string } searchValue param { string } replaceValue returns { string | undefined } export function replaceAll ( value , searchValue , replaceValue ) { return ! value || ! searchValue ? value : value . split ( searchValue ) . join ( replaceValue || "" ) ; } param value return export function isPrimitiveType ( value ) { return ( typeof value !== "object" && typeof value !== "function" ) || value === null ; }