forked from t3-oss/create-t3-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make context independent of req/res (t3-oss#325)
- Loading branch information
1 parent
80e01b9
commit b7879f7
Showing
4 changed files
with
85 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,38 @@ | ||
// src/server/trpc/context.ts | ||
// src/server/router/context.ts | ||
import * as trpc from "@trpc/server"; | ||
import * as trpcNext from "@trpc/server/adapters/next"; | ||
import { unstable_getServerSession as getServerSession } from "next-auth"; | ||
|
||
import { | ||
Session, | ||
unstable_getServerSession as getServerSession, | ||
} from "next-auth"; | ||
import { authOptions as nextAuthOptions } from "../../pages/api/auth/[...nextauth]"; | ||
|
||
type CreateContextOptions = { | ||
session: Session | null; | ||
}; | ||
|
||
/** Use this helper for: | ||
* - testing, where we dont have to Mock Next.js' req/res | ||
* - trpc's `createSSGHelpers` where we don't have req/res | ||
**/ | ||
export const createContextInner = async (opts: CreateContextOptions) => { | ||
return { | ||
session: opts.session, | ||
}; | ||
}; | ||
|
||
/** | ||
* This is the actual context you'll use in your router | ||
* @link https://trpc.io/docs/context | ||
**/ | ||
export const createContext = async ( | ||
opts: trpcNext.CreateNextContextOptions, | ||
) => { | ||
const session = await getServerSession(opts.req, opts.res, nextAuthOptions); | ||
|
||
return { | ||
return await createContextInner({ | ||
session, | ||
}; | ||
}); | ||
}; | ||
|
||
export type Context = trpc.inferAsyncReturnType<typeof createContext>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,40 @@ | ||
// src/server/trpc/context.ts | ||
// src/server/router/context.ts | ||
import * as trpc from "@trpc/server"; | ||
import * as trpcNext from "@trpc/server/adapters/next"; | ||
import { unstable_getServerSession as getServerSession } from "next-auth"; | ||
|
||
import { | ||
Session, | ||
unstable_getServerSession as getServerSession, | ||
} from "next-auth"; | ||
import { authOptions as nextAuthOptions } from "../../pages/api/auth/[...nextauth]"; | ||
import { prisma } from "../db/client"; | ||
|
||
type CreateContextOptions = { | ||
session: Session | null; | ||
}; | ||
|
||
/** Use this helper for: | ||
* - testing, so we dont have to mock Next.js' req/res | ||
* - trpc's `createSSGHelpers` where we don't have req/res | ||
**/ | ||
export const createContextInner = async (opts: CreateContextOptions) => { | ||
return { | ||
session: opts.session, | ||
prisma, | ||
}; | ||
}; | ||
|
||
/** | ||
* This is the actual context you'll use in your router | ||
* @link https://trpc.io/docs/context | ||
**/ | ||
export const createContext = async ( | ||
opts: trpcNext.CreateNextContextOptions, | ||
) => { | ||
const session = await getServerSession(opts.req, opts.res, nextAuthOptions); | ||
|
||
return { | ||
return await createContextInner({ | ||
session, | ||
prisma, | ||
}; | ||
}); | ||
}; | ||
|
||
export type Context = trpc.inferAsyncReturnType<typeof createContext>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters