diff --git a/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts b/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts index 2e6d0a779..e66143a54 100644 --- a/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts +++ b/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.spec.ts @@ -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 = { diff --git a/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.ts b/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.ts index 05a38dd7f..d1ce4f922 100644 --- a/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.ts +++ b/libs/providers/go-feature-flag-web/src/lib/controller/goff-api.ts @@ -22,7 +22,10 @@ export class GoffApiController { const request: DataCollectorRequest = { 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 = {