import { AxiosRequestConfig , default as axios } from "axios" ; import fs from "fs" ; import ProxyAgent from "proxy-agent" ; import { promisify } from "util" ; import type { SpanTag , SpanTags } from "./interfaces" ; export const pick = < T extends object , K extends keyof T > ( base , keys ) => { const definedKeys = keys . filter ( ( key ) => ! ! base [ key ] ) ; const pickedObject = { } ; for ( const key of definedKeys ) { pickedObject [ key ] = base [ key ] ; } return pickedObject ; } ; export const getConfig = async ( configPath ) => { const configFile = await promisify ( fs . readFile ) ( configPath , "utf-8" ) ; return JSON . parse ( configFile ) ; } ; type ProxyType = "http" | "https" | "socks" | "socks4" | "socks4a" | "socks5" | "socks5h" | "pac+data" | "pac+file" | "pac+ftp" | "pac+http" | "pac+https" ; export interface ProxyConfiguration { auth ? : { password : string ; username : string ; } ; host ? : string ; port ? : number ; protocol : ProxyType ; } export const getProxyUrl = ( options ? ) : string => { if ( ! options ) { return "" ; } const { auth , host , port , protocol } = options ; if ( ! host || ! port ) { return "" ; } const authFragment = auth ? `${ auth . username }:${ auth . password }@` : "" ; return `${ protocol }://${ authFragment }${ host }:${ port }` ; } ; export interface RequestOptions { apiKey : string ; appKey ? : string ; baseUrl : string ; headers ? : Map < string , string > ; overrideUrl ? : string ; proxyOpts ? : ProxyConfiguration ; } export const getRequestBuilder = ( options ) => { const { apiKey , appKey , baseUrl , overrideUrl , proxyOpts } = options ; const overrideArgs = ( args ) => { const newArguments = { ... args , headers : { "DD-API-KEY" : apiKey , ... ( appKey ? { "DD-APPLICATION-KEY" : appKey } : { } ) , ... args . headers , } , } ; if ( overrideUrl !== undefined ) { newArguments . url = overrideUrl ; } const proxyAgent = getProxyAgent ( proxyOpts ) ; if ( proxyAgent ) { newArguments . httpAgent = proxyAgent ; newArguments . httpsAgent = proxyAgent ; } if ( options . headers !== undefined ) { options . headers . forEach ( ( value , key ) => { newArguments . headers [ key ] = value ; } ) ; } return newArguments ; } ; const baseConfiguration = { baseURL : baseUrl , proxy : false , } ; return ( args ) => axios . create ( baseConfiguration ) ( overrideArgs ( args ) ) ; } ; export const getProxyAgent = ( proxyOpts ? ) : ReturnType < typeof ProxyAgent > => { const proxyUrlFromConfiguration = getProxyUrl ( proxyOpts ) ; return new ProxyAgent ( proxyUrlFromConfiguration ) ; } ; export const getApiHostForSite = ( site ) => { switch ( site ) { case "datad0g.com" : return `app.${ site }` ; case "datadoghq.com" : case "datadoghq.eu" : default : return `api.${ site }` ; } } ; export const buildPath = ( ... args ) => args . map ( ( part , i ) => { if ( i === 0 ) { return part . trim ( ) . replace ( /[\/]*$/g , "" ) ; } else { return part . trim ( ) . replace ( /(^[\/]*|[\/]*$)/g , "" ) ; } } ) . filter ( ( x ) => x . length ) . join ( "/" ) ; export const removeEmptyValues = ( tags ) => ( Object . keys ( tags ) as SpanTag [ ] ) . reduce ( ( filteredTags , tag ) => { if ( ! tags [ tag ] ) { return filteredTags ; } return { ... filteredTags , [ tag ] : tags [ tag ] , } ; } , { } ) ;