perf: tRPC procedures and middleware refactor (#8419)

* trpc procedures an middleware refactor

* allow use sessionMiddleware without a req object

* sync with the new tRPC structure

* tRPC refactor on routing form app

* import Prisma from @prisma/client

* Lazy load apps from appstore

* remove unrelated changes

* Add types for PaymentService

* type fixes

* Merge branch 'main' into roae85/cal-1514-set-the-user-session-only-on-the

* fix typo

* remove console.log

* remove explicit types from apstore object

* linter fixes

---------

Co-authored-by: Keith Williams <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
authored and GitHub committed 2023-05-09 19:27:05 +00:00
1 parent 03548d8381
commit d6fb0df64f
63 files changed
+1818 -1019

No files matched your search

+29 -31
View File
@@ -1,35 +1,36 @@
import type { GetServerSidePropsContext, NextApiRequest, NextApiResponse } from "next";
import type { Session } from "next-auth";
import { serverSideTranslations } from "next-i18next/serverSideTranslations";
import type { serverSideTranslations } from "next-i18next/serverSideTranslations";
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
import { getLocaleFromHeaders } from "@calcom/lib/i18n";
import prisma from "@calcom/prisma";
import type { SelectedCalendar, User as PrismaUser, Credential } from "@calcom/prisma/client";
import type { SelectedCalendar, User as PrismaUser } from "@calcom/prisma/client";
import type { CreateNextContextOptions } from "@trpc/server/adapters/next";
type CreateContextOptions = CreateNextContextOptions | GetServerSidePropsContext;
export type CreateInnerContextOptions = {
session: Session | null;
session?: Session | null;
locale: string;
user?: Omit<
PrismaUser,
| "locale"
| "twoFactorSecret"
| "emailVerified"
| "password"
| "identityProviderId"
| "invitedTo"
| "allowDynamicBooking"
| "verified"
> & {
locale: NonNullable<PrismaUser["locale"]>;
credentials?: Credential[];
selectedCalendars?: Partial<SelectedCalendar>[];
};
i18n: Awaited<ReturnType<typeof serverSideTranslations>>;
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>>;
} & Partial<CreateContextOptions>;
export type GetSessionFn =
@@ -39,8 +40,6 @@ export type GetSessionFn =
}) => Promise<Session | null>)
| (() => Promise<Session | null>);
const DEFAULT_SESSION_GETTER: GetSessionFn = ({ req, res }) => getServerSession({ req, res });
/**
* Inner context. Will always be available in your procedures, in contrast to the outer context.
*
@@ -61,16 +60,10 @@ export async function createContextInner(opts: CreateInnerContextOptions) {
* Creates context for an incoming request
* @link https://trpc.io/docs/context
*/
export const createContext = async (
{ req, res }: CreateContextOptions,
sessionGetter: GetSessionFn = DEFAULT_SESSION_GETTER
) => {
// for API-response caching see https://trpc.io/docs/caching
const session = await sessionGetter({ req, res });
export const createContext = async ({ req, res }: CreateContextOptions) => {
const locale = getLocaleFromHeaders(req);
const i18n = await serverSideTranslations(getLocaleFromHeaders(req), ["common", "vital"]);
const contextInner = await createContextInner({ session, i18n, locale });
const contextInner = await createContextInner({ locale });
return {
...contextInner,
req,
@@ -79,3 +72,8 @@ export const createContext = async (
};
export type TRPCContext = Awaited<ReturnType<typeof createContext>>;
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">>;