2023-06-06 11:59:57 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
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";
|
2023-05-09 19:27:05 +00:00
|
|
|
import type { serverSideTranslations } from "next-i18next/serverSideTranslations";
|
2021-09-27 14:47:55 +00:00
|
|
|
|
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-05-09 19:27:05 +00:00
|
|
|
import type { SelectedCalendar, User as PrismaUser } 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-03-31 14:38:46 +00:00
|
|
|
export type CreateInnerContextOptions = {
|
2023-05-09 19:27:05 +00:00
|
|
|
session?: Session | null;
|
2023-02-09 01:12:45 +00:00
|
|
|
locale: string;
|
2023-05-09 19:27:05 +00: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-09 01:12:45 +00:00
|
|
|
} & 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>);
|
|
|
|
|
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-05-09 19:27:05 +00:00
|
|
|
export const createContext = async ({ req, res }: CreateContextOptions) => {
|
2023-03-23 18:46:33 +00:00
|
|
|
const locale = getLocaleFromHeaders(req);
|
2023-05-09 19:27:05 +00:00
|
|
|
|
|
|
|
const contextInner = await createContextInner({ 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
|
|
|
};
|
|
|
|
};
|
2023-04-25 22:39:47 +00:00
|
|
|
|
|
|
|
export type TRPCContext = Awaited<ReturnType<typeof createContext>>;
|
2023-05-09 19:27:05 +00: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">>;
|