Skip to content

Commit 1935948

Browse files
committed
chore(release): Release v3.5.0
1 parent bb16188 commit 1935948

29 files changed

+3695
-0
lines changed

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

Lines changed: 197 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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][,advancedOptions])`
52+
53+
##### Arguments
54+
55+
* `config` _(Object)_: Object containing configuration properties and options as described in the [configuration chapter](configuration-options.md).
56+
* `advancedOptions` _(Object)_: Object containing advanced options for creating a core instance. It may contain any of next properties:
57+
* `pkg` _(Object)_: It allows to change the package name and version that is checked by the update notifier. It is useful when publishing custom Mocks Server distributions, so you can check your package version instead of the default one, which is the current version of `@mocks-server/core`. It must be an object containing next properties:
58+
* `name` _(String)_ : Name of the package.
59+
* `version` _(String)_: Installed version of the package.
60+
61+
## Core instance API
62+
63+
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: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
```
22 KB
Loading
21 KB
Loading
892 KB
Loading

0 commit comments

Comments
 (0)