Documentation/example for rest generics in TypeScript #1308
-
ScopeImproves an existing behavior Compatibility
Feature descriptionMorning First off, thanks for msw. It's great! I'm upgrading to the latest version and having an issue quickly understanding the generics on rest (there might have been changes 0.34.0 -> 0.42.3). Is there an example on this? I could not find it. Some times reading the code and type definitions can be a bit hard. I don/t mind writing it up (once I know what is what). Something like the first generic is X and the second is Y and examples. I can see quite a few tickets related to the development of generics support (#352) but cannot find it on the main docs/example page. Cheers Edit: just worked out there is now 3 generics! Body, PathParams, Response. Cool! The definition file was really wide and I could not parse it :). |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
Hey, @philals. Thanks for raising this. I highly recommend reading Type-safe API mocking with MSW. If you spot it not being up to date please let me know. That article should explain the usage of rest/graphql generics. |
Beta Was this translation helpful? Give feedback.
-
Morning @kettanaito just coming back to this. I think the ordering of these changed. I think Params is now the second one. Mixed me up for a little. |
Beta Was this translation helpful? Give feedback.
-
@kettanaito Hey, I noticed that while the deprecated
Am I missing something? Should I use generics for both the rest method and for Thanks a lot! |
Beta Was this translation helpful? Give feedback.
-
What would be the proper way to use the Generics with the
What do you provide, when you don't have Is this the correct way? (providing /**
* Handle the requests for POPULAR Movies
* @see: https://developers.themoviedb.org/3/movies/get-popular-movies
*/
//get: <PathParams<string>, RequestBodyType_2 extends DefaultBodyType, ResponseBodyType_2 extends DefaultBodyType = undefined>
rest.get<{ movieId: string }, undefined, MovieDetail | MovieError>(
'movie/:movieId',
({ params }) => {
const movieId = +params['movieId'];
if (movieId === 436_270) {
return HttpResponse.json<MovieDetail>(BlackAdam);
}
if (movieId === 505_642) {
return HttpResponse.json<MovieDetail>(WakandaForever);
}
return HttpResponse.json<MovieError>({
status_code: 56,
status_message: `Movie with ID: ${movieId} not found`,
});
}
), N.B.
|
Beta Was this translation helpful? Give feedback.
Hey, @philals. Thanks for raising this.
I highly recommend reading Type-safe API mocking with MSW. If you spot it not being up to date please let me know. That article should explain the usage of rest/graphql generics.