1 | /**
|
2 | * @class APIBuilder
|
3 | * The top-level module for managing APIBuilder services.
|
4 | *
|
5 | * An APIBuilder service is a Node.js server that runs in the API Runtime Services.
|
6 | * To create a server, load the APIBuilder module, then use the APIBuilder constructor to create an
|
7 | * APIBuilder instance and invoke its {@link #start start() method}.
|
8 | *
|
9 | * var APIBuilder = require('@axway/api-builder-runtime'),
|
10 | * server = new APIBuilder();
|
11 | *
|
12 | * // Add event listeners or invoke APIs on the APIBuilder instance
|
13 | * var myModel = APIBuilder.createModel('foo', {...});
|
14 | * server.addModel(myModel);
|
15 | *
|
16 | * server.on('starting', function () {
|
17 | * server.logger.trace('Starting server...');
|
18 | * });
|
19 | *
|
20 | * server.start();
|
21 | *
|
22 | * ### Components
|
23 | *
|
24 | * An API Builder service is comprised of several components. You can either define the
|
25 | * components using JavaScript files placed in specific directories, which are automatically loaded
|
26 | * when creating an API Builder instance, or programmatically create components after initializing
|
27 | * an API Builder instance.
|
28 | *
|
29 | * For information about the structure of API Builder services, see
|
30 | * [API Builder guides](https://docs.axway.com/bundle/API_Builder_4x_allOS_en/page/api_builder.html).
|
31 | *
|
32 | * #### Express and Third-Party Middleware
|
33 | *
|
34 | * API Builder uses Express for its web framework. By default, API Builder creates an Express app
|
35 | * instance when creating an API Builder instance, then binds its API endpoints to the app instance.
|
36 | * You can access the Express app instance using the `app` property of the API Builder instance,
|
37 | * then invoke [Express API calls](http://expressjs.com/4x/api.html) on the app instance.
|
38 | *
|
39 | * server.app.all('/api/*', function (req, res, next) {
|
40 | * req.server.logger.info('Intercepted the request!');
|
41 | * next();
|
42 | * });
|
43 | *
|
44 | * Do not pass the error code to the response object's `send()` method. Use the `status()` method
|
45 | * instead. If you pass a number value to the `send()` method, an error will be thrown.
|
46 | *
|
47 | * // Throws an error
|
48 | * res.send(500);
|
49 | *
|
50 | * // Invoke the following methods instead.
|
51 | * res.status(500).send('Uh oh. Something bad happened.');
|
52 | *
|
53 | * API Builder also loads the body-parser, cookie-parser and busboy modules to provide additional
|
54 | * parsing capabilities as well as the compression module.
|
55 | *
|
56 | * For Express template engines, API Builder uses ejs, handlebars, marked and react modules.
|
57 | * Access the template engines using the {@link APIBuilder.Middleware} API.
|
58 | *
|
59 | * #### Connectors
|
60 | *
|
61 | * {@link APIBuilder.Connector Connectors} allow an API Builder service to access data stored
|
62 | * in an external source. Connectors are structured like API Builder service where they have
|
63 | * their own models, dependencies and configuration files.
|
64 | *
|
65 | * You can either use an existing connector or create your own connector.
|
66 | *
|
67 | * #### Models
|
68 | *
|
69 | * {@link APIBuilder.Model Models} provide an interface for the API Builder service to access
|
70 | * data. By default, the service will generate standardized endpoints for a client to access
|
71 | * the data from the API Builder service.
|
72 | *
|
73 | * #### APIs
|
74 | *
|
75 | * {@link APIBuilder.API APIs} provide a way for a client to access your service, execute custom
|
76 | * logic and internally access the service's models, then return data back to the client.
|
77 | *
|
78 | * Create an API to execute a custom operation with model data.
|
79 | *
|
80 | * #### Blocks
|
81 | *
|
82 | * {@link APIBuilder.Block Blocks} are functions that run before or after an API endpoint is
|
83 | executed. * They can be used to modify the API request, to modify the API response or to execute
|
84 | common * tasks like audit logging, caching, rate limiting or recording analytics. Multiple blocks
|
85 | can * be executed before or after an API request. Blocks are optional.
|
86 | *
|
87 | * #### Webs
|
88 | *
|
89 | * Webs are endpoints that render HTML to the client.
|
90 | * Webs are comprised of routes, renderers, templates and static assets.
|
91 | *
|
92 | * {@link APIBuilder.Router Routes} provide the endpoint to access the Web and logic to access the
|
93 | * {@data.
|
94 | *
|
95 | * {@link APIBuilderRendererEngine Renderers} (or template engines) apply local data to your
|
96 | templates * to generate HTML. To access a renderer or create your own renderer, use the {@link
|
97 | APIBuilder.Middleware} API.
|
98 | *
|
99 | * Templates are the files you want to render. Place all templates in the `./web/views` folder
|
100 | * with an appropriate extension--either `ejs`, `hbs`, `html`, `jsx`, `md` or a custom one that
|
101 | * you can define. API Builder selects the renderer engine based on the file extension.
|
102 | *
|
103 | * Place all assets, such as images, style sheets, static HTML files, etc., in the `./web/public`
|
104 | * folder.
|
105 | *
|
106 | * #### Logger
|
107 | *
|
108 | * The {@link APIBuilder.Logger} provides a wrapper for
|
109 | * [bunyan](https://www.npmjs.com/package/bunyan), a JSON logging utility. By default, API Builder
|
110 | * creates a logger instance, which can be accessed using the `logger` property of the API Builder
|
111 | * instance.
|
112 | *
|
113 | * server.logger.trace('breadcrumbs...');
|
114 | * server.logger.info('%s %s', key, value);
|
115 | * server.logger.error(err);
|
116 | *
|
117 | * ### Lifecycle
|
118 | *
|
119 | * The following sections describe the sequence of events that occur when initializing and starting
|
120 | * an API Builder service, then making requests to the server.
|
121 | *
|
122 | * #### Initialization Sequence
|
123 | *
|
124 | * When creating a new API Builder instance, the following events occur:
|
125 | *
|
126 | * 1. Loads the configuration files.
|
127 | * 2. Creates an Express app instance unless you pass `loadOnly` as `true` in the constructor.
|
128 | * 3. Creates the API Builder Console unless its disabled.
|
129 | * 4. Creates a Logger instance.
|
130 | * 5. Registers the connectors.
|
131 | * 6. Loads the model and block files.
|
132 | * 7. Loads API and Web files and binds the endpoints to the Express app instance.
|
133 | * 8. Fires the `loaded` event.
|
134 | *
|
135 | * At this point, you may add event listeners or programmatically add any other API Builder
|
136 | * components.
|
137 | *
|
138 | * #### Startup Sequence
|
139 | *
|
140 | * After invoking the `start()` method on the API Builder instance:
|
141 | *
|
142 | * 1. Fires the `starting` event.
|
143 | * 2. Registers and binds the model's standardized API endpoints to the Express app instance.
|
144 | * 3. Fires the `listening` event.
|
145 | * 4. Binds the server to a port.
|
146 | * 5. Fires the `listen` event.
|
147 | * 6. Fires the `started` event.
|
148 | *
|
149 | * The client can now make requests to the server.
|
150 | *
|
151 | * #### Request Sequence
|
152 | *
|
153 | * When a client makes a request to the API Builder service, API Builder first authenticates the
|
154 | * request, then proceeds by making a series of middleware calls, where the `next()` function needs
|
155 | * to be called after completing each operation.
|
156 | *
|
157 | * 1. Authenticate the request. If you are using a custom security plugin, the `validateRequest()`
|
158 | * method is defined.
|
159 | * 2. For Models, calls the Connector's `startRequest()`, `loginRequired()` and `login()` methods
|
160 | * if defined.
|
161 | * 3. Calls any defined pre-Blocks.
|
162 | * 4. Calls the API, Model or Router logic.
|
163 | * 5. Calls any defined post-Blocks.
|
164 | * 6. For Models, calls the Connector's `endRequest()` method if defined.
|
165 | * 7. Fires the `after` event.
|
166 | *
|
167 | * ### API Builder Middleware Calls
|
168 | *
|
169 | * Each API Builder middleware call (`action` or `execute` property) is passed the same three
|
170 | * objects:
|
171 | *
|
172 | * * [Express Request object](http://expressjs.com/4x/api.html#req)
|
173 | * * [Express Response object](http://expressjs.com/4x/api.html#res)
|
174 | * * Middleware function to call next. Always call the `next()` function to execute the next
|
175 | * middleware call in the sequence.
|
176 | *
|
177 | * In addition to the Express APIs, the request object is also passed a reference to the API Builder
|
178 | * instance as the `server` property. Use the instance to invoke APIs, such as accessing
|
179 | * model data or invoking an API endpoint. Both the request and response objects are passed a
|
180 | * reference to the Logger instance as the `logger` property to log messages.
|
181 | *
|
182 | * For APIBuilder.API instances, the request and response objects are also passed the `model`
|
183 | * and `responseModel` properties, which references the models passed to the API constructor.
|
184 | */
|
185 |
|
186 | /**
|
187 | * @constructor
|
188 | * Loads the configuration files in the `./conf` folder and initializes an API Builder instance.
|
189 | * @param {Object} [config] Additional configuration parameters, which are merged with the
|
190 | * configuration files. @param {Boolean} [loadOnly] Set to `true` to only load the API Builder
|
191 | * components. Does not create the Express app instance or starts the admin console.
|
192 | */
|
193 |
|
194 | /**
|
195 | * @event after
|
196 | * Fired after a request completes. The callback will be passed the request and response objects.
|
197 | */
|
198 | /**
|
199 | * @event error
|
200 | * Fired if an error occurred when setting up the server. The callback will be passed the error
|
201 | * object.
|
202 | */
|
203 | /**
|
204 | * @event listening
|
205 | * Fired before binding the server to a port.
|
206 | */
|
207 | /**
|
208 | * @event listen
|
209 | * Fired when the server can accept connections.
|
210 | */
|
211 | /**
|
212 | * @event loaded
|
213 | * Fired after initializing the API Builder instance.
|
214 | */
|
215 | /**
|
216 | * @event reloaded
|
217 | * Fired after reloading an API Builder component.
|
218 | */
|
219 | /**
|
220 | * @event starting
|
221 | * Fired when the `start()` method is invoked.
|
222 | */
|
223 | /**
|
224 | * @event started
|
225 | * Fired when the `start()` method completes.
|
226 | */
|
227 | /**
|
228 | * @event stopping
|
229 | * Fired when the `stop()` method is invoked.
|
230 | */
|
231 | /**
|
232 | * @event stopped
|
233 | * Fired when the `stop()` method completes.
|
234 | */
|
235 |
|
236 | /**
|
237 | * @method addListener
|
238 | * Binds a callback to an event.
|
239 | * @static
|
240 | * @param {String} name Event name
|
241 | * @param {Function} cb Callback function to execute.
|
242 | */
|
243 | /**
|
244 | * @method on
|
245 | * @alias #static-method-addListener
|
246 | * @static
|
247 | */
|
248 | /**
|
249 | * @method once
|
250 | * Binds an one-time listener to an event.
|
251 | * @static
|
252 | * @param {String} name Event name
|
253 | * @param {Function} cb Callback function to execute.
|
254 | */
|
255 | /**
|
256 | * @method removeListener
|
257 | * Unbinds a callback from an event.
|
258 | * @static
|
259 | * @param {String} name Event name
|
260 | * @param {Function} cb Callback function to remove.
|
261 | */
|
262 | /**
|
263 | * @method removeAllListeners
|
264 | * Unbinds all event callbacks for the specified event.
|
265 | * @static
|
266 | * @param {String} [name] Event name. If omitted, unbinds all event listeners.
|
267 | */
|
268 |
|
269 | /**
|
270 | * @property {Array<APIBuilder.API>} apis
|
271 | * APIs loaded and added to the instance.
|
272 | */
|
273 | /**
|
274 | * @property {Object} app
|
275 | * Express app instance.
|
276 | */
|
277 | /**
|
278 | * @property {Array<APIBuilder.Block>} blocks
|
279 | * Blocks loaded or added to the instance.
|
280 | */
|
281 | /**
|
282 | * @property {Object} config
|
283 | * Configuration object used to initialize the server instance.
|
284 | */
|
285 | /**
|
286 | * @property {Object} express
|
287 | * Express module instance.
|
288 | * @since 1.1.0
|
289 | */
|
290 | /**
|
291 | * @property {Boolean} ignoreDuplicateModels
|
292 | * Set to `true` to ignore duplicate models.
|
293 | */
|
294 | /**
|
295 | * @property {APIBuilder.Logger} logger
|
296 | * Logger instance.
|
297 | */
|
298 | /**
|
299 | * @property {APIBuilder.Middleware} middleware
|
300 | * Middleware instance.
|
301 | */
|
302 | /**
|
303 | * @property {Array<APIBuilder.Model>} models
|
304 | * Models loaded or added to the instance.
|
305 | */
|
306 | /**
|
307 | * @property {Number} port
|
308 | * Port number used by the server instance.
|
309 | */
|
310 | /**
|
311 | * @property {Array<APIBuilder.Router>} routes
|
312 | * Routes loaded to the instance.
|
313 | */
|