Release 2025-05-20-0D8p
This commit is contained in:
@@ -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)}`)
|
||||
|
||||
Reference in New Issue
Block a user