KairoRouter
import { router } from '@kairo-js/router'
The class exported as the router singleton. Provides the core functionality of kairo-router: addon initialization, API calls, event subscriptions, and more.
Properties
afterEvents
readonly afterEvents: KairoAfterEvents
Subscribe to after events.
beforeEvents
readonly beforeEvents: KairoBeforeEvents
Subscribe to before events.
currentTick
readonly currentTick: number
Current tick count since activation (getter).
systemInfo
readonly systemInfo: KairoContext
Addon context info (getter).
Methods
clearRun
clearRun(runId: number): voidCancels a scheduled run created by runInterval() or runTimeout().
Parameters
runId:
numberThe ID of the run to cancel.
Returns: void
emit
emit(eventName: string, payload?: unknown): voidEmits a custom event. Delivered to handlers subscribed via AddonEventRegistry.on().
Parameters
eventName:
stringThe name of the event to emit.
payload:
unknownData to attach to the event (optional).
Returns: void
getAddonId
getAddonId(): string | undefinedReturns the addonId of this addon. Returns undefined before the addon is registered.
Returns: string | undefined
init
init(properties: AddonProperties, options?: RouterInitOptions): voidInitializes the addon. "kairo" must be declared in properties.dependencies, otherwise this call throws a KairoRouterInitError.
Parameters
properties:
AddonPropertiesThe addon's configuration.
options:
RouterInitOptionsOptional initialization options.
Returns: void
Standalone mode
If the addon's required dependencies contain only kairo and kairo-database, the router automatically activates the addon when kairo is not installed. See RouterInitOptions for details.
request
request<TReturn>(
targetAddonId: string,
apiName: string,
args?: unknown,
options?: { timeout?: number },
): Promise<TReturn | CanceledResult>Calls an API and awaits the result. timeout is in ticks; default is 20 ticks.
Parameters
targetAddonId:
stringThe ID of the target addon.
apiName:
stringThe name of the API to call.
args:
unknownArguments to pass to the API (optional).
options.timeout:
numberTimeout in ticks (default 20).
Returns: Promise<TReturn | CanceledResult>
const result = await router.request<{ balance: number }>(
'economy-addon',
'economy/getBalance',
{ playerId: 'abc123' },
)
if ('canceled' in result) {
console.warn('Request cancelled:', result.reason)
} else {
console.log('Balance:', result.balance)
}runInterval
runInterval(callback: () => void, tickInterval?: number): numberSchedules a recurring callback at the given tick interval. Returns a runId to pass to clearRun().
Parameters
callback:
() => voidThe function to run repeatedly.
tickInterval:
numberInterval in ticks (optional).
Returns: number — runId
runTimeout
runTimeout(callback: () => void, tickDelay?: number): numberSchedules a one-shot callback after the given tick delay. Returns a runId to pass to clearRun().
Parameters
callback:
() => voidThe function to run once.
tickDelay:
numberDelay in ticks (optional).
Returns: number — runId
send
send(targetAddonId: string, apiName: string, args?: unknown): voidCalls an API in fire-and-forget fashion. Does not wait for a response. Silently ignored if the target addon does not exist or is inactive.
Parameters
targetAddonId:
stringThe ID of the target addon.
apiName:
stringThe name of the API to call.
args:
unknownArguments to pass to the API (optional).
Returns: void
router.send('economy-addon', 'onTransaction', { amount: 50 })delete
delete(key: string): Promise<void>Deletes a stored value by key. Requires "kairo-database" in dependencies or optionalDependencies. In standalone mode, resolves immediately without any operation.
Parameters
key:
stringThe key to delete.
Returns: Promise<void>
has
has(key: string, options?: { addonId?: string }): Promise<boolean>Returns true if the given key exists in the store. Requires "kairo-database" in dependencies or optionalDependencies. In standalone mode, always returns false.
Parameters
key:
stringThe key to check.
options.addonId:
stringRead from another addon's store (optional).
Returns: Promise<boolean>
load
load<T = unknown>(key: string, options?: { addonId?: string }): Promise<T | undefined>Loads a value from the store. Returns undefined if the key does not exist. Requires "kairo-database" in dependencies or optionalDependencies. In standalone mode, always returns undefined.
Parameters
key:
stringThe key to load.
options.addonId:
stringLoad from another addon's store (optional).
Returns: Promise<T | undefined>
save
save(key: string, value: unknown): Promise<void>Persists a value in the store under the given key. Requires "kairo-database" in dependencies or optionalDependencies. In standalone mode, resolves immediately without any operation.
Parameters
key:
stringThe key to store under.
value:
unknownThe value to persist.
Returns: Promise<void>
waitForWorldLoad
waitForWorldLoad(): Promise<void>Resolves when the world finishes loading.
Returns: Promise<void>