Release 2025-05-20-0D8p

This commit is contained in:
pluja
2025-05-20 01:47:50 +00:00
parent 587480d140
commit af3df8f79a
35 changed files with 1091 additions and 235 deletions

View File

@@ -1,19 +1,31 @@
import { ogImageTemplates } from '../components/OgImage'
import { urlParamsToObject } from '../lib/urls'
import { ogImageTemplates, type OgImageAllTemplatesWithProps } from '../components/OgImage'
import type { APIRoute } from 'astro'
import type { Misc } from 'ts-toolbelt'
export const GET: APIRoute = (context) => {
const { template, ...props } = urlParamsToObject(context.url.searchParams)
function toJSON<T extends Misc.JSON.Value>(data: string | null | undefined): T | undefined {
if (!data) return undefined
try {
return JSON.parse(data) as T
} catch (_error) {
return undefined
}
}
if (!template) return ogImageTemplates.default({}, context)
export const GET: APIRoute = async (context) => {
const { template, ...props } = toJSON<OgImageAllTemplatesWithProps>(
context.url.searchParams.get('data')
) ?? { template: 'default' }
if (!template as unknown) return ogImageTemplates.default({}, context)
if (!(template in ogImageTemplates)) {
console.error(`Invalid template: "${template}"`)
return ogImageTemplates.default({}, context)
}
const response = ogImageTemplates[template as keyof typeof ogImageTemplates](props, context)
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
const response = await ogImageTemplates[template](props as any, context)
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (!response) {
console.error(`Cannot generate image for template: ${template} and props: ${JSON.stringify(props)}`)