Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(go-feature-flag): Support endpoint paths in data collector goff-api.ts #1184

Merged
merged 2 commits into from
Jan 23, 2025
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
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
Loading