UNPKG

actionhero

Version:

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

59 lines (58 loc) 2.57 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Action = void 0; const index_1 = require("../index"); const connection_1 = require("./connection"); /** * 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() { var _a, _b, _c, _d, _e, _f, _g, _h, _j; this.version = (_a = this.version) !== null && _a !== void 0 ? _a : 1; this.description = (_b = this.description) !== null && _b !== void 0 ? _b : this.name; this.inputs = (_c = this.inputs) !== null && _c !== void 0 ? _c : {}; this.outputExample = (_d = this.outputExample) !== null && _d !== void 0 ? _d : {}; this.middleware = (_e = this.middleware) !== null && _e !== void 0 ? _e : []; this.blockedConnectionTypes = (_f = this.blockedConnectionTypes) !== null && _f !== void 0 ? _f : []; this.logLevel = (_g = this.logLevel) !== null && _g !== void 0 ? _g : "info"; this.toDocument = (_h = this.toDocument) !== null && _h !== void 0 ? _h : true; this.matchExtensionMimeType = (_j = this.matchExtensionMimeType) !== null && _j !== void 0 ? _j : 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 && [...connection_1.connectionVerbs].includes(this.name)) { 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.includes(input)) { throw new Error(`input \`${input}\` in action \`${this.name}\` is a reserved param`); } }); } } exports.Action = Action;