You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oaer/lib/main.ts

89 lines
1.5 KiB
TypeScript

/*
* main.ts
* Copyright (C) 2024 veypi <i@veypi.com>
* 2024-07-04 14:34
* Distributed under terms of the MIT license.
*/
import './assets/css/oaer.scss'
1 month ago
import bus from './bus'
import logic from './logic'
1 month ago
import ui from './components'
3 weeks ago
import api, { set_base_host } from './api'
4 weeks ago
import fs from './fs'
4 weeks ago
4 weeks ago
export {
fs
}
export default new class {
private ui?: ui
constructor() {
}
4 weeks ago
local() {
return logic.user
}
access() {
4 weeks ago
return logic.token.app.access!
4 weeks ago
}
4 weeks ago
fs() {
return fs
}
init(host?: string, code?: string) {
if (host) {
logic.host = host
3 weeks ago
set_base_host(host)
}
if (code) {
logic.token.refresh.set(code)
}
4 weeks ago
return logic.init()
}
render_ui(domid = 'voa') {
if (!this.ui) {
this.ui = new ui()
}
let root = document.querySelector('#' + domid)
if (root) {
this.ui.mount(root)
} else {
console.warn(`not found <div id="${domid}"></div>`)
}
}
isValid() {
return logic.token.refresh.isVaild()
}
1 month ago
login() {
bus.emit('login')
}
1 month ago
logout() {
bus.emit('logout')
}
on(evt: string, fn: (d?: any) => void) {
bus.on(evt, fn)
}
4 weeks ago
api() {
return {
user: {
Get: api.user.Get,
List: api.user.List,
},
app: {
Get: api.app.Get,
List: api.app.List,
},
}
}
Token() {
return logic.token.app.raw()
}
TokenRefresh() {
return new Promise<string>((resolve, reject) => {
logic.token.app.update().then((e) => { resolve(e.raw()) }).catch(() => reject)
})
}
}