Skip to content

Commit 8222503

Browse files
chore(internal): move stringifyQuery implementation to internal function
1 parent 34e8eea commit 8222503

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
@@ -12,6 +12,7 @@ import type { APIResponseProps } from './internal/parse';
1212
import { getPlatformHeaders } from './internal/detect-platform';
1313
import * as Shims from './internal/shims';
1414
import * as Opts from './internal/request-options';
15+
import { stringifyQuery } from './internal/utils/query';
1516
import { VERSION } from './version';
1617
import * as Errors from './core/error';
1718
import * as Uploads from './core/uploads';
@@ -305,21 +306,8 @@ export class SentDm {
305306
/**
306307
* Basic re-implementation of `qs.stringify` for primitive types.
307308
*/
308-
protected stringifyQuery(query: Record<string, unknown>): string {
309-
return Object.entries(query)
310-
.filter(([_, value]) => typeof value !== 'undefined')
311-
.map(([key, value]) => {
312-
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
313-
return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
314-
}
315-
if (value === null) {
316-
return `${encodeURIComponent(key)}=`;
317-
}
318-
throw new Errors.SentDmError(
319-
`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.`,
320-
);
321-
})
322-
.join('&');
309+
protected stringifyQuery(query: object | Record<string, unknown>): string {
310+
return stringifyQuery(query);
323311
}
324312

325313
private getUserAgent(): string {
@@ -356,7 +344,7 @@ export class SentDm {
356344
}
357345

358346
if (typeof query === 'object' && query && !Array.isArray(query)) {
359-
url.search = this.stringifyQuery(query as Record<string, unknown>);
347+
url.search = this.stringifyQuery(query);
360348
}
361349

362350
return url.toString();
@@ -795,7 +783,7 @@ export class SentDm {
795783
) {
796784
return {
797785
bodyHeaders: { 'content-type': 'application/x-www-form-urlencoded' },
798-
body: this.stringifyQuery(body as Record<string, unknown>),
786+
body: this.stringifyQuery(body),
799787
};
800788
} else {
801789
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 { SentDmError } 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 SentDmError(
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 { SentDm } from '@sentdm/sentdm';
4-
5-
const { stringifyQuery } = SentDm.prototype as any;
3+
import { stringifyQuery } from '@sentdm/sentdm/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)