import { HttpHeaders } from "../httpHeaders" ; import { HttpOperationResponse } from "../httpOperationResponse" ; import { Constants } from "../util/constants" ; import { WebResourceLike } from "../webResource" ; import { getDefaultUserAgentKey , getPlatformSpecificData } from "./msRestUserAgentPolicy" ; import { BaseRequestPolicy , RequestPolicy , RequestPolicyFactory , RequestPolicyOptionsLike , } from "./requestPolicy" ; export type TelemetryInfo = { key ? : string ; value ? : string ; } ; function getRuntimeInfo ( ) { const msRestRuntime = { key : "ms-rest-js" , value : Constants . msRestVersion , } ; return [ msRestRuntime ] ; } function getUserAgentString ( telemetryInfo , keySeparator = " " , valueSeparator = "/" ) { return telemetryInfo . map ( ( info ) => { const value = info . value ? `${ valueSeparator }${ info . value }` : "" ; return `${ info . key }${ value }` ; } ) . join ( keySeparator ) ; } export const getDefaultUserAgentHeaderName = getDefaultUserAgentKey ; export function getDefaultUserAgentValue ( ) { const runtimeInfo = getRuntimeInfo ( ) ; const platformSpecificData = getPlatformSpecificData ( ) ; const userAgent = getUserAgentString ( runtimeInfo . concat ( platformSpecificData ) ) ; return userAgent ; } export function userAgentPolicy ( userAgentData ? ) { const key = ! userAgentData || userAgentData . key == undefined ? getDefaultUserAgentKey ( ) : userAgentData . key ; const value = ! userAgentData || userAgentData . value == undefined ? getDefaultUserAgentValue ( ) : userAgentData . value ; return { create : ( nextPolicy , options ) => { return new UserAgentPolicy ( nextPolicy , options , key , value ) ; } , } ; } export class UserAgentPolicy extends BaseRequestPolicy { constructor ( readonly _nextPolicy , readonly _options , protected headerKey , protected headerValue ) { super ( _nextPolicy , _options ) ; } sendRequest ( request ) { this . addUserAgentHeader ( request ) ; return this . _nextPolicy . sendRequest ( request ) ; } addUserAgentHeader ( request ) { if ( ! request . headers ) { request . headers = new HttpHeaders ( ) ; } if ( ! request . headers . get ( this . headerKey ) && this . headerValue ) { request . headers . set ( this . headerKey , this . headerValue ) ; } } }