2024-09-30 11:11:56 +03:00
|
|
|
import type { FetchResponse } from 'ofetch'
|
2022-12-22 05:12:37 +03:00
|
|
|
import {
|
|
|
|
createApp,
|
|
|
|
createRouter,
|
|
|
|
defineLazyEventHandler,
|
|
|
|
toNodeListener,
|
|
|
|
} from 'h3'
|
|
|
|
import { createFetch } from 'ofetch'
|
|
|
|
import {
|
|
|
|
createCall,
|
|
|
|
createFetch as createLocalFetch,
|
|
|
|
} from 'unenv/runtime/fetch/index'
|
|
|
|
|
|
|
|
const handlers = [
|
|
|
|
{
|
|
|
|
route: '/api/:server/oauth',
|
2023-02-12 15:21:57 +03:00
|
|
|
handler: defineLazyEventHandler(() => import('~/server/api/[server]/oauth/[origin]').then(r => r.default || r)),
|
2022-12-22 05:12:37 +03:00
|
|
|
},
|
|
|
|
{
|
|
|
|
route: '/api/:server/login',
|
|
|
|
handler: defineLazyEventHandler(() => import('~/server/api/[server]/login').then(r => r.default || r)),
|
|
|
|
},
|
2023-01-10 15:25:57 +03:00
|
|
|
{
|
|
|
|
route: '/api/list-servers',
|
|
|
|
handler: defineLazyEventHandler(() => import('~/server/api/list-servers').then(r => r.default || r)),
|
|
|
|
},
|
2022-12-22 05:12:37 +03:00
|
|
|
]
|
|
|
|
|
|
|
|
// @ts-expect-error undeclared global window property
|
|
|
|
window.__NUXT__.config = {
|
|
|
|
// @ts-expect-error undeclared global window property
|
|
|
|
...window.__NUXT__.config,
|
|
|
|
storage: {},
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineNuxtPlugin(async () => {
|
|
|
|
const config = useRuntimeConfig()
|
|
|
|
|
|
|
|
const h3App = createApp({
|
2024-02-24 19:46:14 +03:00
|
|
|
debug: import.meta.dev,
|
2022-12-22 05:12:37 +03:00
|
|
|
// TODO: add global error handler
|
|
|
|
// onError: (err, event) => {
|
|
|
|
// console.log({ err, event })
|
|
|
|
// },
|
|
|
|
})
|
|
|
|
|
|
|
|
const router = createRouter()
|
|
|
|
|
|
|
|
for (const h of handlers)
|
|
|
|
router.use(h.route, h.handler)
|
|
|
|
|
|
|
|
// @ts-expect-error TODO: fix
|
|
|
|
h3App.use(config.app.baseURL, router)
|
|
|
|
|
|
|
|
const localCall = createCall(toNodeListener(h3App) as any)
|
|
|
|
const localFetch = createLocalFetch(localCall, globalThis.fetch)
|
|
|
|
|
2024-10-09 20:16:07 +03:00
|
|
|
// @ts-expect-error error types are subtly different here in a future nitro version
|
2022-12-22 05:12:37 +03:00
|
|
|
globalThis.$fetch = createFetch({
|
2024-03-19 15:56:14 +03:00
|
|
|
// @ts-expect-error slight differences in api
|
2022-12-22 05:12:37 +03:00
|
|
|
fetch: localFetch,
|
|
|
|
Headers,
|
|
|
|
defaults: { baseURL: config.app.baseURL },
|
|
|
|
})
|
|
|
|
|
|
|
|
const route = useRoute()
|
|
|
|
if (route.path.startsWith('/api')) {
|
2023-01-24 21:52:48 +03:00
|
|
|
const result = (await ($fetch.raw as any)(route.fullPath)) as FetchResponse<unknown>
|
2022-12-22 05:12:37 +03:00
|
|
|
if (result.headers.get('location'))
|
|
|
|
location.href = result.headers.get('location')!
|
|
|
|
}
|
|
|
|
})
|