ActionHero

ActionHero

new ActionHero()

Source:

The ActionHero module.

Classes

Action
ActionheroWebsocketClient
CLI
Connection
Initializer
Server
Task

Type Definitions

ActionCallback(data)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
data Object

The data object.

ActionMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

global Boolean

Is this middleware applied to all actions?

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

preProcessor ActionHero~ActionCallback

Called berore the action runs. Has access to all params, before sanitizartion. Can modify the data object for use in actions.

postProcessor ActionHero~ActionCallback

Called after the action runs.

Middleware definition for Actions

Type:
  • Object
Example
var middleware = {
  name: 'userId checker',
  global: false,
  priority: 1000,
  preProcessor: async (data) => {
    if(!data.params.userId){
      throw new Error('All actions require a userId')
    }
  },
  postProcessor: async (data) => {
    if(data.thing.stuff == false){ data.toRender = false }
  }
}

api.actions.addMiddleware(middleware);

ChatMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

join ActionHero~ChatRoomCallback

Called when a connection joins a room.

leave ActionHero~ChatRoomCallback

Called when a connection leaves a room.

onSayReceive ActionHero~ChatSayCallback

Called when a connection says a message to a room.

say ActionHero~ChatSayCallback

Called when a connection is about to recieve a say message.

Middleware definition for processing chat events. Can be of the

Type:
  • Object
Example
var chatMiddleware = {
  name: 'chat middleware',
  priority: 1000,
  join: (connection, room) => {
    // announce all connections entering a room
    api.chatRoom.broadcast({}, room, 'I have joined the room: ' + connection.id, callback)
  },
  leave:(connection, room, callback) => {
    // announce all connections leaving a room
    api.chatRoom.broadcast({}, room, 'I have left the room: ' + connection.id, callback)
  },
  // Will be executed once per client connection before delivering the message.
  say: (connection, room, messagePayload) => {
    // do stuff
    api.log(messagePayload)
  },
  // Will be executed only once, when the message is sent to the server.
  onSayReceive: (connection, room, messagePayload) => {
    // do stuff
    api.log(messagePayload)
  }
}

api.chatRoom.addMiddleware(chatMiddleware)

ChatRoomCallback(connection, room)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
connection Object

The connection being created/destroyed.

room string

The room being chatted within.

ChatSayCallback(connection, room, messagePayload)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
connection Object

The connection being created/destroyed.

room string

The room being chatted within.

messagePayload Object

The message & metadata.

ConnectionCallback(connection)

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
connection Object

The connection being created/destroyed.

ConnectionMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

create ActionHero~ConnectionCallback

Called for each new connection when it is created.

destroy ActionHero~ConnectionCallback

Called for each connection before it is destroyed.

Middleware definition for processing connection events

Type:
  • Object
Example
var connectionMiddleware = {
  name: 'connection middleware',
  priority: 1000,
  create: (connection) => {
    // do stuff
  },
  destroy:(connection) => {
    // do stuff
  }
}

api.connections.addMiddleware(connectionMiddleware)

TaskCallback()

Source:
See:

This callback is displayed as part of the Requester class.

Parameters:
Name Type Description
this.worker Object

The task worker, if this is a pre or post process step.

this.args Object

If this is a queue step, the arguemnts to the task

this.queue Object

The queue to be used / is being used.

TaskMiddleware

Source:
See:
Properties:
Name Type Description
name string

Unique name for the middleware.

global Boolean

Is this middleware applied to all tasks?

priority Number

Module load order. Defaults to api.config.general.defaultMiddlewarePriority.

preProcessor ActionHero~TaskCallback

Called berore the action runs. Has access to all params, before sanitizartion. Can modify the data object for use in actions.

postProcessor ActionHero~TaskCallback

Called after the action runs.

preEnqueue ActionHero~TaskCallback

Called before a task using this middleware is enqueud.

postEnqueue ActionHero~TaskCallback

Called after a task using this middleware is enqueud.

Middleware definition for Actions

Type:
  • Object
Example
const middleware = {
  name: 'timer',
  global: true,
  priority: 90,
  preProcessor: async () => {
    const worker = this.worker
    worker.startTime = process.hrtime()
  },
  postProcessor: async () => {
    const worker = this.worker
    const elapsed = process.hrtime(worker.startTime)
    const seconds = elapsed[0]
    const millis = elapsed[1] / 1000000
    api.log(worker.job.class + ' done in ' + seconds + ' s and ' + millis + ' ms.', 'info')
  },
  preEnqueue: async () => {
    const arg = this.args[0]
    return (arg === 'ok') // returing `false` will prevent the task from enqueing
  },
  postEnqueue: async () => {
    api.log("Task successfully enqueued!")
  }
}

api.tasks.addMiddleware(middleware)