-
-
Notifications
You must be signed in to change notification settings - Fork 538
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Remove deprecated MockedRequest.body
and cleanup
#1318
Comments
Thanks for opening this one. Yes, in the long-term, we will drop Since it'd change the expectations behind the property anyway, it may make sense to remove it first and then find a suitable approach to re-introduce it as the one that returns a stream you can hook into. Streaming request bodies is in itself a challenge, as there may be multiple handlers reading it (although never at the same time) and each would expect a fresh stream. |
I did not want to start a new issues only for this; but before you drop Example: interface RequestParams {
accountNumber?: string;
installationId?: string;
meterId?: string;
pin?: string;
}
rest.post<RequestParams>(`${endpoints.validation}`, async (req, res, ctx) => {
// Nice typesafe deconstructing
const { accountNumber, meterId, installationId, pin } = req.body;
// "Any" is kinda uncool
const { accountNumber, meterId, installationId, pin } = await req.json();
} |
Hey, @florianmatz. Thanks for raising this concern. Our notion behind the request body reading changes is to lift off some guesswork from the library and make you responsible for reading request bodies explicitly. This affects the types as well. I think what we will do is drop the request body generic entirely, so you could annotate the body upon reading it: const user = await req.json() as User Since there are multiple ways to read the same body, MSW cannot make assumptions about its type given a single generic. Think of it this way, if you annotated the body as What do you think about this direction? Do you think it's safe to hard-code certain request body reading methods' return type, while keep
|
Hey Artem, mmh, I think both is fair, when made clear. I personally would prefer the option to type the And you don't have to use it, if someone likes to stick to a more explicit casting like Maybe a compromise could be something like: json<T>(): Promise<T> So you put your generic directly and explicitely onto the |
There's some serious dark magic with generics sometimes. I believe we're using a similar pattern right now, where we have a Not sure how this will translate to the future |
Inspired by projects like Generated from an OpenAPI spec, you can get auto-completion for the path, path-parameters, request-body props, and response-body props. It works pretty well at the moment, but it relies on the ability to specify the RequestBodyType as generic of the MockedRequest. With So please, keep some way to specify the RequestBodyType in the response-resolver. In case you want to see the generated types... |
That's some outstanding tooling you're building, @nknapp! Yes, as I've stated above, we should keep the That generated types thing reminds me of type generation out of GraphQL schema/queries. A powerful pattern 👍 Have you ever considered writing about it? I'd endorse your post on Twitter if you decide on it. |
Thank you. Yes, I wanted to write about it, updated to the latest version of Last night, I didn't notice that you are a member of |
+1 for passing down the request type generic through to the rest.post<LoginBody, LoginResponse>('/#', async (req, res, ctx) => {
const { username } = await req.json()
return res(
ctx.json({
username,
firstName: 'John'
})
)
}) Because the generic is passed down to Having to typecast // either
const { username } = (await req.json()) as LoginBody;
// or
const { username } = await (req.json() as Promise<LoginBody>); |
Released: v2.0.0 🎉This has been released in v2.0.0! Make sure to always update to the latest version ( Predictable release automation by @ossjs/release. |
Scope
Improves an existing behavior
Compatibility
Feature description
Remove deprecated
MockedRequest.body
and cleanup after few releases with deprecation.#1302 (comment)
The text was updated successfully, but these errors were encountered: