refactor: use runtimeConfig

This commit is contained in:
Daniel Roe 2022-12-12 20:51:50 +00:00
parent 5e717cac03
commit e88d255c07
No known key found for this signature in database
GPG key ID: 22D5008E4F5D9B55
2 changed files with 6 additions and 4 deletions

View file

@ -61,6 +61,7 @@ export default defineNuxtConfig({
namespaceId: '', namespaceId: '',
apiToken: '', apiToken: '',
}, },
opengraphApi: '',
public: { public: {
env: isCI ? isPreview ? 'staging' : 'production' : 'local', env: isCI ? isPreview ? 'staging' : 'production' : 'local',
translateApi: '', translateApi: '',

View file

@ -7,12 +7,12 @@ type OpenGraphClient = ReturnType<typeof opengraph>
let openGraphClient: OpenGraphClient let openGraphClient: OpenGraphClient
function getOpenGraphClient(): OpenGraphClient { function getOpenGraphClient(): OpenGraphClient {
const NUXT_OPENGRAPH_API = process.env.NUXT_OPENGRAPH_API const appId = useRuntimeConfig().opengraphApi
if (typeof NUXT_OPENGRAPH_API !== 'string') if (typeof appId !== 'string')
throw new Error('Missing NUXT_OPENGRAPH_API environment variable.') throw new Error('Missing NUXT_OPENGRAPH_API environment variable.')
if (!openGraphClient) if (!openGraphClient)
openGraphClient = opengraph({ appId: NUXT_OPENGRAPH_API, fullRender: true })! openGraphClient = opengraph({ appId, fullRender: true })!
return openGraphClient return openGraphClient
} }
@ -36,6 +36,7 @@ async function resolveOgImageUrlManually(cardUrl: string): Promise<string> {
} }
export default defineEventHandler(async (event) => { export default defineEventHandler(async (event) => {
const config = useRuntimeConfig()
const { url } = getRouterParams(event) const { url } = getRouterParams(event)
const { fallbackUrl } = getQuery(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 no og:image was found, try to get it from opengraph.io
if (!ogImageUrl) { if (!ogImageUrl) {
const response = await getOpenGraphClient().getSiteInfo(cardUrl).catch(() => const response = await getOpenGraphClient().getSiteInfo(cardUrl).catch(() =>