Skip to content

fix: Handle API URL with or without trailing slash #288

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

Merged
merged 5 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
3 changes: 2 additions & 1 deletion flagsmith-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import angularFetch from './utils/angular-fetch';
import setDynatraceValue from './utils/set-dynatrace-value';
import { EvaluationContext } from './evaluation-context';
import { isTraitEvaluationContext, toEvaluationContext, toTraitEvaluationContextObject } from './utils/types';
import { ensureTrailingSlash } from './utils/ensureTrailingSlash';

enum FlagSource {
"NONE" = "NONE",
Expand Down Expand Up @@ -318,7 +319,7 @@ const Flagsmith = class {
) : {},
} : evaluationContext.identity;
this.evaluationContext = evaluationContext;
this.api = api;
this.api = ensureTrailingSlash(api);
this.headers = headers;
this.getFlagInterval = null;
this.analyticsInterval = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flagsmith",
"version": "9.0.2",
"version": "9.0.3",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"module": "./index.mjs",
Expand Down
2 changes: 1 addition & 1 deletion lib/react-native-flagsmith/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-flagsmith",
"version": "9.0.2",
"version": "9.0.3",
"description": "Feature flagging to support continuous development",
"main": "./index.js",
"repository": {
Expand Down
15 changes: 15 additions & 0 deletions test/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,21 @@ describe('Flagsmith.init', () => {
});
await expect(flagsmith.init(initConfig)).rejects.toThrow(Error);
});
test('should sanitise api url', async () => {
const onChange = jest.fn();
const { flagsmith,initConfig } = getFlagsmith({
api:'https://edge.api.flagsmith.com/api/v1/',
onChange,
});
await flagsmith.init(initConfig)
expect(flagsmith.getState().api).toBe('https://edge.api.flagsmith.com/api/v1/');
const { flagsmith:flagsmith2 } = getFlagsmith({
api:'https://edge.api.flagsmith.com/api/v1',
onChange,
});
await flagsmith2.init(initConfig)
expect(flagsmith2.getState().api).toBe('https://edge.api.flagsmith.com/api/v1/');
});
test('should reject initialize with identity bad key', async () => {
const onChange = jest.fn();
const { flagsmith, initConfig, mockFetch } = getFlagsmith({ onChange, environmentID: 'bad' });
Expand Down
3 changes: 3 additions & 0 deletions utils/ensureTrailingSlash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function ensureTrailingSlash(str: string): string {
return str.endsWith('/') ? str : str + '/';
}
Loading