You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: docs/api-routes-handler.md
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,10 +34,10 @@ This static getter must return a JSON schema defining the specific properties re
34
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
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
36
37
-
#### `constructor(route, mocksServer)`
37
+
#### `constructor(route, core)`
38
38
39
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
-
*`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.
// Next log automatically includes the route variant id
70
+
this._core.logger.info("Sending response");
70
71
res.status(this._error.code);
71
72
res.send({
72
73
message:this._error.message
@@ -105,7 +106,7 @@ module.exports = {
105
106
```
106
107
107
108
:::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)
109
110
:::
110
111
111
112
And now, you can use the custom handler when defining route variants:
Copy file name to clipboardExpand all lines: docs/get-started-intro.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@ keywords:
17
17
18
18
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.
19
19
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__.
Copy file name to clipboardExpand all lines: docs/get-started-routes.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -39,11 +39,11 @@ The format for defining a route is to declare an object containing:
39
39
*__`headers`___(Object)_: Object containing headers to set in the response.
40
40
*__`status`___(Number)_: Status code to send.
41
41
*__`body`___(Object)_: Object to send as body in the response.
42
-
*`middleware(req, res, next, mocksServer)`
42
+
*`middleware(req, res, next, core)`
43
43
*__`req`__ Express middleware `req` object.
44
44
*__`res`__ Express middleware `res` methods.
45
45
*__`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.
47
47
*_`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:
48
48
*__`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.
49
49
*__`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:
Copy file name to clipboardExpand all lines: docs/guides-migrating-from-v1.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ keywords:
15
15
16
16
## Preface
17
17
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.
19
19
20
20
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).
Copy file name to clipboardExpand all lines: docs/guides-migrating-from-v2.md
+32-19Lines changed: 32 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -130,7 +130,11 @@ In order to make compatible your plugins with the v3 version, you have to:
130
130
131
131
* 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.
132
132
* 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:
134
138
135
139
```js
136
140
classPlugin {
@@ -160,43 +164,54 @@ class Plugin {
160
164
}
161
165
```
162
166
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.__
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.
195
210
:::
196
211
197
212
## Configuration API
198
213
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).
200
215
201
216
If you were creating or reading settings like this in v2:
202
217
@@ -225,29 +240,27 @@ class Plugin {
225
240
}
226
241
```
227
242
228
-
Now you have to do it like this in v3:
243
+
Now you have to do it like this in >=3.2:
229
244
230
245
```js
231
246
classPlugin {
232
-
staticgetid() {
233
-
return"myPlugin";
234
-
}
247
+
static id ="myPlugin";
235
248
236
-
constructor({ core, config }) {
249
+
constructor(core) {
237
250
this._core= core;
238
-
this._myOption=config.addOption({
251
+
this._myOption=core.config.addOption({
239
252
name:"myOption",
240
253
type:"string",
241
254
});
242
255
}
243
256
244
257
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}`);
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.
0 commit comments