Skip to content

Instantly share code, notes, and snippets.

@acidjazz
Created April 19, 2022 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acidjazz/18efe79119256b1fe93bb60ed09acefa to your computer and use it in GitHub Desktop.
Save acidjazz/18efe79119256b1fe93bb60ed09acefa to your computer and use it in GitHub Desktop.
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