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: '',
apiToken: '',
},
opengraphApi: '',
public: {
env: isCI ? isPreview ? 'staging' : 'production' : 'local',
translateApi: '',

View file

@ -7,12 +7,12 @@ type OpenGraphClient = ReturnType<typeof opengraph>
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<string> {
}
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(() =>