import { ImageResponse } from 'next/og'; import { NextResponse } from 'next/server'; import { P, match } from 'ts-pattern'; import type { ShareHandlerAPIResponse } from '~/pages/api/share'; export const runtime = 'edge'; const CARD_OFFSET_TOP = 173; const CARD_OFFSET_LEFT = 307; const CARD_WIDTH = 590; const CARD_HEIGHT = 337; const IMAGE_SIZE = { width: 1200, height: 630, }; type SharePageOpenGraphImageProps = { params: { slug: string }; }; export async function GET(_request: Request, { params: { slug } }: SharePageOpenGraphImageProps) { const [interSemiBold, interRegular, caveatRegular, shareFrameImage] = await Promise.all([ fetch(new URL('@documenso/assets/fonts/inter-semibold.ttf', import.meta.url)).then( async (res) => res.arrayBuffer(), ), fetch(new URL('@documenso/assets/fonts/inter-regular.ttf', import.meta.url)).then(async (res) => res.arrayBuffer(), ), fetch(new URL('@documenso/assets/fonts/caveat-regular.ttf', import.meta.url)).then( async (res) => res.arrayBuffer(), ), fetch(new URL('@documenso/assets/static/og-share-frame2.png', import.meta.url)).then( async (res) => res.arrayBuffer(), ), ]); const baseUrl = process.env.NEXT_PUBLIC_WEBAPP_URL || 'http://localhost:3000'; const recipientOrSender: ShareHandlerAPIResponse = await fetch( new URL(`/api/share?slug=${slug}`, baseUrl), ).then(async (res) => res.json()); if ('error' in recipientOrSender) { return NextResponse.json({ error: 'Not found' }, { status: 404 }); } const isRecipient = 'Signature' in recipientOrSender; const signatureImage = match(recipientOrSender) .with({ Signature: P.array(P._) }, (recipient) => { return recipient.Signature?.[0]?.signatureImageAsBase64 || null; }) .otherwise((sender) => { return sender.signature || null; }); const signatureName = match(recipientOrSender) .with({ Signature: P.array(P._) }, (recipient) => { return recipient.name || recipient.email; }) .otherwise((sender) => { return sender.name || sender.email; }); return new ImageResponse( (
{signatureName}
)}