From b7879f709f7668da6b3db6316a0c8c61d6b3f193 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Wed, 17 Aug 2022 13:06:55 +0200 Subject: [PATCH] feat: make context independent of req/res (#325) --- cli/template/addons/trpc/auth-context.ts | 30 ++++++++++++++--- .../addons/trpc/auth-prisma-context.ts | 32 +++++++++++++++---- cli/template/addons/trpc/base-context.ts | 18 ++++++++++- cli/template/addons/trpc/prisma-context.ts | 18 ++++++++++- 4 files changed, 85 insertions(+), 13 deletions(-) diff --git a/cli/template/addons/trpc/auth-context.ts b/cli/template/addons/trpc/auth-context.ts index f6cfbaabb1..c88a83a61b 100644 --- a/cli/template/addons/trpc/auth-context.ts +++ b/cli/template/addons/trpc/auth-context.ts @@ -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; diff --git a/cli/template/addons/trpc/auth-prisma-context.ts b/cli/template/addons/trpc/auth-prisma-context.ts index c1dc601355..fe371a3352 100644 --- a/cli/template/addons/trpc/auth-prisma-context.ts +++ b/cli/template/addons/trpc/auth-prisma-context.ts @@ -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; diff --git a/cli/template/addons/trpc/base-context.ts b/cli/template/addons/trpc/base-context.ts index 457b91aa0b..38ec4a6cb3 100644 --- a/cli/template/addons/trpc/base-context.ts +++ b/cli/template/addons/trpc/base-context.ts @@ -2,8 +2,24 @@ import * as trpc from "@trpc/server"; import * as trpcNext from "@trpc/server/adapters/next"; -export const createContext = (opts: trpcNext.CreateNextContextOptions) => { +type CreateContextOptions = {}; + +/** 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 {}; }; +/** + * This is the actual context you'll use in your router + * @link https://trpc.io/docs/context + **/ +export const createContext = async ( + opts: trpcNext.CreateNextContextOptions, +) => { + return await createContextInner({}); +}; + export type Context = trpc.inferAsyncReturnType; diff --git a/cli/template/addons/trpc/prisma-context.ts b/cli/template/addons/trpc/prisma-context.ts index eb6964c23c..5a0e43a6e9 100644 --- a/cli/template/addons/trpc/prisma-context.ts +++ b/cli/template/addons/trpc/prisma-context.ts @@ -3,10 +3,26 @@ import * as trpc from "@trpc/server"; import * as trpcNext from "@trpc/server/adapters/next"; import { prisma } from "../db/client"; -export const createContext = (opts: trpcNext.CreateNextContextOptions) => { +type CreateContextOptions = {}; + +/** 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 { 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, +) => { + return await createContextInner({}); +}; + export type Context = trpc.inferAsyncReturnType;