From 7683fe258510eaf9bfbdb2734aeb5e5c8452b993 Mon Sep 17 00:00:00 2001 From: shak58 <150069539+shak58@users.noreply.github.com> Date: Fri, 22 Nov 2024 15:48:12 +0000 Subject: [PATCH 1/4] improve computeEndpoint --- src/client/rest/APIRequester.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/client/rest/APIRequester.ts b/src/client/rest/APIRequester.ts index f4d486e..305638e 100644 --- a/src/client/rest/APIRequester.ts +++ b/src/client/rest/APIRequester.ts @@ -52,13 +52,15 @@ export class APIRequester { } private computeEndpoint(endpoint: string) { - const url = new URL(this.baseURL) + const relativeEndpoint = endpoint.replace(/^\/+/, ''); + const baseURLObject = new URL(this.baseURL); - url.pathname === '/' - ? (url.pathname = endpoint) - : (url.pathname += endpoint) + if (!baseURLObject.pathname.endsWith('/')) { + baseURLObject.pathname += '/'; + } + baseURLObject.pathname += relativeEndpoint; - return url.toString() + return baseURLObject.toString(); } public async getRaw( From 84c5001d9a613d3c90c2d0e041a31cdf1b7fad96 Mon Sep 17 00:00:00 2001 From: shak58 <150069539+shak58@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:15:43 +0000 Subject: [PATCH 2/4] test: handles baseURL with path and endpoint without leading slash --- src/client/rest/APIRequester.spec.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/client/rest/APIRequester.spec.ts b/src/client/rest/APIRequester.spec.ts index f951695..7b3c7bb 100644 --- a/src/client/rest/APIRequester.spec.ts +++ b/src/client/rest/APIRequester.spec.ts @@ -54,4 +54,19 @@ describe('APIRequester', () => { } ) }) + + it('handles baseURL with path and endpoint without leading slash', async () => { + mockedAxios.get.mockResolvedValueOnce({ data: null }); + + const request = new APIRequester('https://rest.testnet.initia.xyz/bar'); + await request.get('foo'); + + expect(mockedAxios.get).toHaveBeenCalledWith( + 'https://rest.testnet.initia.xyz/foo', + { + headers: new axios.AxiosHeaders(), + params: {}, + } + ); + }); }) From 3dde97331ac98ebccd2a71570bb9652aa7be3f15 Mon Sep 17 00:00:00 2001 From: shak58 <150069539+shak58@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:19:41 +0000 Subject: [PATCH 3/4] spaces --- src/client/rest/APIRequester.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/client/rest/APIRequester.ts b/src/client/rest/APIRequester.ts index 305638e..f01dcee 100644 --- a/src/client/rest/APIRequester.ts +++ b/src/client/rest/APIRequester.ts @@ -52,10 +52,10 @@ export class APIRequester { } private computeEndpoint(endpoint: string) { - const relativeEndpoint = endpoint.replace(/^\/+/, ''); - const baseURLObject = new URL(this.baseURL); + const relativeEndpoint = endpoint.replace(/^\/+/, ''); + const baseURLObject = new URL(this.baseURL); - if (!baseURLObject.pathname.endsWith('/')) { + if (!baseURLObject.pathname.endsWith('/')) { baseURLObject.pathname += '/'; } baseURLObject.pathname += relativeEndpoint; From 07e873cac53b6ae26eac49a60a1f621cae8e052c Mon Sep 17 00:00:00 2001 From: shak58 <150069539+shak58@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:24:32 +0000 Subject: [PATCH 4/4] fix --- src/client/rest/APIRequester.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client/rest/APIRequester.spec.ts b/src/client/rest/APIRequester.spec.ts index 7b3c7bb..ebb350d 100644 --- a/src/client/rest/APIRequester.spec.ts +++ b/src/client/rest/APIRequester.spec.ts @@ -62,7 +62,7 @@ describe('APIRequester', () => { await request.get('foo'); expect(mockedAxios.get).toHaveBeenCalledWith( - 'https://rest.testnet.initia.xyz/foo', + 'https://rest.testnet.initia.xyz/bar/foo', { headers: new axios.AxiosHeaders(), params: {},