#Route Handler Wrapper
1 messages · Page 1 of 1 (latest)
🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord
🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize
✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)
I think you can create 500.tsx or error.tsx file to do the same.
i need it for the server api part (route.ts) not the client part
Generally, you wrap the api end points with try catch block to take care.
Yeah, but i should wrap every api with same code. It can be quite cumbersome and messy. I'm looking for cleaner way like middleware in express
Yes, I believe you should be able to make wrappers, as long as you’re returning the correct function signature (expected by Next.js in order to make routes out of them)
Remember, the names they end up having need to be one of the valid: GET, POST, PUT, etc.
And only these allowed functions are allowed to be exported from route.ts files
Something like this is what next-auth does.
Next.js has middleware.ts which is executed on every request I believe
Middleware in Next.js isn’t like the middlewares in Express tho. I think he wants a way to centralize the repeating logic instead of writing the boilerplate in every route, and maybe multiple times per route.ts file.
Idk if a wrapper like this will solve that issue, but you can try