Skip to content

Commit 7dc7f81

Browse files
committed
refactor: improve types again, add body prop
1 parent c6bcd25 commit 7dc7f81

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

addon/src/wait-for-fetch.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,33 @@
11
import { default as waitForPromise } from './wait-for-promise.ts';
22

3-
const properties = [
4-
'ok',
5-
'status',
6-
'statusText',
3+
const props = [
4+
'body',
75
'bodyUsed',
86
'headers',
7+
'ok',
98
'redirected',
9+
'status',
10+
'statusText',
1011
'type',
1112
'url',
1213
] as const satisfies (keyof Response)[];
13-
type ResponseProp = (typeof properties)[number];
14+
type ResponseProp = (typeof props)[number];
1415
function isResponseProperty(maybeProp: string): maybeProp is ResponseProp {
15-
return properties.some((prop) => maybeProp === prop);
16+
return props.some((prop) => maybeProp === prop);
17+
}
18+
19+
const fns = [
20+
'arrayBuffer',
21+
'blob',
22+
'bytes',
23+
'clone',
24+
'formData',
25+
'json',
26+
'text',
27+
] as const satisfies (keyof Response)[];
28+
type ResponseFn = (typeof fns)[number];
29+
function isResponseFn(maybeFn: string): maybeFn is ResponseFn {
30+
return fns.some((fn) => maybeFn === fn);
1631
}
1732

1833
/**
@@ -31,21 +46,19 @@ export async function waitForFetch(fetchPromise: ReturnType<typeof fetch>) {
3146
}
3247
const original = Reflect.get(target, prop, receiver);
3348

34-
// For async functions, call them but wrapped in waitForPromise
35-
if (
36-
typeof prop === 'string' &&
37-
['json', 'text', 'arrayBuffer', 'blob', 'formData', 'bytes'].includes(prop)
38-
) {
49+
// Wrap Response functions in test waiter
50+
if (typeof prop === 'string' && isResponseFn(prop)) {
51+
// clone() is sync, no need to wrap in test-waiter
52+
if (prop === 'clone') {
53+
return (...args: unknown[]) => {
54+
return original.call(target, ...args);
55+
};
56+
}
3957
return (...args: unknown[]) => {
4058
return waitForPromise(original.call(target, ...args));
4159
};
4260
}
43-
// For sync functions, just call them
44-
if (typeof prop === 'string' && ['clone'].includes(prop)) {
45-
return (...args: unknown[]) => {
46-
return original.call(target, ...args);
47-
};
48-
}
61+
// return the Reflect.get() result for anything else
4962
return original;
5063
},
5164
});

0 commit comments

Comments
 (0)