Skip to content

Commit 3c528b4

Browse files
chore(internal): move stringifyQuery implementation to internal function
1 parent e1a34c1 commit 3c528b4

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';
@@ -248,21 +249,8 @@ export class ScanDocuments {
248249
/**
249250
* Basic re-implementation of `qs.stringify` for primitive types.
250251
*/
251-
protected stringifyQuery(query: Record<string, unknown>): string {
252-
return Object.entries(query)
253-
.filter(([_, value]) => typeof value !== 'undefined')
254-
.map(([key, value]) => {
255-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
256-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
257-
}
258-
if (value === null) {
259-
return `${encodeURIComponent(key)}=`;
260-
}
261-
throw new Errors.ScanDocumentsError(
262-
`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.`,
263-
);
264-
})
265-
.join('&');
252+
protected stringifyQuery(query: object | Record<string, unknown>): string {
253+
return stringifyQuery(query);
266254
}
267255

268256
private getUserAgent(): string {
@@ -299,7 +287,7 @@ export class ScanDocuments {
299287
}
300288

301289
if (typeof query === 'object' && query && !Array.isArray(query)) {
302-
url.search = this.stringifyQuery(query as Record<string, unknown>);
290+
url.search = this.stringifyQuery(query);
303291
}
304292

305293
return url.toString();
@@ -738,7 +726,7 @@ export class ScanDocuments {
738726
) {
739727
return {
740728
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
741-
body: this.stringifyQuery(body as Record<string, unknown>),
729+
body: this.stringifyQuery(body),
742730
};
743731
} else {
744732
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 { ScanDocumentsError } 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 ScanDocumentsError(
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 { ScanDocuments } from 'scan-documents';
4-
5-
const { stringifyQuery } = ScanDocuments.prototype as any;
3+
import { stringifyQuery } from 'scan-documents/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)