1 | /*
|
2 | * Copyright (c) 2018 One Hill Technologies, LLC
|
3 | *
|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | * you may not use this file except in compliance with the License.
|
6 | * You may obtain a copy of the License at
|
7 | *
|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 | *
|
10 | * Unless required by applicable law or agreed to in writing, software
|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13 | * See the License for the specific language governing permissions and
|
14 | * limitations under the License.
|
15 | */
|
16 |
|
17 | const { BO, computed } = require ('base-object');
|
18 |
|
19 | /**
|
20 | * @class Action
|
21 | *
|
22 | * The base class for all actions.
|
23 | *
|
24 | * _About the events_:
|
25 | *
|
26 | * The event API for the actions is different from the traditional Events API where
|
27 | * the events correspond to objects on this class. Because you cannot gain access to
|
28 | * an Action instance (i.e., it is kept internally), the Events correspond to the
|
29 | * application. This is equivalent to saying the Action supports the ApplicationMessaging
|
30 | * events. We do not use the ApplicationMessaging mixin, however, because we can gain
|
31 | * access to the application object via the `controller` property.
|
32 | */
|
33 | module.exports = BO.extend ({
|
34 | mergedProperties: ['schema'],
|
35 |
|
36 | /**
|
37 | * Optional express-validator schema for validating the request that
|
38 | * trigger the action.
|
39 | */
|
40 | schema: null,
|
41 |
|
42 | /**
|
43 | * Optional middleware function(s) for validating the request that triggered
|
44 | * the action.
|
45 | */
|
46 | validate: null,
|
47 |
|
48 | /**
|
49 | * Execute the request.
|
50 | *
|
51 | * The signature of this method is f(req, res);
|
52 | *
|
53 | * This method has the option of returning a Promise, which informs the framework
|
54 | * that completion of the request is pending.
|
55 | *
|
56 | * @returns {Promise|null}
|
57 | */
|
58 | execute: null,
|
59 |
|
60 | /// The hosting controller for the action.
|
61 | controller: null,
|
62 |
|
63 | /// Quick access to the application.
|
64 | app: computed.alias ('controller.app'),
|
65 |
|
66 | /// @{ Events
|
67 |
|
68 | emit () {
|
69 | return this.app.emit (...arguments);
|
70 | },
|
71 |
|
72 | on () {
|
73 | return this.app.on (...arguments);
|
74 | },
|
75 |
|
76 | once () {
|
77 | return this.app.once (...arguments);
|
78 | },
|
79 |
|
80 | hasListeners (ev) {
|
81 | return this.app.hasListeners (ev);
|
82 | },
|
83 |
|
84 | getListeners (ev) {
|
85 | return this.app.getListeners (ev);
|
86 | }
|
87 |
|
88 | /// @}
|
89 | });
|