#4 api to load open graph metadata added

Signed-off-by: Benny Samir Hierl <bennysamir@posteo.de>
This commit is contained in:
Benny Samir Hierl 2022-02-06 14:40:37 +01:00
parent 0e7cd01428
commit ea0be6fe69
5 changed files with 720 additions and 40 deletions

698
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -25,6 +25,7 @@
"fastify-cors": "^6.0.2", "fastify-cors": "^6.0.2",
"fastify-helmet": "^7.0.1", "fastify-helmet": "^7.0.1",
"fastify-static": "^4.5.0", "fastify-static": "^4.5.0",
"open-graph-scraper": "^4.11.0",
"vue": "^3.2.27", "vue": "^3.2.27",
"vue-i18n": "^9.2.0-beta.30", "vue-i18n": "^9.2.0-beta.30",
"vue-router": "^4.0.12" "vue-router": "^4.0.12"
@ -32,6 +33,7 @@
"devDependencies": { "devDependencies": {
"@rushstack/eslint-patch": "^1.1.0", "@rushstack/eslint-patch": "^1.1.0",
"@types/node": "^16.11.21", "@types/node": "^16.11.21",
"@types/open-graph-scraper": "^4.8.1",
"@vitejs/plugin-vue": "^2.0.1", "@vitejs/plugin-vue": "^2.0.1",
"@vue/eslint-config-prettier": "^7.0.0", "@vue/eslint-config-prettier": "^7.0.0",
"@vue/eslint-config-typescript": "^10.0.0", "@vue/eslint-config-typescript": "^10.0.0",
@ -42,6 +44,7 @@
"eslint": "^8.5.0", "eslint": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0", "eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-vue": "^8.2.0", "eslint-plugin-vue": "^8.2.0",
"husky": "^7.0.0",
"jsdom": "^19.0.0", "jsdom": "^19.0.0",
"nodemon": "^2.0.15", "nodemon": "^2.0.15",
"pino-pretty": "^7.5.1", "pino-pretty": "^7.5.1",
@ -53,7 +56,6 @@
"typescript": "~4.5.4", "typescript": "~4.5.4",
"vite": "^2.7.13", "vite": "^2.7.13",
"vitest": "^0.1.23", "vitest": "^0.1.23",
"vue-tsc": "^0.29.8", "vue-tsc": "^0.29.8"
"husky": "^7.0.0"
} }
} }

View file

@ -1,11 +1,13 @@
import { FastifyInstance } from 'fastify' import { FastifyInstance } from 'fastify'
import { default as wishlistRoute } from './wishlist/' import { default as wishlistRoute } from './wishlist/'
import { default as utilsRoute } from './utils/'
export default { export default {
register: (app: FastifyInstance) => { register: (app: FastifyInstance) => {
return app.register( return app.register(
async (app) => { async (app) => {
await app.register(wishlistRoute, { prefix: '/wishlist' }) await app.register(wishlistRoute, { prefix: '/wishlist' })
await app.register(utilsRoute, { prefix: '/utils' })
}, },
{ prefix: '/api' } { prefix: '/api' }
) )

View file

@ -0,0 +1,6 @@
import { FastifyInstance } from 'fastify'
import { fetchOpenGraph } from './opengraph'
export default async (app: FastifyInstance) => {
await app.route(fetchOpenGraph)
}

View file

@ -0,0 +1,48 @@
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,
})
}
},
}