1
0
mirror of https://github.com/elk-zone/elk synced 2024-12-14 06:38:05 +09:00
elk/modules/tauri/runtime/nitro.client.ts

77 lines
2.2 KiB
TypeScript
Raw Normal View History

import {
createApp,
createRouter,
defineLazyEventHandler,
toNodeListener,
} from 'h3'
2023-01-25 03:52:48 +09:00
import type { FetchResponse } from 'ofetch'
import { createFetch } from 'ofetch'
import {
createCall,
createFetch as createLocalFetch,
} from 'unenv/runtime/fetch/index'
const handlers = [
{
route: '/api/:server/oauth',
2023-02-12 21:21:57 +09:00
handler: defineLazyEventHandler(() => import('~/server/api/[server]/oauth/[origin]').then(r => r.default || r)),
},
{
route: '/api/:server/login',
handler: defineLazyEventHandler(() => import('~/server/api/[server]/login').then(r => r.default || r)),
},
{
route: '/api/list-servers',
handler: defineLazyEventHandler(() => import('~/server/api/list-servers').then(r => r.default || r)),
},
]
// @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-25 01:46:14 +09:00
debug: import.meta.dev,
// 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-03-17 23:32:02 +09:00
// eslint-disable-next-line ts/prefer-ts-expect-error
// @ts-ignore error TS2321: Excessive stack depth comparing types
globalThis.$fetch = createFetch({
2024-03-17 23:32:02 +09:00
// eslint-disable-next-line ts/prefer-ts-expect-error
// @ts-ignore slight differences in api
fetch: localFetch,
Headers,
2024-03-17 23:32:02 +09:00
// eslint-disable-next-line ts/prefer-ts-expect-error
// @ts-ignore error TS2321: Excessive stack depth comparing types
defaults: { baseURL: config.app.baseURL },
})
const route = useRoute()
if (route.path.startsWith('/api')) {
2023-01-25 03:52:48 +09:00
const result = (await ($fetch.raw as any)(route.fullPath)) as FetchResponse<unknown>
if (result.headers.get('location'))
location.href = result.headers.get('location')!
}
})