Skip to content

Commit de86458

Browse files
authored
Merge pull request #288 from Flagsmith/fix/sanitise-api-url
fix: Handle API URL with or without trailing slash
2 parents 9978a11 + cd82354 commit de86458

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

flagsmith-core.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import angularFetch from './utils/angular-fetch';
2323
import setDynatraceValue from './utils/set-dynatrace-value';
2424
import { EvaluationContext } from './evaluation-context';
2525
import { isTraitEvaluationContext, toEvaluationContext, toTraitEvaluationContextObject } from './utils/types';
26+
import { ensureTrailingSlash } from './utils/ensureTrailingSlash';
2627

2728
enum FlagSource {
2829
"NONE" = "NONE",
@@ -318,7 +319,7 @@ const Flagsmith = class {
318319
) : {},
319320
} : evaluationContext.identity;
320321
this.evaluationContext = evaluationContext;
321-
this.api = api;
322+
this.api = ensureTrailingSlash(api);
322323
this.headers = headers;
323324
this.getFlagInterval = null;
324325
this.analyticsInterval = null;

lib/flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "flagsmith",
3-
"version": "9.0.2",
3+
"version": "9.0.3",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"module": "./index.mjs",

lib/react-native-flagsmith/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-flagsmith",
3-
"version": "9.0.2",
3+
"version": "9.0.3",
44
"description": "Feature flagging to support continuous development",
55
"main": "./index.js",
66
"repository": {

test/init.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ describe('Flagsmith.init', () => {
9696
});
9797
await expect(flagsmith.init(initConfig)).rejects.toThrow(Error);
9898
});
99+
test('should sanitise api url', async () => {
100+
const onChange = jest.fn();
101+
const { flagsmith,initConfig } = getFlagsmith({
102+
api:'https://edge.api.flagsmith.com/api/v1/',
103+
onChange,
104+
});
105+
await flagsmith.init(initConfig)
106+
expect(flagsmith.getState().api).toBe('https://edge.api.flagsmith.com/api/v1/');
107+
const { flagsmith:flagsmith2 } = getFlagsmith({
108+
api:'https://edge.api.flagsmith.com/api/v1',
109+
onChange,
110+
});
111+
await flagsmith2.init(initConfig)
112+
expect(flagsmith2.getState().api).toBe('https://edge.api.flagsmith.com/api/v1/');
113+
});
99114
test('should reject initialize with identity bad key', async () => {
100115
const onChange = jest.fn();
101116
const { flagsmith, initConfig, mockFetch } = getFlagsmith({ onChange, environmentID: 'bad' });

utils/ensureTrailingSlash.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function ensureTrailingSlash(str: string): string {
2+
return str.endsWith('/') ? str : str + '/';
3+
}

0 commit comments

Comments
 (0)