-
-
Notifications
You must be signed in to change notification settings - Fork 518
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
feat(HttpResponse): support explicitly empty response body via null generic type #2118
Open
kettanaito
wants to merge
5
commits into
main
Choose a base branch
from
feat/http-response-empty-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3b5c2e1
feat(HttpResponse): support explicitly empty response body via null g…
kettanaito 0428383
fix(RequestHandler): remove XMLHttpRequestInit types
kettanaito f409490
fix(HttpResponse): define "bodyType" symbol
kettanaito 2ea31c3
Merge branch 'main' into feat/http-response-empty-type
kettanaito a18f0cb
Merge branch 'main' into feat/http-response-empty-type
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 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 |
---|---|---|
|
@@ -11,16 +11,14 @@ export interface HttpResponseInit extends ResponseInit { | |
|
||
declare const bodyType: unique symbol | ||
|
||
export interface StrictRequest<BodyType extends DefaultBodyType> | ||
extends Request { | ||
export interface StrictRequest<BodyType extends JsonBodyType> extends Request { | ||
json(): Promise<BodyType> | ||
} | ||
|
||
/** | ||
* Opaque `Response` type that supports strict body type. | ||
*/ | ||
export interface StrictResponse<BodyType extends DefaultBodyType> | ||
extends Response { | ||
interface StrictResponse<BodyType extends DefaultBodyType> extends Response { | ||
readonly [bodyType]: BodyType | ||
} | ||
|
||
|
@@ -35,10 +33,15 @@ export interface StrictResponse<BodyType extends DefaultBodyType> | |
* | ||
* @see {@link https://mswjs.io/docs/api/http-response `HttpResponse` API reference} | ||
*/ | ||
export class HttpResponse extends Response { | ||
constructor(body?: BodyInit | null, init?: HttpResponseInit) { | ||
export class HttpResponse<BodyType extends DefaultBodyType> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The trick is this:
This makes responses constructed and created via static methods to have the same type validation—on the body argument type level, not the return type level of the response resolver. |
||
extends Response | ||
implements StrictResponse<BodyType> | ||
{ | ||
[bodyType]: BodyType = null as any | ||
|
||
constructor(body?: NoInfer<BodyType> | null, init?: HttpResponseInit) { | ||
const responseInit = normalizeResponseInit(init) | ||
super(body, responseInit) | ||
super(body as BodyInit, responseInit) | ||
decorateResponse(this, responseInit) | ||
} | ||
|
||
|
@@ -51,7 +54,7 @@ export class HttpResponse extends Response { | |
static text<BodyType extends string>( | ||
body?: NoInfer<BodyType> | null, | ||
init?: HttpResponseInit, | ||
): StrictResponse<BodyType> { | ||
): HttpResponse<BodyType> { | ||
const responseInit = normalizeResponseInit(init) | ||
|
||
if (!responseInit.headers.has('Content-Type')) { | ||
|
@@ -68,7 +71,7 @@ export class HttpResponse extends Response { | |
) | ||
} | ||
|
||
return new HttpResponse(body, responseInit) as StrictResponse<BodyType> | ||
return new HttpResponse(body, responseInit) | ||
} | ||
|
||
/** | ||
|
@@ -78,9 +81,9 @@ export class HttpResponse extends Response { | |
* HttpResponse.json({ error: 'Not Authorized' }, { status: 401 }) | ||
*/ | ||
static json<BodyType extends JsonBodyType>( | ||
body?: NoInfer<BodyType> | null, | ||
body?: NoInfer<BodyType> | null | undefined, | ||
init?: HttpResponseInit, | ||
): StrictResponse<BodyType> { | ||
): HttpResponse<BodyType> { | ||
const responseInit = normalizeResponseInit(init) | ||
|
||
if (!responseInit.headers.has('Content-Type')) { | ||
|
@@ -100,10 +103,7 @@ export class HttpResponse extends Response { | |
) | ||
} | ||
|
||
return new HttpResponse( | ||
responseText, | ||
responseInit, | ||
) as StrictResponse<BodyType> | ||
return new HttpResponse(responseText as BodyType, responseInit) | ||
} | ||
|
||
/** | ||
|
This file contains 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 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 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
Oops, something went wrong.
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.
Technically,
StrictRequest
only annotates the JSON body reading method so it cannot be of any other type than theJsonBodyType
. As in, it cannot suddenly return a stream.