-import { createOgRoutes } from '@utils/createOgRoutes';
+import type { APIRoute } from 'astro';
+import { getCollection } from 'astro:content';
import formatDate from '@utils/formatDate';
+import generateOpenGraphImage from '@utils/generateOpenGraphImage';
-export const { getStaticPaths, GET } = createOgRoutes(
- 'articles',
- (article) => ({
- date: formatDate(article.data.date),
- id: article.id,
- }),
-);
+export const GET = (async ({ props, url }) => {
+ const entry = props.entry;
+ return generateOpenGraphImage(
+ entry.data.title,
+ entry.data.description,
+ url.origin,
+ );
+}) satisfies APIRoute;
+
+export async function getStaticPaths() {
+ const articles = await getCollection('articles');
+ return articles.map((article) => ({
+ params: { date: formatDate(article.data.date), id: article.id },
+ props: { entry: article },
+ }));
+}
-import { createOgRoutes } from '@utils/createOgRoutes';
+import type { APIRoute } from 'astro';
+import { getCollection } from 'astro:content';
+import generateOpenGraphImage from '@utils/generateOpenGraphImage';
-export const { getStaticPaths, GET } = createOgRoutes(
- 'podcasts',
- (podcast) => ({
- id: podcast.id,
- }),
-);
+export const GET = (async ({ props, url }) => {
+ const entry = props.entry;
+ return generateOpenGraphImage(
+ entry.data.title,
+ entry.data.description,
+ url.origin,
+ );
+}) satisfies APIRoute;
+
+export async function getStaticPaths() {
+ const podcasts = await getCollection('podcasts');
+ return podcasts.map((podcast) => ({
+ params: { id: podcast.id },
+ props: { entry: podcast },
+ }));
+}
-import { createOgRoutes } from '@utils/createOgRoutes';
+import type { APIRoute } from 'astro';
+import { getCollection } from 'astro:content';
+import generateOpenGraphImage from '@utils/generateOpenGraphImage';
import generateStarRating from '@utils/generateStarRating';
-export const { getStaticPaths, GET } = createOgRoutes(
- 'reviews',
- (review) => ({
- category: review.data.category,
- id: review.id,
- }),
- (review) => generateStarRating(review.data.rating) ?? '',
-);
+export const GET = (async ({ props, url }) => {
+ const entry = props.entry;
+ return generateOpenGraphImage(
+ entry.data.title,
+ generateStarRating(entry.data.rating),
+ url.origin,
+ );
+}) satisfies APIRoute;
+
+export async function getStaticPaths() {
+ const reviews = await getCollection('reviews');
+ return reviews.map((review) => ({
+ params: { category: review.data.category, id: review.id },
+ props: { entry: review },
+ }));
+}
+++ /dev/null
-import type { APIRoute, GetStaticPaths } from 'astro';
-import { getCollection, type CollectionEntry } from 'astro:content';
-import generateOpenGraphImage from '@utils/generateOpenGraphImage';
-
-type SiteCollection = 'articles' | 'podcasts' | 'reviews';
-
-export function createOgRoutes<T extends SiteCollection>(
- collectionName: T,
- getParams: (entry: CollectionEntry<T>) => Record<string, string>,
- getDescription?: (entry: CollectionEntry<T>) => string,
-) {
- const getStaticPaths: GetStaticPaths = async () => {
- const entries = await getCollection(collectionName);
- return entries.map((entry) => ({
- params: getParams(entry),
- props: { entry },
- }));
- };
-
- const GET: APIRoute = async ({ props, url }) => {
- if (!props.entry) {
- throw new Error('Missing entry prop in OG route');
- }
- const entry = props.entry as CollectionEntry<T>;
- const description = getDescription
- ? getDescription(entry)
- : ((entry.data as { description?: string }).description ?? '');
- return generateOpenGraphImage(entry.data.title, description, url.origin);
- };
-
- return { getStaticPaths, GET };
-}