Skip to content

Commit 0f11898

Browse files
committed
feat: allow undefined values in URLSearchParams.ish (treated as '')
1 parent e7a4457 commit 0f11898

1 file changed

Lines changed: 29 additions & 5 deletions

File tree

packages/requestish/src/URLSearchParams.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,45 @@ import { isJSONPrimitive, isString } from './isRecord.js';
55
export type ish =
66
| URLSearchParams
77
| ConstructorParameters<typeof URLSearchParams>[0]
8-
| Record<string, JSONPrimitive>;
8+
| Record<string, JSONPrimitive | undefined>
9+
| [string, JSONPrimitive | undefined][];
910

1011
export function from(search?: ish): URLSearchParams {
1112
if (search instanceof URLSearchParams) {
1213
return search;
1314
} else if (
14-
isRecord<string, JSONPrimitive>(search, isString, isJSONPrimitive)
15+
search &&
16+
isRecord<string, JSONPrimitive | undefined>(
17+
search,
18+
isString,
19+
isJSONPrimitive
20+
)
1521
) {
1622
return new URLSearchParams(
1723
Object.fromEntries(
18-
Object.entries(search || {})
19-
.filter(([_, value]) => value !== undefined)
20-
.map(([key, value]) => [key, value?.toString() || ''])
24+
Object.entries(search)
25+
.filter(([__dirname, value]) => value !== undefined)
26+
.map(([key, value]) => [
27+
key,
28+
typeof value === 'string'
29+
? value
30+
: value?.toString() || JSON.stringify(value)
31+
])
2132
)
2233
);
34+
} else if (Array.isArray(search)) {
35+
return new URLSearchParams(
36+
search.map(([key, value]) => [
37+
key,
38+
typeof value === 'string'
39+
? value
40+
: value === null
41+
? 'null'
42+
: value === undefined
43+
? ''
44+
: value.toString()
45+
])
46+
);
2347
}
2448
return new URLSearchParams(search);
2549
}

0 commit comments

Comments
 (0)