Skip to content

Commit 7048aa6

Browse files
chore(internal): move stringifyQuery implementation to internal function
1 parent 257f07b commit 7048aa6

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 Uploads from './core/uploads';
@@ -256,21 +257,8 @@ export class CasParser {
256257
/**
257258
* Basic re-implementation of `qs.stringify` for primitive types.
258259
*/
259-
protected stringifyQuery(query: Record<string, unknown>): string {
260-
return Object.entries(query)
261-
.filter(([_, value]) => typeof value !== 'undefined')
262-
.map(([key, value]) => {
263-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
264-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
265-
}
266-
if (value === null) {
267-
return `${encodeURIComponent(key)}=`;
268-
}
269-
throw new Errors.CasParserError(
270-
`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.`,
271-
);
272-
})
273-
.join('&');
260+
protected stringifyQuery(query: object | Record<string, unknown>): string {
261+
return stringifyQuery(query);
274262
}
275263

276264
private getUserAgent(): string {
@@ -307,7 +295,7 @@ export class CasParser {
307295
}
308296

309297
if (typeof query === 'object' && query && !Array.isArray(query)) {
310-
url.search = this.stringifyQuery(query as Record<string, unknown>);
298+
url.search = this.stringifyQuery(query);
311299
}
312300

313301
return url.toString();
@@ -746,7 +734,7 @@ export class CasParser {
746734
) {
747735
return {
748736
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
749-
body: this.stringifyQuery(body as Record<string, unknown>),
737+
body: this.stringifyQuery(body),
750738
};
751739
} else {
752740
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 { CasParserError } 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 CasParserError(
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 { CasParser } from 'cas-parser-node';
4-
5-
const { stringifyQuery } = CasParser.prototype as any;
3+
import { stringifyQuery } from 'cas-parser-node/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)