Skip to content

Commit

Permalink
fix(go-feature-flag): Support endpoint paths in data collector goff-a…
Browse files Browse the repository at this point in the history
…pi.ts (#1184)

Signed-off-by: Chris Price <[email protected]>
  • Loading branch information
crprice authored Jan 23, 2025
1 parent 62da1cb commit 928e437
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,52 @@ describe('Collect Data API', () => {
);
});

it('should call the API to collect data with endpoint path', async () => {
fetchMock.post('https://gofeatureflag.org/examplepath/v1/data/collector', 200);
const options: GoFeatureFlagWebProviderOptions = {
endpoint: 'https://gofeatureflag.org/examplepath',
apiTimeout: 1000,
};
const goff = new GoffApiController(options);
await goff.collectData(
[
{
key: 'flagKey',
contextKind: 'user',
creationDate: 1733138237486,
default: false,
kind: 'feature',
userKey: 'toto',
value: true,
variation: 'varA',
},
],
{ provider: 'open-feature-js-sdk' },
);
expect(fetchMock.lastUrl()).toBe('https://gofeatureflag.org/examplepath/v1/data/collector');
expect(fetchMock.lastOptions()?.headers).toEqual({
'Content-Type': 'application/json',
Accept: 'application/json',
});
expect(fetchMock.lastOptions()?.body).toEqual(
JSON.stringify({
events: [
{
key: 'flagKey',
contextKind: 'user',
creationDate: 1733138237486,
default: false,
kind: 'feature',
userKey: 'toto',
value: true,
variation: 'varA',
},
],
meta: { provider: 'open-feature-js-sdk' },
}),
);
});

it('should not call the API to collect data if no event provided', async () => {
fetchMock.post('https://gofeatureflag.org/v1/data/collector', 200);
const options: GoFeatureFlagWebProviderOptions = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export class GoffApiController {

const request: DataCollectorRequest<boolean> = { events: events, meta: dataCollectorMetadata };
const endpointURL = new URL(this.endpoint);
endpointURL.pathname = 'v1/data/collector';
const dataCollectorPath = 'v1/data/collector';
endpointURL.pathname = endpointURL.pathname.endsWith('/')
? endpointURL.pathname + dataCollectorPath
: endpointURL.pathname + '/' + dataCollectorPath;

try {
const headers: HeadersInit = {
Expand Down

0 comments on commit 928e437

Please sign in to comment.