Skip to content

Commit

Permalink
feat: make context independent of req/res (t3-oss#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusmarminge authored Aug 17, 2022
1 parent 80e01b9 commit b7879f7
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 13 deletions.
30 changes: 25 additions & 5 deletions cli/template/addons/trpc/auth-context.ts
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>;
32 changes: 26 additions & 6 deletions cli/template/addons/trpc/auth-prisma-context.ts
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>;
18 changes: 17 additions & 1 deletion cli/template/addons/trpc/base-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof createContext>;
18 changes: 17 additions & 1 deletion cli/template/addons/trpc/prisma-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof createContext>;

0 comments on commit b7879f7

Please sign in to comment.