NotLuksus
NotLuksus5mo ago

Equivalent to middleware on createNextApiHandler

Hey, I'm currently migrating our trpc app to the app router and wondered what the equivalent to the middleware property here would be in app router:
import { createNextApiHandler } from '@trpc/server/adapters/next'

import { env } from '~/env.mjs'
import { appRouter } from '~/server/api/routers/app.router'
import { createTRPCContext } from '~/server/api/trpc'
import { initBudgetAsyncLocalStorage } from '~/server/lib/getBudgetCached'

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
middleware: (_res, _req, next) => {
initBudgetAsyncLocalStorage()
return next()
},
onError:
env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`
)
}
: undefined,
})
import { createNextApiHandler } from '@trpc/server/adapters/next'

import { env } from '~/env.mjs'
import { appRouter } from '~/server/api/routers/app.router'
import { createTRPCContext } from '~/server/api/trpc'
import { initBudgetAsyncLocalStorage } from '~/server/lib/getBudgetCached'

// export API handler
export default createNextApiHandler({
router: appRouter,
createContext: createTRPCContext,
middleware: (_res, _req, next) => {
initBudgetAsyncLocalStorage()
return next()
},
onError:
env.NODE_ENV === 'development'
? ({ path, error }) => {
console.error(
`❌ tRPC failed on ${path ?? '<no-path>'}: ${error.message}`
)
}
: undefined,
})
Thank you!
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?