Skip to content

Commit 23a2363

Browse files
authored
feat: Add optional custom headers (GoFeatureFlag) (#1465)
Signed-off-by: Olivier Laviale <olivier.laviale@gmail.com>
1 parent 4576afb commit 23a2363

File tree

6 files changed

+29
-0
lines changed

6 files changed

+29
-0
lines changed

libs/providers/go-feature-flag-web/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ const evaluationCtx: EvaluationContext = {
2929

3030
const goFeatureFlagWebProvider = new GoFeatureFlagWebProvider({
3131
endpoint: endpoint,
32+
customHeadeers: {
33+
'User-Agent': "my-app/1.0.0",
34+
},
3235
// ...
3336
}, logger);
3437

@@ -54,6 +57,7 @@ client.addHandler(ProviderEvents.ConfigurationChanged, () => { //... });
5457
| endpoint | string | | endpoint is the URL where your GO Feature Flag server is located. |
5558
| apiTimeout | number | 0 = no timeout | (optional) timeout is the time in millisecond we wait for an answer from the server. |
5659
| apiKey | string | | (optional) If GO Feature Flag is configured to authenticate the requests, you should provide an API Key to the provider. Please ask the administrator of the relay proxy to provide an API Key. |
60+
| customHeaders | object | | (optional) custom headers to be sent for every HTTP request. |
5761
| websocketRetryInitialDelay | number | 100 | (optional) initial delay in millisecond to wait before retrying to connect the websocket |
5862
| websocketRetryDelayMultiplier | number | 2 | (optional) multiplier of websocketRetryInitialDelay after each failure _(example: 1st connection retry will be after 100ms, second after 200ms, third after 400ms ...)_ |
5963
| websocketMaxRetries | number | 10 | (optional) maximum number of retries before considering the websocket unreachable |

libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ describe('Collect Data API', () => {
1515
endpoint: 'https://gofeatureflag.org',
1616
apiTimeout: 1000,
1717
apiKey: '123456',
18+
customHeaders: {
19+
'User-Agent': 'goff-web/1.0.0',
20+
Authorization: 'Bearer foo',
21+
},
1822
};
1923
const goff = new GoffApiController(options);
2024
await goff.collectData(
@@ -37,6 +41,7 @@ describe('Collect Data API', () => {
3741
'Content-Type': 'application/json',
3842
Accept: 'application/json',
3943
Authorization: `Bearer ${options.apiKey}`,
44+
'User-Agent': 'goff-web/1.0.0',
4045
});
4146
expect(fetchMock.lastOptions()?.body).toEqual(
4247
JSON.stringify({
@@ -62,6 +67,9 @@ describe('Collect Data API', () => {
6267
const options: GoFeatureFlagWebProviderOptions = {
6368
endpoint: 'https://gofeatureflag.org',
6469
apiTimeout: 1000,
70+
customHeaders: {
71+
'User-Agent': 'goff-web/2.0.0',
72+
},
6573
};
6674
const goff = new GoffApiController(options);
6775
await goff.collectData(
@@ -83,6 +91,7 @@ describe('Collect Data API', () => {
8391
expect(fetchMock.lastOptions()?.headers).toEqual({
8492
'Content-Type': 'application/json',
8593
Accept: 'application/json',
94+
'User-Agent': 'goff-web/2.0.0',
8695
});
8796
expect(fetchMock.lastOptions()?.body).toEqual(
8897
JSON.stringify({

libs/providers/go-feature-flag-web/src/lib/controller/goff-api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ export class GoffApiController {
4242
Accept: 'application/json',
4343
};
4444

45+
if (this.options.customHeaders) {
46+
Object.assign(headers, this.options.customHeaders);
47+
}
48+
4549
if (this.options.apiKey) {
4650
headers['Authorization'] = `Bearer ${this.options.apiKey}`;
4751
}

libs/providers/go-feature-flag-web/src/lib/go-feature-flag-web-provider.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ describe('GoFeatureFlagWebProvider', () => {
322322
apiTimeout: 1000,
323323
maxRetries: 1,
324324
apiKey: 'my-api-key',
325+
customHeaders: {
326+
'User-Agent': 'goff-web/3.0.0',
327+
Authorization: 'Bearer foo',
328+
},
325329
},
326330
logger,
327331
);
@@ -338,6 +342,7 @@ describe('GoFeatureFlagWebProvider', () => {
338342
const headers = lastCall[1]?.headers as never;
339343
expect(headers).not.toBeUndefined();
340344
expect(headers['Authorization']).toBe('Bearer my-api-key');
345+
expect(headers['User-Agent']).toBe('goff-web/3.0.0');
341346
return;
342347
}
343348
expect(true).toBe(false);

libs/providers/go-feature-flag-web/src/lib/go-feature-flag-web-provider.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ export class GoFeatureFlagWebProvider implements Provider {
4444
private readonly _apiTimeout: number;
4545
// apiKey is the key used to identify your request in GO Feature Flag
4646
private readonly _apiKey: string | undefined;
47+
// customHeaders to be sent for every HTTP request.
48+
private readonly _customHeaders: Record<string, string> | undefined;
4749
// initial delay in millisecond to wait before retrying to connect
4850
private readonly _retryInitialDelay;
4951
// multiplier of _retryInitialDelay after each failure
@@ -68,6 +70,7 @@ export class GoFeatureFlagWebProvider implements Provider {
6870
this._retryDelayMultiplier = options.retryDelayMultiplier || 2;
6971
this._maxRetries = options.maxRetries || 10;
7072
this._apiKey = options.apiKey;
73+
this._customHeaders = options.customHeaders;
7174
this._disableDataCollection = options.disableDataCollection || false;
7275

7376
this._collectorManager = new CollectorManager(options, logger);
@@ -321,6 +324,7 @@ export class GoFeatureFlagWebProvider implements Provider {
321324
'Content-Type': 'application/json',
322325
Accept: 'application/json',
323326
// we had the authorization header only if we have an API Key
327+
...(this._customHeaders || {}),
324328
...(this._apiKey ? { Authorization: `Bearer ${this._apiKey}` } : {}),
325329
},
326330
body: JSON.stringify(request),

libs/providers/go-feature-flag-web/src/lib/model.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export interface GoFeatureFlagWebProviderOptions {
4545
// Default: null
4646
apiKey?: string;
4747

48+
// customHeaders (optional) custom headers to be sent for every HTTP request.
49+
customHeaders?: Record<string, string>;
50+
4851
// initial delay in millisecond to wait before retrying to connect to GO Feature Flag (websocket and API)
4952
// Default: 100 ms
5053
retryInitialDelay?: number;

0 commit comments

Comments
 (0)