Skip to content

Commit a3568f1

Browse files
committed
fix: stricter type checking on Body and URLSearchParams
1 parent 7119854 commit a3568f1

3 files changed

Lines changed: 18 additions & 5 deletions

File tree

packages/requestish/src/Body.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { isIterable, isRecord } from '@battis/typescript-tricks';
12
import type { FetchBody } from 'openid-client';
23
import {
34
from as URLSearchParams_from,
45
ish as URLSearchParams_ish
56
} from './URLSearchParams.js';
6-
import { isJSONEntries, isJSONRecord } from './is.js';
77

88
export type ish =
99
| FetchBody
@@ -22,9 +22,21 @@ export async function from(body: ish): Promise<FetchBody | undefined> {
2222
body instanceof URLSearchParams
2323
) {
2424
return body;
25-
} else if (isJSONRecord(body)) {
26-
return URLSearchParams_from(body);
27-
} else if (isJSONEntries(body)) {
25+
} else if (
26+
isRecord(body) ||
27+
Array.isArray(body) ||
28+
isIterable<[string, string]>(
29+
body,
30+
(elt: unknown): elt is [string, string] => {
31+
return (
32+
Array.isArray(elt) &&
33+
elt.length == 2 &&
34+
typeof elt[0] === 'string' &&
35+
typeof elt[1] === 'string'
36+
);
37+
}
38+
)
39+
) {
2840
return URLSearchParams_from(body);
2941
}
3042
return new Response(body).arrayBuffer();

packages/requestish/src/URLSearchParams.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function from(search?: ish): URLSearchParams {
4343
);
4444
} else if (isJSONEntries(search)) {
4545
return new URLSearchParams(
46-
search.map(([key, value]) => [key, String.from(value)])
46+
search.map(([key, value]): [string, string] => [key, String.from(value)])
4747
);
4848
}
4949
return new URLSearchParams(search);

packages/requestish/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"extends": "@tsconfig/recommended/tsconfig.json",
33
"compilerOptions": {
44
"declaration": true,
5+
"lib": ["ES2019"],
56
"module": "nodenext",
67
"moduleResolution": "nodenext",
78
"outDir": "./dist"

0 commit comments

Comments
 (0)