|
| 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 validationSchema()` |
| 31 | + |
| 32 | +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: |
| 33 | + |
| 34 | +* You must only define those properties added by the route handler to the variant definition. Those that are common to all route variant types must not be defined. So, you shouldn't use `additionalProperties:false` at the root level of the schema. Otherwise, the properties that are common to all route variants would be considered invalid. |
| 35 | +* 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. |
| 36 | + |
| 37 | +#### `constructor(route, core)` |
| 38 | + |
| 39 | +* `route`: All route and route variants properties from the `route` definition _(`method`, `url`, `variantId`, and all other properties defined in the route variant object)_. |
| 40 | +* `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. |
| 41 | + |
| 42 | +#### `middleware(req, res, next)` |
| 43 | + |
| 44 | +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). |
| 45 | + |
| 46 | +#### `get plainResponsePreview()` |
| 47 | + |
| 48 | +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. |
| 49 | + |
| 50 | +## Example |
| 51 | + |
| 52 | +Here you have an example of how a custom Routes Handler should be defined: |
| 53 | + |
| 54 | +```javascript |
| 55 | +// ./CustomRoutesHandler.js |
| 56 | + |
| 57 | +class CustomRoutesHandler { |
| 58 | + static get id() { |
| 59 | + return "error"; |
| 60 | + } |
| 61 | + |
| 62 | + constructor(routeVariant, core) { |
| 63 | + this._error = routeVariant.error; |
| 64 | + this._variantId = routeVariant.variantId; |
| 65 | + this._core = core; |
| 66 | + } |
| 67 | + |
| 68 | + middleware(req, res, next) { |
| 69 | + // Next log automatically includes the route variant id |
| 70 | + this._core.logger.info("Sending response"); |
| 71 | + res.status(this._error.code); |
| 72 | + res.send({ |
| 73 | + message: this._error.message |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + get plainResponsePreview() { |
| 78 | + return { |
| 79 | + body: { |
| 80 | + message: this._error.message |
| 81 | + }, |
| 82 | + status: this._error.code, |
| 83 | + }; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = CustomRoutesHandler; |
| 88 | +``` |
| 89 | + |
| 90 | +Then you can add your custom Routes Handler using the configuration file: |
| 91 | + |
| 92 | +```javascript |
| 93 | +// mocks.config.js |
| 94 | + |
| 95 | +const CustomRoutesHandler = require("./CustomRoutesHandler"); |
| 96 | + |
| 97 | +module.exports = { |
| 98 | + routesHandlers: [CustomRoutesHandler], |
| 99 | + server: { |
| 100 | + port: 3100, |
| 101 | + }, |
| 102 | + mocks: { |
| 103 | + selected: "base", |
| 104 | + }, |
| 105 | +}; |
| 106 | +``` |
| 107 | + |
| 108 | +:::note |
| 109 | +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) |
| 110 | +::: |
| 111 | + |
| 112 | +And now, you can use the custom handler when defining route variants: |
| 113 | + |
| 114 | +```js |
| 115 | +// mocks/routes/users.js |
| 116 | + |
| 117 | +module.exports = [ |
| 118 | + { |
| 119 | + id: "get-users", |
| 120 | + url: "/api/users", |
| 121 | + method: "GET", |
| 122 | + variants: [ |
| 123 | + { |
| 124 | + id: "error-400", |
| 125 | + handler: "error", // id of the handler to be used |
| 126 | + error: { |
| 127 | + code: 400, |
| 128 | + message: "Error message", |
| 129 | + } |
| 130 | + } |
| 131 | + { |
| 132 | + // This one will use the "default" handler |
| 133 | + id: "empty", |
| 134 | + response: { |
| 135 | + status: 200, |
| 136 | + body: [] |
| 137 | + } |
| 138 | + }, |
| 139 | + ] |
| 140 | + } |
| 141 | +] |
| 142 | +``` |
0 commit comments