|
|
|
|
|
|
|
|
|
export default async ($mod) => {
|
|
|
|
|
console.log('load vbase')
|
|
|
|
|
// Load i18n
|
|
|
|
|
try {
|
|
|
|
|
const langs = await (await fetch($mod.scoped + '/langs.json')).json()
|
|
|
|
|
$mod.$i18n.load(langs)
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Failed to load langs.json', e)
|
|
|
|
|
}
|
|
|
|
|
if (!$mod.$auth) {
|
|
|
|
|
let VBase = (await import($mod.scoped + '/vbase.js')).default
|
|
|
|
|
$mod.users = {}
|
|
|
|
|
const vbase = new VBase($mod.scoped, $mod.scoped + '/login', $mod.users)
|
|
|
|
|
$mod.$auth = vbase
|
|
|
|
|
}
|
|
|
|
|
$mod.$fetch = async (url, opts = {}) => {
|
|
|
|
|
let fullUrl = url
|
|
|
|
|
if (opts.params) {
|
|
|
|
|
const qs = new URLSearchParams()
|
|
|
|
|
for (const [k, v] of Object.entries(opts.params)) {
|
|
|
|
|
if (v != null) qs.append(k, v)
|
|
|
|
|
}
|
|
|
|
|
fullUrl += '?' + qs.toString()
|
|
|
|
|
}
|
|
|
|
|
const init = { method: opts.method || 'GET', headers: {} }
|
|
|
|
|
if (opts.body != null && init.method !== 'GET') {
|
|
|
|
|
init.headers['Content-Type'] = 'application/json'
|
|
|
|
|
init.body = JSON.stringify(opts.body)
|
|
|
|
|
}
|
|
|
|
|
const r = await $mod.fetch(fullUrl, init)
|
|
|
|
|
const d = await r.json().catch(() => ({}))
|
|
|
|
|
if (!r.ok) throw new Error(d.message || d.error || `HTTP ${r.status}`)
|
|
|
|
|
return d
|
|
|
|
|
}
|
|
|
|
|
}
|