Skip to content

Commit

Permalink
Merge pull request #97 from shak58/patch-1
Browse files Browse the repository at this point in the history
Fix computeEndpoint method
  • Loading branch information
joon9823 authored Nov 27, 2024
2 parents 86d0d19 + 07e873c commit f247aee
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
15 changes: 15 additions & 0 deletions src/client/rest/APIRequester.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/bar/foo',
{
headers: new axios.AxiosHeaders(),
params: {},
}
);
});
})
12 changes: 7 additions & 5 deletions src/client/rest/APIRequester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Expand Down

0 comments on commit f247aee

Please sign in to comment.