1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-04 00:18:01 +01:00
unleash.unleash/src/lib/routes/admin-api/event.ts
Ivar Conradi Østhus 2bcdb5ec31
fix: Controller wraps handler with try/catch (#909)
By having the controller perform try/catch around the
handler function allows us to add extra safety to all
our controllers and safeguards that we will always catch
exceptions thrown by a controller method.
2021-08-13 10:36:19 +02:00

47 lines
1.5 KiB
TypeScript

import { IUnleashConfig } from '../../types/option';
import { IUnleashServices } from '../../types/services';
import EventService from '../../services/event-service';
import { ADMIN } from '../../types/permissions';
const Controller = require('../controller');
const eventDiffer = require('../../event-differ');
const version = 1;
export default class EventController extends Controller {
private eventService: EventService;
constructor(
config: IUnleashConfig,
{ eventService }: Pick<IUnleashServices, 'eventService'>,
) {
super(config);
this.eventService = eventService;
this.get('/', this.getEvents, ADMIN);
this.get('/:name', this.getEventsForToggle);
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async getEvents(req, res): Promise<void> {
const events = await this.eventService.getEvents();
eventDiffer.addDiffs(events);
res.json({ version, events });
}
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
async getEventsForToggle(req, res): Promise<void> {
const toggleName = req.params.name;
const events = await this.eventService.getEventsForToggle(toggleName);
if (events) {
eventDiffer.addDiffs(events);
res.json({ toggleName, events });
} else {
res.status(404).json({ error: 'Could not find events' });
}
}
}
module.exports = EventController;