-
-
Notifications
You must be signed in to change notification settings - Fork 583
fix: support async generators as response resolvers #2108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
991a7b4
test: add generator resolver tests
kettanaito f324d33
fix: support async generator response resolvers
kettanaito baf112e
fix: accept AsyncGenerator as response resolver type
kettanaito a6a29bc
Merge branch 'main' into fix/generator-resolver
kettanaito 11d3719
fix(RequestHandler): mark as used early, opt-out in generators
kettanaito aac734a
fix(RequestHandler): unwrap AsyncGenerator generic from MaybePromise
kettanaito 8e26859
test: add return type tests for generators
kettanaito 180b722
chore: revert MaybeAsync to AsyncGenerator generic
kettanaito 8053895
Merge branch 'main' into fix/generator-resolver
kettanaito f815d58
Merge branch 'main' into fix/generator-resolver
kettanaito 3d6d083
chore: print tsconfig used in type tests
kettanaito 93da9ec
Merge branch 'main' into fix/generator-resolver
kettanaito 63fdd4b
fix: use iterables instead of generators (#2213)
jakebailey 9f75946
test: add type test for resolver return type
kettanaito 5c76326
test(generator): simplify fetch assertions
kettanaito 3325ae1
test(types): import from build, not source
kettanaito File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,32 @@ | ||
| /** | ||
| * This is the same as TypeScript's `Iterable`, but with all three type parameters. | ||
| * @todo Remove once TypeScript 5.6 is the minimum. | ||
| */ | ||
| export interface Iterable<T, TReturn, TNext> { | ||
| [Symbol.iterator](): Iterator<T, TReturn, TNext> | ||
| } | ||
|
|
||
| /** | ||
| * This is the same as TypeScript's `AsyncIterable`, but with all three type parameters. | ||
| * @todo Remove once TypeScript 5.6 is the minimum. | ||
| */ | ||
| export interface AsyncIterable<T, TReturn, TNext> { | ||
| [Symbol.asyncIterator](): AsyncIterator<T, TReturn, TNext> | ||
| } | ||
|
|
||
| /** | ||
| * Determines if the given function is an iterator. | ||
| */ | ||
| export function isIterable<IteratorType>( | ||
| fn: any, | ||
| ): fn is Generator<IteratorType, IteratorType, IteratorType> { | ||
| ): fn is | ||
| | Iterable<IteratorType, IteratorType, IteratorType> | ||
| | AsyncIterable<IteratorType, IteratorType, IteratorType> { | ||
| if (!fn) { | ||
| return false | ||
| } | ||
|
|
||
| return typeof (fn as Generator<unknown>)[Symbol.iterator] == 'function' | ||
| return ( | ||
| Reflect.has(fn, Symbol.iterator) || Reflect.has(fn, Symbol.asyncIterator) | ||
| ) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| /** | ||
| * @vitest-environment node | ||
| */ | ||
| import { http, HttpResponse, delay } from 'msw' | ||
| import { setupServer } from 'msw/node' | ||
|
|
||
| const server = setupServer() | ||
|
|
||
| async function fetchJson(input: string | URL | Request, init?: RequestInit) { | ||
| return fetch(input, init).then((response) => response.json()) | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| server.listen() | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| server.resetHandlers() | ||
| }) | ||
|
|
||
| afterAll(() => { | ||
| server.close() | ||
| }) | ||
|
|
||
| it('supports generator function as response resolver', async () => { | ||
| server.use( | ||
| http.get('https://example.com/weather', function* () { | ||
| let degree = 10 | ||
|
|
||
| while (degree < 13) { | ||
| degree++ | ||
| yield HttpResponse.json(degree) | ||
| } | ||
|
|
||
| degree++ | ||
| return HttpResponse.json(degree) | ||
| }), | ||
| ) | ||
|
|
||
| // Must respond with yielded responses. | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(11) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(12) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(13) | ||
| // Must respond with the final "done" response. | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
| // Must keep responding with the final "done" response. | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
| }) | ||
|
|
||
| it('supports async generator function as response resolver', async () => { | ||
| server.use( | ||
| http.get('https://example.com/weather', async function* () { | ||
| await delay(20) | ||
|
|
||
| let degree = 10 | ||
|
|
||
| while (degree < 13) { | ||
| degree++ | ||
| yield HttpResponse.json(degree) | ||
| } | ||
|
|
||
| degree++ | ||
| return HttpResponse.json(degree) | ||
| }), | ||
| ) | ||
|
|
||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(11) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(12) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(13) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
| }) | ||
|
|
||
| it('supports generator function as one-time response resolver', async () => { | ||
| server.use( | ||
| http.get( | ||
| 'https://example.com/weather', | ||
| function* () { | ||
| let degree = 10 | ||
|
|
||
| while (degree < 13) { | ||
| degree++ | ||
| yield HttpResponse.json(degree) | ||
| } | ||
|
|
||
| degree++ | ||
| return HttpResponse.json(degree) | ||
| }, | ||
| { once: true }, | ||
| ), | ||
| http.get('*', () => { | ||
| return HttpResponse.json('fallback') | ||
| }), | ||
| ) | ||
|
|
||
| // Must respond with the yielded incrementing responses. | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(11) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(12) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(13) | ||
| // Must respond with the "done" final response from the iterator. | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual(14) | ||
| // Must respond with the other handler since the generator one is used. | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual( | ||
| 'fallback', | ||
| ) | ||
| await expect(fetchJson('https://example.com/weather')).resolves.toEqual( | ||
| 'fallback', | ||
| ) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { it } from 'vitest' | ||
| import { http, HttpResponse } from 'msw' | ||
|
|
||
| it('supports generator function as response resolver', () => { | ||
| http.get<never, never, { value: number }>('/', function* () { | ||
| yield HttpResponse.json({ value: 1 }) | ||
| yield HttpResponse.json({ value: 2 }) | ||
| return HttpResponse.json({ value: 3 }) | ||
| }) | ||
|
|
||
| http.get<never, never, { value: string }>('/', function* () { | ||
| yield HttpResponse.json({ value: 'one' }) | ||
| yield HttpResponse.json({ | ||
| // @ts-expect-error Expected string, got number. | ||
| value: 2, | ||
| }) | ||
| return HttpResponse.json({ value: 'three' }) | ||
| }) | ||
| }) | ||
|
|
||
| it('supports async generator function as response resolver', () => { | ||
| http.get<never, never, { value: number }>('/', async function* () { | ||
| yield HttpResponse.json({ value: 1 }) | ||
| yield HttpResponse.json({ value: 2 }) | ||
| return HttpResponse.json({ value: 3 }) | ||
| }) | ||
|
|
||
| http.get<never, never, { value: string }>('/', async function* () { | ||
| yield HttpResponse.json({ value: 'one' }) | ||
| yield HttpResponse.json({ | ||
| // @ts-expect-error Expected string, got number. | ||
| value: 2, | ||
| }) | ||
| return HttpResponse.json({ value: 'three' }) | ||
| }) | ||
| }) | ||
|
|
||
| it('supports returning nothing from generator resolvers', () => { | ||
| http.get<never, never, { value: string }>('/', function* () {}) | ||
| http.get<never, never, { value: string }>('/', async function* () {}) | ||
| }) | ||
|
|
||
| it('supports returning undefined from generator resolvers', () => { | ||
| http.get<never, never, { value: string }>('/', function* () { | ||
| return undefined | ||
| }) | ||
| http.get<never, never, { value: string }>('/', async function* () { | ||
| return undefined | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's important to mark the handler as used immediately, even if using generators then opts-out from this behavior. This is what we promise right now, so let's keep that promise.