Skip to content

Commit

Permalink
feat(nodejs): allow setting arbitrary headers (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps committed May 10, 2024
1 parent 4c71b34 commit b8f1286
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
14 changes: 8 additions & 6 deletions flipt-node/src/evaluation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ import {

export class Evaluation {
private url: string;
private headers: object;
private headers: Record<string, string>;
private timeout?: number;

public constructor(
url: string,
timeout?: number,
authenticationStrategy?: AuthenticationStrategy
authenticationStrategy?: AuthenticationStrategy,
headers?: Record<string, string>
) {
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;
}
Expand Down
8 changes: 7 additions & 1 deletion flipt-node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface FliptClientOptions {
url?: string;
authenticationStrategy?: AuthenticationStrategy;
timeout?: number;
headers?: Record<string, string>;
}

export interface AuthenticationStrategy {
Expand Down Expand Up @@ -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
);
}
}
Expand Down
23 changes: 10 additions & 13 deletions flipt-node/src/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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: [
{
Expand Down

0 comments on commit b8f1286

Please sign in to comment.