Skip to content

Commit 7650df5

Browse files
authored
Merge pull request #246 from mocks-server/release
Modify 3.6 API
2 parents fc4e0d6 + 3218ed4 commit 7650df5

File tree

20 files changed

+256
-235
lines changed

20 files changed

+256
-235
lines changed

docs/api/core/mock.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
id: mock
3+
title: core.mock
4+
description: Methods of the core.mock 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+
- mock
19+
---
20+
21+
```mdx-code-block
22+
import ExampleDetails from '@site/src/components/ExampleDetails';
23+
```
24+
25+
## Preface
26+
27+
The `core.mock` object provides methods related to the API mock served, so it contains methods related to [`routes`](usage/routes.md), [`variants`](usage/variants.md) and [`collections`](usage/collections.md).
28+
29+
## API
30+
31+
### onChange()
32+
33+
__`core.mock.onChange(callback)`__: Add a callback to be executed when anything related to the API mock changes. So, it is triggered whenever collections or routes change, and also whenever mock settings change, such as `mock.collections.selected` or `mock.delay`. It returns a function for removing the added callback.
34+
* `callback()` (Function): Function to be executed on change collections.
35+
36+
```mdx-code-block
37+
<ExampleDetails>
38+
```
39+
40+
```js
41+
// Add event listener and store the method allowing to remove it
42+
// highlight-next-line
43+
const removeListener = core.mock.onChange(() => {
44+
console.log("API mock has changed!");
45+
});
46+
47+
// Remove event listener
48+
// highlight-next-line
49+
removeListener();
50+
```
51+
52+
```mdx-code-block
53+
</ExampleDetails>
54+
```
55+
56+
### createLoaders()
57+
58+
__`core.mock.createLoaders()`__: Return methods allowing to load routes and collections. Each method reloads only the collections or routes added by it, so, it allows to have many different sources for loading routes or collections without having conflicts between them.
59+
60+
It returns an object containing next methods:
61+
62+
* `loadRoutes(routes)`: `<Array of routes>` Load [`routes`](usage/routes.md) definitions with the same format that they are defined when using files. Each time this method is called, __all previously loaded routes will be replaced by the new ones, but only those added using this method__. Routes loaded by the files loader or other plugins will remain.
63+
* `loadCollections(collections)`: `<Array of collections>` Load [`collections`](usage/collections.md) definitions with the same format that they are defined when using files. Each time this method is called, __all previously loaded collections will be replaced by the new ones, but only those added using this method__. Collections loaded by the files loader or other plugins will remain.
64+
65+
```mdx-code-block
66+
<ExampleDetails>
67+
```
68+
69+
```js
70+
const { routes, collections } = require("./fixtures");
71+
72+
// highlight-next-line
73+
const { loadRoutes, loadCollections } = core.mock.createLoaders();
74+
75+
loadRoutes(routes);
76+
loadCollections(collections);
77+
```
78+
79+
:::caution
80+
Note that, if you want your routes and collections to replace other defined previously, you should not call to `createLoaders` each different time you are going to use the methods. Instead of that, you should keep a reference to the `loadRoutes` and `loadCollections` methods and use always those references.
81+
:::
82+
83+
```mdx-code-block
84+
</ExampleDetails>
85+
```
86+
87+
### useVariant()
88+
89+
__`core.mock.useVariant(variantId)`__: Define a [route variant](usage/variants.md) to be used instead of the one defined in the current collection. The change is stored in memory only, so the original collection route variants are restored whenever the selected collection changes or routes are reloaded.
90+
* `variantId` _(String)_: Route variant id, with the format `"[routeId]:[variantId]"`.
91+
92+
### restoreVariants()
93+
94+
__`core.mock.restoreVariants()`__: Restore current collection route variants. It removes all variants defined with the `useVariant` method.
95+
96+
## Routes API
97+
98+
:::info
99+
The `core.mock.routes` object provides methods related to [`routes`](usage/routes.md) and [`variants`](usage/variants.md).
100+
:::
101+
102+
103+
### plain
104+
105+
__`core.mock.routes.plain`__: Returns an array with all defined routes in plain format.
106+
107+
### plainVariants
108+
109+
__`core.mock.routes.plainVariants`__: Returns an array with all defined variants in plain format.
110+
111+
## Collections API
112+
113+
:::info
114+
The `core.mock.collections` object provides access to methods related to [`collections`](usage/collections.md).
115+
:::
116+
117+
### select()
118+
119+
__`core.mock.collections.select(collectionId)`__: Changes the current collection. The API mock will use the routes and variants defined in the selected collection.
120+
* `collectionId` _(String)_: Collection id.
121+
122+
### selected
123+
124+
__`core.mock.collections.selected`__: Getter returning a `collection` instance correspondent to the currently selected collection.
125+
126+
### ids
127+
128+
__`core.mock.collections.ids`__: Getter returning an array with all collection ids.
129+
130+
### plain
131+
132+
__`core.mock.collections.plain`__: Returns an array with current collections in plain format.

docs/api/core/routes.md

Lines changed: 0 additions & 170 deletions
This file was deleted.

docs/api/core/variant-handlers.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
id: variant-handlers
3+
title: core.variantHandlers
4+
description: Methods of the core.variantHandlers 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+
- variant
19+
- handler
20+
- variantHandlers
21+
---
22+
23+
```mdx-code-block
24+
import ExampleDetails from '@site/src/components/ExampleDetails';
25+
```
26+
27+
## Preface
28+
29+
The `core.variantHandlers` object provides methods related to [Variant Handlers](variant-handlers/intro.md)
30+
31+
## API
32+
33+
### register()
34+
35+
__`core.variantHandlers.register(VariantHandler)`__: Register a [Variant Handler](variant-handlers/intro.md).
36+
* `VariantHandler`: `<Class>` Custom Variant handler. Read the [creating Variant Handlers chapter](variant-handlers/development.md) for further info.
37+
38+
```mdx-code-block
39+
<ExampleDetails>
40+
```
41+
42+
```js
43+
import MyVariantHandler from "./MyVariantHandler";
44+
45+
// highlight-next-line
46+
core.variantHandlers.register(MyVariantHandler);
47+
```
48+
49+
```mdx-code-block
50+
</ExampleDetails>
51+
```

0 commit comments

Comments
 (0)