-
Notifications
You must be signed in to change notification settings - Fork 135
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
Typescript: Type 'string' is not assignable to type 'Body | undefined' #233
Comments
Stale issue message |
This is happening to me as well |
Any updates/workarounds for this? |
@julianferres @dbrosio3 If you still have this issue, here is a post with a small workaround: https://www.reddit.com/r/nextjs/comments/16z4l49/how_the_hell_do_i_test_nextjs_app_router_api/ In short just add the typecheck for request to the API route handler as follows:
|
@julianferres @dbrosio3 please try the latest release v1.14.0 |
@eugef could you please check how it works with app router (Next.js v.13+) ? I see it only accepts types of NextApiRequest and NextApiResponse which are the part of pages router. Reference to api types: I still have the following error while trying to run the test with mocking the API route:
Api route example:
Test file:
|
Hi @mhaligowski, can you please check the comment above. |
I ended up fixing the .json issue by mocking that
|
@Meags27 this workaround worked for me as well, thank you! |
Sorry it took me a little longer to reply! I don't think adding the support will be difficult at all. The potential problem I see is that the App Router classes ( I guess we could try adding |
@Stalex89 if possible, would you be able to test out #291 ? NextJS made a little weird decision (IMHO) to utilize Web API's object as base classes. IIUC those generally represent HTTP requests from the client, compared to Express or IncomingMessage, which represent server-side objects. I don't think it messes |
Stale issue message |
I found that
Body
interface supports only[key: string]: any;
. But in many projects, the body may include stringified JSON, like this:Because of that, the
eslint
throws the error below:Probably, we can add a type like
export type BodyString = string;
and apply it to theRequestOptions
interface like this:It will solve the issue for eslint users. Probably there is a more elegant solution but the idea remains the same.
Also, any workarounds are welcome!
The text was updated successfully, but these errors were encountered: