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

fix: narrow body type for HttpResponse.json() to be serializable json #1838

Merged
merged 5 commits into from
Nov 7, 2023

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Nov 7, 2023

Changes

  • The body type of the HttpResponse.json() static method must be a serializable JSON. Narrow the argument type to reflect that.
  • Exports the new JsonBodyType type from the core.

@kettanaito kettanaito changed the title fix(HttpResponse): preserves given ReadableStream body to HttpResponse.json() fix(HttpResponse): preserve given ReadableStream body to HttpResponse.json() Nov 7, 2023
@kettanaito kettanaito changed the title fix(HttpResponse): preserve given ReadableStream body to HttpResponse.json() fix(HttpResponse): narrow body type for HttpResponse.json() to be serializable json Nov 7, 2023
@kettanaito kettanaito changed the title fix(HttpResponse): narrow body type for HttpResponse.json() to be serializable json fix: narrow body type for HttpResponse.json() to be serializable json Nov 7, 2023
@kettanaito kettanaito merged commit 2af274c into main Nov 7, 2023
9 checks passed
@kettanaito kettanaito deleted the fix/http-response-compliance branch November 7, 2023 10:15
@@ -19,6 +19,14 @@ export type DefaultBodyType =
| null
| undefined

export type JsonBodyType =
| Record<string, any>
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: do we want to be slightly more prescriptive than Record<string, any> here, Record<string, JsonBodyType> might be the most correct option?

also I think we'd need to account for Array<JsonBodyType> too? (and potentially their readonly varieties?)

Copy link
Member Author

Choose a reason for hiding this comment

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

Surprisingly, TypeScript is permissive about this and arrays extend Record<string, any> quite well. It's the any part that does the trick.

As to narrowing down the type from any to a recursive JsonBodyType, I'm not so sure about it. It sounds like the right thing to do but there's always corner cases people run into, and it's hard to predict what will happen if we make this body type that strict.

@kettanaito
Copy link
Member Author

Released: v2.0.4 🎉

This has been released in v2.0.4!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

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

Successfully merging this pull request may close these issues.

2 participants