Skip to content

Commit f247aee

Browse files
authored
Merge pull request #97 from shak58/patch-1
Fix computeEndpoint method
2 parents 86d0d19 + 07e873c commit f247aee

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/client/rest/APIRequester.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,19 @@ describe('APIRequester', () => {
5454
}
5555
)
5656
})
57+
58+
it('handles baseURL with path and endpoint without leading slash', async () => {
59+
mockedAxios.get.mockResolvedValueOnce({ data: null });
60+
61+
const request = new APIRequester('https://rest.testnet.initia.xyz/bar');
62+
await request.get('foo');
63+
64+
expect(mockedAxios.get).toHaveBeenCalledWith(
65+
'https://rest.testnet.initia.xyz/bar/foo',
66+
{
67+
headers: new axios.AxiosHeaders(),
68+
params: {},
69+
}
70+
);
71+
});
5772
})

src/client/rest/APIRequester.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ export class APIRequester {
5252
}
5353

5454
private computeEndpoint(endpoint: string) {
55-
const url = new URL(this.baseURL)
55+
const relativeEndpoint = endpoint.replace(/^\/+/, '');
56+
const baseURLObject = new URL(this.baseURL);
5657

57-
url.pathname === '/'
58-
? (url.pathname = endpoint)
59-
: (url.pathname += endpoint)
58+
if (!baseURLObject.pathname.endsWith('/')) {
59+
baseURLObject.pathname += '/';
60+
}
61+
baseURLObject.pathname += relativeEndpoint;
6062

61-
return url.toString()
63+
return baseURLObject.toString();
6264
}
6365

6466
public async getRaw<T>(

0 commit comments

Comments
 (0)