Reference

API reference and CLI commands

BaseAction

The base class for all actions.

import { BaseAction } from '@foadonis/actions'

export default class MyAction extends BaseAction {
  async handle() {
    // Your business logic
  }
}

Properties

PropertyTypeDescription
loggerLoggerLogger instance scoped to the action name

Static Methods

run(...args)

Runs the action with the given arguments. Returns the result of the handle method.

const result = await MyAction.run(arg1, arg2)

useRunner(runner)

Sets the runner instance for the action. Called automatically by the provider.

BaseAction.useRunner(runner)

AsController

Interface for actions that can be used as HTTP controllers.

import { BaseAction, type AsController } from '@foadonis/actions'
import { type HttpContext } from '@adonisjs/core/http'

export default class MyAction extends BaseAction implements AsController {
  async handle() {
    // Business logic
  }

  async asController(ctx: HttpContext) {
    // HTTP handling
  }
}

Methods

MethodParametersDescription
asControllerctx: HttpContextCalled when the action is invoked as a route handler

AsCommand

Interface for actions that can be used as CLI commands.

import { BaseAction, type AsCommand } from '@foadonis/actions'
import { type BaseCommand } from '@adonisjs/core/ace'

export default class MyAction extends BaseAction implements AsCommand {
  static commandName = 'my:command'
  static description = 'Description of the command'

  async handle() {
    // Business logic
  }

  async asCommand(command: BaseCommand) {
    // CLI handling
  }
}

Static Properties

PropertyTypeDescription
commandNamestringCustom command name (optional, defaults to action:<action-name>)
descriptionstringCommand description shown in node ace list

Methods

MethodParametersDescription
asCommandcommand: BaseCommandCalled when the action is invoked as a CLI command

AsListener

Interface for actions that can be used as event listeners.

import { BaseAction, type AsListener } from '@foadonis/actions'

export default class MyAction extends BaseAction implements AsListener<MyEvent> {
  async handle() {
    // Business logic
  }

  async asListener(event: MyEvent) {
    // Event handling
  }
}

Methods

MethodParametersDescription
asListenerevent: TCalled when the subscribed event is emitted

loader

Helper function for lazy-loading actions in routes and events.

import { loader } from '@foadonis/actions'

const actionLoader = loader(() => import('#actions/my_action'))

// Use as controller
actionLoader.asController()

// Use as listener
actionLoader.asListener()

Methods

MethodReturnsDescription
asController()RouteFnReturns a route handler function
asListener()ListenerFnReturns an event listener function

ActionsRunner

The runner responsible for resolving and executing actions.

import { ActionsRunner } from '@foadonis/actions'

Methods

MethodParametersReturnsDescription
dispatchactionClass, runner, container?Promise<any>Resolves and executes an action
resolveAction, resolver?Promise<Action>Resolves an action from the container

Container Binding

The runner is available in the container as actions.runner:

const runner = await app.container.make('actions.runner')

CLI Commands

make:action

Creates a new action file.

node ace make:action <name>

Arguments:

ArgumentDescription
nameThe name of the action (e.g., CreateUser, SendNotification)

Examples:

# Creates app/actions/create_user_action.ts
node ace make:action CreateUser

# Creates app/actions/billing/process_payment_action.ts
node ace make:action billing/ProcessPayment

Configuration

indexActions

Assembler hook for indexing actions. Generates a barrel file at .adonisjs/server/actions.ts.

adonisrc.ts
import { defineConfig } from '@adonisjs/core/app'
import { indexActions } from '@foadonis/actions'

export default defineConfig({
  hooks: {
    init: [indexActions()],
  },
})

Options:

OptionTypeDefaultDescription
sourcestring'app/actions'Source directory for actions
importAliasstring'#actions'Import alias for actions
globstring[]['**/*_action.ts']Glob patterns for matching action files

Example with custom options:

adonisrc.ts
import { defineConfig } from '@adonisjs/core/app'
import { indexActions } from '@foadonis/actions'

export default defineConfig({
  hooks: {
    init: [
      indexActions({
        source: 'app/actions',
        importAlias: '#actions',
        glob: ['**/*_action.ts'],
      }),
    ],
  },
})

On this page