Skip to content

Commit b0ae817

Browse files
authored
Merge pull request #229 from mocks-server/release
Next release v3.2
2 parents 4f42354 + 614f685 commit b0ae817

17 files changed

+326
-212
lines changed

custom-wordlist.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ cors
1515
CORS
1616
customizable
1717
dependant
18+
descendent
1819
Docusaurus
1920
ecmascript
2021
env
@@ -38,6 +39,7 @@ lifecycle
3839
localhost
3940
middleware
4041
middlewares
42+
mocksServer
4143
Namespaces
4244
namespaces
4345
Namespace
@@ -56,6 +58,7 @@ md
5658
redux
5759
reusability
5860
stringified
61+
subcollection
5962
subfolders
6063
submenu
6164
theirself
@@ -64,4 +67,4 @@ url
6467
uri
6568
uris
6669
www
67-
mocksServer
70+

docs/api-mocks-server-api.md

Lines changed: 135 additions & 83 deletions
Large diffs are not rendered by default.

docs/api-programmatic-usage.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const Core = require("@mocks-server/core");
2323
const AdminApi = require("@mocks-server/plugin-admin-api");
2424
const InquirerCli = require("@mocks-server/plugin-inquirer-cli");
2525

26-
const mocksServer = new Core({
26+
const core = new Core({
2727
config: {
2828
readFile: false,
2929
readEnvironment: false,
@@ -34,8 +34,7 @@ const mocksServer = new Core({
3434
},
3535
});
3636

37-
mocksServer
38-
.init({
37+
core.init({
3938
server: {
4039
port: 3500
4140
},
@@ -44,7 +43,7 @@ mocksServer
4443
},
4544
log: "debug"
4645
})
47-
.then(server.start);
46+
.then(core.start);
4847
```
4948

5049
## Core API
@@ -57,4 +56,4 @@ mocksServer
5756

5857
## Core instance API
5958

60-
Please read the `mocksServer` API chapter to know all available methods and getters in the `mocksServer` instance that the `Core` class returns.
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.

docs/api-routes-and-mocks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
id: api-routes-and-mocks
33
title: Routes and Mocks
4-
description: mocksServer API
4+
description: Routes and Mocks API
55
keywords:
66
- mocks server
77
- api

docs/api-routes-handler.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ This static getter must return a JSON schema defining the specific properties re
3434
* 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.
3535
* 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.
3636

37-
#### `constructor(route, mocksServer)`
37+
#### `constructor(route, core)`
3838

3939
* `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-
* `mocksServer`: The [`mocksServer` instance](api-mocks-server-api.md).
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.
4141

4242
#### `middleware(req, res, next)`
4343

@@ -59,14 +59,15 @@ class CustomRoutesHandler {
5959
return "error";
6060
}
6161

62-
constructor(routeVariant, mocksServer) {
62+
constructor(routeVariant, core) {
6363
this._error = routeVariant.error;
6464
this._variantId = routeVariant.variantId;
65-
this._mocksServer = mocksServer;
65+
this._core = core;
6666
}
6767

6868
middleware(req, res, next) {
69-
this._mocksServer.tracer.info(`Sending route variant "${this._variantId}"`);
69+
// Next log automatically includes the route variant id
70+
this._core.logger.info("Sending response");
7071
res.status(this._error.code);
7172
res.send({
7273
message: this._error.message
@@ -105,7 +106,7 @@ module.exports = {
105106
```
106107

107108
:::note
108-
You can also add Route Handlers programmatically using the [`mocksServer.addRoutesHandler` method](api-mocks-server-api.md) (useful to be used from [plugins](plugins-developing-plugins.md), for example)
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)
109110
:::
110111

111112
And now, you can use the custom handler when defining route variants:

docs/get-started-intro.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ keywords:
1717

1818
This project provides a mock server that can __simulate and store multiple API responses for each different route__. It can be added as a dependency of your project, and started simply running an NPM command.
1919

20-
Providing an interactive command line user interface and a REST API for __changing the responses of the API while it is running, it is easy to use both for development and testing__.
20+
Providing an interactive command line user interface and a REST API for __changing the responses of the mocked API while it is running, it is easy to use both for development and testing__.
2121

2222
### Main features
2323

docs/get-started-routes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ The format for defining a route is to declare an object containing:
3939
* __`headers`__ _(Object)_: Object containing headers to set in the response.
4040
* __`status`__ _(Number)_: Status code to send.
4141
* __`body`__ _(Object)_: Object to send as body in the response.
42-
* `middleware(req, res, next, mocksServer)`
42+
* `middleware(req, res, next, core)`
4343
* __`req`__ Express middleware `req` object.
4444
* __`res`__ Express middleware `res` methods.
4545
* __`next`__ Express middleware `next` method.
46-
* __`mocksServer`__ Mocks Server instance methods. Using this you could change the settings of the server itself from a request. [Read the API docs for further info](api-mocks-server-api.md) about available methods.
46+
* __`core`__ Mocks Server core instance methods. Using this you could change the settings of the server itself from a request, for example. [Read the API docs for further info](api-mocks-server-api.md) about all available methods.
4747
* _`handler:"proxy"`_ [Proxy handler](plugins-proxy.md) provided by `@mocks-server/plugin-proxy`, included in the "main" distribution. The variant can contain next extra properties:
4848
* __`host`__ _(String|Function)_: The proxy host. Equivalent to the [`express-http-proxy` `host` option](https://github.com/villadora/express-http-proxy#host), so it can also be a function.
4949
* __`options`__ _(Object)_: Object containing any of the [options supported by the `express-http-proxy` package](https://github.com/villadora/express-http-proxy#options). Some of them are:

docs/guides-migrating-from-v1.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ keywords:
1515

1616
## Preface
1717

18-
Mocks Server v2.x introduced the concept of ["route variants"](get-started-intro.md), and made more restrictive the way for organizing mocks and routes files. It also introduced other improvements, as the possibility of [using express middlewares](guides-using-middlewares.md), made simpler the [mocksServer API](api-mocks-server-api.md), etc.
18+
Mocks Server v2.x introduced the concept of ["route variants"](get-started-intro.md), and made more restrictive the way for organizing mocks and routes files. It also introduced other improvements, as the possibility of [using express middlewares](guides-using-middlewares.md), made simpler the [core API](api-mocks-server-api.md), etc.
1919

2020
All of these changes made very difficult to handle old v1 `behaviors` and `fixtures` together with the new format of `routes`, so, in order to maintain backward compatibility, Mocks Server v2 can handle two folders at a time, one containing v1.x `fixtures` and `behaviors`, and another one containing v2.x [`mocks`](get-started-mocks.md) and [`routes`](get-started-routes.md).
2121

docs/guides-migrating-from-v2.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ In order to make compatible your plugins with the v3 version, you have to:
130130

131131
* Add an `id` property to them (an static property in the case of Class plugins). This allows the core to create an appropriate namespace for the plugins and pass to them the correspondent `config` and `alerts` objects.
132132
* Change the Core API deprecated methods for creating and reading settings if you were using them by the new ones. Read the [configuration API section bellow for further info](#configuration-api).
133-
* Change the arguments that they receive in the `constructor`, `register`, `init`, `start` and `stop` methods. Now they will receive a single argument as an object containing all needed methods and properties. Previously, the `core` API was received as first argument, and from now it is received as a `{ core }` property in the first argument. So a plugin that in V2 was defined as:
133+
* Change the arguments that they receive in the `constructor`, `register`, `init`, `start` and `stop` methods. Now they will receive:
134+
* __3.1__: A single argument as an object containing all needed methods and properties Previously, the `core` API was received as first argument, and from now it is received as a `{ core }` property in the first argument.
135+
* __>=3.2__: A Mocks Server [`core` instance](api-mocks-server-api.md), but with some methods specifically scoped for the plugin. The core API docs also give details about the methods that are modified when the core is passed to a plugin.
136+
137+
So, a plugin that in v2 was defined as:
134138

135139
```js
136140
class Plugin {
@@ -160,43 +164,54 @@ class Plugin {
160164
}
161165
```
162166

163-
Now it has to be defined as:
167+
In v3.1 had to be defined as in the next example, but you shouldn't use the `core` property, because it will be removed in the next major version. __Better use the `v3.2` example, which will be fully compatible with v4.__
164168

165169
```js
166170
class Plugin {
167-
static get id() {
168-
return "myPlugin";
169-
}
171+
static id = "myPlugin";
170172

171173
constructor({ core, loadMocks, loadRoutes, alerts, config }) {
172174
// Do your stuff here
173175
}
176+
}
177+
```
178+
179+
__And in any version equal or greater than v3.2 it has to be defined as:__
174180

175-
register({ core, loadMocks, loadRoutes, alerts, config }) {
181+
```js
182+
class Plugin {
183+
static id = "myPlugin";
184+
185+
constructor({ onChangeMocks, onChangeAlerts, logger, loadMocks, loadRoutes, alerts, config }) {
186+
// Here you receive all of the core API methods in one single argument
187+
// The parameter is destructured only for example purpose
188+
}
189+
190+
register(core) {
176191
// Do your stuff here
177192
}
178193

179-
init({ core, loadMocks, loadRoutes, alerts, config }) {
194+
init(core) {
180195
// Do your stuff here
181196
}
182197

183-
start({ core, loadMocks, loadRoutes, alerts, config }) {
198+
start(core) {
184199
// Do your stuff here
185200
}
186201

187-
stop({ core, loadMocks, loadRoutes, alerts, config }) {
202+
stop(core) {
188203
// Do your stuff here
189204
}
190205
}
191206
```
192207

193208
:::note
194-
The old `addAlert` and `removeAlerts` methods can still be used in v3, but they are considered deprecated since v3.1.0 and will be removed in next major version. Any usage of these methods would produce an alert. Read the [updated documentation about creating plugins](plugins-developing-plugins.md) for further info about how to use them.
209+
The old `addAlert` and `removeAlerts` methods can still be used in v3, but they are considered deprecated since v3.1.0 and will be removed in next major version. The `core` property added in v3.1 can be still used in any v3.x version, but it will be also removed in v4. Any usage of these methods would produce an alert. Read the [updated documentation about creating plugins](plugins-developing-plugins.md) for further info about how to use them.
195210
:::
196211

197212
## Configuration API
198213

199-
As a result of the [programmatic API changes](#programmatic-api), old methods for creating or reading settings are not available any more, such as `mocksServer.addSetting`, `mocksServer.settings.get`, `mocksServer.settings.set` or `mocksServer.lowLevelConfig`. Now you can create, read, or listen to configuration changes using the `mocksServer.config` object, or using the `config` object received in plugins, which is already namespaced with each plugin name. Here you have a brief example of how you can migrate from one system to another. For further information, you should read the [updated documentation about creating plugins](plugins-developing-plugins.md).
214+
As a result of the [programmatic API changes](#programmatic-api), old methods for creating or reading settings are not available any more, such as `core.addSetting`, `core.settings.get`, `core.settings.set` or `core.lowLevelConfig`. Now you can create, read, or listen to configuration changes using the `core.config` object, or using the `config` object received in plugins, which is already namespaced with each plugin name. Here you have a brief example of how you can migrate from one system to another. For further information, you should read the [updated documentation about creating plugins](plugins-developing-plugins.md).
200215

201216
If you were creating or reading settings like this in v2:
202217

@@ -225,29 +240,27 @@ class Plugin {
225240
}
226241
```
227242

228-
Now you have to do it like this in v3:
243+
Now you have to do it like this in >=3.2:
229244

230245
```js
231246
class Plugin {
232-
static get id() {
233-
return "myPlugin";
234-
}
247+
static id = "myPlugin";
235248

236-
constructor({ core, config }) {
249+
constructor(core) {
237250
this._core = core;
238-
this._myOption = config.addOption({
251+
this._myOption = core.config.addOption({
239252
name: "myOption",
240253
type: "string",
241254
});
242255
}
243256

244257
start() {
245-
this._core.tracer.info(`Current value of my option is ${this._myOption.value}`);
258+
this._core.logger.info(`Current value of my option is ${this._myOption.value}`);
246259
this._myOption.onChange(this._onChangeMyOption.bind(this));
247260
}
248261

249262
_onChangeMyOption(newValue) {
250-
this._core.tracer.info(`My option changed to: ${newValue}`);
263+
this._core.logger.info(`My option changed to: ${newValue}`);
251264
}
252265
}
253266
```

docs/guides-migrating-from-v3.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
id: guides-migrating-from-v3
3+
title: Preparing for v4
4+
description: How to prepare migration from v3.x to v4.x
5+
keywords:
6+
- mocks server
7+
- tutorial
8+
- migration
9+
- version
10+
- update
11+
- legacy
12+
- guide
13+
- deprecated
14+
---
15+
16+
:::warning
17+
If you are already using Mocks Server v2.x you should [migrate first from v2.x to v3.x](guides-migrating-from-v2.md), and then read this chapter to prepare migration to v4.x.
18+
:::
19+
20+
## Preface
21+
22+
Even when v4 release is still not published, we are deprecating some things in v3 that will be removed in v4. While v4 is not released, every change in v3.x will be completely backward compatible, but __users upgrading to next minor versions would probably receive alerts about usage of deprecated methods, etc__.
23+
24+
So, every time you upgrade a minor version and receive a deprecation alert, you can come to this page and see how to adapt your code for the next major version, so you'll be able to prepare to it progressively and finally update to v4 without breaking changes.
25+
26+
27+
## Changes summary
28+
29+
The main breaking changes in v4.x will be:
30+
31+
* __Some core API methods will be removed__. Read [core API](#core-api) below for further info.
32+
* __Legacy alerts object will be removed__. Read [alerts](#alerts) for further info.
33+
* __Arguments received by the plugins__. Read [plugins](#plugins) below for further info.
34+
35+
## Core API
36+
37+
* __`core.tracer`__: The `tracer` object will be completely removed and using it from v3.2 produces an alert. You must use `core.logger` instead, which is already namespaced when passed to plugins and route middlewares. [Read the logger API docs](api-mocks-server-api.md#logger) for further info.
38+
39+
## Alerts
40+
41+
* The __`core.alerts`__ getter in the core when created programmatically was different to the `core.alerts` property received in the plugins from v3.2 due to backward compatibility reasons. In v4 it will return an `alerts` instance in the first case too. In the first case, it was returning a plain alerts collection. So, if you are using a programmatic root core, you should start using `core.alertsApi.customFlat` to get the same values, because that alias will be maintained in v4. Note that, in the case of plugins, you should continue using the `alerts` property, which will not change in v4.
42+
* The __`addAlert`__ and __`removeAlerts`__ methods that were being passed to plugins were deprecated in v3.1, and they will be removed in v4. Plugins were receiving an `alerts` property in the core to be used instead, which was also available in the root programmatic core using `core.alertsApi`. [Read the alerts API docs](api-mocks-server-api.md#alerts) for further info.
43+
44+
45+
## Plugins
46+
47+
### Core property
48+
49+
In version 3.1 a `core` property was added to the argument passed to the plugins. From `v3.2`, the whole core API was passed as first argument, but with some methods specifically scoped for the plugin. The `core` property was also maintained for backward compatibility but using it produced an alert. So, in v4 the `core` property will be definitively removed.
50+
51+
V3 example:
52+
53+
```js
54+
class Plugin {
55+
constructor({ loadMocks, core, loadRoutes, addAlert, removeAlerts }) {
56+
// core property was almost an alias containing again all of the rest of properties received
57+
}
58+
}
59+
```
60+
V4 example:
61+
```js
62+
class Plugin {
63+
constructor({ loadMocks, loadRoutes, addAlert, removeAlerts }) {
64+
// core property is no longer received
65+
}
66+
}
67+
```

0 commit comments

Comments
 (0)