NEO
NEO
TtRPC
Created by NEO on 5/21/2023 in #❓-help
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
}
})
16 replies
TtRPC
Created by NEO on 4/7/2023 in #❓-help
tRPC standalone server in monorepo
Hi, I'm using t3-stack monorepo as my base and I've swapped out NextJS backend for standalone HTTP server but I just can't get it work Discord wouldn't let me post the code as it was too long so I've uploaded the api.ts and client.ts along with the error to gist https://gist.github.com/neo773/7c8a314785d5e4f54c724cde7e5fa65c
13 replies
TtRPC
Created by NEO on 10/28/2022 in #❓-help
Websockets not sending data
4 replies
TtRPC
Created by NEO on 10/14/2022 in #❓-help
CORS in standalone server
5 replies
TtRPC
Created by NEO on 10/13/2022 in #❓-help
Can't get client to work
6 replies
TtRPC
Created by NEO on 10/13/2022 in #❓-help
Request context inside middleware?
Hi, Is it possible to get the request context inside a middleware somehow? I'm trying to migrate a Express Based API
const t = initTRPC.context<Context>().create();

const useAuthentication = t.middleware(({ next, ctx }) => {
const token = ctx.req.headers['authorization']
})
const t = initTRPC.context<Context>().create();

const useAuthentication = t.middleware(({ next, ctx }) => {
const token = ctx.req.headers['authorization']
})
8 replies