perf: Slim down loggedInViewer tRPC router (#20111)

* perf: Slim down loggedInViewer tRPC router

* Fixed trpc client calls for new routes

* Moved bookingUnconfirmedCount

* Moved getUserTopBanners to me router

* Moved shouldVerifyEmail to me router

* Moved i18n from public to its own router

* fix ssrInit

* fix ssrInit usage

* fix type check

* better naming

* fix

* fix

* Fix types

* Removed used of importHandler

---------

Co-authored-by: hbjORbj <[email protected]>
This commit is contained in:
Keith WilliamsandhbjORbj authored and GitHub committed 2025-03-16 18:22:48 -07:00
1 parent 424d94f16d
commit 20d6eb5200
81 files changed
+162 -188

No files matched your search

@@ -1,7 +1,7 @@
import { trpc } from "../trpc";
export function useEmailVerifyCheck() {
const emailCheck = trpc.viewer.shouldVerifyEmail.useQuery(undefined, {
const emailCheck = trpc.viewer.me.shouldVerifyEmail.useQuery(undefined, {
retry(failureCount) {
return failureCount > 3;
},
+1 -1
View File
@@ -1,7 +1,7 @@
import { trpc } from "../trpc";
export function useMeQuery() {
const meQuery = trpc.viewer.me.useQuery(undefined, {
const meQuery = trpc.viewer.me.get.useQuery(undefined, {
retry(failureCount) {
return failureCount > 3;
},
+2
View File
@@ -14,7 +14,9 @@ export const ENDPOINTS = [
"eventTypes",
"features",
"highPerf",
"i18n",
"insights",
"me",
"payments",
"public",
"timezones",
+1 -1
View File
@@ -67,7 +67,7 @@ export function createNextApiHandler(router: AnyRouter, isPublic = false, namesp
// i18n and cityTimezones are now being accessed using the CalComVersion, which updates on every release,
// letting the clients get the new versions when the version number changes.
i18n: SETTING_FOR_CACHED_BY_VERSION,
"i18n.get": SETTING_FOR_CACHED_BY_VERSION,
cityTimezones: SETTING_FOR_CACHED_BY_VERSION,
// FIXME: Using `max-age=1, stale-while-revalidate=60` fails some booking tests.
@@ -7,7 +7,6 @@ import { ZAppCredentialsByTypeInputSchema } from "./appCredentialsByType.schema"
import { ZConnectAndJoinInputSchema } from "./connectAndJoin.schema";
import { ZConnectedCalendarsInputSchema } from "./connectedCalendars.schema";
import { ZDeleteCredentialInputSchema } from "./deleteCredential.schema";
import { ZDeleteMeInputSchema } from "./deleteMe.schema";
import { ZEventTypeOrderInputSchema } from "./eventTypeOrder.schema";
import { ZGetCalVideoRecordingsInputSchema } from "./getCalVideoRecordings.schema";
import { ZGetDownloadLinkOfCalVideoRecordingsInputSchema } from "./getDownloadLinkOfCalVideoRecordings.schema";
@@ -17,9 +16,6 @@ import { ZNoShowInputSchema } from "./markNoShow.schema";
import { ZOutOfOfficeInputSchema } from "./outOfOfficeCreateOrUpdate.schema";
import { ZOutOfOfficeEntriesListSchema } from "./outOfOfficeEntriesList.schema";
import { ZOutOfOfficeDelete } from "./outOfOfficeEntryDelete.schema";
import { me } from "./procedures/me";
import { myStats } from "./procedures/myStats";
import { platformMe } from "./procedures/platformMe";
import { teamsAndUserProfilesQuery } from "./procedures/teamsAndUserProfilesQuery";
import { ZRemoveNotificationsSubscriptionInputSchema } from "./removeNotificationsSubscription.schema";
import { ZRoutingFormOrderInputSchema } from "./routingFormOrder.schema";
@@ -34,12 +30,6 @@ const NAMESPACE = "loggedInViewer";
const namespaced = (s: string) => `${NAMESPACE}.${s}`;
type AppsRouterHandlerCache = {
me?: typeof import("./me.handler").meHandler;
myStats?: typeof import("./myStats.handler").myStatsHandler;
platformMe?: typeof import("./platformMe.handler").platformMeHandler;
shouldVerifyEmail?: typeof import("./shouldVerifyEmail.handler").shouldVerifyEmailHandler;
deleteMe?: typeof import("./deleteMe.handler").deleteMeHandler;
deleteMeWithoutPassword?: typeof import("./deleteMeWithoutPassword.handler").deleteMeWithoutPasswordHandler;
connectedCalendars?: typeof import("./connectedCalendars.handler").connectedCalendarsHandler;
setDestinationCalendar?: typeof import("./setDestinationCalendar.handler").setDestinationCalendarHandler;
integrations?: typeof import("./integrations.handler").integrationsHandler;
@@ -53,13 +43,11 @@ type AppsRouterHandlerCache = {
submitFeedback?: typeof import("./submitFeedback.handler").submitFeedbackHandler;
locationOptions?: typeof import("./locationOptions.handler").locationOptionsHandler;
deleteCredential?: typeof import("./deleteCredential.handler").deleteCredentialHandler;
bookingUnconfirmedCount?: typeof import("./bookingUnconfirmedCount.handler").bookingUnconfirmedCountHandler;
getCalVideoRecordings?: typeof import("./getCalVideoRecordings.handler").getCalVideoRecordingsHandler;
getDownloadLinkOfCalVideoRecordings?: typeof import("./getDownloadLinkOfCalVideoRecordings.handler").getDownloadLinkOfCalVideoRecordingsHandler;
getUsersDefaultConferencingApp?: typeof import("./getUsersDefaultConferencingApp.handler").getUsersDefaultConferencingAppHandler;
updateUserDefaultConferencingApp?: typeof import("./updateUserDefaultConferencingApp.handler").updateUserDefaultConferencingAppHandler;
teamsAndUserProfilesQuery?: typeof import("./teamsAndUserProfilesQuery.handler").teamsAndUserProfilesQuery;
getUserTopBanners?: typeof import("./getUserTopBanners.handler").getUserTopBannersHandler;
connectAndJoin?: typeof import("./connectAndJoin.handler").Handler;
outOfOfficeCreateOrUpdate?: typeof import("./outOfOfficeCreateOrUpdate.handler").outOfOfficeCreateOrUpdate;
outOfOfficeEntriesList?: typeof import("./outOfOfficeEntriesList.handler").outOfOfficeEntriesList;
@@ -75,38 +63,6 @@ type AppsRouterHandlerCache = {
const UNSTABLE_HANDLER_CACHE: AppsRouterHandlerCache = {};
export const loggedInViewerRouter = router({
me,
myStats,
platformMe,
deleteMe: authedProcedure.input(ZDeleteMeInputSchema).mutation(async ({ ctx, input }) => {
if (!UNSTABLE_HANDLER_CACHE.deleteMe) {
UNSTABLE_HANDLER_CACHE.deleteMe = (await import("./deleteMe.handler")).deleteMeHandler;
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.deleteMe) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.deleteMe({ ctx, input });
}),
deleteMeWithoutPassword: authedProcedure.mutation(async ({ ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.deleteMeWithoutPassword) {
UNSTABLE_HANDLER_CACHE.deleteMeWithoutPassword = (
await import("./deleteMeWithoutPassword.handler")
).deleteMeWithoutPasswordHandler;
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.deleteMeWithoutPassword) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.deleteMeWithoutPassword({ ctx });
}),
connectedCalendars: authedProcedure.input(ZConnectedCalendarsInputSchema).query(async ({ ctx, input }) => {
if (!UNSTABLE_HANDLER_CACHE.connectedCalendars) {
UNSTABLE_HANDLER_CACHE.connectedCalendars = (
@@ -307,21 +263,6 @@ export const loggedInViewerRouter = router({
return UNSTABLE_HANDLER_CACHE.deleteCredential({ ctx, input });
}),
bookingUnconfirmedCount: authedProcedure.query(async ({ ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.bookingUnconfirmedCount) {
UNSTABLE_HANDLER_CACHE.bookingUnconfirmedCount = (
await import("./bookingUnconfirmedCount.handler")
).bookingUnconfirmedCountHandler;
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.bookingUnconfirmedCount) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.bookingUnconfirmedCount({ ctx });
}),
getCalVideoRecordings: authedProcedure
.input(ZGetCalVideoRecordingsInputSchema)
.query(async ({ ctx, input }) => {
@@ -339,21 +280,6 @@ export const loggedInViewerRouter = router({
return UNSTABLE_HANDLER_CACHE.getCalVideoRecordings({ ctx, input });
}),
getUserTopBanners: authedProcedure.query(async ({ ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.getUserTopBanners) {
UNSTABLE_HANDLER_CACHE.getUserTopBanners = (
await import("./getUserTopBanners.handler")
).getUserTopBannersHandler;
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.getUserTopBanners) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.getUserTopBanners({ ctx });
}),
getDownloadLinkOfCalVideoRecordings: authedProcedure
.input(ZGetDownloadLinkOfCalVideoRecordingsInputSchema)
.query(async ({ ctx, input }) => {
@@ -402,20 +328,6 @@ export const loggedInViewerRouter = router({
return UNSTABLE_HANDLER_CACHE.updateUserDefaultConferencingApp({ ctx, input });
}),
shouldVerifyEmail: authedProcedure.query(async ({ ctx }) => {
if (!UNSTABLE_HANDLER_CACHE.shouldVerifyEmail) {
UNSTABLE_HANDLER_CACHE.shouldVerifyEmail = (
await import("./shouldVerifyEmail.handler")
).shouldVerifyEmailHandler;
}
// Unreachable code but required for type safety
if (!UNSTABLE_HANDLER_CACHE.shouldVerifyEmail) {
throw new Error("Failed to load handler");
}
return UNSTABLE_HANDLER_CACHE.shouldVerifyEmail({ ctx });
}),
teamsAndUserProfilesQuery,
connectAndJoin: authedProcedure.input(ZConnectAndJoinInputSchema).mutation(async ({ ctx, input }) => {
if (!UNSTABLE_HANDLER_CACHE.connectAndJoin) {
@@ -1,8 +0,0 @@
import authedProcedure from "../../../procedures/authedProcedure";
import { ZMeInputSchema } from "../me.schema";
export const me = authedProcedure.input(ZMeInputSchema).query(async ({ ctx, input }) => {
const handler = (await import("../me.handler")).meHandler;
return handler({ ctx, input });
});
@@ -2,7 +2,6 @@ import publicProcedure from "../../procedures/publicProcedure";
import { importHandler, router } from "../../trpc";
import { slotsRouter } from "../viewer/slots/_router";
import { ZUserEmailVerificationRequiredSchema } from "./checkIfUserEmailVerificationRequired.schema";
import { i18nInputSchema } from "./i18n.schema";
import { ZMarkHostAsNoShowInputSchema } from "./markHostAsNoShow.schema";
import { event } from "./procedures/event";
import { session } from "./procedures/session";
@@ -17,10 +16,6 @@ const namespaced = (s: string) => `${NAMESPACE}.${s}`;
// things that unauthenticated users can query about themselves
export const publicViewerRouter = router({
session,
i18n: publicProcedure.input(i18nInputSchema).query(async (opts) => {
const handler = await importHandler(namespaced("i18n"), () => import("./i18n.handler"));
return handler(opts);
}),
countryCode: publicProcedure.query(async (opts) => {
const handler = await importHandler(namespaced("countryCode"), () => import("./countryCode.handler"));
return handler(opts);
@@ -21,6 +21,8 @@ import { dsyncRouter } from "./dsync/_router";
import { eventTypesRouter } from "./eventTypes/_router";
import { googleWorkspaceRouter } from "./googleWorkspace/_router";
import { highPerfRouter } from "./highPerf/_router";
import { i18nRouter } from "./i18n/_router";
import { meRouter } from "./me/_router";
import { oAuthRouter } from "./oAuth/_router";
import { viewerOrganizationsRouter } from "./organizations/_router";
import { paymentsRouter } from "./payments/_router";
@@ -35,7 +37,7 @@ export const viewerRouter = mergeRouters(
loggedInViewerRouter,
router({
loggedInViewerRouter,
me: meRouter,
public: publicViewerRouter,
auth: authRouter,
deploymentSetup: deploymentSetupRouter,
@@ -52,6 +54,7 @@ export const viewerRouter = mergeRouters(
workflows: workflowsRouter,
saml: ssoRouter,
dsync: dsyncRouter,
i18n: i18nRouter,
insights: insightsRouter,
payments: paymentsRouter,
// NOTE: Add all app related routes in the bottom till the problem described in @calcom/app-store/trpc-routers.ts is solved.
@@ -0,0 +1,10 @@
import publicProcedure from "../../../procedures/publicProcedure";
import { router } from "../../../trpc";
import { i18nInputSchema } from "./i18n.schema";
export const i18nRouter = router({
get: publicProcedure.input(i18nInputSchema).query(async (opts) => {
const handler = (await import("./i18n.handler")).i18nHandler;
return handler(opts);
}),
});
@@ -0,0 +1,36 @@
import authedProcedure from "../../../procedures/authedProcedure";
import { router } from "../../../trpc";
import { ZDeleteMeInputSchema } from "./deleteMe.schema";
import { get } from "./procedures/get";
export const meRouter = router({
bookingUnconfirmedCount: authedProcedure.query(async ({ ctx }) => {
const handler = (await import("./bookingUnconfirmedCount.handler")).bookingUnconfirmedCountHandler;
return handler({ ctx });
}),
deleteMe: authedProcedure.input(ZDeleteMeInputSchema).mutation(async ({ ctx, input }) => {
const handler = (await import("./deleteMe.handler")).deleteMeHandler;
return handler({ ctx, input });
}),
deleteMeWithoutPassword: authedProcedure.mutation(async ({ ctx }) => {
const handler = (await import("./deleteMeWithoutPassword.handler")).deleteMeWithoutPasswordHandler;
return handler({ ctx });
}),
get,
getUserTopBanners: authedProcedure.query(async ({ ctx }) => {
const handler = (await import("./getUserTopBanners.handler")).getUserTopBannersHandler;
return handler({ ctx });
}),
myStats: authedProcedure.query(async ({ ctx }) => {
const handler = (await import("./myStats.handler")).myStatsHandler;
return handler({ ctx });
}),
platformMe: authedProcedure.query(async ({ ctx }) => {
const handler = (await import("./platformMe.handler")).platformMeHandler;
return handler({ ctx });
}),
shouldVerifyEmail: authedProcedure.query(async ({ ctx }) => {
const handler = (await import("./shouldVerifyEmail.handler")).shouldVerifyEmailHandler;
return handler({ ctx });
}),
});
@@ -8,17 +8,17 @@ import { IdentityProvider, MembershipRole } from "@calcom/prisma/enums";
import { userMetadata } from "@calcom/prisma/zod-utils";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import type { TMeInputSchema } from "./me.schema";
import type { TGetInputSchema } from "./get.schema";
type MeOptions = {
ctx: {
user: NonNullable<TrpcSessionUser>;
session: Session;
};
input: TMeInputSchema;
input: TGetInputSchema;
};
export const meHandler = async ({ ctx, input }: MeOptions) => {
export const getHandler = async ({ ctx, input }: MeOptions) => {
const crypto = await import("crypto");
const { user: sessionUser, session } = ctx;
@@ -1,9 +1,9 @@
import { z } from "zod";
export const ZMeInputSchema = z
export const ZGetInputSchema = z
.object({
includePasswordAdded: z.boolean().optional(),
})
.optional();
export type TMeInputSchema = z.infer<typeof ZMeInputSchema>;
export type TGetInputSchema = z.infer<typeof ZGetInputSchema>;
@@ -4,8 +4,8 @@ import { prisma } from "@calcom/prisma";
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
import type { TrpcSessionUser } from "@calcom/trpc/server/trpc";
import { checkIfOrgNeedsUpgradeHandler } from "../viewer/organizations/checkIfOrgNeedsUpgrade.handler";
import { getUpgradeableHandler } from "../viewer/teams/getUpgradeable.handler";
import { checkIfOrgNeedsUpgradeHandler } from "../organizations/checkIfOrgNeedsUpgrade.handler";
import { getUpgradeableHandler } from "../teams/getUpgradeable.handler";
import { checkInvalidAppCredentials } from "./checkForInvalidAppCredentials";
import { shouldVerifyEmailHandler } from "./shouldVerifyEmail.handler";
@@ -0,0 +1,8 @@
import authedProcedure from "../../../../procedures/authedProcedure";
import { ZGetInputSchema } from "../get.schema";
export const get = authedProcedure.input(ZGetInputSchema).query(async ({ ctx, input }) => {
const handler = (await import("../get.handler")).getHandler;
return handler({ ctx, input });
});
@@ -1,4 +1,4 @@
import authedProcedure from "../../../procedures/authedProcedure";
import authedProcedure from "../../../../procedures/authedProcedure";
export const myStats = authedProcedure.query(async ({ ctx, input }) => {
const handler = (await import("../myStats.handler")).myStatsHandler;
@@ -1,4 +1,4 @@
import authedProcedure from "../../../procedures/authedProcedure";
import authedProcedure from "../../../../procedures/authedProcedure";
export const platformMe = authedProcedure.query(async ({ ctx }) => {
const handler = (await import("../platformMe.handler")).platformMeHandler;