Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wrap createContext in helperfunc to make it independent of req/res #324

Merged
merged 4 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-pots-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"create-t3-app": minor
---

feat: wrap createContext with helper function to make it independent of req/res
38 changes: 26 additions & 12 deletions cli/template/addons/trpc/auth-context.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,38 @@
// 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,
opts: trpcNext.CreateNextContextOptions,
) => {
const req = opts?.req;
const res = opts?.res;

const session =
req && res && (await getServerSession(req, res, nextAuthOptions));
const session = await getServerSession(opts.req, opts.res, nextAuthOptions);

return {
req,
res,
return await createContextInner({
session,
};
});
};

type Context = trpc.inferAsyncReturnType<typeof createContext>;
Expand Down
40 changes: 27 additions & 13 deletions cli/template/addons/trpc/auth-prisma-context.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
// 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";

export const createContext = async (
opts?: trpcNext.CreateNextContextOptions,
) => {
const req = opts?.req;
const res = opts?.res;

const session =
req && res && (await getServerSession(req, res, nextAuthOptions));
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 {
req,
res,
session,
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 await createContextInner({
session,
});
};

type Context = trpc.inferAsyncReturnType<typeof createContext>;

export const createRouter = () => trpc.router<Context>();
24 changes: 17 additions & 7 deletions cli/template/addons/trpc/base-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,24 @@
import * as trpc from "@trpc/server";
import * as trpcNext from "@trpc/server/adapters/next";

export const createContext = (opts?: trpcNext.CreateNextContextOptions) => {
const req = opts?.req;
const res = opts?.res;
type CreateContextOptions = {};

return {
req,
res,
};
/** 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({});
};

type Context = trpc.inferAsyncReturnType<typeof createContext>;
Expand Down
21 changes: 16 additions & 5 deletions cli/template/addons/trpc/prisma-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,28 @@ 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) => {
const req = opts?.req;
const res = opts?.res;
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 {
req,
res,
prisma,
};
};

/**
* This is the actual context you'll use in your router
* @link https://trpc.io/docs/context
**/
export const createContext = (opts: trpcNext.CreateNextContextOptions) => {
return createContextInner({
prisma,
});
juliusmarminge marked this conversation as resolved.
Show resolved Hide resolved
};

type Context = trpc.inferAsyncReturnType<typeof createContext>;

export const createRouter = () => trpc.router<Context>();