This commit is contained in:
overtrue
2024-12-08 14:19:47 +08:00
parent efbbec2ee1
commit b86e3f1591
43 changed files with 13827 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
import ApiClient from "~/lib/api-client"
export default defineNuxtPlugin((nuxtApp) => {
const token = useCookie('token')
const api = new ApiClient(
'/api',
token.value as string | undefined,
{
async onResponseError({ response }: { response: { status: number } }) {
if (response.status === 401) {
await nuxtApp.runWithContext(() => navigateTo('/auth/login'))
}
}
}
)
// Expose to useNuxtApp().$api, useNuxtApp().$apiFetch
return {
provide: {
api,
apiFetch: api.getInstance()
}
}
})