Skip to content

Commit e958ac4

Browse files
committed
feat: publish release v3.2
1 parent 868df25 commit e958ac4

30 files changed

+3354
-2
lines changed

docs/guides-migrating-from-v3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ V3 example:
5252

5353
```js
5454
class Plugin {
55-
constructor({ loadMocks, core, loadRoutes, addAlert, removeAlerts }) {
55+
constructor({ logger, loadMocks, core, loadRoutes, addAlert, removeAlerts }) {
5656
// core property was almost an alias containing again all of the rest of properties received
5757
}
5858
}
5959
```
6060
V4 example:
6161
```js
6262
class Plugin {
63-
constructor({ loadMocks, loadRoutes, addAlert, removeAlerts }) {
63+
constructor({ logger, loadMocks, loadRoutes }) {
6464
// core property is no longer received
6565
}
6666
}

versioned_docs/version-3.2.0/api-mocks-server-api.md

Lines changed: 197 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
---
2+
id: api-programmatic-usage
3+
title: Programmatic usage
4+
description: Programmatic usage of Mocks Server
5+
keywords:
6+
- mocks server
7+
- programmatic
8+
- api
9+
- advanced usage
10+
- customization
11+
---
12+
13+
## Core usage
14+
15+
The server can be instantiated and started programmatically using the [@mocks-server/core](https://www.npmjs.com/package/@mocks-server/core) package, which does not include plugins.
16+
17+
You can also register your own or another existing plugins, so you could even create your custom distribution with plugins of your choice.
18+
19+
### Example
20+
21+
```javascript
22+
const Core = require("@mocks-server/core");
23+
const AdminApi = require("@mocks-server/plugin-admin-api");
24+
const InquirerCli = require("@mocks-server/plugin-inquirer-cli");
25+
26+
const core = new Core({
27+
config: {
28+
readFile: false,
29+
readEnvironment: false,
30+
readArguments: false,
31+
},
32+
plugins: {
33+
register: [AdminApi, InquirerCli]
34+
},
35+
});
36+
37+
core.init({
38+
server: {
39+
port: 3500
40+
},
41+
mocks: {
42+
delay: 1000
43+
},
44+
log: "debug"
45+
})
46+
.then(core.start);
47+
```
48+
49+
## Core API
50+
51+
#### `new Core([config])`
52+
53+
##### Arguments
54+
55+
* `config` _(Object)_: Object containing configuration properties and options as described in the [configuration chapter](configuration-options.md).
56+
57+
## Core instance API
58+
59+
Please read the [`core` API chapter](api-mocks-server-api.md) to know all available methods and getters in the `core` instance that the `Core` class returns.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
id: api-routes-and-mocks
3+
title: Routes and Mocks
4+
description: Routes and Mocks API
5+
keywords:
6+
- mocks server
7+
- api
8+
- routes
9+
- mocks
10+
- properties
11+
- options
12+
---
13+
14+
## Preface
15+
16+
The main concepts to understand when working with Mocks Server are `Routes`, `Route variants` and `Mocks`. That's why their APIs are explained in the "Get started" section.
17+
18+
## Routes
19+
20+
Read the [Routes chapter in the "Get started"](get-started-routes.md) section for a detailed explanation about how to define routes and all of its available properties.
21+
22+
## Mocks
23+
24+
Read the [Mocks chapter in the "Get started"](get-started-mocks.md) section for a detailed explanation about how to define mocks and all of its available properties.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
```
22 KB
Loading
21 KB
Loading
892 KB
Loading

0 commit comments

Comments
 (0)