-
-
Notifications
You must be signed in to change notification settings - Fork 517
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
base: main
Are you sure you want to change the base?
Conversation
kettanaito
commented
Mar 30, 2024
- Closes feat(HttpResponse): add empty response helper for strict empty responses #1807
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
The trick is this:
implements StrictResponse<T>
- All static methods return
HttpRespone<T>
now, notStrictResponse
. - The response resolver type is annotated to return
HttpResponse<T>
as well.
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.
@@ -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 { |
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 the JsonBodyType
. As in, it cannot suddenly return a stream.
I would like to merge this after #2108. |
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.
Hi @kettanaito, this looks good to me. I haven't tested it but it looks like it will be able to achieve what I have been trying to achieve with #1807. Thank you! Looking forward to using it and integrating all the recent typing changes into a v1.0 openapi-msw release. :)
@christoph-fricke, thank you for your feedback! I'd love to get #2108 merged first before this one. I'm a bit stuck with the type error in that pull request so that hangs for now. |