Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions libs/providers/go-feature-flag-web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ const evaluationCtx: EvaluationContext = {

const goFeatureFlagWebProvider = new GoFeatureFlagWebProvider({
endpoint: endpoint,
customHeadeers: {
'User-Agent': "my-app/1.0.0",
},
// ...
}, logger);

Expand All @@ -54,6 +57,7 @@ client.addHandler(ProviderEvents.ConfigurationChanged, () => { //... });
| endpoint | string | | endpoint is the URL where your GO Feature Flag server is located. |
| apiTimeout | number | 0 = no timeout | (optional) timeout is the time in millisecond we wait for an answer from the server. |
| 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. |
| customHeaders | object | | (optional) custom headers to be sent for every HTTP request. |
| websocketRetryInitialDelay | number | 100 | (optional) initial delay in millisecond to wait before retrying to connect the websocket |
| websocketRetryDelayMultiplier | number | 2 | (optional) multiplier of websocketRetryInitialDelay after each failure _(example: 1st connection retry will be after 100ms, second after 200ms, third after 400ms ...)_ |
| websocketMaxRetries | number | 10 | (optional) maximum number of retries before considering the websocket unreachable |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ describe('Collect Data API', () => {
endpoint: 'https://gofeatureflag.org',
apiTimeout: 1000,
apiKey: '123456',
customHeaders: {
'User-Agent': 'goff-web/1.0.0',
Authorization: 'Bearer foo',
},
};
const goff = new GoffApiController(options);
await goff.collectData(
Expand All @@ -37,6 +41,7 @@ describe('Collect Data API', () => {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: `Bearer ${options.apiKey}`,
'User-Agent': 'goff-web/1.0.0',
});
expect(fetchMock.lastOptions()?.body).toEqual(
JSON.stringify({
Expand All @@ -62,6 +67,9 @@ describe('Collect Data API', () => {
const options: GoFeatureFlagWebProviderOptions = {
endpoint: 'https://gofeatureflag.org',
apiTimeout: 1000,
customHeaders: {
'User-Agent': 'goff-web/2.0.0',
},
};
const goff = new GoffApiController(options);
await goff.collectData(
Expand All @@ -83,6 +91,7 @@ describe('Collect Data API', () => {
expect(fetchMock.lastOptions()?.headers).toEqual({
'Content-Type': 'application/json',
Accept: 'application/json',
'User-Agent': 'goff-web/2.0.0',
});
expect(fetchMock.lastOptions()?.body).toEqual(
JSON.stringify({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export class GoffApiController {
Accept: 'application/json',
};

if (this.options.customHeaders) {
Object.assign(headers, this.options.customHeaders);
}

if (this.options.apiKey) {
headers['Authorization'] = `Bearer ${this.options.apiKey}`;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ describe('GoFeatureFlagWebProvider', () => {
apiTimeout: 1000,
maxRetries: 1,
apiKey: 'my-api-key',
customHeaders: {
'User-Agent': 'goff-web/3.0.0',
Authorization: 'Bearer foo',
},
},
logger,
);
Expand All @@ -338,6 +342,7 @@ describe('GoFeatureFlagWebProvider', () => {
const headers = lastCall[1]?.headers as never;
expect(headers).not.toBeUndefined();
expect(headers['Authorization']).toBe('Bearer my-api-key');
expect(headers['User-Agent']).toBe('goff-web/3.0.0');
return;
}
expect(true).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class GoFeatureFlagWebProvider implements Provider {
private readonly _apiTimeout: number;
// apiKey is the key used to identify your request in GO Feature Flag
private readonly _apiKey: string | undefined;
// customHeaders to be sent for every HTTP request.
private readonly _customHeaders: Record<string, string> | undefined;
// initial delay in millisecond to wait before retrying to connect
private readonly _retryInitialDelay;
// multiplier of _retryInitialDelay after each failure
Expand All @@ -68,6 +70,7 @@ export class GoFeatureFlagWebProvider implements Provider {
this._retryDelayMultiplier = options.retryDelayMultiplier || 2;
this._maxRetries = options.maxRetries || 10;
this._apiKey = options.apiKey;
this._customHeaders = options.customHeaders;
this._disableDataCollection = options.disableDataCollection || false;

this._collectorManager = new CollectorManager(options, logger);
Expand Down Expand Up @@ -321,6 +324,7 @@ export class GoFeatureFlagWebProvider implements Provider {
'Content-Type': 'application/json',
Accept: 'application/json',
// we had the authorization header only if we have an API Key
...(this._customHeaders || {}),
...(this._apiKey ? { Authorization: `Bearer ${this._apiKey}` } : {}),
},
body: JSON.stringify(request),
Expand Down
3 changes: 3 additions & 0 deletions libs/providers/go-feature-flag-web/src/lib/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export interface GoFeatureFlagWebProviderOptions {
// Default: null
apiKey?: string;

// customHeaders (optional) custom headers to be sent for every HTTP request.
customHeaders?: Record<string, string>;

// initial delay in millisecond to wait before retrying to connect to GO Feature Flag (websocket and API)
// Default: 100 ms
retryInitialDelay?: number;
Expand Down