Add opengraph types

This commit is contained in:
Shinigami92 2022-12-12 16:27:33 +01:00
parent 0f1937340d
commit a34e7e23f0
2 changed files with 31 additions and 4 deletions

21
opengraph-io.d.ts vendored Normal file
View file

@ -0,0 +1,21 @@
// TODO @Shinigami92 2022-12-12: @Shinigami92 might add @types/opengraph-io to DefinitelyTyped
// Or directly to the repo
declare module 'opengraph-io' {
function opengraph(options: {
appId: string
fullRender?: boolean
}): {
getSiteInfo(url: string): Promise<{
hybridGraph: {
image: string
},
openGraph: {
image: {
url: string
}
}
}>
}
export = opengraph
}

View file

@ -2,11 +2,17 @@ import opengraph from 'opengraph-io'
// This API-Endpoint will be cached via nuxt.config.ts -> nitro.routeRules['/api/og-image'].cache.maxAge = 86400
let openGraphClient: any = null
type OpenGraphClient = ReturnType<typeof opengraph>
function getOpenGraphClient(): any {
if (openGraphClient == null)
openGraphClient = opengraph({ appId: process.env.NUXT_OPENGRAPH_API, fullRender: true })
let openGraphClient: OpenGraphClient
function getOpenGraphClient(): OpenGraphClient {
const NUXT_OPENGRAPH_API = process.env.NUXT_OPENGRAPH_API
if (typeof NUXT_OPENGRAPH_API !== 'string')
throw new Error('Missing NUXT_OPENGRAPH_API environment variable.')
if (!openGraphClient)
openGraphClient = opengraph({ appId: NUXT_OPENGRAPH_API, fullRender: true })!
return openGraphClient
}