1 | import { IOpenable } from 'pip-services3-commons-node';
|
2 | import { IUnreferenceable } from 'pip-services3-commons-node';
|
3 | import { IConfigurable } from 'pip-services3-commons-node';
|
4 | import { IReferenceable } from 'pip-services3-commons-node';
|
5 | import { IReferences } from 'pip-services3-commons-node';
|
6 | import { ConfigParams } from 'pip-services3-commons-node';
|
7 | import { DependencyResolver } from 'pip-services3-commons-node';
|
8 | import { CompositeLogger } from 'pip-services3-components-node';
|
9 | import { CompositeCounters } from 'pip-services3-components-node';
|
10 | import { CounterTiming } from 'pip-services3-components-node';
|
11 | import { Schema } from 'pip-services3-commons-node';
|
12 | import { HttpEndpoint } from './HttpEndpoint';
|
13 | import { IRegisterable } from './IRegisterable';
|
14 | import { ISwaggerService } from './ISwaggerService';
|
15 | /**
|
16 | * Abstract service that receives remove calls via HTTP/REST protocol.
|
17 | *
|
18 | * ### Configuration parameters ###
|
19 | *
|
20 | * - base_route: base route for remote URI
|
21 | * - dependencies:
|
22 | * - endpoint: override for HTTP Endpoint dependency
|
23 | * - controller: override for Controller dependency
|
24 | * - connection(s):
|
25 | * - 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]]
|
26 | * - protocol: connection protocol: http or https
|
27 | * - host: host name or IP address
|
28 | * - port: port number
|
29 | * - uri: resource URI or connection string with all parameters in it
|
30 | * - credential - the HTTPS credentials:
|
31 | * - ssl_key_file: the SSL private key in PEM
|
32 | * - ssl_crt_file: the SSL certificate in PEM
|
33 | * - ssl_ca_file: the certificate authorities (root cerfiticates) in PEM
|
34 | *
|
35 | * ### References ###
|
36 | *
|
37 | * - <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
|
38 | * - <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
|
39 | * - <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
|
40 | * - <code>\*:endpoint:http:\*:1.0</code> (optional) [[HttpEndpoint]] reference
|
41 | *
|
42 | * @see [[RestClient]]
|
43 | *
|
44 | * ### Example ###
|
45 | *
|
46 | * class MyRestService extends RestService {
|
47 | * private _controller: IMyController;
|
48 | * ...
|
49 | * public constructor() {
|
50 | * base();
|
51 | * this._dependencyResolver.put(
|
52 | * "controller",
|
53 | * new Descriptor("mygroup","controller","*","*","1.0")
|
54 | * );
|
55 | * }
|
56 | *
|
57 | * public setReferences(references: IReferences): void {
|
58 | * base.setReferences(references);
|
59 | * this._controller = this._dependencyResolver.getRequired<IMyController>("controller");
|
60 | * }
|
61 | *
|
62 | * public register(): void {
|
63 | * registerRoute("get", "get_mydata", null, (req, res) => {
|
64 | * let correlationId = req.param("correlation_id");
|
65 | * let id = req.param("id");
|
66 | * this._controller.getMyData(correlationId, id, this.sendResult(req, res));
|
67 | * });
|
68 | * ...
|
69 | * }
|
70 | * }
|
71 | *
|
72 | * let service = new MyRestService();
|
73 | * service.configure(ConfigParams.fromTuples(
|
74 | * "connection.protocol", "http",
|
75 | * "connection.host", "localhost",
|
76 | * "connection.port", 8080
|
77 | * ));
|
78 | * service.setReferences(References.fromTuples(
|
79 | * new Descriptor("mygroup","controller","default","default","1.0"), controller
|
80 | * ));
|
81 | *
|
82 | * service.open("123", (err) => {
|
83 | * console.log("The REST service is running on port 8080");
|
84 | * });
|
85 | */
|
86 | export declare abstract class RestService implements IOpenable, IConfigurable, IReferenceable, IUnreferenceable, IRegisterable {
|
87 | private static readonly _defaultConfig;
|
88 | protected _config: ConfigParams;
|
89 | private _references;
|
90 | private _localEndpoint;
|
91 | private _opened;
|
92 | /**
|
93 | * The base route.
|
94 | */
|
95 | protected _baseRoute: string;
|
96 | /**
|
97 | * The HTTP endpoint that exposes this service.
|
98 | */
|
99 | protected _endpoint: HttpEndpoint;
|
100 | /**
|
101 | * The dependency resolver.
|
102 | */
|
103 | protected _dependencyResolver: DependencyResolver;
|
104 | /**
|
105 | * The logger.
|
106 | */
|
107 | protected _logger: CompositeLogger;
|
108 | /**
|
109 | * The performance counters.
|
110 | */
|
111 | protected _counters: CompositeCounters;
|
112 | protected _swaggerService: ISwaggerService;
|
113 | protected _swaggerEnable: boolean;
|
114 | protected _swaggerRoute: string;
|
115 | /**
|
116 | * Configures component by passing configuration parameters.
|
117 | *
|
118 | * @param config configuration parameters to be set.
|
119 | */
|
120 | configure(config: ConfigParams): void;
|
121 | /**
|
122 | * Sets references to dependent components.
|
123 | *
|
124 | * @param references references to locate the component dependencies.
|
125 | */
|
126 | setReferences(references: IReferences): void;
|
127 | /**
|
128 | * Unsets (clears) previously set references to dependent components.
|
129 | */
|
130 | unsetReferences(): void;
|
131 | private createEndpoint;
|
132 | /**
|
133 | * Adds instrumentation to log calls and measure call time.
|
134 | * It returns a CounterTiming object that is used to end the time measurement.
|
135 | *
|
136 | * @param correlationId (optional) transaction id to trace execution through call chain.
|
137 | * @param name a method name.
|
138 | * @returns CounterTiming object to end the time measurement.
|
139 | */
|
140 | protected instrument(correlationId: string, name: string): CounterTiming;
|
141 | /**
|
142 | * Adds instrumentation to error handling.
|
143 | *
|
144 | * @param correlationId (optional) transaction id to trace execution through call chain.
|
145 | * @param name a method name.
|
146 | * @param err an occured error
|
147 | * @param result (optional) an execution result
|
148 | * @param callback (optional) an execution callback
|
149 | */
|
150 | protected instrumentError(correlationId: string, name: string, err: any, result?: any, callback?: (err: any, result: any) => void): void;
|
151 | /**
|
152 | * Checks if the component is opened.
|
153 | *
|
154 | * @returns true if the component has been opened and false otherwise.
|
155 | */
|
156 | isOpen(): boolean;
|
157 | /**
|
158 | * Opens the component.
|
159 | *
|
160 | * @param correlationId (optional) transaction id to trace execution through call chain.
|
161 | * @param callback callback function that receives error or null no errors occured.
|
162 | */
|
163 | open(correlationId: string, callback?: (err: any) => void): void;
|
164 | /**
|
165 | * Closes component and frees used resources.
|
166 | *
|
167 | * @param correlationId (optional) transaction id to trace execution through call chain.
|
168 | * @param callback callback function that receives error or null no errors occured.
|
169 | */
|
170 | close(correlationId: string, callback?: (err: any) => void): void;
|
171 | /**
|
172 | * Creates a callback function that sends result as JSON object.
|
173 | * That callack function call be called directly or passed
|
174 | * as a parameter to business logic components.
|
175 | *
|
176 | * If object is not null it returns 200 status code.
|
177 | * For null results it returns 204 status code.
|
178 | * If error occur it sends ErrorDescription with approproate status code.
|
179 | *
|
180 | * @param req a HTTP request object.
|
181 | * @param res a HTTP response object.
|
182 | * @param callback function that receives execution result or error.
|
183 | */
|
184 | protected sendResult(req: any, res: any): (err: any, result: any) => void;
|
185 | /**
|
186 | * Creates a callback function that sends newly created object as JSON.
|
187 | * That callack function call be called directly or passed
|
188 | * as a parameter to business logic components.
|
189 | *
|
190 | * If object is not null it returns 201 status code.
|
191 | * For null results it returns 204 status code.
|
192 | * If error occur it sends ErrorDescription with approproate status code.
|
193 | *
|
194 | * @param req a HTTP request object.
|
195 | * @param res a HTTP response object.
|
196 | * @param callback function that receives execution result or error.
|
197 | */
|
198 | protected sendCreatedResult(req: any, res: any): (err: any, result: any) => void;
|
199 | /**
|
200 | * Creates a callback function that sends deleted object as JSON.
|
201 | * That callack function call be called directly or passed
|
202 | * as a parameter to business logic components.
|
203 | *
|
204 | * If object is not null it returns 200 status code.
|
205 | * For null results it returns 204 status code.
|
206 | * If error occur it sends ErrorDescription with approproate status code.
|
207 | *
|
208 | * @param req a HTTP request object.
|
209 | * @param res a HTTP response object.
|
210 | * @param callback function that receives execution result or error.
|
211 | */
|
212 | protected sendDeletedResult(req: any, res: any): (err: any, result: any) => void;
|
213 | /**
|
214 | * Sends error serialized as ErrorDescription object
|
215 | * and appropriate HTTP status code.
|
216 | * If status code is not defined, it uses 500 status code.
|
217 | *
|
218 | * @param req a HTTP request object.
|
219 | * @param res a HTTP response object.
|
220 | * @param error an error object to be sent.
|
221 | */
|
222 | protected sendError(req: any, res: any, error: any): void;
|
223 | private appendBaseRoute;
|
224 | /**
|
225 | * Registers a route in HTTP endpoint.
|
226 | *
|
227 | * @param method HTTP method: "get", "head", "post", "put", "delete"
|
228 | * @param route a command route. Base route will be added to this route
|
229 | * @param schema a validation schema to validate received parameters.
|
230 | * @param action an action function that is called when operation is invoked.
|
231 | */
|
232 | protected registerRoute(method: string, route: string, schema: Schema, action: (req: any, res: any) => void): void;
|
233 | /**
|
234 | * Registers a route with authorization in HTTP endpoint.
|
235 | *
|
236 | * @param method HTTP method: "get", "head", "post", "put", "delete"
|
237 | * @param route a command route. Base route will be added to this route
|
238 | * @param schema a validation schema to validate received parameters.
|
239 | * @param authorize an authorization interceptor
|
240 | * @param action an action function that is called when operation is invoked.
|
241 | */
|
242 | protected registerRouteWithAuth(method: string, route: string, schema: Schema, authorize: (req: any, res: any, next: () => void) => void, action: (req: any, res: any) => void): void;
|
243 | /**
|
244 | * Registers a middleware for a given route in HTTP endpoint.
|
245 | *
|
246 | * @param route a command route. Base route will be added to this route
|
247 | * @param action an action function that is called when middleware is invoked.
|
248 | */
|
249 | protected registerInterceptor(route: string, action: (req: any, res: any, next: () => void) => void): void;
|
250 | /**
|
251 | * Returns correlationId from request
|
252 | * @param req - http request
|
253 | * @return Returns correlationId from request
|
254 | */
|
255 | getCorrelationId(req: any): string;
|
256 | /**
|
257 | * Registers all service routes in HTTP endpoint.
|
258 | *
|
259 | * This method is called by the service and must be overriden
|
260 | * in child classes.
|
261 | */
|
262 | abstract register(): void;
|
263 | protected registerOpenApiSpecFromFile(path: string): void;
|
264 | protected registerOpenApiSpec(content: string): void;
|
265 | }
|