UNPKG

4.81 kBTypeScriptView Raw
1import { ConfigParams } from 'pip-services3-commons-node';
2import { IReferences } from 'pip-services3-commons-node';
3import { ConnectionParams } from './ConnectionParams';
4/**
5 * Helper class to retrieve component connections.
6 *
7 * If connections are configured to be retrieved from [[IDiscovery]],
8 * it automatically locates [[IDiscovery]] in component references
9 * and retrieve connections from there using discovery_key parameter.
10 *
11 * ### Configuration parameters ###
12 *
13 * - __connection:__
14 * - 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]]
15 * - ... other connection parameters
16 *
17 * - __connections:__ alternative to connection
18 * - [connection params 1]: first connection parameters
19 * - ... connection parameters for key 1
20 * - [connection params N]: Nth connection parameters
21 * - ... connection parameters for key N
22 *
23 * ### References ###
24 *
25 * - <code>\*:discovery:\*:\*:1.0</code> (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/connect.idiscovery.html IDiscovery]] services to resolve connections
26 *
27 * @see [[ConnectionParams]]
28 * @see [[IDiscovery]]
29 *
30 * ### Example ###
31 *
32 * let config = ConfigParams.fromTuples(
33 * "connection.host", "10.1.1.100",
34 * "connection.port", 8080
35 * );
36 *
37 * let connectionResolver = new ConnectionResolver();
38 * connectionResolver.configure(config);
39 * connectionResolver.setReferences(references);
40 *
41 * connectionResolver.resolve("123", (err, connection) => {
42 * // Now use connection...
43 * });
44 */
45export declare class ConnectionResolver {
46 private readonly _connections;
47 private _references;
48 /**
49 * Creates a new instance of connection resolver.
50 *
51 * @param config (optional) component configuration parameters
52 * @param references (optional) component references
53 */
54 constructor(config?: ConfigParams, references?: IReferences);
55 /**
56 * Configures component by passing configuration parameters.
57 *
58 * @param config configuration parameters to be set.
59 */
60 configure(config: ConfigParams): void;
61 /**
62 * Sets references to dependent components.
63 *
64 * @param references references to locate the component dependencies.
65 */
66 setReferences(references: IReferences): void;
67 /**
68 * Gets all connections configured in component configuration.
69 *
70 * Redirect to Discovery services is not done at this point.
71 * If you need fully fleshed connection use [[resolve]] method instead.
72 *
73 * @returns a list with connection parameters
74 */
75 getAll(): ConnectionParams[];
76 /**
77 * Adds a new connection to component connections
78 *
79 * @param connection new connection parameters to be added
80 */
81 add(connection: ConnectionParams): void;
82 private resolveInDiscovery;
83 /**
84 * Resolves a single component connection. If connections are configured to be retrieved
85 * from Discovery service it finds a [[IDiscovery]] and resolves the connection there.
86 *
87 * @param correlationId (optional) transaction id to trace execution through call chain.
88 * @param callback callback function that receives resolved connection or error.
89 *
90 * @see [[IDiscovery]]
91 */
92 resolve(correlationId: string, callback: (err: any, result: ConnectionParams) => void): void;
93 private resolveAllInDiscovery;
94 /**
95 * Resolves all component connection. If connections are configured to be retrieved
96 * from Discovery service it finds a [[IDiscovery]] and resolves the connection there.
97 *
98 * @param correlationId (optional) transaction id to trace execution through call chain.
99 * @param callback callback function that receives resolved connections or error.
100 *
101 * @see [[IDiscovery]]
102 */
103 resolveAll(correlationId: string, callback: (err: any, result: ConnectionParams[]) => void): void;
104 private registerInDiscovery;
105 /**
106 * Registers the given connection in all referenced discovery services.
107 * This method can be used for dynamic service discovery.
108 *
109 * @param correlationId (optional) transaction id to trace execution through call chain.
110 * @param connection a connection to register.
111 * @param callback callback function that receives registered connection or error.
112 *
113 * @see [[IDiscovery]]
114 */
115 register(correlationId: string, connection: ConnectionParams, callback: (err: any) => void): void;
116}