UNPKG

6.9 kBTypeScriptView Raw
1import { IReferences } from 'pip-services3-commons-node';
2import { DependencyResolver } from 'pip-services3-commons-node';
3import { Schema } from 'pip-services3-commons-node';
4import { Container } from 'pip-services3-container-node';
5import { CounterTiming } from 'pip-services3-components-node';
6import { CompositeCounters } from 'pip-services3-components-node';
7/**
8 * Abstract AWS Lambda function, that acts as a container to instantiate and run components
9 * and expose them via external entry point.
10 *
11 * When handling calls "cmd" parameter determines which what action shall be called, while
12 * other parameters are passed to the action itself.
13 *
14 * Container configuration for this Lambda function is stored in <code>"./config/config.yml"</code> file.
15 * But this path can be overriden by <code>CONFIG_PATH</code> environment variable.
16 *
17 * ### Configuration parameters ###
18 *
19 * - dependencies:
20 * - controller: override for Controller dependency
21 * - connections:
22 * - discovery_key: (optional) a key to retrieve the connection from [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/connect.idiscovery.html IDiscovery]]
23 * - region: (optional) AWS region
24 * - credentials:
25 * - store_key: (optional) a key to retrieve the credentials from [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/auth.icredentialstore.html ICredentialStore]]
26 * - access_id: AWS access/client id
27 * - access_key: AWS access/client id
28 *
29 * ### References ###
30 *
31 * - <code>\*:logger:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/log.ilogger.html ILogger]] components to pass log messages
32 * - <code>\*:counters:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/count.icounters.html ICounters]] components to pass collected measurements
33 * - <code>\*:discovery:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/connect.idiscovery.html IDiscovery]] services to resolve connection
34 * - <code>\*:credential-store:\*:\*:1.0</code> (optional) Credential stores to resolve credentials
35 *
36 * @see [[LambdaClient]]
37 *
38 * ### Example ###
39 *
40 * class MyLambdaFunction extends LambdaFunction {
41 * private _controller: IMyController;
42 * ...
43 * public constructor() {
44 * base("mygroup", "MyGroup lambda function");
45 * this._dependencyResolver.put(
46 * "controller",
47 * new Descriptor("mygroup","controller","*","*","1.0")
48 * );
49 * }
50 *
51 * public setReferences(references: IReferences): void {
52 * base.setReferences(references);
53 * this._controller = this._dependencyResolver.getRequired<IMyController>("controller");
54 * }
55 *
56 * public register(): void {
57 * registerAction("get_mydata", null, (params, callback) => {
58 * let correlationId = params.correlation_id;
59 * let id = params.id;
60 * this._controller.getMyData(correlationId, id, callback);
61 * });
62 * ...
63 * }
64 * }
65 *
66 * let lambda = new MyLambdaFunction();
67 *
68 * service.run((err) => {
69 * console.log("MyLambdaFunction is started");
70 * });
71 */
72export declare abstract class LambdaFunction extends Container {
73 /**
74 * The performanc counters.
75 */
76 protected _counters: CompositeCounters;
77 /**
78 * The dependency resolver.
79 */
80 protected _dependencyResolver: DependencyResolver;
81 /**
82 * The map of registred validation schemas.
83 */
84 protected _schemas: {
85 [id: string]: Schema;
86 };
87 /**
88 * The map of registered actions.
89 */
90 protected _actions: {
91 [id: string]: any;
92 };
93 /**
94 * The default path to config file.
95 */
96 protected _configPath: string;
97 /**
98 * Creates a new instance of this lambda function.
99 *
100 * @param name (optional) a container name (accessible via ContextInfo)
101 * @param description (optional) a container description (accessible via ContextInfo)
102 */
103 constructor(name?: string, description?: string);
104 private getConfigPath;
105 private getParameters;
106 private captureErrors;
107 private captureExit;
108 /**
109 * Sets references to dependent components.
110 *
111 * @param references references to locate the component dependencies.
112 */
113 setReferences(references: IReferences): void;
114 /**
115 * Adds instrumentation to log calls and measure call time.
116 * It returns a CounterTiming object that is used to end the time measurement.
117 *
118 * @param correlationId (optional) transaction id to trace execution through call chain.
119 * @param name a method name.
120 * @returns CounterTiming object to end the time measurement.
121 */
122 protected instrument(correlationId: string, name: string): CounterTiming;
123 /**
124 * Runs this lambda function, loads container configuration,
125 * instantiate components and manage their lifecycle,
126 * makes this function ready to access action calls.
127 *
128 * @param callback callback function that receives error or null for success.
129 */
130 run(callback?: (err: any) => void): void;
131 /**
132 * Registers all actions in this lambda function.
133 *
134 * This method is called by the service and must be overriden
135 * in child classes.
136 */
137 protected abstract register(): void;
138 /**
139 * Registers an action in this lambda function.
140 *
141 * @param cmd a action/command name.
142 * @param schema a validation schema to validate received parameters.
143 * @param action an action function that is called when action is invoked.
144 */
145 protected registerAction(cmd: string, schema: Schema, action: (params: any, callback: (err: any, result: any) => void) => void): void;
146 private execute;
147 private handler;
148 /**
149 * Gets entry point into this lambda function.
150 *
151 * @param event an incoming event object with invocation parameters.
152 * @param context a context object with local references.
153 */
154 getHandler(): (event: any, context: any) => void;
155 /**
156 * Calls registered action in this lambda function.
157 * "cmd" parameter in the action parameters determin
158 * what action shall be called.
159 *
160 * This method shall only be used in testing.
161 *
162 * @param params action parameters.
163 * @param callback callback function that receives action result or error.
164 */
165 act(params: any, callback: (err: any, result: any) => void): void;
166}