mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-25 09:37:35 +00:00
feat(shell): log error if calling not registered api
This commit is contained in:
parent
79934d435b
commit
b660c4cb38
@ -1,14 +1,21 @@
|
|||||||
import type { Methods } from '../types'
|
import type { Methods } from '../types'
|
||||||
|
import { l } from './logger'
|
||||||
|
|
||||||
|
const notimplSym = Symbol('chronocat.internal.notimpl')
|
||||||
|
|
||||||
export type ApiImpl<M extends keyof Methods> = ((
|
export type ApiImpl<M extends keyof Methods> = ((
|
||||||
...args: Methods[M][0]
|
...args: Methods[M][0]
|
||||||
) => Promise<Methods[M][1]>) & {
|
) => Promise<Methods[M][1]>) & {
|
||||||
|
[notimplSym]: boolean
|
||||||
|
|
||||||
engine: string
|
engine: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Api = {
|
export type Api = {
|
||||||
[M in keyof Methods]: ApiImpl<M>
|
[M in keyof Methods]: ApiImpl<M>
|
||||||
} & {
|
} & {
|
||||||
|
notimpl: typeof notimplSym
|
||||||
|
|
||||||
register: (
|
register: (
|
||||||
engine: string,
|
engine: string,
|
||||||
) => <M extends keyof Methods>(
|
) => <M extends keyof Methods>(
|
||||||
@ -17,7 +24,26 @@ export type Api = {
|
|||||||
) => void
|
) => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const api = {} as Api
|
const buildNotimpl = (name: string) => {
|
||||||
|
const fn = () =>
|
||||||
|
l.error(new Error(`没有引擎提供 ${name}。可能没有加载所需的引擎?`), {
|
||||||
|
code: 2159,
|
||||||
|
throw: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
fn[notimplSym] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
const handler: ProxyHandler<Api> = {
|
||||||
|
get: function (target, name) {
|
||||||
|
return typeof name === 'symbol' ||
|
||||||
|
Object.prototype.hasOwnProperty.call(target, name)
|
||||||
|
? target[name as keyof Methods]
|
||||||
|
: buildNotimpl(name)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
export const api = new Proxy({} as Api, handler)
|
||||||
|
|
||||||
api.register =
|
api.register =
|
||||||
(engine: string) =>
|
(engine: string) =>
|
||||||
|
@ -72,6 +72,8 @@ export interface Methods {
|
|||||||
|
|
||||||
// Internal
|
// Internal
|
||||||
|
|
||||||
|
'chronocat.internal.notimpl': [[], never]
|
||||||
|
|
||||||
'chronocat.internal.message.create.forward': [
|
'chronocat.internal.message.create.forward': [
|
||||||
[MessageCreatePayload],
|
[MessageCreatePayload],
|
||||||
Message[],
|
Message[],
|
||||||
|
Loading…
Reference in New Issue
Block a user