wishlist-app/src/api/routes/utils/opengraph.ts
Benny Samir Hierl ea0be6fe69 #4 api to load open graph metadata added
Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
2022-02-06 14:40:37 +01:00

48 lines
1.1 KiB
TypeScript

import { FastifyRequest, FastifyReply, RouteOptions } from 'fastify'
import ogs from 'open-graph-scraper'
interface fetchOpenGraphRequest extends FastifyRequest {
query: {
url: string
}
}
export const fetchOpenGraph = <RouteOptions>{
method: 'GET',
url: '/fetch-open-graph',
schema: {
querystring: {
type: 'object',
required: ['url'],
properties: {
url: { type: 'string', format: 'uri' },
},
},
response: {
200: {
type: 'object',
properties: {
title: { type: 'string' },
description: { type: 'string' },
image: { type: 'string' },
},
},
},
},
handler: async (request: fetchOpenGraphRequest, reply: FastifyReply) => {
const { result } = await ogs({
url: request.query.url,
})
request.log.debug(result)
if (result.success) {
const image =
//@ts-expect-error: url not defined
result.ogImage && result.ogImage.url ? result.ogImage.url : ''
reply.send({
title: result.ogTitle || '',
description: result.ogDescription || '',
image,
})
}
},
}