UNPKG

5.38 kBTypeScriptView Raw
1/** @module clients */
2import { IOpenable } from 'pip-services3-commons-node';
3import { IConfigurable } from 'pip-services3-commons-node';
4import { IReferenceable } from 'pip-services3-commons-node';
5import { IReferences } from 'pip-services3-commons-node';
6import { DependencyResolver } from 'pip-services3-commons-node';
7import { CompositeLogger } from 'pip-services3-components-node';
8import { CompositeCounters } from 'pip-services3-components-node';
9import { ConfigParams } from 'pip-services3-commons-node';
10import { CounterTiming } from 'pip-services3-components-node';
11/**
12 * Abstract client that calls controller directly in the same memory space.
13 *
14 * It is used when multiple microservices are deployed in a single container (monolyth)
15 * and communication between them can be done by direct calls rather then through
16 * the network.
17 *
18 * ### Configuration parameters ###
19 *
20 * - dependencies:
21 * - controller: override controller descriptor
22 *
23 * ### References ###
24 *
25 * - <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
26 * - <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
27 * - <code>\*:controller:\*:\*:1.0</code> controller to call business methods
28 *
29 * ### Example ###
30 *
31 * class MyDirectClient extends DirectClient<IMyController> implements IMyClient {
32 *
33 * public constructor() {
34 * super();
35 * this._dependencyResolver.put('controller', new Descriptor(
36 * "mygroup", "controller", "*", "*", "*"));
37 * }
38 * ...
39 *
40 * public getData(correlationId: string, id: string,
41 * callback: (err: any, result: MyData) => void): void {
42 *
43 * let timing = this.instrument(correlationId, 'myclient.get_data');
44 * this._controller.getData(correlationId, id, (err, result) => {
45 * timing.endTiming();
46 * this.instrumentError(correlationId, 'myclient.get_data', err, result, callback);
47 * });
48 * }
49 * ...
50 * }
51 *
52 * let client = new MyDirectClient();
53 * client.setReferences(References.fromTuples(
54 * new Descriptor("mygroup","controller","default","default","1.0"), controller
55 * ));
56 *
57 * client.getData("123", "1", (err, result) => {
58 * ...
59 * });
60 */
61export declare abstract class DirectClient<T> implements IConfigurable, IReferenceable, IOpenable {
62 /**
63 * The controller reference.
64 */
65 protected _controller: T;
66 /**
67 * The open flag.
68 */
69 protected _opened: boolean;
70 /**
71 * The logger.
72 */
73 protected _logger: CompositeLogger;
74 /**
75 * The performance counters
76 */
77 protected _counters: CompositeCounters;
78 /**
79 * The dependency resolver to get controller reference.
80 */
81 protected _dependencyResolver: DependencyResolver;
82 /**
83 * Creates a new instance of the client.
84 */
85 constructor();
86 /**
87 * Configures component by passing configuration parameters.
88 *
89 * @param config configuration parameters to be set.
90 */
91 configure(config: ConfigParams): void;
92 /**
93 * Sets references to dependent components.
94 *
95 * @param references references to locate the component dependencies.
96 */
97 setReferences(references: IReferences): void;
98 /**
99 * Adds instrumentation to log calls and measure call time.
100 * It returns a CounterTiming object that is used to end the time measurement.
101 *
102 * @param correlationId (optional) transaction id to trace execution through call chain.
103 * @param name a method name.
104 * @returns CounterTiming object to end the time measurement.
105 */
106 protected instrument(correlationId: string, name: string): CounterTiming;
107 /**
108 * Adds instrumentation to error handling.
109 *
110 * @param correlationId (optional) transaction id to trace execution through call chain.
111 * @param name a method name.
112 * @param err an occured error
113 * @param result (optional) an execution result
114 * @param callback (optional) an execution callback
115 */
116 protected instrumentError(correlationId: string, name: string, err: any, result?: any, callback?: (err: any, result: any) => void): void;
117 /**
118 * Checks if the component is opened.
119 *
120 * @returns true if the component has been opened and false otherwise.
121 */
122 isOpen(): boolean;
123 /**
124 * Opens the component.
125 *
126 * @param correlationId (optional) transaction id to trace execution through call chain.
127 * @param callback callback function that receives error or null no errors occured.
128 */
129 open(correlationId: string, callback?: (err: any) => void): void;
130 /**
131 * Closes component and frees used resources.
132 *
133 * @param correlationId (optional) transaction id to trace execution through call chain.
134 * @param callback callback function that receives error or null no errors occured.
135 */
136 close(correlationId: string, callback?: (err: any) => void): void;
137}