import { cache } from "react"; import { detectLanguage } from "@/app/common/lang-detection/detection"; import { Metadata } from "next"; export type Paste = { /** * The paste content. */ content: string; /** * The date the paste was created. */ created: string; /** * The detected language of the paste. */ language: string; }; /** * Fetches a paste from the API. */ export const getPaste = cache(async (id: string) => { const response: Response = await fetch( `${process.env.NEXT_PUBLIC_API_ENDPOINT}/${id}`, { next: { revalidate: 300, // Keep this response cached for 5 minutes }, }, ); const json = await response.json(); if (json.code && json.message) { return undefined; } return { content: json.content, created: json.created, language: await detectLanguage(json.content), }; }) as (id: string) => Promise; /** * Generates metadata for a paste. * * @param id The ID of the paste. */ export async function generatePasteMetadata(id: string): Promise { const data: Paste | undefined = await getPaste(id); if (data == undefined) { return { description: "Not found", }; } return { title: `Paste - ${id}.${data.language}`, description: `Click to view the paste.`, }; }