ApiRegistration
通过 ev.api 访问的接口。只能在 router.beforeEvents.startup 事件中操作。
typescript
interface ApiRegistration {
register<TArgs, TReturn>(apiName: string, handler: (args: TArgs) => TReturn | Promise<TReturn>): void
hook<TArgs, TReturn>(targetAddonId: string, apiName: string, options: HookOptions<TArgs, TReturn>): void
}方法
hook
typescript
hook<TArgs, TReturn>(
targetAddonId: string,
apiName: string,
options: HookOptions<TArgs, TReturn>,
): void注册一个 hook,用于拦截其他插件的 API 调用。
参数
targetAddonId:
string要 hook 的目标插件 ID。
apiName:
string要 hook 的 API 名称。
options:
HookOptions<TArgs, TReturn>Hook 配置。
返回值: void
typescript
// Mutate args
ev.api.hook('economy-addon', 'economy/getBalance', {
before: async (ctx) => {
ctx.args = { ...ctx.args, audited: true }
},
})
// Post-process result
ev.api.hook('economy-addon', 'economy/getBalance', {
after: async (ctx) => {
ctx.result = { ...ctx.result, taxRate: 0.1 }
},
})register
typescript
register<TArgs, TReturn>(
apiName: string,
handler: (args: TArgs, ctx: ApiHandlerContext) => TReturn | Promise<TReturn>,
): void注册本插件提供的 API 处理器。在同一 addonId 下重复注册相同的 apiName 会抛出错误。
参数
apiName:
string要注册的 API 名称。
handler:
(args: TArgs, ctx:ApiHandlerContext) => TReturn | Promise<TReturn>API 被调用时执行的处理器。可通过
ctx.callerAddonId识别调用方。
返回值: void
typescript
ev.api.register<{ playerId: string }, { balance: number }>(
'economy/getBalance',
async ({ playerId }, ctx) => {
console.log(`调用方:${ctx.callerAddonId}`)
return { balance: getBalance(playerId) }
},
)API 名称命名空间
使用斜线分隔的命名空间(如 economy/getBalance)可避免命名冲突并提升可发现性。