mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-23 00:22:19 +01:00
- By default only accepts 'application/json' - Routes that need different content-type, can call post or put with additional arguments, one per content-type you need to support.
69 lines
1.7 KiB
JavaScript
69 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const { Router } = require('express');
|
|
const checkPermission = require('../middleware/permission-checker');
|
|
const requireContentType = require('../middleware/content_type_checker');
|
|
/**
|
|
* Base class for Controllers to standardize binding to express Router.
|
|
*/
|
|
class Controller {
|
|
constructor(config) {
|
|
const router = Router();
|
|
this.app = router;
|
|
this.config = config;
|
|
}
|
|
|
|
get(path, handler, permission) {
|
|
this.app.get(
|
|
path,
|
|
checkPermission(this.config, permission),
|
|
handler.bind(this),
|
|
);
|
|
}
|
|
|
|
post(path, handler, permission, ...acceptedContentTypes) {
|
|
this.app.post(
|
|
path,
|
|
checkPermission(this.config, permission),
|
|
requireContentType(...acceptedContentTypes),
|
|
handler.bind(this),
|
|
);
|
|
}
|
|
|
|
put(path, handler, permission, ...acceptedContentTypes) {
|
|
this.app.put(
|
|
path,
|
|
checkPermission(this.config, permission),
|
|
requireContentType(...acceptedContentTypes),
|
|
handler.bind(this),
|
|
);
|
|
}
|
|
|
|
delete(path, handler, permission) {
|
|
this.app.delete(
|
|
path,
|
|
checkPermission(this.config, permission),
|
|
handler.bind(this),
|
|
);
|
|
}
|
|
|
|
fileupload(path, filehandler, handler, permission) {
|
|
this.app.post(
|
|
path,
|
|
checkPermission(this.config, permission),
|
|
filehandler,
|
|
handler.bind(this),
|
|
);
|
|
}
|
|
|
|
use(path, router) {
|
|
this.app.use(path, router);
|
|
}
|
|
|
|
get router() {
|
|
return this.app;
|
|
}
|
|
}
|
|
|
|
module.exports = Controller;
|