1 | /// <reference types="node" />
|
2 | import * as express from 'express';
|
3 | import { asyncHandler, retry, sleep, verifyBodyParam, verifyQueryParam } from './utils';
|
4 | export { asyncHandler, retry, sleep, verifyBodyParam, verifyQueryParam };
|
5 | import * as http from 'http';
|
6 | /**
|
7 | * Logging interface. Allows log from multiple microservices to be aggregated.
|
8 | */
|
9 | export interface ILog {
|
10 | /**
|
11 | * Issue a warning.
|
12 | */
|
13 | warn(...args: any[]): void;
|
14 | /**
|
15 | * Issue an information message.
|
16 | */
|
17 | info(...args: any[]): void;
|
18 | /**
|
19 | * Issue a verbose message.
|
20 | */
|
21 | verbose(...args: any[]): void;
|
22 | /**
|
23 | * Record an error message.
|
24 | */
|
25 | error(...args: any[]): void;
|
26 | /**
|
27 | * Record an exception that was thrown
|
28 | */
|
29 | exception(err: any, ...args: any[]): void;
|
30 | }
|
31 | /**
|
32 | * Interface used to time various events.
|
33 | */
|
34 | export interface ITimer {
|
35 | /**
|
36 | * Start a timer.
|
37 | *
|
38 | * @param timerName The name of the timer.
|
39 | */
|
40 | start(timerName: string): void;
|
41 | /**
|
42 | * Stop a timer.
|
43 | *
|
44 | * @param timerName The name of the timer.
|
45 | */
|
46 | stop(timerName: string): void;
|
47 | }
|
48 | /**
|
49 | * Interface for a service to output metrics.
|
50 | */
|
51 | export interface IMetrics {
|
52 | /**
|
53 | * Output a discrete metric value.
|
54 | *
|
55 | * @param name The name of the metric.
|
56 | * @param value The value of the metric.
|
57 | */
|
58 | discrete(name: string, value: number): void;
|
59 | /**
|
60 | * Output a continous metric value.
|
61 | *
|
62 | * @param name The name of the metric.
|
63 | * @param value The value of the metric.
|
64 | */
|
65 | continuous(name: string, value: number): void;
|
66 | }
|
67 | /**
|
68 | * Configures a microservice.
|
69 | */
|
70 | export interface IMicroServiceConfig {
|
71 | /**
|
72 | * Enable verbose logging from micro.
|
73 | */
|
74 | verbose?: boolean;
|
75 | /**
|
76 | * Host to run the REST API.
|
77 | */
|
78 | host?: string;
|
79 | /**
|
80 | * Port to run the REST API.
|
81 | */
|
82 | port?: number;
|
83 | /**
|
84 | * Defines the connection to the RabbitMQ server.
|
85 | */
|
86 | messagingHost?: string;
|
87 | }
|
88 | /**
|
89 | * Interface for responding to events.
|
90 | */
|
91 | export interface IEventResponse {
|
92 | /**
|
93 | * Acknoledge that the event was successfully handled.
|
94 | */
|
95 | ack(): Promise<void>;
|
96 | }
|
97 | export interface IEventHandler {
|
98 | }
|
99 | /**
|
100 | * Defines a potentially asynchronous callback function for handling an incoming event.
|
101 | */
|
102 | export declare type EventHandlerFn<EventArgsT> = (eventArgs: EventArgsT, response: IEventResponse) => Promise<void>;
|
103 | /**
|
104 | * Defines a potentially asynchronous callback function for handling an incoming HTTP GET request.
|
105 | */
|
106 | export declare type GetRequestHandlerFn = (request: express.Request, response: express.Response) => Promise<void>;
|
107 | /**
|
108 | * Defines a potentially asynchronous callback function for handling an incoming HTTP POST request.
|
109 | */
|
110 | export declare type PostRequestHandlerFn = (request: express.Request, response: express.Response) => Promise<void>;
|
111 | /**
|
112 | * Interface for defining HTTP routes on a REST API.
|
113 | */
|
114 | export interface IHttpServer {
|
115 | /**
|
116 | * Create a handler for incoming HTTP GET requests.
|
117 | * Implemented by Express under the hood.
|
118 | */
|
119 | get(route: string, requestHandler: GetRequestHandlerFn): void;
|
120 | /**
|
121 | * Create a handler for incoming HTTP POST requests.
|
122 | * Implemented by Express under the hood
|
123 | *
|
124 | * @param route
|
125 | * @param requestHandler
|
126 | */
|
127 | post(route: string, requestHandler: PostRequestHandlerFn): void;
|
128 | /**
|
129 | * Setup serving of static files.
|
130 | *
|
131 | * @param dirPath The path to the directory that contains static files.
|
132 | */
|
133 | static(dirPath: string): void;
|
134 | }
|
135 | /**
|
136 | * Interface for issuing HTTP requests to a REST API.
|
137 | */
|
138 | export interface IHttpRequest {
|
139 | /**
|
140 | * Make a HTTP get request to another service.
|
141 | *
|
142 | * @param serviceName The name (logical or host) of the service.
|
143 | * @param route The HTTP route on the service to make the request to.
|
144 | * @param params Query parameters for the request.
|
145 | */
|
146 | get(serviceName: string, route: string, body?: any): Promise<any>;
|
147 | /**
|
148 | * Make a HTTP get request to another service.
|
149 | *
|
150 | * @param serviceName The name (logical or host) of the service.
|
151 | * @param route The HTTP route on the service to make the request to.
|
152 | * @param params Query parameters for the request.
|
153 | */
|
154 | post(serviceName: string, route: string, body: any): Promise<void>;
|
155 | /**
|
156 | * Forward HTTP get request to another named service.
|
157 | * The response from the forward requests is automatically piped into the passed in response.
|
158 | *
|
159 | * @param serviceName The name of the service to forward the request to.
|
160 | * @param route The HTTP GET route to forward to.
|
161 | * @param params Query parameters for the request.
|
162 | * @param res The stream to pipe response to.
|
163 | */
|
164 | forward(serviceName: string, route: string, req: express.Request, res: express.Response): void;
|
165 | }
|
166 | /**
|
167 | * Interface that represents a particular microservice instance.
|
168 | */
|
169 | export interface IMicroService {
|
170 | /**
|
171 | * Returns true if the messaging system is currently available.
|
172 | */
|
173 | isMessagingAvailable(): boolean;
|
174 | /**
|
175 | * Create a handler for a named incoming event from a direct queue.
|
176 | * Implemented by Rabbitmq under the hood for reliable messaging.
|
177 | *
|
178 | * @param eventName The name of the event to handle.
|
179 | * @param eventHandler Callback to be invoke when the incoming event is received.
|
180 | */
|
181 | on<EventArgsT = any>(eventName: string, eventHandlerFn: EventHandlerFn<EventArgsT>): Promise<IEventHandler>;
|
182 | /**
|
183 | * Unregister a previously register event handler.
|
184 | *
|
185 | * @param eventHandler The event handler to unregister.
|
186 | */
|
187 | off(eventHandler: IEventHandler): void;
|
188 | /**
|
189 | * Create a once-off handler for a named incoming event.
|
190 | * The event handler will only be invoke once before the event is unregistered.
|
191 | * Implemented by Rabbitmq under the hood for reliable messaging.
|
192 | *
|
193 | * @param eventName The name of the event to handle.
|
194 | * @param eventHandler Callback to be invoke when the incoming event is received.
|
195 | */
|
196 | once<EventArgsT = any>(eventName: string, eventHandlerFn: EventHandlerFn<EventArgsT>): Promise<void>;
|
197 | /***
|
198 | * Wait for a single incoming event, returns the events arguments and then unregister the event handler.
|
199 | *
|
200 | * @param eventName The name of the event to handle.
|
201 | *
|
202 | * @returns A promise to resolve the incoming event's arguments.
|
203 | */
|
204 | waitForOneEvent<EventArgsT = any>(eventName: string): Promise<EventArgsT>;
|
205 | /**
|
206 | * Emit a named outgoing event.
|
207 | * Implemented by Rabbitmq under the hood for reliable messaging.
|
208 | *
|
209 | * @param eventName The name of the event to emit.
|
210 | * @param eventArgs Event args to publish with the event and be received at the other end.
|
211 | */
|
212 | emit<EventArgsT = any>(eventName: string, eventArgs: EventArgsT): Promise<void>;
|
213 | /**
|
214 | * Reference to the logging interface.
|
215 | * This allows the logging from multiple microservices to be aggregated.
|
216 | */
|
217 | readonly log: ILog;
|
218 | /**
|
219 | * Reference to the timer interface.
|
220 | * Allows a service to time code for performance.
|
221 | */
|
222 | readonly timer: ITimer;
|
223 | /**
|
224 | * Reference to the metrics interface.
|
225 | * Allows a service to output metrics.
|
226 | */
|
227 | readonly metrics: IMetrics;
|
228 | /**
|
229 | * Reference to the express object.
|
230 | */
|
231 | readonly expressApp: express.Express;
|
232 | /**
|
233 | * Reference to the HTTP server.
|
234 | */
|
235 | readonly httpServer: http.Server;
|
236 | /**
|
237 | * Interface for defining the REST API.
|
238 | */
|
239 | readonly rest: IHttpServer;
|
240 | /**
|
241 | * Interface for issuing HTTP requests to a REST API.
|
242 | */
|
243 | readonly request: IHttpRequest;
|
244 | /**
|
245 | * Starts the microservice.
|
246 | * It starts listening for incoming HTTP requests and events.
|
247 | */
|
248 | start(): Promise<void>;
|
249 | }
|
250 | /**
|
251 | * Instantiates a microservice.
|
252 | *
|
253 | * @param [config] Optional configuration for the microservice.
|
254 | */
|
255 | export declare function micro(config?: IMicroServiceConfig): IMicroService;
|