2023-06-06 08:59:57 -03:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2023-03-10 20:45:24 -03:00
|
|
|
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";
|
2022-08-03 16:18:26 -03:00
|
|
|
import type { Session } from "next-auth";
|
2023-05-09 16:27:05 -03:00
|
|
|
import type { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
2021-09-27 11:47:55 -03:00
|
|
|
|
2023-10-10 13:36:28 -03:00
|
|
|
import { getLocale } from "@calcom/features/auth/lib/getLocale";
|
2023-11-30 19:16:40 -03:00
|
|
|
import getIP from "@calcom/lib/getIP";
|
2023-11-15 18:50:20 -03:00
|
|
|
import prisma, { readonlyPrisma } from "@calcom/prisma";
|
2023-05-09 16:27:05 -03:00
|
|
|
import type { SelectedCalendar, User as PrismaUser } from "@calcom/prisma/client";
|
2022-07-22 14:27:06 -03:00
|
|
|
|
2023-02-28 18:40:19 -03:00
|
|
|
import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
2021-10-14 16:22:01 -03:00
|
|
|
|
2023-02-28 18:40:19 -03:00
|
|
|
type CreateContextOptions = CreateNextContextOptions | GetServerSidePropsContext;
|
2021-10-20 13:00:11 -03:00
|
|
|
|
2023-03-31 11:38:46 -03:00
|
|
|
export type CreateInnerContextOptions = {
|
2023-11-30 19:16:40 -03:00
|
|
|
sourceIp?: string;
|
2023-05-09 16:27:05 -03:00
|
|
|
session?: Session | null;
|
2023-02-08 22:12:45 -03:00
|
|
|
locale: string;
|
2023-05-09 16:27:05 -03:00
|
|
|
user?:
|
|
|
|
| Omit<
|
|
|
|
PrismaUser,
|
|
|
|
| "locale"
|
|
|
|
| "twoFactorSecret"
|
|
|
|
| "emailVerified"
|
|
|
|
| "password"
|
|
|
|
| "identityProviderId"
|
|
|
|
| "invitedTo"
|
|
|
|
| "allowDynamicBooking"
|
|
|
|
| "verified"
|
|
|
|
> & {
|
|
|
|
locale: Exclude<PrismaUser["locale"], null>;
|
|
|
|
credentials?: Credential[];
|
|
|
|
selectedCalendars?: Partial<SelectedCalendar>[];
|
|
|
|
rawAvatar?: string;
|
|
|
|
};
|
|
|
|
i18n?: Awaited<ReturnType<typeof serverSideTranslations>>;
|
2023-02-08 22:12:45 -03:00
|
|
|
} & Partial<CreateContextOptions>;
|
|
|
|
|
2023-03-10 20:45:24 -03:00
|
|
|
export type GetSessionFn =
|
|
|
|
| ((_options: {
|
|
|
|
req: GetServerSidePropsContext["req"] | NextApiRequest;
|
|
|
|
res: GetServerSidePropsContext["res"] | NextApiResponse;
|
|
|
|
}) => Promise<Session | null>)
|
|
|
|
| (() => Promise<Session | null>);
|
|
|
|
|
2023-02-08 22:12:45 -03:00
|
|
|
/**
|
|
|
|
* Inner context. Will always be available in your procedures, in contrast to the outer context.
|
|
|
|
*
|
|
|
|
* Also useful for:
|
|
|
|
* - testing, so you don't have to mock Next.js' `req`/`res`
|
|
|
|
* - tRPC's `createSSGHelpers` where we don't have `req`/`res`
|
|
|
|
*
|
|
|
|
* @see https://trpc.io/docs/context#inner-and-outer-context
|
|
|
|
*/
|
|
|
|
export async function createContextInner(opts: CreateInnerContextOptions) {
|
|
|
|
return {
|
|
|
|
prisma,
|
2023-11-15 18:50:20 -03:00
|
|
|
insightsDb: readonlyPrisma,
|
2023-02-08 22:12:45 -03:00
|
|
|
...opts,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-27 11:47:55 -03:00
|
|
|
/**
|
|
|
|
* Creates context for an incoming request
|
|
|
|
* @link https://trpc.io/docs/context
|
|
|
|
*/
|
2023-06-10 08:02:50 -03:00
|
|
|
export const createContext = async ({ req, res }: CreateContextOptions, sessionGetter?: GetSessionFn) => {
|
2023-10-10 13:36:28 -03:00
|
|
|
const locale = await getLocale(req);
|
2023-11-30 19:16:40 -03:00
|
|
|
|
|
|
|
// This type may not be accurate if this request is coming from SSG init but they both should satisfy the requirements of getIP.
|
|
|
|
// TODO: @sean - figure out a way to make getIP be happy with trpc req. params
|
|
|
|
const sourceIp = getIP(req as NextApiRequest);
|
2023-06-10 08:02:50 -03:00
|
|
|
const session = !!sessionGetter ? await sessionGetter({ req, res }) : null;
|
2023-11-30 19:16:40 -03:00
|
|
|
const contextInner = await createContextInner({ locale, session, sourceIp });
|
2021-09-27 11:47:55 -03:00
|
|
|
return {
|
2023-02-08 22:12:45 -03:00
|
|
|
...contextInner,
|
|
|
|
req,
|
|
|
|
res,
|
2021-09-27 11:47:55 -03:00
|
|
|
};
|
|
|
|
};
|
2023-04-25 19:39:47 -03:00
|
|
|
|
|
|
|
export type TRPCContext = Awaited<ReturnType<typeof createContext>>;
|
2023-05-09 16:27:05 -03:00
|
|
|
export type TRPCContextInner = Awaited<ReturnType<typeof createContextInner>>;
|
|
|
|
export type WithLocale<T extends TRPCContext = any> = T &
|
|
|
|
Required<Pick<CreateInnerContextOptions, "i18n" | "locale">>;
|
|
|
|
export type WithSession<T extends TRPCContext = any> = T &
|
|
|
|
Required<Pick<CreateInnerContextOptions, "session">>;
|