Skip to content

Commit 465458d

Browse files
committed
chore: release v3.6.0
1 parent 2c009be commit 465458d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+5710
-4
lines changed

docusaurus.config.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
function docsUrl(page) {
2-
// TODO, remove "next"
3-
return `docs/next/${page}`;
2+
return `docs/${page}`;
43
}
54

65
module.exports = {

src/pages/index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ import { config } from "@fortawesome/fontawesome-svg-core";
2424
config.autoAddCss = false;
2525

2626
function docsUrl(page) {
27-
// TODO, remove "next"
28-
return `docs/next/${page}`;
27+
return `docs/${page}`;
2928
}
3029

3130
const textContents = {
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
id: javascript
3+
title: JavaScript API
4+
description: Mocks Server JavaScript API
5+
keywords:
6+
- mocks server
7+
- programmatic
8+
- api
9+
- core
10+
- methods
11+
- properties
12+
- getters
13+
- advanced usage
14+
- JavaScript
15+
- js
16+
- node
17+
- nodejs
18+
---
19+
20+
```mdx-code-block
21+
import DocCardList from '@theme/DocCardList';
22+
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
23+
```
24+
25+
## Preface
26+
27+
Mocks Server provides its `core` instance to plugins, middlewares and other system elements. It contains methods allowing to configure it, start or stop it, listen to its events, etc. Using it also enables to tap into, modify, or extend its internal behavior.
28+
29+
:::caution
30+
Use only the API methods described in this docs. Use other methods under your own risk, and take into account that they may change in minor versions without considering it as a breaking change.
31+
:::
32+
33+
## Constructor
34+
35+
__`new Core([config], [advancedOptions])`__: Creates a new Mocks Server core instance. Read the [Javascript integrations](../integrations/javascript.md) for further info.
36+
* `config` _(Object)_: Any of [Mocks Server core options](../configuration/options.md#core-options) or [plugins options](../configuration/options.md#plugin-options). Command line arguments, environment variables and configuration file options would override the values defined here.
37+
* `advancedOptions` _(Object)_:
38+
* `pkg` _(Object)_: Useful when creating custom distributions. It allows to define a different package name and version to check for updates. For example, the `@mocks-server/main` distribution provides its name and version to the core using this parameter, so when the core checks for updates, it uses these properties instead of its own.
39+
* `name` _(String)_: Package name
40+
* `version` _(String)_: Current package version
41+
42+
```js
43+
const Core = require("@mocks-server/core");
44+
45+
// highlight-next-line
46+
const core = new Core({
47+
server: {
48+
port: 3500
49+
},
50+
});
51+
52+
// highlight-next-line
53+
core.init().then(async () => {
54+
// highlight-start
55+
await core.start();
56+
await core.stop();
57+
// highlight-end
58+
});
59+
```
60+
61+
:::warning
62+
The `Core` constructor is exported only by the `@mocks-server/core` package, and it doesn't include any pre-installed plugins, so you should [install them by yourself](../plugins/installation.md) if you choose this method for starting Mocks Server programmatically. Read the [next section](#creating-an-instance-with-pre-installed-plugins) for an alternative method of creating a core instance using the `@mocks-server/main` package, which includes pre-installed plugins and other optimal configuration to start it programmatically.
63+
:::
64+
65+
### Creating an instance with pre-installed plugins
66+
67+
The `@mocks-server/main` distribution includes some plugins providing useful integrations, such as the [Admin Api Plugin](../integrations/rest-api.md), etc. If you create a new server instance using the `@mocks-server/core` package as in the example above, you would have to [install your desired plugins](../plugins/installation.md) by your own (which may be useful to [create your own distribution](../integrations/javascript.md#creating-your-own-distribution), for example).
68+
69+
Note also that the [default configuration](../configuration/options.md) of `@mocks-server/core` is intended to be used in CLI, so it tries to load files, read arguments and environment variables, etc. which might not be ideal to start it programmatically in Jest tests, for example.
70+
71+
But it is also possible to create an instance with all `@mocks-server/main` distribution plugins and configuration optimized for a programmatic usage using the next function exported by the library:
72+
73+
__`createServer([config])`__: Returns a new core instance. The `config` provided is merged with the default package config, which includes some pre-installed plugins and optimized configuration for a programmatic usage:
74+
* It disables the files load. The scaffold is not created and routes and collections must have to be loaded using the JavaScript API.
75+
* It disables loading configuration from file, environment variables or arguments. So, possible conflicts are avoided and all configuration has to be provided programmatically.
76+
* It disables the interactive CLI plugin.
77+
78+
```js
79+
const { createServer } = require("@mocks-server/main");
80+
const { routes, collections } = require("./fixtures");
81+
82+
// highlight-next-line
83+
const core = createServer({
84+
server: {
85+
port: 3500
86+
},
87+
});
88+
89+
// highlight-next-line
90+
core.init().then(async () => {
91+
// highlight-start
92+
const { loadRoutes, loadCollections } = core.mock.createLoaders();
93+
loadRoutes(routes);
94+
loadCollections(collections);
95+
96+
await core.start();
97+
// highlight-end
98+
});
99+
```
100+
101+
### Other ways of accessing to the core instance
102+
103+
Apart from creating your own `core` instance programmatically, you can also use it from other system elements, because it is passed as an argument to them. Some elements to which the core instance is passed are:
104+
105+
* __Plugins__: A plugin receives the core instance on its constructor and in all of its standardized methods. Read [plugins development](../plugins/development.md) for further info.
106+
* __Variant handlers__: A variant handler receives the core instance on its constructor. Read [variant handlers development](../variant-handlers/development.md) for further info.
107+
* __middleware variants__: The core is passed from the `middleware` variant handler to the middleware functions defined in that type of variants. So, it can be used directly in Express middlewares. Read the [middleware variant chapter](../usage/variants/middleware.md) for further info.
108+
109+
## API
110+
111+
### init()
112+
113+
__`core.init([config])`__: Register plugins, initialize options and prepare all other internal dependencies needed to start the server. Returns a promise. Accepts next arguments:
114+
* `config` _(Object)_: Any of [Mocks Server options](../configuration/options.md#core-options) or [plugin options](../configuration/options.md#plugin-options). If provided, the config passed in the [constructor](#constructor) would be merged with this one. Command line arguments, environment variables and configuration file options would override the values defined here. Options are internally available using the [`core.config` API](./javascript/config.md) once they are initialized.
115+
116+
### start()
117+
118+
__`core.start()`__: Start the server and plugins. Returns a promise. It calls to the `init` method internally if it was not done before.
119+
120+
### stop()
121+
122+
__`core.stop()`__: Stop the server and plugins. Returns a promise.
123+
124+
## Children objects APIs
125+
126+
```mdx-code-block
127+
<DocCardList items={useCurrentSidebarCategory().items}/>
128+
```
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
id: alerts
3+
title: core.alerts
4+
description: Methods of the core.alerts JavaScript API
5+
keywords:
6+
- mocks server
7+
- programmatic
8+
- api
9+
- core
10+
- methods
11+
- properties
12+
- getters
13+
- advanced usage
14+
- JavaScript
15+
- js
16+
- node
17+
- nodejs
18+
- alerts
19+
---
20+
21+
```mdx-code-block
22+
import ExampleDetails from '@site/src/components/ExampleDetails';
23+
```
24+
25+
## Preface
26+
27+
The `core.alerts` object provides access to methods related to Mocks Server alerts. Use alerts to inform the user about deprecated methods or other warning messages, or about current errors. For example, when an error happens loading files, the server adds automatically an alert in order to let the user know about the error.
28+
29+
:::info
30+
Here are described only some methods of the `alerts` API, for further info please read the [`@mocks-server/nested-collections` docs](https://github.com/mocks-server/main/tree/master/packages/nested-collections/README.md), but take into account that in Mocks Server, the alerts `set` method is extended and supports passing a third `error` argument.
31+
:::
32+
33+
:::caution
34+
Use only the API methods described in this docs. Use other methods under your own risk, and take into account that they may change in minor versions without considering it as a breaking change.
35+
:::
36+
37+
:::warning
38+
When the `core` is received in the plugin, you must use `core.alerts`, but when you are creating your own core instance programmatically, then you must use `core.alertsApi` instead. This was made in v3.2 due to backward compatibility reasons, and it will be fixed in next major version.
39+
:::
40+
41+
## API
42+
43+
:::caution
44+
When the core is passed to a plugin as a parameter, the `alerts` object is an `alerts` subcollection instead of the root `alerts` instance. The `alerts` subcollection is namespaced with the plugin id, so it is easy to trace where the alerts come from.
45+
:::
46+
47+
### onChange()
48+
49+
__`core.alerts.onChange(callback)`__: Add a callback to be executed when alerts change. Returns a function for removing the added callback.
50+
* `callback()` (Function): Function to be executed whenever alerts change.
51+
52+
```mdx-code-block
53+
<ExampleDetails>
54+
```
55+
56+
```js
57+
// Add event listener and store the method allowing to remove it
58+
// highlight-next-line
59+
const removeListener = core.alerts.onChange(() => {
60+
console.log("Alerts have changed!");
61+
});
62+
63+
// Remove event listener
64+
// highlight-next-line
65+
removeListener();
66+
```
67+
68+
```mdx-code-block
69+
</ExampleDetails>
70+
```
71+
72+
### set()
73+
74+
__`core.alerts.set(id, message, error)`__: Adds an alert or modify it.
75+
* __`id`__ _(String)_: The id for the alert to be added or modified in case it already exists.
76+
* __`message`__ _(String)_: Message for the alert.
77+
* __`error`__ _(Error)_: Optional. Error causing the alert.
78+
79+
### remove()
80+
81+
__`core.alerts.remove(id)`__: Removes an alert.
82+
* __`id`__ _(String)_: Id of the alert to be removed.
83+
84+
### clean()
85+
86+
__`core.alerts.clean`__: Removes all alerts, including descendant collections.
87+
88+
### collection()
89+
90+
__`core.alerts.collection(id)`__: Allows to create a new subcollection of alerts or returns an already existent one. The returned collection will have all of the same methods described for `alerts`. It is useful to group alerts by its type. The `context` property of the alerts created in a child collection will include all parent collections ids joined with `:`, so the user can know the alert's "path".
91+
92+
### flat
93+
94+
__`core.alerts.flat`__: Returns all collection items and descendent collection items in a flat array. It adds a `collection` id to each item. For nested collections, the `id` is built with all parents ids and self id joined with `:`.
95+
96+
### root
97+
98+
__`core.alerts.root`__: Getter returning the root `alerts` object. It is useful when trying to access to the root Mocks Server alerts from a plugin, but use it with caution because you will be accessing to all of the elements alerts, not only to those owned by the plugin.

0 commit comments

Comments
 (0)