|
| 1 | +--- |
| 2 | +id: api-routes-handler |
| 3 | +title: Routes Handler |
| 4 | +description: How to add custom routes handlers |
| 5 | +keywords: |
| 6 | + - mocks server |
| 7 | + - customization |
| 8 | + - routes |
| 9 | + - routes variants |
| 10 | + - handler |
| 11 | + - format |
| 12 | +--- |
| 13 | + |
| 14 | +## What is a Routes Handler? |
| 15 | + |
| 16 | +A "Routes handler" is the element at charge of handling the [routes variants declarations](get-started-routes.md), and sending the appropriate response. |
| 17 | + |
| 18 | +Mocks Server includes only one Routes Handler by default, which accepts routes variants declared in the [format described in the `routes` chapter](get-started-routes.md), but __you can add your own route variants formats__. |
| 19 | + |
| 20 | +This feature, combined with the [plugins development](plugins-developing-plugins.md), gives you the possibility of adding to Mocks Server almost every new feature you want. |
| 21 | + |
| 22 | +## Creating custom Routes Handlers |
| 23 | + |
| 24 | +A Routes Handler should be defined as a `Class` containing: |
| 25 | + |
| 26 | +#### `static get id()` |
| 27 | + |
| 28 | +This static getter will be used to recognize the Routes Handler. When defining a route variant, the handler to be used can be defined using the `handler` property. That property in route variants should be the `id` of the Route Handler to be used. |
| 29 | + |
| 30 | +#### `static get version()` |
| 31 | + |
| 32 | +This property was added in v3.5 in order to allow to adapt to the next major version without breaking changes. It must return "4" in order to be able to use the API described in this page. Please read other version docs to check old handlers APIs. |
| 33 | + |
| 34 | +#### `static get deprecated()` |
| 35 | + |
| 36 | +This property was added in v3.5 in order to allow to adapt to the next major version without breaking changes. It should be added to Handlers using the old API in order to inform users that they shouldn't use it because it will be deprecated in v4. An alert will be added if this getter returns `true`. |
| 37 | + |
| 38 | +#### `static get validationSchema()` |
| 39 | + |
| 40 | +This static getter must return a JSON schema defining the specific properties required by the handler. It will be used by the core to validate the route variants of this type. `ajv` is used under the hood to perform validations. Take into account next points when defining the json schema: |
| 41 | + |
| 42 | +* You must only define those properties defined in the `response` property of the variant definition. |
| 43 | +* Mocks Server supports a special JSON schema keyword named `instanceof`. You can use it to indicate that a property must be an instance of a `Function`, or a `RegExp` for example. |
| 44 | + |
| 45 | +#### `constructor(response, core)` |
| 46 | + |
| 47 | +* `response`: All properties in the `response` property of the route variant definition. |
| 48 | +* `core`: The [`core` instance](api-mocks-server-api.md), but with some methods specifically scoped for each route variant, such as `core.logger` and `core.alerts`, which are namespaced using each route variant id. So, logs or alerts from each different route variant can be easily identified. |
| 49 | + |
| 50 | +#### `middleware(req, res, next)` |
| 51 | + |
| 52 | +This is the middleware that will be called when the route url matches and the specific variant type corresponds to this route handler (it is equal to the route handler id). |
| 53 | + |
| 54 | +#### `get preview()` |
| 55 | + |
| 56 | +This getter should return a plain object containing an approached preview of the response that will be sent when the route variant is used. This is useful to provide information to other plugins or Mocks Server interfaces. If you have not enough information to predict the response (as in the case of `middlewares` handler, for example), you should return `null`. |
| 57 | + |
| 58 | +## Example |
| 59 | + |
| 60 | +Here you have an example of how a custom Routes Handler should be defined: |
| 61 | + |
| 62 | +```js |
| 63 | +// ./CustomRoutesHandler.js |
| 64 | + |
| 65 | +class CustomRoutesHandler { |
| 66 | + static get id() { |
| 67 | + return "error"; |
| 68 | + } |
| 69 | + |
| 70 | + static get version() { |
| 71 | + return "4"; |
| 72 | + } |
| 73 | + |
| 74 | + static get validationSchema() { |
| 75 | + return { |
| 76 | + type: "object", |
| 77 | + properties: { |
| 78 | + code: { |
| 79 | + type: "number", |
| 80 | + }, |
| 81 | + message: { |
| 82 | + type: "string", |
| 83 | + }, |
| 84 | + }, |
| 85 | + required: ["code", "message"], |
| 86 | + additionalProperties: false, |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + constructor(response, core) { |
| 91 | + this._code = response.code; |
| 92 | + this._message = response.message; |
| 93 | + this._core = core; |
| 94 | + } |
| 95 | + |
| 96 | + middleware(req, res, next) { |
| 97 | + // Next log automatically includes the route variant id |
| 98 | + this._core.logger.info("Sending response"); |
| 99 | + res.status(this._code); |
| 100 | + res.send({ |
| 101 | + message: this._message |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + get preview() { |
| 106 | + return { |
| 107 | + body: { |
| 108 | + message: this._message |
| 109 | + }, |
| 110 | + status: this._code, |
| 111 | + }; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +module.exports = CustomRoutesHandler; |
| 116 | +``` |
| 117 | + |
| 118 | +Then you can add your custom Routes Handler using the configuration file: |
| 119 | + |
| 120 | +```javascript |
| 121 | +// mocks.config.js |
| 122 | + |
| 123 | +const CustomRoutesHandler = require("./CustomRoutesHandler"); |
| 124 | + |
| 125 | +module.exports = { |
| 126 | + routesHandlers: [CustomRoutesHandler], |
| 127 | + server: { |
| 128 | + port: 3100, |
| 129 | + }, |
| 130 | + mocks: { |
| 131 | + selected: "base", |
| 132 | + }, |
| 133 | +}; |
| 134 | +``` |
| 135 | + |
| 136 | +:::note |
| 137 | +You can also add Route Handlers programmatically using the [`core.addRoutesHandler` method](api-mocks-server-api.md) (useful to be used from [plugins](plugins-developing-plugins.md), for example) |
| 138 | +::: |
| 139 | + |
| 140 | +And now, you can use the custom handler when defining route variants: |
| 141 | + |
| 142 | +```js |
| 143 | +// mocks/routes/users.js |
| 144 | + |
| 145 | +module.exports = [ |
| 146 | + { |
| 147 | + id: "get-users", |
| 148 | + url: "/api/users", |
| 149 | + method: "GET", |
| 150 | + variants: [ |
| 151 | + { |
| 152 | + id: "error-400", |
| 153 | + handler: "error", // id of the handler to be used |
| 154 | + response: { // object that the handler will receive |
| 155 | + code: 400, |
| 156 | + message: "Error message", |
| 157 | + }, |
| 158 | + } |
| 159 | + { |
| 160 | + // This one will use the "json" handler |
| 161 | + id: "empty", |
| 162 | + handler: "json", |
| 163 | + response: { |
| 164 | + status: 200, |
| 165 | + body: [] |
| 166 | + } |
| 167 | + }, |
| 168 | + ] |
| 169 | + } |
| 170 | +] |
| 171 | +``` |
0 commit comments