UNPKG

3.04 kBJavaScriptView Raw
1/**
2 * @class APIBuilder.Block
3 * A Block is a function that is executed before or after calling an API endpoint.
4 *
5 * To create a block, pass the constructor an implementation object, API Builder configuration
6 * object and API Builder instance to create a block instance, then pass the Block instance to the
7 * API Builder instance's {@link APIBuilder#addBlock addBlock()} method.
8 *
9 * var block = new APIBuilder.Block({
10 * name: 'blocky',
11 * execute: function (request, response, next) {
12 * next();
13 * }
14 * });
15 * server.addBlock(block);
16 *
17 * To use a block, assign it to the `before` or `after` property of the implementation object
18 * passed to the API or Model constructor. A Block is identified by its `name` property.
19 * You may specify more than one block by using an array. The blocks will be executed in order.
20 *
21 * var fooAPI = new APIBuilder.API({
22 * group: 'foo',
23 * path: '/api/foo/',
24 * method: 'GET',
25 * description: 'Get all the Foo objects',
26 * before: ['block1', 'block2'],
27 * model: 'testuser',
28 * action: function(request, response, next) {
29 * request.model.findAll(function(err, results) {
30 * if (err) {
31 * next(err);
32 * } else {
33 * next(null, results);
34 * }
35 * });
36 * }
37 * }, server.config, server);
38 * fooAPI.bind(server.app);
39 *
40 * Alternatively, you can define your Blocks using JavaScript files. For details, see the
41 * [API Builder Blocks
42 * [guide](https://docs.axway.com/bundle/API_Builder_4x_allOS_en/page/api_builder_blocks.html).
43 */
44
45/**
46 * @constructor
47 * Creates an new instance of a Block.
48 * @param {Dictionary<APIBuilder.Block>} impl Implementation object.
49 * Set any Block properties on the object except the ones marked non-creation.
50 * The {@link #name} property must be set.
51 * @param {Object} config API Builder configuration object.
52 * @param {APIBuilder} apibuilder API Builder instance.
53 * @throws Error Missing `name` parameter.
54 */
55
56/**
57 * @property {APIBuilder} apibuilder
58 * @nonCreation
59 * API Builder instance associated with the Block instance.
60 */
61/**
62 * @property {Object} config
63 * @nonCreation
64 * Configuration object used to initialize the Block instance.
65 */
66/**
67 * @property {Function} constructor
68 * Custom constructor to execute custom initialization logic. The new block instance is passed to
69 * `this` in the function.
70 */
71/**
72 * @property {String} description
73 * Human-readable description of the block, which is used by the generated API documentation.
74 */
75/**
76 * @property {Function} execute
77 * Logic to execute. The function is passed a `request` object, `response` object and `next()`
78 * callback. After the operation completes, call the `next()` function.
79 */
80/**
81 * @property {String} filename
82 * @nonCreation
83 * File used to load the Block instance.
84 */
85/**
86 * @property {String} name
87 * Name used to identify the block.
88 */