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

Commit da9dce8

Browse files
committed
feat: Add API resources for core v2 mocks, routes and routesVariants. Move v1 APIs to legacy folder
1 parent 8daeb53 commit da9dce8

12 files changed

Lines changed: 194 additions & 109 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
## [unreleased]
1212
### Added
13+
- Add mocks, routes and routes-variants apis
1314
### Changed
15+
- Move behaviors and fixtures apis to legacy folder
1416
### Fixed
15-
- docs(readme): Add missed API resources.
1617
### Removed
18+
- Remove deprecated api paths
1719

1820
## [1.5.0] - 2020-12-25
1921

src/About.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class AboutApi {
2020
constructor(core) {
2121
this._core = core;
2222
this._tracer = core.tracer;
23-
this._fixtures = this._core.fixtures;
2423
this._router = express.Router();
2524
this._router.get("/", this.getAbout.bind(this));
2625
}

src/Behaviors.js renamed to src/Mocks.js

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,44 +16,31 @@ const Boom = require("@hapi/boom");
1616

1717
const { PLUGIN_NAME } = require("./constants");
1818

19-
class BehaviorsApi {
19+
class MocksApi {
2020
constructor(core) {
2121
this._core = core;
2222
this._tracer = core.tracer;
23-
this._behaviors = this._core.behaviors;
23+
this._mocks = this._core.mocks;
2424
this._router = express.Router();
2525
this._router.get("/", this.getCollection.bind(this));
2626
this._router.get("/:id", this.getModel.bind(this));
2727
}
2828

29-
_parseModel(behavior) {
30-
return {
31-
id: behavior.id,
32-
name: behavior.name, // TODO, deprecate name property
33-
fixtures: behavior.fixtures.map((fixture) => fixture.id),
34-
extendedFrom: behavior.extendedFrom,
35-
};
36-
}
37-
38-
_parseCollection() {
39-
return this._behaviors.collection.map(this._parseModel);
40-
}
41-
4229
getCollection(req, res) {
43-
this._tracer.verbose(`${PLUGIN_NAME}: Sending behaviors | ${req.id}`);
30+
this._tracer.verbose(`${PLUGIN_NAME}: Sending mocks | ${req.id}`);
4431
res.status(200);
45-
res.send(this._parseCollection());
32+
res.send(this._core.mocks.plainMocks);
4633
}
4734

4835
getModel(req, res, next) {
4936
const id = req.params.id;
50-
this._tracer.verbose(`${PLUGIN_NAME}: Sending behavior ${id} | ${req.id}`);
51-
const foundBehavior = this._behaviors.collection.find((behavior) => behavior.id === id);
52-
if (foundBehavior) {
37+
this._tracer.verbose(`${PLUGIN_NAME}: Sending mock ${id} | ${req.id}`);
38+
const foundMock = this._core.mocks.plainMocks.find((mock) => mock.id === id);
39+
if (foundMock) {
5340
res.status(200);
54-
res.send(this._parseModel(foundBehavior));
41+
res.send(foundMock);
5542
} else {
56-
next(Boom.notFound(`Behavior with id "${id}" was not found`));
43+
next(Boom.notFound(`Mock with id "${id}" was not found`));
5744
}
5845
}
5946

@@ -62,4 +49,4 @@ class BehaviorsApi {
6249
}
6350
}
6451

65-
module.exports = BehaviorsApi;
52+
module.exports = MocksApi;

src/Plugin.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,28 @@ const express = require("express");
1313
const {
1414
DEFAULT_BASE_PATH,
1515
SETTINGS,
16-
BEHAVIORS,
16+
MOCKS,
1717
ABOUT,
18-
FIXTURES,
18+
ROUTES,
19+
ROUTES_VARIANTS,
1920
ALERTS,
20-
} = require("@mocks-server/admin-api-paths");
21+
LEGACY,
22+
} = require("./adminApiPaths");
2123

2224
const packageInfo = require("../package.json");
2325
const DeprecatedApi = require("./deprecated/Api");
2426

27+
const About = require("./About");
2528
const Settings = require("./Settings");
26-
const Behaviors = require("./Behaviors");
2729
const Alerts = require("./Alerts");
28-
const Fixtures = require("./Fixtures");
29-
const About = require("./About");
30+
const Mocks = require("./Mocks");
31+
const Routes = require("./Routes");
32+
const RoutesVariants = require("./RoutesVariants");
3033

3134
const {
3235
ADMIN_API_PATH_OPTION,
3336
ADMIN_API_DEPRECATED_PATHS_OPTION,
3437
PLUGIN_NAME,
35-
DEPRECATED_API_PATH,
3638
} = require("./constants");
3739

3840
class Plugin {
@@ -42,10 +44,11 @@ class Plugin {
4244
this._settings = this._core.settings;
4345
this._deprecatedApi = new DeprecatedApi(core);
4446
this._settingsApi = new Settings(this._core);
45-
this._behaviorsApi = new Behaviors(this._core);
47+
this._mocksApi = new Mocks(this._core);
4648
this._alertsApi = new Alerts(this._core);
4749
this._aboutApi = new About(this._core);
48-
this._fixturesApi = new Fixtures(this._core);
50+
this._routesApi = new Routes(this._core);
51+
this._routesVariantsApi = new RoutesVariants(this._core);
4952
core.addSetting({
5053
name: ADMIN_API_PATH_OPTION,
5154
type: "string",
@@ -88,11 +91,12 @@ class Plugin {
8891

8992
_initRouter() {
9093
this._router = express.Router();
91-
this._router.use(SETTINGS, this._settingsApi.router);
92-
this._router.use(BEHAVIORS, this._behaviorsApi.router);
9394
this._router.use(ABOUT, this._aboutApi.router);
94-
this._router.use(FIXTURES, this._fixturesApi.router);
95+
this._router.use(SETTINGS, this._settingsApi.router);
9596
this._router.use(ALERTS, this._alertsApi.router);
97+
this._router.use(MOCKS, this._mocksApi.router);
98+
this._router.use(ROUTES, this._routesApi.router);
99+
this._router.use(ROUTES_VARIANTS, this._routesVariantsApi.router);
96100
}
97101

98102
_addDeprecatedRouter() {
@@ -101,14 +105,14 @@ class Plugin {
101105
this._settings.get(ADMIN_API_DEPRECATED_PATHS_OPTION) === true &&
102106
!this._addedDeprecatedRouter
103107
) {
104-
this._core.addRouter(DEPRECATED_API_PATH, this._deprecatedApi.router);
108+
this._core.addRouter(LEGACY, this._deprecatedApi.router);
105109
this._addedDeprecatedRouter = true;
106110
}
107111
}
108112

109113
_removeDeprecatedRouter() {
110114
if (this._addedDeprecatedRouter) {
111-
this._core.removeRouter(DEPRECATED_API_PATH, this._deprecatedApi.router);
115+
this._core.removeRouter(LEGACY, this._deprecatedApi.router);
112116
this._addedDeprecatedRouter = false;
113117
}
114118
}

src/Routes.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2019 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 { PLUGIN_NAME } = require("./constants");
18+
19+
class RoutesApi {
20+
constructor(core) {
21+
this._core = core;
22+
this._tracer = core.tracer;
23+
this._router = express.Router();
24+
this._router.get("/", this.getCollection.bind(this));
25+
this._router.get("/:id", this.getModel.bind(this));
26+
}
27+
28+
getCollection(req, res) {
29+
this._tracer.verbose(`${PLUGIN_NAME}: Sending routes | ${req.id}`);
30+
res.status(200);
31+
res.send(this._core.mocks.plainRoutes);
32+
}
33+
34+
getModel(req, res, next) {
35+
const id = req.params.id;
36+
this._tracer.verbose(`${PLUGIN_NAME}: Sending route ${id} | ${req.id}`);
37+
const foundRoute = this._core.mocks.plainRoutes.find((route) => route.id === id);
38+
if (foundRoute) {
39+
res.status(200);
40+
res.send(foundRoute);
41+
} else {
42+
next(Boom.notFound(`Route with id "${id}" was not found`));
43+
}
44+
}
45+
46+
get router() {
47+
return this._router;
48+
}
49+
}
50+
51+
module.exports = RoutesApi;

src/RoutesVariants.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2019 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 { PLUGIN_NAME } = require("./constants");
18+
19+
class RoutesVariants {
20+
constructor(core) {
21+
this._core = core;
22+
this._tracer = core.tracer;
23+
this._router = express.Router();
24+
this._router.get("/", this.getCollection.bind(this));
25+
this._router.get("/:id", this.getModel.bind(this));
26+
}
27+
28+
getCollection(req, res) {
29+
this._tracer.verbose(`${PLUGIN_NAME}: Sending routes variants | ${req.id}`);
30+
res.status(200);
31+
res.send(this._core.mocks.plainRoutesVariants);
32+
}
33+
34+
getModel(req, res, next) {
35+
const id = req.params.id;
36+
this._tracer.verbose(`${PLUGIN_NAME}: Sending route variant ${id} | ${req.id}`);
37+
const foundRoute = this._core.mocks.plainRoutesVariants.find((route) => route.id === id);
38+
if (foundRoute) {
39+
res.status(200);
40+
res.send(foundRoute);
41+
} else {
42+
next(Boom.notFound(`Route variant with id "${id}" was not found`));
43+
}
44+
}
45+
46+
get router() {
47+
return this._router;
48+
}
49+
}
50+
51+
module.exports = RoutesVariants;

src/adminApiPaths.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const DEFAULT_BASE_PATH = "/admin";
2+
const ABOUT = "/about";
3+
const SETTINGS = "/settings";
4+
const ALERTS = "/alerts";
5+
6+
const MOCKS = "/mocks";
7+
const ROUTES = "/routes";
8+
const ROUTES_VARIANTS = "/routes-variants";
9+
10+
// legacy v1
11+
12+
const LEGACY = "/legacy";
13+
const BEHAVIORS = "/behaviors";
14+
const FIXTURES = "/fixtures";
15+
16+
module.exports = {
17+
DEFAULT_BASE_PATH,
18+
ABOUT,
19+
SETTINGS,
20+
ALERTS,
21+
MOCKS,
22+
ROUTES,
23+
ROUTES_VARIANTS,
24+
25+
LEGACY,
26+
BEHAVIORS,
27+
FIXTURES,
28+
};

src/constants.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,4 @@ module.exports = {
1212
ADMIN_API_PATH_OPTION: "adminApiPath",
1313
ADMIN_API_DEPRECATED_PATHS_OPTION: "adminApiDeprecatedPaths",
1414
PLUGIN_NAME: "plugin-admin-api",
15-
DEPRECATED_API_PATH: "/mocks",
1615
};

src/deprecated/Api.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ Unless required by applicable law or agreed to in writing, software distributed
1010
*/
1111

1212
const express = require("express");
13-
const { DEFAULT_BASE_PATH } = require("@mocks-server/admin-api-paths");
13+
const { DEFAULT_BASE_PATH, BEHAVIORS, FIXTURES } = require("../adminApiPaths");
1414

1515
const Behaviors = require("./Behaviors");
16-
const Settings = require("./Settings");
16+
const Fixtures = require("./Fixtures");
1717

1818
const { DEPRECATED_API_PATH, PLUGIN_NAME } = require("../constants");
1919

@@ -27,6 +27,7 @@ class Api {
2727

2828
init() {
2929
const behaviorsRouter = new Behaviors(this._core).router;
30+
const fixturesRouter = new Fixtures(this._core).router;
3031
this._router = express.Router();
3132
this._router.use((req, res, next) => {
3233
this._core.tracer.deprecationWarn(
@@ -36,9 +37,8 @@ class Api {
3637
next();
3738
});
3839

39-
this._router.use("/features", behaviorsRouter);
40-
this._router.use("/behaviors", behaviorsRouter);
41-
this._router.use("/settings", new Settings(this._core.settings, this._tracer).router);
40+
this._router.use(FIXTURES, fixturesRouter);
41+
this._router.use(BEHAVIORS, behaviorsRouter);
4242
}
4343

4444
get router() {

0 commit comments

Comments
 (0)