add contextmenu

This commit is contained in:
polv committed 2021-10-24 18:16:46 +07:00
1 parent 1c53acbe2c
commit f0bd330d04
18 files changed
+912 -131

No files matched your search

+114
View File
@@ -0,0 +1,114 @@
import _axios, { AxiosInstance } from 'axios'
class Api {
axios: AxiosInstance
_isAuthorized = true
_user = ''
constructor() {
this.axios = _axios.create()
}
async setWanikaniApiKey(apiKey?: string) {
const setApiKey = () => {
if (apiKey) {
this.axios.defaults.headers.common.Authorization = `Bearer ${apiKey}`
} else {
delete this.axios.defaults.headers.common.Authorization
}
}
setApiKey()
const r = await this.user_getSettings()
if (r) return r
apiKey = ''
setApiKey()
return null
}
async isAuthorized(): Promise<boolean> {
if (this._isAuthorized && !this._user) {
const r = await this.user_getSettings()
if (r) {
this._user = r.identifier
this._isAuthorized = true
}
}
return this._isAuthorized
}
async user_getSettings() {
const r = await this.axios
.get('/api/user', {
params: {
select: 'level,levelMin,identifier',
},
})
.then(
(r) =>
r.data as {
level: number
levelMin?: number
identifier: string
}
)
.then((r) => ({
...r,
levelMin: r.levelMin || 1,
}))
.catch(() => null)
if (!r) {
return
}
return r
}
async user_updateSettings(u: { levelMin: number }) {
return await this.axios.patch('/api/user', u)
}
async quiz_getMany(params: {
entries: string[]
select: string[]
type?: string
direction?: string
}) {
return await this.axios.post('/api/quiz/getMany', params).then(
(r) =>
r.data as {
result: {
id: string
entry: string
}[]
}
)
}
async quiz_create(params: { entries: string[]; type: string }) {
return await this.axios.put('/api/quiz', params).then(
(r) =>
r.data as {
result: {
ids: string[]
}[]
}
)
}
async quiz_deleteMany(params: { entries: string[]; type: string }) {
return await this.axios.post('/api/quiz/deleteMany', params)
}
async extra_delete(params: { id: string }) {
return await this.axios.delete('/extra/delete', {
params,
})
}
}
export const api = new Api()