2022-10-18 17:46:22 +00:00
|
|
|
import { ImageResponse } from "@vercel/og";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-10-18 17:46:22 +00:00
|
|
|
import type { SatoriOptions } from "satori";
|
|
|
|
import { z } from "zod";
|
|
|
|
|
2022-11-01 08:16:08 +00:00
|
|
|
import { Meeting, App, Generic } from "@calcom/lib/OgImages";
|
2022-10-18 17:46:22 +00:00
|
|
|
|
|
|
|
const calFont = fetch(new URL("../../../../public/fonts/cal.ttf", import.meta.url)).then((res) =>
|
|
|
|
res.arrayBuffer()
|
|
|
|
);
|
|
|
|
|
|
|
|
const interFont = fetch(new URL("../../../../public/fonts/Inter-Regular.ttf", import.meta.url)).then((res) =>
|
|
|
|
res.arrayBuffer()
|
|
|
|
);
|
|
|
|
|
|
|
|
const interFontMedium = fetch(new URL("../../../../public/fonts/Inter-Medium.ttf", import.meta.url)).then(
|
|
|
|
(res) => res.arrayBuffer()
|
|
|
|
);
|
|
|
|
|
|
|
|
export const config = {
|
2023-02-28 23:38:46 +00:00
|
|
|
runtime: "edge",
|
2022-10-18 17:46:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const meetingSchema = z.object({
|
|
|
|
imageType: z.literal("meeting"),
|
|
|
|
title: z.string(),
|
|
|
|
names: z.string().array(),
|
|
|
|
usernames: z.string().array(),
|
|
|
|
meetingProfileName: z.string(),
|
|
|
|
meetingImage: z.string().nullable().optional(),
|
|
|
|
});
|
|
|
|
|
|
|
|
const appSchema = z.object({
|
|
|
|
imageType: z.literal("app"),
|
|
|
|
name: z.string(),
|
|
|
|
description: z.string(),
|
|
|
|
slug: z.string(),
|
|
|
|
});
|
|
|
|
|
2022-11-01 08:16:08 +00:00
|
|
|
const genericSchema = z.object({
|
|
|
|
imageType: z.literal("generic"),
|
|
|
|
title: z.string(),
|
|
|
|
description: z.string(),
|
|
|
|
});
|
|
|
|
|
2022-10-18 17:46:22 +00:00
|
|
|
export default async function handler(req: NextApiRequest) {
|
|
|
|
const { searchParams } = new URL(`${req.url}`);
|
|
|
|
const imageType = searchParams.get("type");
|
|
|
|
|
|
|
|
const [calFontData, interFontData, interFontMediumData] = await Promise.all([
|
|
|
|
calFont,
|
|
|
|
interFont,
|
|
|
|
interFontMedium,
|
|
|
|
]);
|
|
|
|
const ogConfig = {
|
2022-12-13 21:22:38 +00:00
|
|
|
width: 1200,
|
|
|
|
height: 630,
|
2022-10-18 17:46:22 +00:00
|
|
|
fonts: [
|
|
|
|
{ name: "inter", data: interFontData, weight: 400 },
|
|
|
|
{ name: "inter", data: interFontMediumData, weight: 500 },
|
|
|
|
{ name: "cal", data: calFontData, weight: 400 },
|
2022-10-25 00:20:01 +00:00
|
|
|
{ name: "cal", data: calFontData, weight: 600 },
|
2022-10-18 17:46:22 +00:00
|
|
|
] as SatoriOptions["fonts"],
|
|
|
|
};
|
|
|
|
|
|
|
|
switch (imageType) {
|
|
|
|
case "meeting": {
|
|
|
|
const { names, usernames, title, meetingProfileName, meetingImage } = meetingSchema.parse({
|
|
|
|
names: searchParams.getAll("names"),
|
|
|
|
usernames: searchParams.getAll("usernames"),
|
|
|
|
title: searchParams.get("title"),
|
|
|
|
meetingProfileName: searchParams.get("meetingProfileName"),
|
|
|
|
meetingImage: searchParams.get("meetingImage"),
|
|
|
|
imageType,
|
|
|
|
});
|
2022-12-13 21:22:38 +00:00
|
|
|
const img = new ImageResponse(
|
2022-10-18 17:46:22 +00:00
|
|
|
(
|
|
|
|
<Meeting
|
|
|
|
title={title}
|
|
|
|
profile={{ name: meetingProfileName, image: meetingImage }}
|
|
|
|
users={names.map((name, index) => ({ name, username: usernames[index] }))}
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
ogConfig
|
2022-12-13 21:22:38 +00:00
|
|
|
) as { body: Buffer };
|
|
|
|
|
|
|
|
return new Response(img.body, { status: 200 });
|
2022-10-18 17:46:22 +00:00
|
|
|
}
|
|
|
|
case "app": {
|
|
|
|
const { name, description, slug } = appSchema.parse({
|
|
|
|
name: searchParams.get("name"),
|
|
|
|
description: searchParams.get("description"),
|
|
|
|
slug: searchParams.get("slug"),
|
|
|
|
imageType,
|
|
|
|
});
|
2022-12-13 21:22:38 +00:00
|
|
|
const img = new ImageResponse(<App name={name} description={description} slug={slug} />, ogConfig) as {
|
|
|
|
body: Buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Response(img.body, { status: 200 });
|
2022-10-18 17:46:22 +00:00
|
|
|
}
|
2022-11-01 08:16:08 +00:00
|
|
|
|
|
|
|
case "generic": {
|
|
|
|
const { title, description } = genericSchema.parse({
|
|
|
|
title: searchParams.get("title"),
|
|
|
|
description: searchParams.get("description"),
|
|
|
|
imageType,
|
|
|
|
});
|
|
|
|
|
2022-12-13 21:22:38 +00:00
|
|
|
const img = new ImageResponse(<Generic title={title} description={description} />, ogConfig) as {
|
|
|
|
body: Buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Response(img.body, { status: 200 });
|
2022-11-01 08:16:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 17:46:22 +00:00
|
|
|
default:
|
|
|
|
return new Response("What you're looking for is not here..", { status: 404 });
|
|
|
|
}
|
|
|
|
}
|