#Route Handler Wrapper

1 messages · Page 1 of 1 (latest)

soft hearth
#

Is it possible to create wrapper for Next JS 15 Route Handler ? I want to create wrapper for catching global errors, logging, and authentication middleware

blissful barnBOT
#

🔎 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)

modest fable
#

I think you can create 500.tsx or error.tsx file to do the same.

soft hearth
#

i need it for the server api part (route.ts) not the client part

modest fable
#

Generally, you wrap the api end points with try catch block to take care.

soft hearth
#

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

plucky axle
#

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.

rigid mango
plucky axle
# plucky axle Something like this is what `next-auth` does.

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