Skip to content

Commit 60f7ea6

Browse files
chore(internal): move stringifyQuery implementation to internal function
1 parent 81ec737 commit 60f7ea6

4 files changed

Lines changed: 31 additions & 21 deletions

File tree

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 Uploads from './core/uploads';
@@ -323,21 +324,8 @@ export class ImageKit {
323324
/**
324325
* Basic re-implementation of `qs.stringify` for primitive types.
325326
*/
326-
protected stringifyQuery(query: Record<string, unknown>): string {
327-
return Object.entries(query)
328-
.filter(([_, value]) => typeof value !== 'undefined')
329-
.map(([key, value]) => {
330-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
331-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
332-
}
333-
if (value === null) {
334-
return `${encodeURIComponent(key)}=`;
335-
}
336-
throw new Errors.ImageKitError(
337-
`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.`,
338-
);
339-
})
340-
.join('&');
327+
protected stringifyQuery(query: object | Record<string, unknown>): string {
328+
return stringifyQuery(query);
341329
}
342330

343331
private getUserAgent(): string {
@@ -374,7 +362,7 @@ export class ImageKit {
374362
}
375363

376364
if (typeof query === 'object' && query && !Array.isArray(query)) {
377-
url.search = this.stringifyQuery(query as Record<string, unknown>);
365+
url.search = this.stringifyQuery(query);
378366
}
379367

380368
return url.toString();
@@ -813,7 +801,7 @@ export class ImageKit {
813801
) {
814802
return {
815803
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
816-
body: this.stringifyQuery(body as Record<string, unknown>),
804+
body: this.stringifyQuery(body),
817805
};
818806
} else {
819807
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 { ImageKitError } 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 ImageKitError(
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 { ImageKit } from '@imagekit/nodejs';
4-
5-
const { stringifyQuery } = ImageKit.prototype as any;
3+
import { stringifyQuery } from '@imagekit/nodejs/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)