add query params parser util (#496)

This commit is contained in:
Alex Johansson 2021-08-19 14:32:51 +02:00 committed by GitHub
parent f63aa5d550
commit a8ef195546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

3
lib/asStringOrNull.tsx Normal file
View File

@ -0,0 +1,3 @@
export function asStringOrNull(str: unknown) {
return typeof str === "string" ? str : null;
}

View File

@ -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: {