mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
feat: createContext
This commit is contained in:
1 parent
6326b84a60
commit
529869671c
3 files changed
+18
-8
No files matched your search
+2
-1
@@ -19,6 +19,7 @@ import expiryRemovalScheduler from './containers/expiryChecker.js'
|
|||||||
import setupTest from './utils/setupTest.js'
|
import setupTest from './utils/setupTest.js'
|
||||||
import logger from './utils/logger.js'
|
import logger from './utils/logger.js'
|
||||||
import { appRouter } from './routers/index.js'
|
import { appRouter } from './routers/index.js'
|
||||||
|
import { createContext } from './trpc.js'
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
if (!config.secret) {
|
if (!config.secret) {
|
||||||
@@ -76,7 +77,7 @@ async function main() {
|
|||||||
'/trpc',
|
'/trpc',
|
||||||
trpcExpress.createExpressMiddleware({
|
trpcExpress.createExpressMiddleware({
|
||||||
router: appRouter,
|
router: appRouter,
|
||||||
// createContext,
|
createContext,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
app.use(errorHandler)
|
app.use(errorHandler)
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { router, publicProcedure } from '../trpc.js'
|
import { router, publicProcedure } from '../trpc.js'
|
||||||
|
|
||||||
export const appRouter = router({
|
export const appRouter = router({
|
||||||
userList: publicProcedure.query(async () => {
|
userList: publicProcedure.query(async (opts) => {
|
||||||
// Retrieve users from a datasource, this is an imaginary database
|
// Retrieve users from a datasource, this is an imaginary database
|
||||||
return 'hello'
|
return opts.ctx
|
||||||
}),
|
}),
|
||||||
})
|
})
|
||||||
// Export type router type signature,
|
// Export type router type signature,
|
||||||
|
|||||||
+14
-5
@@ -1,9 +1,18 @@
|
|||||||
import { initTRPC } from '@trpc/server'
|
import { initTRPC } from '@trpc/server'
|
||||||
/**
|
import { CreateNextContextOptions } from '@trpc/server/adapters/next'
|
||||||
* Initialization of tRPC backend
|
|
||||||
* Should be done only once per backend!
|
export const createContext = async (opts: CreateNextContextOptions) => {
|
||||||
*/
|
const { session, user, ip } = opts.req
|
||||||
const t = initTRPC.create()
|
return {
|
||||||
|
session,
|
||||||
|
user,
|
||||||
|
ip,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export type AppContext = Awaited<ReturnType<typeof createContext>>
|
||||||
|
|
||||||
|
const t = initTRPC.context<AppContext>().create()
|
||||||
/**
|
/**
|
||||||
* Export reusable router and procedure helpers
|
* Export reusable router and procedure helpers
|
||||||
* that can be used throughout the router
|
* that can be used throughout the router
|
||||||
|
|||||||
Reference in new issue
Block a user