Skip to content

Commit

Permalink
feat: additional middlewares per resource/method (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
rombat committed Dec 18, 2023
1 parent 2da829d commit 1c6103f
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion services/docs/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ module.exports = class DocsService extends CampsiService {
return next();
};

const additionalMiddlewares = (req, res, next) => {
if (typeof req.resource?.additionalMiddlewares?.[req.method] !== 'function') {
return next();
}
return req.resource?.additionalMiddlewares[req.method](req, res, next);
};

this.router.use('/', (req, res, next) => {
req.options = service.options;
req.service = service;
Expand All @@ -41,7 +48,8 @@ module.exports = class DocsService extends CampsiService {
// #swagger.tags = ['DOCSERVICE']
// #swagger.ignore = always
'/[:]soft-delete',
handlers.softDelete);
handlers.softDelete
);

this.router.param(
// #swagger.tags = ['DOCSERVICE']
Expand All @@ -59,48 +67,56 @@ module.exports = class DocsService extends CampsiService {
// #swagger.tags = ['DOCSERVICE']
// #swagger.ignore = true
'/:resource',
additionalMiddlewares,
handlers.getDocuments
);
this.router.post(
'/:resource/:id/locks',
// #swagger.tags = ['DOCSERVICE']
// #swagger.ignore = true
additionalMiddlewares,
handlers.lockDocument
);
this.router.get(
'/:resource/:id/locks',
// #swagger.ignore = true
// #swagger.tags = ['DOCSERVICE']
additionalMiddlewares,
handlers.getLocks
);
this.router.get(
// #swagger.ignore = true
// #swagger.tags = ['DOCSERVICE'],
'/:resource/:id/users',
additionalMiddlewares,
handlers.getDocUsers
);
this.router.post(
// #swagger.tags = ['DOCSERVICE'],
// #swagger.ignore = true
'/:resource/:id/users',
additionalMiddlewares,
handlers.postDocUser
);
this.router.delete(
/* #swagger.tags = ['DOCSERVICE'],
#swagger.ignore = true
*/
'/:resource/:id/users/:user',
additionalMiddlewares,
handlers.delDocUser
);
this.router.post(
'/:resource/:id/:state/locks',
// #swagger.ignore = true
additionalMiddlewares,
handlers.lockDocument
);
this.router.get(
// #swagger.tags = ['DOCSERVICE']
// #swagger.ignore = true
'/:resource/:id/:state',
additionalMiddlewares,
handlers.getDoc
);
this.router.get(
Expand Down Expand Up @@ -140,12 +156,14 @@ module.exports = class DocsService extends CampsiService {
*/

'/:resource/:id',
additionalMiddlewares,
handlers.getDoc
);
this.router.postAsync(
// #swagger.tags = ['DOCSERVICE'],
// #swagger.ignore = true
'/:resource/:state',
additionalMiddlewares,
validateWriteAccess,
handlers.postDoc
);
Expand Down Expand Up @@ -179,20 +197,23 @@ module.exports = class DocsService extends CampsiService {
}
*/
'/:resource',
additionalMiddlewares,
validateWriteAccess,
handlers.postDoc
);
this.router.put(
// #swagger.tags = ['DOCSERVICE'],
// #swagger.ignore = true
'/:resource/:id/state',
additionalMiddlewares,
validateWriteAccess,
handlers.putDocState
);
this.router.putAsync(
// #swagger.tags = ['DOCSERVICE'],
// #swagger.ignore = true
'/:resource/:id/:state',
additionalMiddlewares,
validateWriteAccess,
handlers.putDoc
);
Expand Down Expand Up @@ -230,13 +251,15 @@ module.exports = class DocsService extends CampsiService {
}
*/
'/:resource/:id',
additionalMiddlewares,
validateWriteAccess,
handlers.putDoc
);
this.router.patch(
// #swagger.tags = ['DOCSERVICE'],
// #swagger.ignore = true
'/:resource/:id',
additionalMiddlewares,
validateWriteAccess,
handlers.patchDoc
);
Expand Down Expand Up @@ -266,17 +289,20 @@ module.exports = class DocsService extends CampsiService {
}
*/
'/:resource/:id',
additionalMiddlewares,
handlers.delDoc
);
this.router.delete(
// #swagger.tags = ['DOCSERVICE'],
// #swagger.ignore = true
'/:resource/:id/:state',
additionalMiddlewares,
handlers.delDoc
);
this.router.delete(
'/:resource/:id/locks/:lock',
// #swagger.ignore = true
additionalMiddlewares,
handlers.deleteLock
);

Expand Down

0 comments on commit 1c6103f

Please sign in to comment.