ApiRegistration
Accessed via ev.addonApi. Only operable inside the router.beforeEvents.startup event.
interface ApiRegistration {
register<TArgs, TReturn>(apiName: string, handler: (args: TArgs, ctx: ApiHandlerContext) => TReturn | Promise<TReturn>): void
hook<TArgs, TReturn>(targetAddonId: string, apiName: string, options: HookOptions<TArgs, TReturn>): void
}Methods
hook
hook<TArgs, TReturn>(
targetAddonId: string,
apiName: string,
options: HookOptions<TArgs, TReturn>,
): voidRegisters a hook that intercepts another addon's API calls.
Parameters
targetAddonId:
stringThe ID of the addon to hook into.
apiName:
stringThe name of the API to hook.
options:
HookOptions<TArgs, TReturn>Hook configuration.
Returns: void
// Mutate args
ev.addonApi.hook('economy-addon', 'economy/getBalance', {
before: async (ctx) => {
ctx.args = { ...ctx.args, audited: true }
},
})
// Post-process result
ev.addonApi.hook('economy-addon', 'economy/getBalance', {
after: async (ctx) => {
ctx.result = { ...ctx.result, taxRate: 0.1 }
},
})register
register<TArgs, TReturn>(
apiName: string,
handler: (args: TArgs, ctx: ApiHandlerContext) => TReturn | Promise<TReturn>,
): voidRegisters an API handler provided by this addon. Registering the same apiName twice within the same addonId throws an error.
Parameters
apiName:
stringThe name of the API to register.
handler:
(args: TArgs, ctx:ApiHandlerContext) => TReturn | Promise<TReturn>The handler to invoke when the API is called.
ctx.callerAddonIdidentifies the caller.
Returns: void
ev.addonApi.register<{ playerId: string }, { balance: number }>(
'economy/getBalance',
async ({ playerId }, ctx) => {
console.log(`Called by ${ctx.callerAddonId}`)
return { balance: getBalance(playerId) }
},
)API Name Namespacing
Use slash-separated namespaces like economy/getBalance to avoid collisions and improve discoverability.