Skip to content

Commit bd3a6b6

Browse files
committed
fix: fix trailing slash issues causing 404
1 parent 6429725 commit bd3a6b6

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/api-endpoint.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ describe('ApiEndpoints', () => {
4242
`${DEFAULT_BASE_URL}/flag-config/v1/config`,
4343
);
4444
});
45+
46+
it('should remove a trailing slash', () => {
47+
const apiEndpoints = new ApiEndpoints({});
48+
expect(apiEndpoints.endpoint('/data/').toString()).toEqual(`${DEFAULT_BASE_URL}/data`);
49+
});
4550
});

src/http-client.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ export interface IBanditParametersResponse {
4646
bandits: Record<string, BanditParameters>;
4747
}
4848

49+
const urlToString = (url: URL) => {
50+
url.pathname = url.pathname.replace(/\/$/, '');
51+
return url.toString();
52+
};
53+
4954
export interface IHttpClient {
5055
getUniversalFlagConfiguration(): Promise<IUniversalFlagConfigResponse | undefined>;
5156
getBanditParameters(): Promise<IBanditParametersResponse | undefined>;
@@ -89,7 +94,7 @@ export default class FetchHttpClient implements IHttpClient {
8994
const controller = new AbortController();
9095
const signal = controller.signal;
9196
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
92-
const response = await fetch(url.toString(), { signal });
97+
const response = await fetch(urlToString(url), { signal });
9398
// Clear timeout when response is received within the budget.
9499
clearTimeout(timeoutId);
95100

@@ -114,7 +119,7 @@ export default class FetchHttpClient implements IHttpClient {
114119
const signal = controller.signal;
115120
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
116121

117-
const response = await fetch(url.toString(), {
122+
const response = await fetch(urlToString(url), {
118123
method: 'POST',
119124
headers: {
120125
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)