-
Notifications
You must be signed in to change notification settings - Fork 687
/
Copy pathmain.ts
56 lines (46 loc) · 1.47 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import './assets/css/main.css'
import { createApp, defineAsyncComponent, ref } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import ui from '@nuxt/ui/vue-plugin'
import App from './app.vue'
const pages = import.meta.glob('../../playground/app/pages/**/*.vue')
const components = import.meta.glob('../../playground/app/components/**/*.vue')
const routes = Object.keys(pages).map((path) => {
const name = path.match(/\.\.\/\.\.\/playground\/app\/pages(.*)\.vue$/)![1].toLowerCase()
return {
path: name === '/index' ? '/' : name,
component: pages[path]
}
})
const router = createRouter({
routes,
history: createWebHistory()
})
const app = createApp(App)
Object.entries(components).forEach(([path, component]) => {
const name = path.split('/').pop()!.replace('.vue', '')
app.component(name, defineAsyncComponent(component as any))
})
app.use(router)
app.use(ui)
// @ts-expect-error unknown global property
globalThis.useFetch = async (url: string, options: RequestInit & { transform?: (data) => any } = {}) => {
const data = ref()
const status = ref('idle')
async function _fetch() {
status.value = 'loading'
try {
data.value = await fetch(url, options).then(r => r.json()).then(r => options.transform ? options.transform(r) : r)
status.value = 'success'
} catch (error) {
console.error(error)
status.value = 'error'
}
}
_fetch()
return Promise.resolve({
data,
status
})
}
app.mount('#app')