feat(shell): log error if calling not registered api

This commit is contained in:
Il Harper 2024-03-06 11:03:42 +08:00
parent 79934d435b
commit b660c4cb38
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 29 additions and 1 deletions

View File

@ -1,14 +1,21 @@
import type { Methods } from '../types'
import { l } from './logger'
const notimplSym = Symbol('chronocat.internal.notimpl')
export type ApiImpl<M extends keyof Methods> = ((
...args: Methods[M][0]
) => Promise<Methods[M][1]>) & {
[notimplSym]: boolean
engine: string
}
export type Api = {
[M in keyof Methods]: ApiImpl<M>
} & {
notimpl: typeof notimplSym
register: (
engine: string,
) => <M extends keyof Methods>(
@ -17,7 +24,26 @@ export type Api = {
) => 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 =
(engine: string) =>

View File

@ -72,6 +72,8 @@ export interface Methods {
// Internal
'chronocat.internal.notimpl': [[], never]
'chronocat.internal.message.create.forward': [
[MessageCreatePayload],
Message[],