From de7f49ed86308a451af8888c91a2aa387b0cf3f5 Mon Sep 17 00:00:00 2001 From: Landon Gavin Date: Tue, 6 Apr 2021 09:48:35 -0400 Subject: [PATCH] SchemaRegistry: Add http agent argument Add option to pass in HTTP agent that binds to mappersmith --- src/SchemaRegistry.ts | 4 ++-- src/api/index.ts | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/SchemaRegistry.ts b/src/SchemaRegistry.ts index 6194557..3f31ffd 100644 --- a/src/SchemaRegistry.ts +++ b/src/SchemaRegistry.ts @@ -50,10 +50,10 @@ export default class SchemaRegistry { public cache: Cache constructor( - { auth, clientId, host, retry }: SchemaRegistryAPIClientArgs, + { auth, clientId, host, retry, agent }: SchemaRegistryAPIClientArgs, options?: SchemaRegistryAPIClientOptions, ) { - this.api = API({ auth, clientId, host, retry }) + this.api = API({ auth, clientId, host, retry, agent }) this.cache = new Cache() this.options = options } diff --git a/src/api/index.ts b/src/api/index.ts index 4c6d426..859e971 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,4 +1,5 @@ -import forge, { Client, Authorization } from 'mappersmith' +import { Agent } from 'http' +import forge, { Authorization, Client, configs } from 'mappersmith' import RetryMiddleware, { RetryMiddlewareOptions } from 'mappersmith/middleware/retry/v2' import BasicAuthMiddleware from 'mappersmith/middleware/basic-auth' @@ -19,6 +20,8 @@ export interface SchemaRegistryAPIClientArgs { auth?: Authorization clientId?: string retry?: Partial + /** HTTP Agent that will be passed to underlying API calls */ + agent?: Agent } // TODO: Improve typings @@ -43,8 +46,20 @@ export default ({ clientId, host, retry = {}, -}: SchemaRegistryAPIClientArgs): SchemaRegistryAPIClient => - forge({ + agent, +}: SchemaRegistryAPIClientArgs): SchemaRegistryAPIClient => { + // TODO: figure out how to only bind agent to host URL + // if an agent was provided, bind the agent to the mappersmith configs + if (agent) { + configs.gatewayConfigs.HTTP = { + configure() { + return { + agent, + } + } + } + } + return forge({ clientId: clientId || DEFAULT_API_CLIENT_ID, ignoreGlobalMiddleware: true, host, @@ -100,3 +115,4 @@ export default ({ }, }, }) +}