Created
April 19, 2022 18:44
-
-
Save acidjazz/18efe79119256b1fe93bb60ed09acefa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineNuxtPlugin, useNuxtApp, useRuntimeConfig } from '#app' | |
import Api from '~/lib/api' | |
export default defineNuxtPlugin((nuxtApp) => { | |
const config = useRuntimeConfig() | |
const { $toast } = useNuxtApp() | |
return { | |
provide: { | |
api: | |
new Api({ | |
req: nuxtApp.ssrContext?.req, | |
res: nuxtApp.ssrContext?.res, | |
fetchOptions: { | |
baseURL: config.apiURL, | |
}, | |
webUrl: config.webURL, | |
redirect: { | |
logout: '/', | |
login: '/home', | |
}, | |
}, $toast), | |
}, | |
} | |
}) | |
declare module '#app' { | |
interface NuxtApp { | |
$api: Api | |
} | |
} | |
declare module '@vue/runtime-core' { | |
interface ComponentCustomProperties { | |
$api (): Api | |
} | |
} | |
export { } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment