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

106 lines
2.0 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'
import bus from './bus'
import logic from './logic'
import ui from './components'
import api, { set_base_host } from './api'
import fs from './fs'
export {
fs
}
export default new class {
private ui?: ui
constructor() {
}
logic() {
return logic
}
local() {
return logic.user
}
access() {
return logic.token.app.access!
}
fs() {
return fs
}
init(host?: string, aid?: string, code?: string) {
if (host) {
logic.host = host
set_base_host(host)
}
if (aid) {
logic.app_id = aid
}
if (code) {
logic.token.refresh.set(code)
logic.token.app.clear()
logic.token.oa.clear()
}
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()
}
login() {
bus.emit('login')
}
logout() {
bus.emit('logout')
}
urlwarp(url: string, params?: { [key: string]: any }) {
return logic.urlwarp(url, params)
}
goto(url: string, params?: { [key: string]: any }, newtab = false) {
return logic.goto(url, params, newtab)
}
on(evt: string, fn: (d?: any) => void) {
bus.on(evt, fn)
}
api() {
return {
user: {
Get: api.user.Get,
List: api.user.List,
},
app: {
Get: api.app.Get,
List: api.app.List,
},
token: {
Post: api.token.Post
}
}
}
Token() {
return logic.token.app.raw()
}
TokenRefresh() {
return new Promise<string>((resolve, reject) => {
logic.token.app.update().then((e) => { resolve(e.raw()) }).catch(() => reject)
})
}
}