Skip to content

ApiRegistration

ev.addonApi를 통해 접근하는 인터페이스입니다. 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

다른 애드온의 API 호출을 가로채는 훅을 등록합니다.

매개변수

  • targetAddonId: string

    훅을 연결할 애드온의 ID.

  • apiName: string

    훅을 연결할 API의 이름.

  • options: HookOptions<TArgs, TReturn>

    훅 설정.

반환값: void

typescript
// 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

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.addonApi.register<{ playerId: string }, { balance: number }>(
  'economy/getBalance',
  async ({ playerId }, ctx) => {
    console.log(`호출자: ${ctx.callerAddonId}`)
    return { balance: getBalance(playerId) }
  },
)

API 이름 네임스페이싱

충돌을 방지하고 검색 가능성을 높이기 위해 economy/getBalance와 같이 슬래시로 구분된 네임스페이스를 사용하세요.

Released under the MIT License.