UNPKG

actionhero

Version:

The reusable, scalable, and quick node.js API server for stateless and stateful applications

71 lines (70 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Action = void 0; const index_1 = require("../index"); /** * Create a new Actionhero Action. The required properties of an action. These can be defined statically (this.name) or as methods which return a value. *```js * import { Action } from "actionhero"; * * export default class RandomNumber extends Action { * constructor () { * super() * this.name = 'randomNumber' * this.description = 'I am an API method which will generate a random number' * this.outputExample = { randomNumber: 0.1234 } * } * async run ({ response }) { * response.randomNumber = Math.random() * } *} *``` */ class Action { constructor() { const coreProperties = this.defaults(); for (const key in coreProperties) { if (!this[key]) { this[key] = coreProperties[key]; } if (typeof this[key] === "function") { this[key] = this[key](); } } } defaults() { return { name: null, version: 1, description: this.name, outputExample: {}, inputs: {}, middleware: [], blockedConnectionTypes: [], logLevel: "info", matchExtensionMimeType: true, toDocument: true, }; } validate() { if (!this.name) { throw new Error("name is required for this action"); } if (!this.description) { throw new Error(`description is required for the action \`${this.name}\``); } if (!this.run || typeof this.run !== "function") { throw new Error(`action \`${this.name}\` has no run method`); } if (index_1.api.connections && index_1.api.connections.allowedVerbs.indexOf(this.name) >= 0) { throw new Error(`action \`${this.name}\` is a reserved verb for connections. choose a new name`); } Object.keys(this.inputs).forEach((input) => { if (index_1.api.params.globalSafeParams.indexOf(input) >= 0) { throw new Error(`input \`${input}\` in action \`${this.name}\` is a reserved param`); } }); } } exports.Action = Action;