Skip to content

Commit 6e1c564

Browse files
chore(internal): move stringifyQuery implementation to internal function
1 parent 193076b commit 6e1c564

4 files changed

Lines changed: 16 additions & 23 deletions

File tree

src/core.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
APIConnectionTimeoutError,
77
APIUserAbortError,
88
} from './error';
9+
import { stringifyQuery } from './internal/utils/query';
910
import {
1011
kind as shimsKind,
1112
type Readable,
@@ -528,27 +529,14 @@ export abstract class APIClient {
528529
}
529530

530531
if (typeof query === 'object' && query && !Array.isArray(query)) {
531-
url.search = this.stringifyQuery(query as Record<string, unknown>);
532+
url.search = this.stringifyQuery(query);
532533
}
533534

534535
return url.toString();
535536
}
536537

537-
protected stringifyQuery(query: Record<string, unknown>): string {
538-
return Object.entries(query)
539-
.filter(([_, value]) => typeof value !== 'undefined')
540-
.map(([key, value]) => {
541-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
542-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
543-
}
544-
if (value === null) {
545-
return `${encodeURIComponent(key)}=`;
546-
}
547-
throw new ContextualAIError(
548-
`Cannot stringify type ${typeof value}; Expected string, number, boolean, or null. If you need to pass nested query parameters, you can manually encode them, e.g. { query: { 'foo[key1]': value1, 'foo[key2]': value2 } }, and please open a GitHub issue requesting better support for your use case.`,
549-
);
550-
})
551-
.join('&');
538+
protected stringifyQuery(query: object | Record<string, unknown>): string {
539+
return stringifyQuery(query);
552540
}
553541

554542
async fetchWithTimeout(

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

33
import { type Agent } from './_shims/index';
4-
import * as qs from './internal/qs';
4+
import { stringifyQuery } from './internal/utils/query';
55
import * as Core from './core';
66
import * as Errors from './error';
77
import * as Pagination from './pagination';
@@ -235,8 +235,8 @@ export class ContextualAI extends Core.APIClient {
235235
return { Authorization: `Bearer ${this.apiKey}` };
236236
}
237237

238-
protected override stringifyQuery(query: Record<string, unknown>): string {
239-
return qs.stringify(query, { arrayFormat: 'repeat' });
238+
protected override stringifyQuery(query: object | Record<string, unknown>): string {
239+
return stringifyQuery(query);
240240
}
241241

242242
static ContextualAI = this;

src/internal/utils/query.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import * as qs from '../qs/stringify';
4+
5+
export function stringifyQuery(query: object | Record<string, unknown>) {
6+
return qs.stringify(query, { arrayFormat: 'repeat' });
7+
}

tests/stringifyQuery.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
22

3-
import { ContextualAI } from 'contextual-client';
4-
5-
const { stringifyQuery } = ContextualAI.prototype as any;
3+
import { stringifyQuery } from 'contextual-client/internal/utils/query';
64

75
describe(stringifyQuery, () => {
86
for (const [input, expected] of [
@@ -15,7 +13,7 @@ describe(stringifyQuery, () => {
1513
'e=f',
1614
)}=${encodeURIComponent('g&h')}`,
1715
],
18-
]) {
16+
] as const) {
1917
it(`${JSON.stringify(input)} -> ${expected}`, () => {
2018
expect(stringifyQuery(input)).toEqual(expected);
2119
});

0 commit comments

Comments
 (0)