import { IOpenable } from 'pip-services3-commons-node'; import { IUnreferenceable } from 'pip-services3-commons-node'; import { IConfigurable } from 'pip-services3-commons-node'; import { IReferenceable } from 'pip-services3-commons-node'; import { IReferences } from 'pip-services3-commons-node'; import { ConfigParams } from 'pip-services3-commons-node'; import { DependencyResolver } from 'pip-services3-commons-node'; import { CompositeLogger } from 'pip-services3-components-node'; import { CompositeCounters } from 'pip-services3-components-node'; import { CounterTiming } from 'pip-services3-components-node'; import { Schema } from 'pip-services3-commons-node'; import { HttpEndpoint } from './HttpEndpoint'; import { IRegisterable } from './IRegisterable'; import { ISwaggerService } from './ISwaggerService'; /** * Abstract service that receives remove calls via HTTP/REST protocol. * * ### Configuration parameters ### * * - base_route: base route for remote URI * - dependencies: * - endpoint: override for HTTP Endpoint dependency * - controller: override for Controller dependency * - connection(s): * - 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]] * - protocol: connection protocol: http or https * - host: host name or IP address * - port: port number * - uri: resource URI or connection string with all parameters in it * - credential - the HTTPS credentials: * - ssl_key_file: the SSL private key in PEM * - ssl_crt_file: the SSL certificate in PEM * - ssl_ca_file: the certificate authorities (root cerfiticates) in PEM * * ### References ### * * - \*:logger:\*:\*:1.0 (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/log.ilogger.html ILogger]] components to pass log messages * - \*:counters:\*:\*:1.0 (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/count.icounters.html ICounters]] components to pass collected measurements * - \*:discovery:\*:\*:1.0 (optional) [[https://pip-services3-node.github.io/pip-services3-components-node/interfaces/connect.idiscovery.html IDiscovery]] services to resolve connection * - \*:endpoint:http:\*:1.0 (optional) [[HttpEndpoint]] reference * * @see [[RestClient]] * * ### Example ### * * class MyRestService extends RestService { * private _controller: IMyController; * ... * public constructor() { * base(); * this._dependencyResolver.put( * "controller", * new Descriptor("mygroup","controller","*","*","1.0") * ); * } * * public setReferences(references: IReferences): void { * base.setReferences(references); * this._controller = this._dependencyResolver.getRequired("controller"); * } * * public register(): void { * registerRoute("get", "get_mydata", null, (req, res) => { * let correlationId = req.param("correlation_id"); * let id = req.param("id"); * this._controller.getMyData(correlationId, id, this.sendResult(req, res)); * }); * ... * } * } * * let service = new MyRestService(); * service.configure(ConfigParams.fromTuples( * "connection.protocol", "http", * "connection.host", "localhost", * "connection.port", 8080 * )); * service.setReferences(References.fromTuples( * new Descriptor("mygroup","controller","default","default","1.0"), controller * )); * * service.open("123", (err) => { * console.log("The REST service is running on port 8080"); * }); */ export declare abstract class RestService implements IOpenable, IConfigurable, IReferenceable, IUnreferenceable, IRegisterable { private static readonly _defaultConfig; protected _config: ConfigParams; private _references; private _localEndpoint; private _opened; /** * The base route. */ protected _baseRoute: string; /** * The HTTP endpoint that exposes this service. */ protected _endpoint: HttpEndpoint; /** * The dependency resolver. */ protected _dependencyResolver: DependencyResolver; /** * The logger. */ protected _logger: CompositeLogger; /** * The performance counters. */ protected _counters: CompositeCounters; protected _swaggerService: ISwaggerService; protected _swaggerEnable: boolean; protected _swaggerRoute: string; /** * Configures component by passing configuration parameters. * * @param config configuration parameters to be set. */ configure(config: ConfigParams): void; /** * Sets references to dependent components. * * @param references references to locate the component dependencies. */ setReferences(references: IReferences): void; /** * Unsets (clears) previously set references to dependent components. */ unsetReferences(): void; private createEndpoint; /** * Adds instrumentation to log calls and measure call time. * It returns a CounterTiming object that is used to end the time measurement. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param name a method name. * @returns CounterTiming object to end the time measurement. */ protected instrument(correlationId: string, name: string): CounterTiming; /** * Adds instrumentation to error handling. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param name a method name. * @param err an occured error * @param result (optional) an execution result * @param callback (optional) an execution callback */ protected instrumentError(correlationId: string, name: string, err: any, result?: any, callback?: (err: any, result: any) => void): void; /** * Checks if the component is opened. * * @returns true if the component has been opened and false otherwise. */ isOpen(): boolean; /** * Opens the component. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param callback callback function that receives error or null no errors occured. */ open(correlationId: string, callback?: (err: any) => void): void; /** * Closes component and frees used resources. * * @param correlationId (optional) transaction id to trace execution through call chain. * @param callback callback function that receives error or null no errors occured. */ close(correlationId: string, callback?: (err: any) => void): void; /** * Creates a callback function that sends result as JSON object. * That callack function call be called directly or passed * as a parameter to business logic components. * * If object is not null it returns 200 status code. * For null results it returns 204 status code. * If error occur it sends ErrorDescription with approproate status code. * * @param req a HTTP request object. * @param res a HTTP response object. * @param callback function that receives execution result or error. */ protected sendResult(req: any, res: any): (err: any, result: any) => void; /** * Creates a callback function that sends newly created object as JSON. * That callack function call be called directly or passed * as a parameter to business logic components. * * If object is not null it returns 201 status code. * For null results it returns 204 status code. * If error occur it sends ErrorDescription with approproate status code. * * @param req a HTTP request object. * @param res a HTTP response object. * @param callback function that receives execution result or error. */ protected sendCreatedResult(req: any, res: any): (err: any, result: any) => void; /** * Creates a callback function that sends deleted object as JSON. * That callack function call be called directly or passed * as a parameter to business logic components. * * If object is not null it returns 200 status code. * For null results it returns 204 status code. * If error occur it sends ErrorDescription with approproate status code. * * @param req a HTTP request object. * @param res a HTTP response object. * @param callback function that receives execution result or error. */ protected sendDeletedResult(req: any, res: any): (err: any, result: any) => void; /** * Sends error serialized as ErrorDescription object * and appropriate HTTP status code. * If status code is not defined, it uses 500 status code. * * @param req a HTTP request object. * @param res a HTTP response object. * @param error an error object to be sent. */ protected sendError(req: any, res: any, error: any): void; private appendBaseRoute; /** * Registers a route in HTTP endpoint. * * @param method HTTP method: "get", "head", "post", "put", "delete" * @param route a command route. Base route will be added to this route * @param schema a validation schema to validate received parameters. * @param action an action function that is called when operation is invoked. */ protected registerRoute(method: string, route: string, schema: Schema, action: (req: any, res: any) => void): void; /** * Registers a route with authorization in HTTP endpoint. * * @param method HTTP method: "get", "head", "post", "put", "delete" * @param route a command route. Base route will be added to this route * @param schema a validation schema to validate received parameters. * @param authorize an authorization interceptor * @param action an action function that is called when operation is invoked. */ protected registerRouteWithAuth(method: string, route: string, schema: Schema, authorize: (req: any, res: any, next: () => void) => void, action: (req: any, res: any) => void): void; /** * Registers a middleware for a given route in HTTP endpoint. * * @param route a command route. Base route will be added to this route * @param action an action function that is called when middleware is invoked. */ protected registerInterceptor(route: string, action: (req: any, res: any, next: () => void) => void): void; /** * Returns correlationId from request * @param req - http request * @return Returns correlationId from request */ getCorrelationId(req: any): string; /** * Registers all service routes in HTTP endpoint. * * This method is called by the service and must be overriden * in child classes. */ abstract register(): void; protected registerOpenApiSpecFromFile(path: string): void; protected registerOpenApiSpec(content: string): void; }