From 65c84b8a4edc8fc5a744f51334573805f0669e64 Mon Sep 17 00:00:00 2001 From: Saleh Alghusson <1331721+qirh@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:49:02 +0000 Subject: [PATCH] fix: preserve base URL query parameters when joining paths --- src/client.ts | 9 +++++++-- src/index.test.ts | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/client.ts b/src/client.ts index 6437da4..6bbd98a 100644 --- a/src/client.ts +++ b/src/client.ts @@ -256,12 +256,17 @@ export class Perplexity implements SdkTransport { defaultBaseURL?: string | undefined, ): string { const baseURL = (!this.#baseURLOverridden() && defaultBaseURL) || this.baseURL; + // Join paths without the base query so it cannot absorb the request path. + const baseQueryIndex = baseURL.indexOf('?'); + const basePath = baseQueryIndex === -1 ? baseURL : baseURL.slice(0, baseQueryIndex); + const baseQuery = + isAbsoluteURL(path) || baseQueryIndex === -1 ? {} : Object.fromEntries(new URL(baseURL).searchParams); const url = isAbsoluteURL(path) ? new URL(path) - : new URL(baseURL + (baseURL.endsWith('/') && path.startsWith('/') ? path.slice(1) : path)); + : new URL(basePath + (basePath.endsWith('/') && path.startsWith('/') ? path.slice(1) : path)); const defaultQuery = this.defaultQuery(); - const pathQuery = Object.fromEntries(url.searchParams); + const pathQuery = { ...baseQuery, ...Object.fromEntries(url.searchParams) }; if (!isEmptyObj(defaultQuery) || !isEmptyObj(pathQuery)) { query = { ...pathQuery, ...defaultQuery, ...query }; } diff --git a/src/index.test.ts b/src/index.test.ts index 06bc586..54e6e70 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -402,6 +402,20 @@ describe('instantiate client', () => { assert.deepStrictEqual(client.buildURL('/foo', null), 'http://localhost:5000/custom/path/foo'); }); + it('query parameters', () => { + for (const baseURL of [ + 'http://localhost:5000/custom/path?key=x', + 'http://localhost:5000/custom/path/?key=x', + ]) { + const client = new Perplexity({ baseURL, apiKey: 'My API Key' }); + assert.deepStrictEqual(client.buildURL('/foo', null), 'http://localhost:5000/custom/path/foo?key=x'); + assert.deepStrictEqual( + client.buildURL('/foo?bar=1', { key: 'y', baz: 2 }), + 'http://localhost:5000/custom/path/foo?key=y&bar=1&baz=2', + ); + } + }); + afterEach(() => { process.env['PERPLEXITY_BASE_URL'] = undefined; });