Skip to content

Commit 6c5754b

Browse files
chore(internal): move stringifyQuery implementation to internal function
1 parent e87a3a8 commit 6c5754b

File tree

4 files changed

+31
-21
lines changed

4 files changed

+31
-21
lines changed

src/client.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type { APIResponseProps } from './internal/parse';
1111
import { getPlatformHeaders } from './internal/detect-platform';
1212
import * as Shims from './internal/shims';
1313
import * as Opts from './internal/request-options';
14+
import { stringifyQuery } from './internal/utils/query';
1415
import { VERSION } from './version';
1516
import * as Errors from './core/error';
1617
import * as Pagination from './core/pagination';
@@ -348,21 +349,8 @@ export class Kernel {
348349
/**
349350
* Basic re-implementation of `qs.stringify` for primitive types.
350351
*/
351-
protected stringifyQuery(query: Record<string, unknown>): string {
352-
return Object.entries(query)
353-
.filter(([_, value]) => typeof value !== 'undefined')
354-
.map(([key, value]) => {
355-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
356-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
357-
}
358-
if (value === null) {
359-
return `${encodeURIComponent(key)}=`;
360-
}
361-
throw new Errors.KernelError(
362-
`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.`,
363-
);
364-
})
365-
.join('&');
352+
protected stringifyQuery(query: object | Record<string, unknown>): string {
353+
return stringifyQuery(query);
366354
}
367355

368356
private getUserAgent(): string {
@@ -399,7 +387,7 @@ export class Kernel {
399387
}
400388

401389
if (typeof query === 'object' && query && !Array.isArray(query)) {
402-
url.search = this.stringifyQuery(query as Record<string, unknown>);
390+
url.search = this.stringifyQuery(query);
403391
}
404392

405393
return url.toString();
@@ -862,7 +850,7 @@ export class Kernel {
862850
) {
863851
return {
864852
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
865-
body: this.stringifyQuery(body as Record<string, unknown>),
853+
body: this.stringifyQuery(body),
866854
};
867855
} else {
868856
return this.#encoder({ body, headers });

src/internal/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './utils/env';
66
export * from './utils/log';
77
export * from './utils/uuid';
88
export * from './utils/sleep';
9+
export * from './utils/query';

src/internal/utils/query.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
import { KernelError } from '../../core/error';
4+
5+
/**
6+
* Basic re-implementation of `qs.stringify` for primitive types.
7+
*/
8+
export function stringifyQuery(query: object | Record<string, unknown>) {
9+
return Object.entries(query)
10+
.filter(([_, value]) => typeof value !== 'undefined')
11+
.map(([key, value]) => {
12+
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
13+
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
14+
}
15+
if (value === null) {
16+
return `${encodeURIComponent(key)}=`;
17+
}
18+
throw new KernelError(
19+
`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.`,
20+
);
21+
})
22+
.join('&');
23+
}

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 { Kernel } from '@onkernel/sdk';
4-
5-
const { stringifyQuery } = Kernel.prototype as any;
3+
import { stringifyQuery } from '@onkernel/sdk/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)