feat: add support for client-side server to tauri module (#501)
This commit is contained in:
parent
2b5051e123
commit
9800697670
8 changed files with 143 additions and 2 deletions
|
@ -1,4 +1,4 @@
|
|||
import { addPlugin, createResolver, defineNuxtModule, useNuxt } from '@nuxt/kit'
|
||||
import { addImports, addPlugin, createResolver, defineNuxtModule, useNuxt } from '@nuxt/kit'
|
||||
|
||||
export default defineNuxtModule({
|
||||
meta: {
|
||||
|
@ -11,11 +11,30 @@ export default defineNuxtModule({
|
|||
if (!process.env.TAURI_PLATFORM)
|
||||
return
|
||||
|
||||
if (nuxt.options.dev)
|
||||
nuxt.options.ssr = false
|
||||
|
||||
nuxt.options.alias = {
|
||||
...nuxt.options.alias,
|
||||
'unstorage/drivers/fs': 'unenv/runtime/mock/proxy',
|
||||
'unstorage/drivers/cloudflare-kv-http': 'unenv/runtime/mock/proxy',
|
||||
'node:events': 'unenv/runtime/node/events/index',
|
||||
}
|
||||
|
||||
nuxt.hook('vite:extend', ({ config }) => {
|
||||
config.build!.target = ['es2021', 'chrome100', 'safari13']
|
||||
config.envPrefix = [...config.envPrefix || [], 'VITE_', 'TAURI_']
|
||||
})
|
||||
|
||||
// prevent creation of server routes
|
||||
nuxt.hook('nitro:config', (config) => {
|
||||
config.srcDir = './_nonexistent'
|
||||
config.scanDirs = []
|
||||
})
|
||||
|
||||
addImports({ name: 'useStorage', from: resolve('./runtime/storage') })
|
||||
|
||||
addPlugin(resolve('./runtime/logging.client'))
|
||||
addPlugin(resolve('./runtime/nitro.client'))
|
||||
},
|
||||
})
|
||||
|
|
71
modules/tauri/runtime/nitro.client.ts
Normal file
71
modules/tauri/runtime/nitro.client.ts
Normal file
|
@ -0,0 +1,71 @@
|
|||
import {
|
||||
createApp,
|
||||
createRouter,
|
||||
defineLazyEventHandler,
|
||||
toNodeListener,
|
||||
} from 'h3'
|
||||
import { createFetch } from 'ofetch'
|
||||
import { parseURL } from 'ufo'
|
||||
import {
|
||||
createCall,
|
||||
createFetch as createLocalFetch,
|
||||
} from 'unenv/runtime/fetch/index'
|
||||
|
||||
const handlers = [
|
||||
{
|
||||
route: '/api/:server/oauth',
|
||||
handler: defineLazyEventHandler(() => import('~/server/api/[server]/oauth').then(r => r.default || r)),
|
||||
},
|
||||
{
|
||||
route: '/api/:server/login',
|
||||
handler: defineLazyEventHandler(() => import('~/server/api/[server]/login').then(r => r.default || r)),
|
||||
},
|
||||
]
|
||||
|
||||
const { protocol, host } = parseURL(window.location.href)
|
||||
|
||||
// @ts-expect-error undeclared global window property
|
||||
window.__NUXT__.config = {
|
||||
// @ts-expect-error undeclared global window property
|
||||
...window.__NUXT__.config,
|
||||
deployUrl: `${protocol}//${host}`,
|
||||
storage: {},
|
||||
}
|
||||
|
||||
export default defineNuxtPlugin(async () => {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const h3App = createApp({
|
||||
debug: process.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)
|
||||
|
||||
// @ts-expect-error slight differences in api
|
||||
globalThis.$fetch = createFetch({
|
||||
// @ts-expect-error slight differences in api
|
||||
fetch: localFetch,
|
||||
Headers,
|
||||
defaults: { baseURL: config.app.baseURL },
|
||||
})
|
||||
|
||||
const route = useRoute()
|
||||
if (route.path.startsWith('/api')) {
|
||||
const result = await $fetch.raw(route.fullPath)
|
||||
if (result.headers.get('location'))
|
||||
location.href = result.headers.get('location')!
|
||||
}
|
||||
})
|
27
modules/tauri/runtime/storage.ts
Normal file
27
modules/tauri/runtime/storage.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import { createStorage } from 'unstorage'
|
||||
import { Store } from 'tauri-plugin-store-api'
|
||||
|
||||
const store = new Store('.servers.dat')
|
||||
const storage = createStorage()
|
||||
storage.mount('servers', {
|
||||
getKeys() {
|
||||
return store.keys()
|
||||
},
|
||||
async removeItem(key: string) {
|
||||
await store.delete(key)
|
||||
},
|
||||
clear() {
|
||||
return store.clear()
|
||||
},
|
||||
hasItem(key: string) {
|
||||
return store.has(key)
|
||||
},
|
||||
setItem(key: string, value: any) {
|
||||
return store.set(key, value)
|
||||
},
|
||||
getItem(key: string) {
|
||||
return store.get(key)
|
||||
},
|
||||
})
|
||||
|
||||
export const useStorage = () => storage
|
Loading…
Add table
Add a link
Reference in a new issue