chore: migrate OG image generation API to App Router (#19568)
* migrate * improve apiRouteMiddleware wrapper type * refactor * fix type check * fix build * fix * Do not import stripe lib in getServerErrorFromUnknown * address comment * remove comment * add url * keep the location * remove space * add unit test for getServerErrorFromUnknown * improve * fix
This commit is contained in:
1 parent
c1ef57e3c2
commit
87f161d944
2 files changed
+15
-15
No files matched your search
@@ -6,7 +6,7 @@ import { NextResponse } from "next/server";
|
||||
import { getServerErrorFromUnknown } from "@calcom/lib/server/getServerErrorFromUnknown";
|
||||
|
||||
export const apiRouteMiddleware =
|
||||
(handler: (req: NextRequest, { params }: { params: Params }) => Promise<NextResponse>) =>
|
||||
(handler: (req: NextRequest, { params }: { params: Params }) => Promise<NextResponse | Response>) =>
|
||||
async (req: NextRequest, { params }: { params: Params }) => {
|
||||
try {
|
||||
return await handler(req, { params });
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import { apiRouteMiddleware } from "app/api/apiRouteMiddleware";
|
||||
import type { NextRequest } from "next/server";
|
||||
import { ImageResponse } from "next/server";
|
||||
import type { SatoriOptions } from "satori";
|
||||
import { z } from "zod";
|
||||
|
||||
import { Meeting, App, Generic } from "@calcom/lib/OgImages";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
|
||||
const calFont = fetch(new URL("/fonts/cal.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer());
|
||||
|
||||
const interFont = fetch(new URL("/fonts/Inter-Regular.ttf", WEBAPP_URL)).then((res) => res.arrayBuffer());
|
||||
|
||||
const interFontMedium = fetch(new URL("/fonts/Inter-Medium.ttf", WEBAPP_URL)).then((res) =>
|
||||
res.arrayBuffer()
|
||||
);
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
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(),
|
||||
});
|
||||
|
||||
const genericSchema = z.object({
|
||||
imageType: z.literal("generic"),
|
||||
title: z.string(),
|
||||
description: z.string(),
|
||||
});
|
||||
|
||||
async function handler(req: NextRequest) {
|
||||
const { searchParams } = new URL(`${req.url}`);
|
||||
const imageType = searchParams.get("type");
|
||||
|
||||
const [calFontData, interFontData, interFontMediumData] = await Promise.all([
|
||||
calFont,
|
||||
interFont,
|
||||
interFontMedium,
|
||||
]);
|
||||
const ogConfig = {
|
||||
width: 1200,
|
||||
height: 630,
|
||||
fonts: [
|
||||
{ name: "inter", data: interFontData, weight: 400 },
|
||||
{ name: "inter", data: interFontMediumData, weight: 500 },
|
||||
{ name: "cal", data: calFontData, weight: 400 },
|
||||
{ name: "cal", data: calFontData, weight: 600 },
|
||||
] 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,
|
||||
});
|
||||
|
||||
const img = new ImageResponse(
|
||||
(
|
||||
<Meeting
|
||||
title={title}
|
||||
profile={{ name: meetingProfileName, image: meetingImage }}
|
||||
users={names.map((name, index) => ({ name, username: usernames[index] }))}
|
||||
/>
|
||||
),
|
||||
ogConfig
|
||||
);
|
||||
|
||||
return new Response(img.body, {
|
||||
status: 200,
|
||||
headers: { "Content-Type": "image/png", "cache-control": "max-age=0" },
|
||||
});
|
||||
}
|
||||
case "app": {
|
||||
const { name, description, slug } = appSchema.parse({
|
||||
name: searchParams.get("name"),
|
||||
description: searchParams.get("description"),
|
||||
slug: searchParams.get("slug"),
|
||||
imageType,
|
||||
});
|
||||
const img = new ImageResponse(<App name={name} description={description} slug={slug} />, ogConfig);
|
||||
|
||||
return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } });
|
||||
}
|
||||
|
||||
case "generic": {
|
||||
const { title, description } = genericSchema.parse({
|
||||
title: searchParams.get("title"),
|
||||
description: searchParams.get("description"),
|
||||
imageType,
|
||||
});
|
||||
|
||||
const img = new ImageResponse(<Generic title={title} description={description} />, ogConfig);
|
||||
|
||||
return new Response(img.body, { status: 200, headers: { "Content-Type": "image/png" } });
|
||||
}
|
||||
|
||||
default:
|
||||
return new Response("What you're looking for is not here..", { status: 404 });
|
||||
}
|
||||
}
|
||||
|
||||
const getHandler = apiRouteMiddleware(handler);
|
||||
|
||||
export { getHandler as GET };
|
||||
Reference in new issue
Block a user