2023-03-10 23:45:24 +00:00
|
|
|
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";
|
2022-08-03 19:18:26 +00:00
|
|
|
import type { Session } from "next-auth";
|
2021-10-14 10:57:49 +00:00
|
|
|
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
2021-09-27 14:47:55 +00:00
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
2022-07-29 14:28:53 +00:00
|
|
|
import { getLocaleFromHeaders } from "@calcom/lib/i18n";
|
2022-07-22 17:27:06 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2023-03-23 18:46:33 +00:00
|
|
|
import type { SelectedCalendar, User as PrismaUser, Credential } from "@calcom/prisma/client";
|
2022-07-22 17:27:06 +00:00
|
|
|
|
2023-02-28 21:40:19 +00:00
|
|
|
import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
|
2021-10-14 19:22:01 +00:00
|
|
|
|
2023-02-28 21:40:19 +00:00
|
|
|
type CreateContextOptions = CreateNextContextOptions | GetServerSidePropsContext;
|
2021-10-20 16:00:11 +00:00
|
|
|
|
2023-02-09 01:12:45 +00:00
|
|
|
type CreateInnerContextOptions = {
|
|
|
|
session: Session | null;
|
|
|
|
locale: string;
|
2023-03-23 18:46:33 +00:00
|
|
|
user?: Omit<
|
|
|
|
PrismaUser,
|
|
|
|
| "locale"
|
|
|
|
| "twoFactorSecret"
|
|
|
|
| "emailVerified"
|
|
|
|
| "password"
|
|
|
|
| "identityProviderId"
|
|
|
|
| "invitedTo"
|
|
|
|
| "allowDynamicBooking"
|
|
|
|
| "verified"
|
|
|
|
> & {
|
|
|
|
locale: NonNullable<PrismaUser["locale"]>;
|
|
|
|
credentials?: Credential[];
|
|
|
|
selectedCalendars?: Partial<SelectedCalendar>[];
|
|
|
|
};
|
2023-02-09 01:12:45 +00:00
|
|
|
i18n: Awaited<ReturnType<typeof serverSideTranslations>>;
|
|
|
|
} & Partial<CreateContextOptions>;
|
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
export type GetSessionFn =
|
|
|
|
| ((_options: {
|
|
|
|
req: GetServerSidePropsContext["req"] | NextApiRequest;
|
|
|
|
res: GetServerSidePropsContext["res"] | NextApiResponse;
|
|
|
|
}) => Promise<Session | null>)
|
|
|
|
| (() => Promise<Session | null>);
|
|
|
|
|
|
|
|
const DEFAULT_SESSION_GETTER: GetSessionFn = ({ req, res }) => getServerSession({ req, res });
|
|
|
|
|
2023-02-09 01:12:45 +00: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,
|
|
|
|
...opts,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-27 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* Creates context for an incoming request
|
|
|
|
* @link https://trpc.io/docs/context
|
|
|
|
*/
|
2023-03-10 23:45:24 +00:00
|
|
|
export const createContext = async (
|
|
|
|
{ req, res }: CreateContextOptions,
|
|
|
|
sessionGetter: GetSessionFn = DEFAULT_SESSION_GETTER
|
|
|
|
) => {
|
2021-09-27 14:47:55 +00:00
|
|
|
// for API-response caching see https://trpc.io/docs/caching
|
2023-03-10 23:45:24 +00:00
|
|
|
const session = await sessionGetter({ req, res });
|
2021-09-27 14:47:55 +00:00
|
|
|
|
2023-03-23 18:46:33 +00:00
|
|
|
const locale = getLocaleFromHeaders(req);
|
|
|
|
const i18n = await serverSideTranslations(getLocaleFromHeaders(req), ["common", "vital"]);
|
|
|
|
const contextInner = await createContextInner({ session, i18n, locale });
|
2021-09-27 14:47:55 +00:00
|
|
|
return {
|
2023-02-09 01:12:45 +00:00
|
|
|
...contextInner,
|
|
|
|
req,
|
|
|
|
res,
|
2021-09-27 14:47:55 +00:00
|
|
|
};
|
|
|
|
};
|