Skip to content

Scheduling infrastructure for absolutely everyone.

add query params parser util (#496)

Alex Johanssona8ef19554617
diff --git a/lib/asStringOrNull.tsx b/lib/asStringOrNull.tsx
new file mode 100644
index 00000000000..5b661e420e9
--- /dev/null
+++ b/lib/asStringOrNull.tsx
@@ -0,0 +1,3 @@
+export function asStringOrNull(str: unknown) {
+ return typeof str === "string" ? str : null;
+}
diff --git a/pages/[user]/[type].tsx b/pages/[user]/[type].tsx
index 3ca2b93b6b5..2d96c38cc1b 100644
--- a/pages/[user]/[type].tsx
+++ b/pages/[user]/[type].tsx
@@ -16,6 +16,7 @@ import TimeOptions from "../../components/booking/TimeOptions";
import PoweredByCalendso from "../../components/ui/PoweredByCalendso";
import { timeZone } from "../../lib/clock";
import { collectPageParameters, telemetryEventTypes, useTelemetry } from "../../lib/telemetry";
+import { asStringOrNull } from "@lib/asStringOrNull";
export default function Type(props: InferGetServerSidePropsType<typeof getServerSideProps>) {
// Get router variables
@@ -238,9 +239,13 @@ export default function Type(props: InferGetServerSidePropsType<typeof getServer
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
// get query params and typecast them to string
// (would be even better to assert them instead of typecasting)
- const userParam = context.query.user as string;
- const typeParam = context.query.type as string;
- const dateParam = context.query.date as string | undefined;
+ const userParam = asStringOrNull(context.query.user);
+ const typeParam = asStringOrNull(context.query.type);
+ const dateParam = asStringOrNull(context.query.date);
+
+ if (!userParam || !typeParam) {
+ throw new Error(`File is not named [type]/[user]`);
+ }
const user = await prisma.user.findFirst({
where: {