diff --git a/opengraph-io.d.ts b/opengraph-io.d.ts new file mode 100644 index 00000000..43aaa4e2 --- /dev/null +++ b/opengraph-io.d.ts @@ -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 +} \ No newline at end of file diff --git a/server/api/og-image.ts b/server/api/og-image.ts index 14362511..f0743b25 100644 --- a/server/api/og-image.ts +++ b/server/api/og-image.ts @@ -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 }