All checks were successful
Deploy App / docker (ubuntu-latest) (push) Successful in 1m4s
64 lines
1.3 KiB
TypeScript
64 lines
1.3 KiB
TypeScript
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<Paste | undefined>;
|
|
|
|
/**
|
|
* Generates metadata for a paste.
|
|
*
|
|
* @param id The ID of the paste.
|
|
*/
|
|
export async function generatePasteMetadata(id: string): Promise<Metadata> {
|
|
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.`,
|
|
};
|
|
}
|