2021-09-27 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* This file contains tRPC's HTTP response handler
|
|
|
|
*/
|
2023-02-14 15:01:34 +00:00
|
|
|
import { z } from "zod";
|
|
|
|
|
2023-03-10 23:45:24 +00:00
|
|
|
import { getServerSession } from "@calcom/features/auth/lib/getServerSession";
|
2022-07-22 17:27:06 +00:00
|
|
|
import * as trpcNext from "@calcom/trpc/server/adapters/next";
|
2023-03-10 23:45:24 +00:00
|
|
|
import { createContext as createTrpcContext } from "@calcom/trpc/server/createContext";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { appRouter } from "@calcom/trpc/server/routers/_app";
|
2021-09-27 14:47:55 +00:00
|
|
|
|
|
|
|
export default trpcNext.createNextApiHandler({
|
|
|
|
router: appRouter,
|
|
|
|
/**
|
|
|
|
* @link https://trpc.io/docs/context
|
|
|
|
*/
|
2023-03-10 23:45:24 +00:00
|
|
|
createContext: ({ req, res }) => {
|
|
|
|
const sessionGetter = () => getServerSession({ req, res });
|
|
|
|
|
|
|
|
return createTrpcContext({ req, res }, sessionGetter);
|
|
|
|
},
|
2021-09-27 14:47:55 +00:00
|
|
|
/**
|
|
|
|
* @link https://trpc.io/docs/error-handling
|
|
|
|
*/
|
|
|
|
onError({ error }) {
|
|
|
|
if (error.code === "INTERNAL_SERVER_ERROR") {
|
|
|
|
// send to bug reporting
|
|
|
|
console.error("Something went wrong", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Enable query batching
|
|
|
|
*/
|
|
|
|
batching: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @link https://trpc.io/docs/caching#api-response-caching
|
|
|
|
*/
|
2022-06-19 15:02:00 +00:00
|
|
|
responseMeta({ ctx, paths, type, errors }) {
|
2023-02-14 15:01:34 +00:00
|
|
|
// Some helpers relevant to this function only
|
|
|
|
const ONE_DAY_IN_SECONDS = 60 * 60 * 24;
|
|
|
|
// assuming you have all your public routes with the keyword `public` in them
|
2023-02-10 21:06:52 +00:00
|
|
|
const allPublic = paths && paths.every((path) => path.startsWith("viewer.public."));
|
|
|
|
// checking that no procedures errored
|
|
|
|
const allOk = errors.length === 0;
|
|
|
|
// checking we're doing a query request
|
|
|
|
const isQuery = type === "query";
|
2023-02-14 15:01:34 +00:00
|
|
|
const noHeaders = {};
|
2023-02-09 00:35:43 +00:00
|
|
|
|
2023-02-14 15:01:34 +00:00
|
|
|
// We cannot set headers on SSG queries
|
|
|
|
if (!ctx?.res) return noHeaders;
|
2023-02-09 01:12:45 +00:00
|
|
|
|
2023-02-14 15:01:34 +00:00
|
|
|
const defaultHeaders: Record<"headers", Record<string, string>> = {
|
|
|
|
headers: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const timezone = z.string().safeParse(ctx.req?.headers["x-vercel-ip-timezone"]);
|
|
|
|
if (timezone.success) defaultHeaders.headers["x-cal-timezone"] = timezone.data;
|
|
|
|
|
|
|
|
// We need all these conditions to be true to set cache headers
|
|
|
|
if (!(allPublic && allOk && isQuery)) return defaultHeaders;
|
|
|
|
|
|
|
|
// No cache by default
|
|
|
|
defaultHeaders.headers["cache-control"] = `no-cache`;
|
|
|
|
|
|
|
|
// Our cache can change depending on our current paths value. Since paths is an array,
|
|
|
|
// we want to create a map that can match potential paths with their desired cache value
|
|
|
|
const cacheRules = {
|
|
|
|
"viewer.public.session": `no-cache`,
|
|
|
|
"viewer.public.i18n": `no-cache`,
|
|
|
|
// Revalidation time here should be 1 second, per https://github.com/calcom/cal.com/pull/6823#issuecomment-1423215321
|
2023-02-14 17:07:58 +00:00
|
|
|
"viewer.public.slots.getSchedule": `no-cache`, // FIXME
|
2023-03-02 20:42:41 +00:00
|
|
|
"viewer.public.cityTimezones": `max-age=${ONE_DAY_IN_SECONDS}, stale-while-revalidate`,
|
2023-02-14 15:01:34 +00:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
// Find which element above is an exact match for this group of paths
|
|
|
|
const matchedPath = paths.find((v) => v in cacheRules) as keyof typeof cacheRules;
|
|
|
|
|
|
|
|
if (matchedPath) defaultHeaders.headers["cache-control"] = cacheRules[matchedPath];
|
|
|
|
|
|
|
|
return defaultHeaders;
|
2022-06-19 15:02:00 +00:00
|
|
|
},
|
2021-09-27 14:47:55 +00:00
|
|
|
});
|