/
app.ts
52 lines (46 loc) · 1.67 KB
/
app.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
import { acceptHMRUpdate, defineStore } from "pinia"
import { combinePaths, createErrorStatus, MetadataTypes, ResponseStatus } from "@servicestack/client"
import { client } from "@/api"
export const useAppStore = defineStore('app', () => {
// State
const loading = ref<Boolean>(false)
const error = ref<ResponseStatus|null>()
const metadata = ref<MetadataTypes|undefined>()
const types = computed(() => metadata.value?.types ?? [])
const load = async (force?:boolean) => {
if (metadata.value && !force) return
loading.value = true
let r = await fetch(combinePaths(client.baseUrl, '/types/metadata.json'))
loading.value = false
if (r.ok) {
let json = await r.text()
metadata.value = JSON.parse(json) as MetadataTypes
} else {
error.value = createErrorStatus(r.statusText)
}
}
const getType = (name:string) => metadata.value?.types?.find(x => x.name?.toLowerCase() == name.toLowerCase())
const enumOptions = (name:string) => {
let to:{[key:string]: any} = {}
let type = getType(name)
if (type && type.isEnum && type.enumNames != null) {
for (let i=0; i<type.enumNames.length; i++) {
const name: string = type.enumNames[i]
const key = (type.enumValues != null ? type.enumValues[i] : null) ?? name
to[key] = name
}
}
return to
}
return {
error,
loading,
metadata,
types,
load,
getType,
enumOptions,
}
})
if (import.meta.hot)
import.meta.hot.accept(acceptHMRUpdate(useAppStore, import.meta.hot))