After years and years fighting with and against JavaScript build stacks, I eventually gave a try to Next.js and fell in love with it for two simple...
For further actions, you may consider blocking this person and/or reporting abuse
Great article and nice clean code. I want to follow your approach but do not get it working with
withApiAuthRequired
(nextjs-auth0). Could you point me in the right direction? Keep getting the error 'invalid hook call'.Thanks a lot!
nc().use()
requires the function arg to be compatible with the props it receives. If you can't make it work out of the box, you may need to wrap it:Thanks for your help! Below a suggestion from my side to replace
const handler = nc().use(errorHandler)
withconst handler = router()
.Yeah my original code didn't work, that's a good way to put it. I just wouldn't call it router to avoid confusion, because it's just a handler, the router being managed inside Next core :).
Here is my strategy:
I searched for example how to test Next.js api with supertest and this is really useful.
The thing is
return apiResolver(req, res, undefined, handler);
3rd arg is query params so if you have a route/api/posts/:id
inside Next.js controllerreq.query.id
will not be passed and will be hardcodedundefined
. Do you know how to pass api dynamic route params?You're right, you can extract it from
req
with the help of theqs
(query string) package if it needs specific formatting.Have a look here: nodejs.org/en/knowledge/HTTP/clien... :)
Thank you for great article. Just quick note that this is not working for me now:
The only way for me is to specify
onError
andonNotFound
directly onnc
instance:Otherway the initial error callbacks are invoked.
Thanks! Indeed, my workaround here is to define a function that returns a new instance of a base handler:
Article updated :).
This is incredible. I just put together a tutorial on next-middleware, but this is so much easier. Thanks for sharing.
Yeah, next-connect really makes everything so much easier. It also handles async error handling out of the box. It's perfect :D. But nice article of yours though, you must have learned quite a few things in the making!