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

Commit 6634469

Browse files
committed
feat(#78): Add alerts router
1 parent f5beee1 commit 6634469

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
## [unreleased]
1212
### Added
13+
- feat(#78): Add alerts router
1314
### Changed
1415
- chore(deps): Update dependencies
1516
### Fixed

src/Alerts.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
Copyright 2020 Javier Brea
3+
4+
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
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
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.
9+
*/
10+
11+
"use strict";
12+
13+
const express = require("express");
14+
const Boom = require("@hapi/boom");
15+
16+
const { PLUGIN_NAME } = require("./constants");
17+
18+
class AlertsApi {
19+
constructor(core) {
20+
this._core = core;
21+
this._tracer = core.tracer;
22+
this._alerts = this._core.alerts;
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+
_parseModel(alert) {
29+
return {
30+
id: alert.context,
31+
context: alert.context,
32+
message: alert.message,
33+
error: alert.error
34+
? {
35+
name: alert.error.name,
36+
message: alert.error.message,
37+
stack: alert.error.stack,
38+
fileName: alert.error.fileName,
39+
lineNumber: alert.error.lineNumber,
40+
columnNumber: alert.error.columnNumber,
41+
}
42+
: null,
43+
};
44+
}
45+
46+
_parseCollection() {
47+
return this._alerts.map(this._parseModel);
48+
}
49+
50+
getCollection(req, res) {
51+
this._tracer.verbose(`${PLUGIN_NAME}: Sending alerts | ${req.id}`);
52+
res.status(200);
53+
res.send(this._parseCollection());
54+
}
55+
56+
getModel(req, res, next) {
57+
const id = req.params.id;
58+
this._tracer.verbose(`${PLUGIN_NAME}: Sending alert ${id} | ${req.id}`);
59+
const foundAlert = this._alerts.find((alert) => alert.context === id);
60+
if (foundAlert) {
61+
res.status(200);
62+
res.send(this._parseModel(foundAlert));
63+
} else {
64+
next(Boom.notFound(`Alert with id "${id}" was not found`));
65+
}
66+
}
67+
68+
get router() {
69+
return this._router;
70+
}
71+
}
72+
73+
module.exports = AlertsApi;

src/Plugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ const {
1616
BEHAVIORS,
1717
ABOUT,
1818
FIXTURES,
19+
ALERTS,
1920
} = require("@mocks-server/admin-api-paths");
2021

2122
const packageInfo = require("../package.json");
2223
const DeprecatedApi = require("./deprecated/Api");
2324

2425
const Settings = require("./Settings");
2526
const Behaviors = require("./Behaviors");
27+
const Alerts = require("./Alerts");
2628
const Fixtures = require("./Fixtures");
2729
const About = require("./About");
2830

@@ -41,6 +43,7 @@ class Plugin {
4143
this._deprecatedApi = new DeprecatedApi(core);
4244
this._settingsApi = new Settings(this._core);
4345
this._behaviorsApi = new Behaviors(this._core);
46+
this._alertsApi = new Alerts(this._core);
4447
this._aboutApi = new About(this._core);
4548
this._fixturesApi = new Fixtures(this._core);
4649
core.addSetting({
@@ -78,6 +81,7 @@ class Plugin {
7881
this._router.use(BEHAVIORS, this._behaviorsApi.router);
7982
this._router.use(ABOUT, this._aboutApi.router);
8083
this._router.use(FIXTURES, this._fixturesApi.router);
84+
this._router.use(ALERTS, this._alertsApi.router);
8185
}
8286

8387
_addDeprecatedRouter() {

0 commit comments

Comments
 (0)