From b8f12866f530dde6bb12b6119dc2b38f2a9a89ef Mon Sep 17 00:00:00 2001 From: Mark Phelps <209477+markphelps@users.noreply.github.com> Date: Fri, 10 May 2024 09:32:53 -0400 Subject: [PATCH] feat(nodejs): allow setting arbitrary headers (#203) --- flipt-node/src/evaluation/index.ts | 14 ++++++++------ flipt-node/src/index.ts | 8 +++++++- flipt-node/src/integration.test.ts | 23 ++++++++++------------- 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/flipt-node/src/evaluation/index.ts b/flipt-node/src/evaluation/index.ts index 4608d51b..61b57a6b 100644 --- a/flipt-node/src/evaluation/index.ts +++ b/flipt-node/src/evaluation/index.ts @@ -9,20 +9,22 @@ import { export class Evaluation { private url: string; - private headers: object; + private headers: Record; private timeout?: number; public constructor( url: string, timeout?: number, - authenticationStrategy?: AuthenticationStrategy + authenticationStrategy?: AuthenticationStrategy, + headers?: Record ) { this.url = url; - this.headers = {}; + this.headers = headers || {}; if (!!authenticationStrategy) { - this.headers = Object.fromEntries( - authenticationStrategy.authenticate().entries() - ); + this.headers = { + ...this.headers, + ...Object.fromEntries(authenticationStrategy.authenticate()) + }; } this.timeout = timeout; } diff --git a/flipt-node/src/index.ts b/flipt-node/src/index.ts index e067c0a1..19dba338 100644 --- a/flipt-node/src/index.ts +++ b/flipt-node/src/index.ts @@ -9,6 +9,7 @@ interface FliptClientOptions { url?: string; authenticationStrategy?: AuthenticationStrategy; timeout?: number; + headers?: Record; } export interface AuthenticationStrategy { @@ -65,10 +66,15 @@ export class FliptClient { clientOptions.authenticationStrategy = options.authenticationStrategy; } + if (options?.headers !== undefined) { + clientOptions.headers = options.headers; + } + this.evaluation = new Evaluation( clientOptions.url || defaultURL, clientOptions.timeout, - clientOptions.authenticationStrategy + clientOptions.authenticationStrategy, + clientOptions.headers ); } } diff --git a/flipt-node/src/integration.test.ts b/flipt-node/src/integration.test.ts index 9e295f6b..69ca151a 100644 --- a/flipt-node/src/integration.test.ts +++ b/flipt-node/src/integration.test.ts @@ -13,12 +13,19 @@ if (!authToken) { process.exit(1); } -test("variant", async () => { - const client = new FliptClient({ +let client: FliptClient; + +beforeEach(() => { + client = new FliptClient({ url: fliptUrl, - authenticationStrategy: new ClientTokenAuthentication(authToken) + authenticationStrategy: new ClientTokenAuthentication(authToken), + headers: { + "x-custom-header": "custom-value" + } }); +}); +test("variant", async () => { const variant = await client.evaluation.variant({ namespaceKey: "default", flagKey: "flag1", @@ -34,11 +41,6 @@ test("variant", async () => { }); test("boolean", async () => { - const client = new FliptClient({ - url: fliptUrl, - authenticationStrategy: new ClientTokenAuthentication(authToken) - }); - const boolean = await client.evaluation.boolean({ namespaceKey: "default", flagKey: "flag_boolean", @@ -52,11 +54,6 @@ test("boolean", async () => { }); test("batch", async () => { - const client = new FliptClient({ - url: fliptUrl, - authenticationStrategy: new ClientTokenAuthentication(authToken) - }); - const batch = await client.evaluation.batch({ requests: [ {