Skip to content
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
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kettanaito
Copy link
Member

@@ -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>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The trick is this:

  1. implements StrictResponse<T>
  2. All static methods return HttpRespone<T> now, not StrictResponse.
  3. 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 {
Copy link
Member Author

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.

@kettanaito
Copy link
Member Author

I would like to merge this after #2108.

Copy link
Contributor

@christoph-fricke christoph-fricke left a 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. :)

@kettanaito
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants