From e88d255c0798b968c9bc402081f3c08468465c2f Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Mon, 12 Dec 2022 20:51:50 +0000 Subject: [PATCH] refactor: use runtimeConfig --- nuxt.config.ts | 1 + server/api/og-image/[url].ts | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/nuxt.config.ts b/nuxt.config.ts index b0a140f5..5f0286a5 100644 --- a/nuxt.config.ts +++ b/nuxt.config.ts @@ -61,6 +61,7 @@ export default defineNuxtConfig({ namespaceId: '', apiToken: '', }, + opengraphApi: '', public: { env: isCI ? isPreview ? 'staging' : 'production' : 'local', translateApi: '', diff --git a/server/api/og-image/[url].ts b/server/api/og-image/[url].ts index 36e0d746..db484128 100644 --- a/server/api/og-image/[url].ts +++ b/server/api/og-image/[url].ts @@ -7,12 +7,12 @@ type OpenGraphClient = ReturnType let openGraphClient: OpenGraphClient function getOpenGraphClient(): OpenGraphClient { - const NUXT_OPENGRAPH_API = process.env.NUXT_OPENGRAPH_API - if (typeof NUXT_OPENGRAPH_API !== 'string') + const appId = useRuntimeConfig().opengraphApi + if (typeof appId !== 'string') throw new Error('Missing NUXT_OPENGRAPH_API environment variable.') if (!openGraphClient) - openGraphClient = opengraph({ appId: NUXT_OPENGRAPH_API, fullRender: true })! + openGraphClient = opengraph({ appId, fullRender: true })! return openGraphClient } @@ -36,6 +36,7 @@ async function resolveOgImageUrlManually(cardUrl: string): Promise { } export default defineEventHandler(async (event) => { + const config = useRuntimeConfig() const { url } = getRouterParams(event) const { fallbackUrl } = getQuery(event) @@ -64,7 +65,7 @@ export default defineEventHandler(async (event) => { '', ) - if (process.env.NUXT_OPENGRAPH_API) { + if (config.opengraphApi) { // If no og:image was found, try to get it from opengraph.io if (!ogImageUrl) { const response = await getOpenGraphClient().getSiteInfo(cardUrl).catch(() =>