Skip to content

Commit bd0bdb7

Browse files
committed
1 parent 7bf3356 commit bd0bdb7

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

addon/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ export {
2020
export { default as buildWaiter, _resetWaiterNames } from './build-waiter.ts';
2121
export { default as waitForPromise } from './wait-for-promise.ts';
2222
export { default as waitFor } from './wait-for.ts';
23+
export { waitForFetch } from './wait-for-fetch.ts';

addon/src/wait-for-fetch.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { default as waitForPromise } from './wait-for-promise';
2+
3+
export async function waitForFetch(fetchPromise: ReturnType<typeof fetch>) {
4+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
5+
waitForPromise(fetchPromise);
6+
7+
const response = await fetchPromise;
8+
9+
return new Proxy(response, {
10+
get(target, prop, receiver) {
11+
const original = Reflect.get(target, prop, receiver);
12+
13+
if (
14+
typeof prop === 'string' &&
15+
['json', 'text', 'arrayBuffer', 'blob', 'formData'].includes(prop)
16+
) {
17+
return (...args: unknown[]) => {
18+
const parsePromise = original.call(target, ...args);
19+
20+
return waitForPromise(parsePromise);
21+
};
22+
}
23+
24+
return original;
25+
},
26+
});
27+
}

0 commit comments

Comments
 (0)