Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}
Expand Down
14 changes: 14 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
Loading