UNPKG

2.12 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const metadataReading_1 = require("./internal/metadata/metadataReading");
4const string_1 = require("./internal/util/string");
5const scan_1 = require("./scan");
6const constructionUtils_1 = require("./util/constructionUtils");
7/**
8 * Create a HandleCommand instance with the appropriate metadata wrapping
9 * the given function
10 * @param h handle function
11 * @param factory construction function
12 * @param {string} name can be omitted if the function isn't exported
13 * @param {string} description
14 * @param {string[]} intent
15 * @param {Tag[]} tags
16 * @return {HandleCommand<P>}
17 */
18function commandHandlerFrom(h, factory, name = h.name || `Command${string_1.generateHash(h.toString())}`, description = name, intent = [], tags = []) {
19 const handler = new FunctionWrappingCommandHandler(name, description, h, factory, tags, intent);
20 scan_1.registerCommand(handler);
21 return handler;
22}
23exports.commandHandlerFrom = commandHandlerFrom;
24class FunctionWrappingCommandHandler {
25 constructor(name, description, h, parametersFactory,
26 // tslint:disable-next-line:variable-name
27 _tags = [],
28 // tslint:disable-next-line:variable-name
29 _intent = []) {
30 this.name = name;
31 this.description = description;
32 this.h = h;
33 this.parametersFactory = parametersFactory;
34 this._tags = _tags;
35 this._intent = _intent;
36 const newParamInstance = this.freshParametersInstance();
37 const md = metadataReading_1.metadataFromInstance(newParamInstance);
38 this.parameters = md.parameters;
39 this.mapped_parameters = md.mapped_parameters;
40 this.values = md.values;
41 this.secrets = md.secrets;
42 this.intent = string_1.toStringArray(_intent);
43 this.tags = string_1.toStringArray(_tags).map(t => ({ name: t, description: t }));
44 }
45 freshParametersInstance() {
46 return constructionUtils_1.toFactory(this.parametersFactory)();
47 }
48 handle(ctx, params) {
49 return this.h(ctx, params);
50 }
51}
52//# sourceMappingURL=onCommand.js.map
\No newline at end of file