elk/server/api/og-image.ts

56 lines
1.4 KiB
TypeScript
Raw Normal View History

2022-12-12 17:55:57 +03:00
import opengraph from 'opengraph-io'
2022-12-12 16:26:30 +03:00
// This API-Endpoint will be cached via nuxt.config.ts -> nitro.routeRules['/api/og-image'].cache.maxAge = 86400
2022-12-12 17:55:57 +03:00
let openGraphClient: any = null
function getOpenGraphClient(): any {
if (openGraphClient == null)
openGraphClient = opengraph({ appId: process.env.NUXT_OPENGRAPH_API, fullRender: true })
return openGraphClient
2022-12-12 16:26:30 +03:00
}
2022-12-11 01:09:11 +03:00
2022-12-12 18:00:26 +03:00
function extractOgImageUrl(html: string): string {
const match = html.match(/<meta property="og:image" content="([^"]+)" \/>/)
return match?.[1] ?? ''
}
2022-12-11 01:09:11 +03:00
export default defineEventHandler(async (event) => {
const { cardUrl } = getQuery(event)
if (!cardUrl) {
throw createError({
statusCode: 422,
statusMessage: 'Missing cardUrl.',
})
}
if (typeof cardUrl !== 'string') {
throw createError({
statusCode: 422,
statusMessage: 'cardUrl must be string.',
})
}
2022-12-12 18:00:26 +03:00
let ogImageUrl = ''
// First we want to try to get the og:image from the html
// But sometimes it is not included due to async JS loading
const html = await $fetch<string>(cardUrl)
ogImageUrl = extractOgImageUrl(html)
2022-12-12 17:55:57 +03:00
2022-12-12 18:00:26 +03:00
// If no og:image was found, try to get it from opengraph.io
if (!ogImageUrl) {
const response = await getOpenGraphClient().getSiteInfo(cardUrl)
ogImageUrl = response?.openGraph?.image?.url ?? ''
}
2022-12-12 17:55:57 +03:00
2022-12-12 18:00:26 +03:00
// eslint-disable-next-line no-console
2022-12-12 17:55:57 +03:00
console.log(JSON.stringify({ cardUrl, ogImageUrl }))
2022-12-11 01:09:11 +03:00
await send(event, ogImageUrl)
})
2022-12-12 16:26:30 +03:00