Skip to content
This repository was archived by the owner on Mar 28, 2022. It is now read-only.

Commit f7bca60

Browse files
committed
feat: add resource to admin custom routes variants
1 parent 77b5898 commit f7bca60

19 files changed

Lines changed: 276 additions & 221 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1313
- Add mocks, routes and routes-variants apis
1414
### Changed
1515
- Move behaviors and fixtures apis under legacy folder
16+
- refactor: Use helper to create routers with same structure
1617
### Fixed
1718
### Removed
1819
- Remove deprecated api paths

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,46 @@
77

88
# [![Mocks Server][logo-url]][website-url] Mocks Server Plugin Admin Api
99

10-
Plugin for [Mocks Server][website-url] that provides an API REST that allows to change dinamically the current behavior, change delay time, and another [Mocks Server options][options-url].
10+
Plugin for [Mocks Server][website-url] that provides an API REST that allows to change the current behavior, change delay time, and another [Mocks Server options][options-url].
1111

12-
This is __very useful when running acceptance tests, as you can change the behavior of the api__ simply making an HTTP request in your tests `before` method, for example.
12+
This is __very useful when running acceptance tests, as you can change the current mock__ simply with a HTTP request in the `before` method of your tests, for example.
13+
14+
A __client for the administration api__ is also distributed as a separated package: [@mocks-server/admin-api-client](https://www.npmjs.com/package/@mocks-server/admin-api-client).
1315

1416
## Usage
1517

1618
This plugin is included in the [main distribution of the Mocks Server project][main-distribution-url], so you can refer to the [official documentation website][website-url].
1719

1820
## Options
1921

20-
* `adminApiPath` - Base path for the administration api. Default is "/admin". You should change it only if there is any conflict with the api you are mocking.
21-
* `adminApiDeprecatedPaths` - Boolean option, disables deprecated "/mocks" api path, which is still enabled by default.
22+
* __`adminApiPath`__ _(String)_: Path for the administration api. Default is `/admin`. You should change it only in case there is any conflict with the api you are mocking.
2223

23-
Read more about [how to define options for the mocks-server plugins here](https://www.mocks-server.org/docs/configuration-options).
24+
Read more about [how to set options in Mocks Server here](https://www.mocks-server.org/docs/configuration-options).
2425

2526
## API Resources
2627

2728
Available api resources are:
2829

29-
* `GET` `/admin/about` Returns plugin information.
30+
* `GET` `/admin/about` Returns "plugin-admin-api" information.
3031
* Response body example: `{ "version": "1.2.0" }`
31-
* `GET` `/admin/behaviors` Returns behaviors collection.
32-
* `GET` `/admin/behaviors/:id` Returns an specific behavior.
33-
* `GET` `/admin/fixtures` Returns fixtures collection.
34-
* `GET` `/admin/fixtures/:id` Returns an specific fixture.
32+
* `GET` `/admin/mocks` Returns mocks.
33+
* `GET` `/admin/mocks/:id` Returns a specific mock.
34+
* `GET` `/admin/routes` Returns routes collection.
35+
* `GET` `/admin/routes/:id` Returns a specific route.
36+
* `GET` `/admin/routes-variants` Returns routes variants collection.
37+
* `GET` `/admin/routes-variants/:id` Returns a specific route variant.
38+
* `GET` `/admin/mock-custom-routes-variants` Returns an array of currently custom routes variants ids.
39+
* `POST` `/admin/mock-custom-routes-variants` Defines a route variant to be used by current mock.
40+
* Request body example: `{ "id": "users:error" }`
41+
* `DELETE` `/admin/mock-custom-routes-variants` Restore routes variants to those defined in current mock.
3542
* `GET` `/admin/settings` Returns current server settings.
36-
* Response body example: `{ "delay": 0, behavior: "foo-behavior", path: "mocks" }`
43+
* Response body example: `{ "delay": 0, mock: "foo-mock", path: "mocks" }`
3744
* `PATCH` `/admin/settings` Changes current server settings.
3845
* Request body example: `{ "delay": 3000 }`
3946
* `GET` `/admin/alerts` Returns current alerts.
40-
* `GET` `/admin/alerts/:id` Returns an specific alert. The alert `id` is equivalent to the alert `context`.
47+
* `GET` `/admin/alerts/:id` Returns a specific alert. The alert `id` is equivalent to the alert `context` _(read the [developing plugins chapter](plugins-developing-plugins.md) for further info about alerts)_.
4148

42-
> Deprecated api resources under "/mocks" api path are still available.
49+
> v1.x deprecated api resources are also still available under the `/legacy` path.
4350
4451
## Contributing
4552

jest.e2e.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
// testMatch: ["<rootDir>/test/e2e/**/stop-plugin.spec.js"],
1010

1111
// Indicates whether the coverage information should be collected while executing the test
12-
collectCoverage: false,
12+
collectCoverage: true,
1313

1414
// The test environment that will be used for testing
1515
testEnvironment: "node",

package-lock.json

Lines changed: 71 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@mocks-server/plugin-admin-api",
33
"version": "1.5.0",
4-
"description": "Plugin for Mocks Server. Provides a REST API for administrating settings, fixtures and behaviors",
4+
"description": "Mocks Server plugin providing a REST API for administration",
55
"keywords": [
66
"mocks-server-plugin",
77
"administration",
@@ -31,15 +31,15 @@
3131
"test:unit": "npm run test"
3232
},
3333
"peerDependencies": {
34-
"@mocks-server/core": ">=1.3.0"
34+
"@mocks-server/core": ">=2.0.0"
3535
},
3636
"dependencies": {
37-
"@mocks-server/admin-api-paths": "2.0.0-beta.1",
37+
"@mocks-server/admin-api-paths": "2.0.0-beta.2",
3838
"@hapi/boom": "9.1.0",
3939
"express": "4.17.1"
4040
},
4141
"devDependencies": {
42-
"@mocks-server/core": "1.6.0",
42+
"@mocks-server/core": "2.0.0-beta.1",
4343
"eslint": "7.12.1",
4444
"eslint-plugin-no-only-tests": "2.4.0",
4545
"eslint-config-prettier": "6.15.0",

src/About.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Unless required by applicable law or agreed to in writing, software distributed
1313

1414
const express = require("express");
1515

16-
const { PLUGIN_NAME } = require("./constants");
16+
const { PLUGIN_NAME } = require("./support/constants");
1717
const { version } = require("../package.json");
1818

1919
class AboutApi {

src/Alerts.js

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,25 @@ Unless required by applicable law or agreed to in writing, software distributed
1212
"use strict";
1313

1414
const express = require("express");
15-
const Boom = require("@hapi/boom");
1615

17-
const { PLUGIN_NAME } = require("./constants");
16+
const { addCollectionMiddleware, addModelMiddleware } = require("./support/middlewares");
1817

1918
class AlertsApi {
2019
constructor(core) {
2120
this._core = core;
2221
this._tracer = core.tracer;
2322
this._router = express.Router();
24-
this._router.get("/", this.getCollection.bind(this));
25-
this._router.get("/:id", this.getModel.bind(this));
23+
addCollectionMiddleware(this._router, {
24+
name: "alerts",
25+
getItems: this._parseCollection.bind(this),
26+
tracer: core.tracer,
27+
});
28+
addModelMiddleware(this._router, {
29+
name: "alert",
30+
getItems: this._getCollection.bind(this),
31+
parseItem: this._parseModel.bind(this),
32+
tracer: core.tracer,
33+
});
2634
}
2735

2836
_parseModel(alert) {
@@ -44,22 +52,8 @@ class AlertsApi {
4452
return this._core.alerts.map(this._parseModel);
4553
}
4654

47-
getCollection(req, res) {
48-
this._tracer.verbose(`${PLUGIN_NAME}: Sending alerts | ${req.id}`);
49-
res.status(200);
50-
res.send(this._parseCollection());
51-
}
52-
53-
getModel(req, res, next) {
54-
const id = req.params.id;
55-
this._tracer.verbose(`${PLUGIN_NAME}: Sending alert ${id} | ${req.id}`);
56-
const foundAlert = this._core.alerts.find((alert) => alert.context === id);
57-
if (foundAlert) {
58-
res.status(200);
59-
res.send(this._parseModel(foundAlert));
60-
} else {
61-
next(Boom.notFound(`Alert with id "${id}" was not found`));
62-
}
55+
_getCollection() {
56+
return this._core.alerts;
6357
}
6458

6559
get router() {

src/CustomRoutesVariants.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2020 Javier Brea
3+
Copyright 2019 XbyOrange
4+
5+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
10+
*/
11+
12+
"use strict";
13+
14+
const express = require("express");
15+
const Boom = require("@hapi/boom");
16+
17+
const { addCollectionMiddleware } = require("./support/middlewares");
18+
19+
class CustomRoutesVariants {
20+
constructor(core) {
21+
this._core = core;
22+
this._tracer = core.tracer;
23+
this._router = express.Router();
24+
addCollectionMiddleware(this._router, {
25+
name: "custom routes variants",
26+
getItems: this._getCollection.bind(this),
27+
tracer: core.tracer,
28+
});
29+
30+
this._router.post("/", this.add.bind(this));
31+
this._router.delete("/", this.delete.bind(this));
32+
}
33+
34+
_getCollection() {
35+
return this._core.mocks.customRoutesVariants;
36+
}
37+
38+
add(req, res, next) {
39+
const id = req.body.id;
40+
const routeVariant = this._core.mocks.plainRoutesVariants.find(
41+
(routeVariant) => routeVariant.id === id
42+
);
43+
if (routeVariant) {
44+
this._core.mocks.useRouteVariant(id);
45+
res.status(204);
46+
res.send();
47+
} else {
48+
next(Boom.badRequest(`Route variant with id "${id}" was not found`));
49+
}
50+
}
51+
52+
delete(req, res) {
53+
this._core.mocks.restoreRoutesVariants();
54+
res.status(204);
55+
res.send();
56+
}
57+
58+
get router() {
59+
return this._router;
60+
}
61+
}
62+
63+
module.exports = CustomRoutesVariants;

0 commit comments

Comments
 (0)