This commit is contained in:
polv committed 2021-10-24 23:49:09 +07:00
1 parent 70066f3086
commit 0348f84255
4 files changed
+260 -36

No files matched your search

+47 -20
View File
@@ -1,6 +1,20 @@
import _axios, { AxiosInstance } from 'axios'
export type IType = 'character' | 'vocabulary' | 'sentence'
export type ITypeLevel = 'character' | 'vocabulary'
export type IType = ITypeLevel | 'sentence'
export interface ILevelBrowserByType {
whatToShow: string[]
}
export type ILevelBrowser = Record<ITypeLevel, ILevelBrowserByType>
export interface IUserSettings {
identifier: string
level: number
levelMin?: number
level_browser?: ILevelBrowser
}
export interface IEntry {
id: string
@@ -41,18 +55,19 @@ class Api {
}
setApiKey()
const r = await this.user_getSettings()
if (r) return r
apiKey = ''
setApiKey()
return null
return await this.isAuthorized().then((r) => {
if (!r) {
apiKey = ''
setApiKey()
}
return r
})
}
async isAuthorized(): Promise<boolean> {
if (this._isAuthorized && !this._user) {
const r = await this.user_getSettings()
const r = await this.user_getSettings({ select: ['identifier'] })
if (r) {
this._user = r.identifier
this._isAuthorized = true
@@ -62,21 +77,15 @@ class Api {
return this._isAuthorized
}
async user_getSettings() {
async user_getSettings(params: { select: (keyof IUserSettings)[] }) {
const r = await this.axios
.get('/api/user', {
params: {
select: 'level,levelMin,identifier',
...params,
select: params.select.join(','),
},
})
.then(
(r) =>
r.data as {
level: number
levelMin?: number
identifier: string
}
)
.then((r) => r.data as IUserSettings)
.then((r) => ({
...r,
levelMin: r.levelMin || 1,
@@ -84,13 +93,16 @@ class Api {
.catch(() => null)
if (!r) {
return
return null
}
return r
}
async user_updateSettings(u: { levelMin: number }) {
async user_updateSettings(u: {
levelMin?: number
level_browser?: ILevelBrowser
}) {
return await this.axios.patch('/api/user', u)
}
@@ -258,6 +270,21 @@ class Api {
}
)
}
async library_getLevelList(payload: {
type: ITypeLevel
settings: ILevelBrowserByType
}) {
return await this.axios.post('/api/library/getLevelList', payload).then(
(r) =>
r.data as {
result: {
entry: string
level: number
}[]
}
)
}
}
export const api = new Api()