Skip to content

Scheduling infrastructure for absolutely everyone.

fix: Reduced boilerplate for publicViewer router (#10849)

Omar López42cd56578d00
diff --git a/packages/emails/src/components/LocationInfo.tsx b/packages/emails/src/components/LocationInfo.tsx
index 2662cbc3025..2388ce432e4 100644
--- a/packages/emails/src/components/LocationInfo.tsx
+++ b/packages/emails/src/components/LocationInfo.tsx
@@ -5,7 +5,6 @@ import { getVideoCallUrlFromCalEvent } from "@calcom/lib/CalEventParser";
import logger from "@calcom/lib/logger";
import type { CalendarEvent } from "@calcom/types/Calendar";
-import { CallToActionIcon } from "./CallToActionIcon";
import { Info } from "./Info";
export function LocationInfo(props: { calEvent: CalendarEvent; t: TFunction }) {
diff --git a/packages/trpc/server/routers/publicViewer/_router.tsx b/packages/trpc/server/routers/publicViewer/_router.tsx
index 9688a83d0f2..b0d0e4cfd30 100644
--- a/packages/trpc/server/routers/publicViewer/_router.tsx
+++ b/packages/trpc/server/routers/publicViewer/_router.tsx
@@ -1,136 +1,52 @@
import sessionMiddleware from "../../middlewares/sessionMiddleware";
import publicProcedure from "../../procedures/publicProcedure";
-import { router } from "../../trpc";
+import { importHandler, router } from "../../trpc";
import { slotsRouter } from "../viewer/slots/_router";
import { ZEventInputSchema } from "./event.schema";
import { i18nInputSchema } from "./i18n.schema";
import { ZSamlTenantProductInputSchema } from "./samlTenantProduct.schema";
import { ZStripeCheckoutSessionInputSchema } from "./stripeCheckoutSession.schema";
-type PublicViewerRouterHandlerCache = {
- session?: typeof import("./session.handler").sessionHandler;
- i18n?: typeof import("./i18n.handler").i18nHandler;
- countryCode?: typeof import("./countryCode.handler").countryCodeHandler;
- samlTenantProduct?: typeof import("./samlTenantProduct.handler").samlTenantProductHandler;
- stripeCheckoutSession?: typeof import("./stripeCheckoutSession.handler").stripeCheckoutSessionHandler;
- cityTimezones?: typeof import("./cityTimezones.handler").cityTimezonesHandler;
- event?: typeof import("./event.handler").eventHandler;
-};
+const NAMESPACE = "publicViewer";
-const UNSTABLE_HANDLER_CACHE: PublicViewerRouterHandlerCache = {};
+const namespaced = (s: string) => `${NAMESPACE}.${s}`;
// things that unauthenticated users can query about themselves
export const publicViewerRouter = router({
- session: publicProcedure.use(sessionMiddleware).query(async ({ ctx }) => {
- if (!UNSTABLE_HANDLER_CACHE.session) {
- UNSTABLE_HANDLER_CACHE.session = await import("./session.handler").then((mod) => mod.sessionHandler);
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.session) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.session({
- ctx,
- });
+ session: publicProcedure.use(sessionMiddleware).query(async (opts) => {
+ const handler = await importHandler(namespaced("session"), () => import("./session.handler"));
+ return handler(opts);
}),
-
- i18n: publicProcedure.input(i18nInputSchema).query(async ({ ctx, input }) => {
- if (!UNSTABLE_HANDLER_CACHE.i18n) {
- UNSTABLE_HANDLER_CACHE.i18n = await import("./i18n.handler").then((mod) => mod.i18nHandler);
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.i18n) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.i18n({ ctx, input });
+ i18n: publicProcedure.input(i18nInputSchema).query(async (opts) => {
+ const handler = await importHandler(namespaced("i18n"), () => import("./i18n.handler"));
+ return handler(opts);
}),
-
- countryCode: publicProcedure.query(async ({ ctx }) => {
- if (!UNSTABLE_HANDLER_CACHE.countryCode) {
- UNSTABLE_HANDLER_CACHE.countryCode = await import("./countryCode.handler").then(
- (mod) => mod.countryCodeHandler
- );
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.countryCode) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.countryCode({
- ctx,
- });
+ countryCode: publicProcedure.query(async (opts) => {
+ const handler = await importHandler(namespaced("countryCode"), () => import("./countryCode.handler"));
+ return handler(opts);
}),
-
- samlTenantProduct: publicProcedure.input(ZSamlTenantProductInputSchema).mutation(async ({ ctx, input }) => {
- if (!UNSTABLE_HANDLER_CACHE.samlTenantProduct) {
- UNSTABLE_HANDLER_CACHE.samlTenantProduct = await import("./samlTenantProduct.handler").then(
- (mod) => mod.samlTenantProductHandler
- );
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.samlTenantProduct) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.samlTenantProduct({
- ctx,
- input,
- });
+ samlTenantProduct: publicProcedure.input(ZSamlTenantProductInputSchema).mutation(async (opts) => {
+ const handler = await importHandler(
+ namespaced("samlTenantProduct"),
+ () => import("./samlTenantProduct.handler")
+ );
+ return handler(opts);
}),
-
- stripeCheckoutSession: publicProcedure.input(ZStripeCheckoutSessionInputSchema).query(async ({ input }) => {
- if (!UNSTABLE_HANDLER_CACHE.stripeCheckoutSession) {
- UNSTABLE_HANDLER_CACHE.stripeCheckoutSession = await import("./stripeCheckoutSession.handler").then(
- (mod) => mod.stripeCheckoutSessionHandler
- );
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.stripeCheckoutSession) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.stripeCheckoutSession({
- input,
- });
+ stripeCheckoutSession: publicProcedure.input(ZStripeCheckoutSessionInputSchema).query(async (opts) => {
+ const handler = await importHandler(
+ namespaced("stripeCheckoutSession"),
+ () => import("./stripeCheckoutSession.handler")
+ );
+ return handler(opts);
}),
-
cityTimezones: publicProcedure.query(async () => {
- if (!UNSTABLE_HANDLER_CACHE.cityTimezones) {
- UNSTABLE_HANDLER_CACHE.cityTimezones = await import("./cityTimezones.handler").then(
- (mod) => mod.cityTimezonesHandler
- );
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.cityTimezones) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.cityTimezones();
+ const handler = await importHandler(namespaced("cityTimezones"), () => import("./cityTimezones.handler"));
+ return handler();
}),
-
// REVIEW: This router is part of both the public and private viewer router?
slots: slotsRouter,
- event: publicProcedure.input(ZEventInputSchema).query(async ({ ctx, input }) => {
- if (!UNSTABLE_HANDLER_CACHE.event) {
- UNSTABLE_HANDLER_CACHE.event = await import("./event.handler").then((mod) => mod.eventHandler);
- }
-
- // Unreachable code but required for type safety
- if (!UNSTABLE_HANDLER_CACHE.event) {
- throw new Error("Failed to load handler");
- }
-
- return UNSTABLE_HANDLER_CACHE.event({
- ctx,
- input,
- });
+ event: publicProcedure.input(ZEventInputSchema).query(async (opts) => {
+ const handler = await importHandler(namespaced("event"), () => import("./event.handler"));
+ return handler(opts);
}),
});
diff --git a/packages/trpc/server/routers/publicViewer/cityTimezones.handler.ts b/packages/trpc/server/routers/publicViewer/cityTimezones.handler.ts
index 7dc896aedc3..9d8d0663e4e 100644
--- a/packages/trpc/server/routers/publicViewer/cityTimezones.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/cityTimezones.handler.ts
@@ -28,3 +28,5 @@ export const cityTimezonesHandler = async () => {
return uniqueCities;
};
+
+export default cityTimezonesHandler;
diff --git a/packages/trpc/server/routers/publicViewer/countryCode.handler.ts b/packages/trpc/server/routers/publicViewer/countryCode.handler.ts
index ca72cdd95a3..ed67a3d959c 100644
--- a/packages/trpc/server/routers/publicViewer/countryCode.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/countryCode.handler.ts
@@ -10,3 +10,5 @@ export const countryCodeHandler = async ({ ctx }: CountryCodeOptions) => {
const countryCode: string | string[] = req?.headers?.["x-vercel-ip-country"] ?? "";
return { countryCode: Array.isArray(countryCode) ? countryCode[0] : countryCode };
};
+
+export default countryCodeHandler;
diff --git a/packages/trpc/server/routers/publicViewer/event.handler.ts b/packages/trpc/server/routers/publicViewer/event.handler.ts
index 58c0752fa2c..1552d085c6b 100644
--- a/packages/trpc/server/routers/publicViewer/event.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/event.handler.ts
@@ -18,3 +18,5 @@ export const eventHandler = async ({ ctx, input }: EventHandlerOptions) => {
);
return event;
};
+
+export default eventHandler;
diff --git a/packages/trpc/server/routers/publicViewer/i18n.handler.ts b/packages/trpc/server/routers/publicViewer/i18n.handler.ts
index 3b5e9556d53..77593be0410 100644
--- a/packages/trpc/server/routers/publicViewer/i18n.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/i18n.handler.ts
@@ -21,3 +21,5 @@ export const i18nHandler = async ({ input }: I18nOptions) => {
locale,
};
};
+
+export default i18nHandler;
diff --git a/packages/trpc/server/routers/publicViewer/samlTenantProduct.handler.ts b/packages/trpc/server/routers/publicViewer/samlTenantProduct.handler.ts
index 23f58730dff..584fb5176d2 100644
--- a/packages/trpc/server/routers/publicViewer/samlTenantProduct.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/samlTenantProduct.handler.ts
@@ -16,3 +16,5 @@ export const samlTenantProductHandler = ({ ctx, input }: SamlTenantProductOption
return samlTenantProduct(prisma, email);
};
+
+export default samlTenantProductHandler;
diff --git a/packages/trpc/server/routers/publicViewer/session.handler.ts b/packages/trpc/server/routers/publicViewer/session.handler.ts
index 1b4b4454f06..d64cfa45d83 100644
--- a/packages/trpc/server/routers/publicViewer/session.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/session.handler.ts
@@ -9,3 +9,5 @@ type SessionOptions = {
export const sessionHandler = async ({ ctx }: SessionOptions) => {
return ctx.session;
};
+
+export default sessionHandler;
diff --git a/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts b/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts
index c317a0b22a4..6ff5ac1aa61 100644
--- a/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts
+++ b/packages/trpc/server/routers/publicViewer/stripeCheckoutSession.handler.ts
@@ -61,3 +61,5 @@ export const stripeCheckoutSessionHandler = async ({ input }: StripeCheckoutSess
};
}
};
+
+export default stripeCheckoutSessionHandler;