NEO
NEO2y ago

Cannot access 't' before initialization

Hi, I'm migrating my app to a mono repo but I just can't get past this error for some reason Cannot access 't' before initialization when using the tRPC instance anywhere be it a middleware or router The server is a standalone node server useAuthenticaton.ts
import { TRPCError } from '@trpc/server'
import { t, User } from '../server/api'
import * as Auth from '../services/auth/auth'

const useAuthentication = t.middleware(async ({ ctx, next }) => {
try {
const authToken = ctx.req.headers['token'] as string
const authResult = (await Auth.verifyJWTToken(authToken)) as User

return next({
ctx: {
user: authResult
}
})
} catch (error) {
throw new TRPCError({
code: 'UNAUTHORIZED'
})
}
})

export default useAuthentication
import { TRPCError } from '@trpc/server'
import { t, User } from '../server/api'
import * as Auth from '../services/auth/auth'

const useAuthentication = t.middleware(async ({ ctx, next }) => {
try {
const authToken = ctx.req.headers['token'] as string
const authResult = (await Auth.verifyJWTToken(authToken)) as User

return next({
ctx: {
user: authResult
}
})
} catch (error) {
throw new TRPCError({
code: 'UNAUTHORIZED'
})
}
})

export default useAuthentication
api.ts
export type User = JwtPayload & {
userid: number
email: string
}

export type Context = inferAsyncReturnType<typeof createContext>

export type ServerContext = { user: User } & Context

export const t = initTRPC
.context<ServerContext>()
.meta<OpenApiMeta>()
.create({
transformer: superjson,
errorFormatter: ({ error, shape }) => {
if (
error.code === 'INTERNAL_SERVER_ERROR' &&
process.env.NODE_ENV === 'production'
) {
return { ...shape, message: 'Internal server error' }
}
return shape
}
})
export type User = JwtPayload & {
userid: number
email: string
}

export type Context = inferAsyncReturnType<typeof createContext>

export type ServerContext = { user: User } & Context

export const t = initTRPC
.context<ServerContext>()
.meta<OpenApiMeta>()
.create({
transformer: superjson,
errorFormatter: ({ error, shape }) => {
if (
error.code === 'INTERNAL_SERVER_ERROR' &&
process.env.NODE_ENV === 'production'
) {
return { ...shape, message: 'Internal server error' }
}
return shape
}
})
12 Replies
kevin.hill.fortunabmc
You could try exporting aliases of the functions? like this? Then maybe you will keep the reference of t in the one file
NEO
NEOOP2y ago
Turns out I can't define middlewares in a separate file, moving them under the api.ts solved the issue for me
kevin.hill.fortunabmc
You can do this
kevin.hill.fortunabmc
and create folders to house scoped procedures
kevin.hill.fortunabmc
this is procedures.ts
NEO
NEOOP2y ago
Interesting, I might try this, wasted so much time on this already haha
kevin.hill.fortunabmc
I have too, so I am happy to share what I've learned I posted my own discussion here if you want to check it out https://discord.com/channels/867764511159091230/1111426709519093840 I used it to have a bunch of different, service-specific, scoped procedures that have the proper clients attached asanaProcedure, salesforceProcedure, etc..
kevin.hill.fortunabmc
My router is insanely huge
NEO
NEOOP2y ago
I don't have subrouters instead a route file that has multiple procedures
NEO
NEOOP2y ago
Was trying your approach to middleware doesn't seem like it would work for chaining multiple middlewares
NEO
NEOOP2y ago
Came with a new solution, this passes the tRPC instance as a parameter to the middleware and you can still chain multiple middlewares together

Did you find this page helpful?