@tsed/common
Version:
A TypeScript Framework on top of Express
46 lines • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UseBefore = void 0;
const core_1 = require("@tsed/core");
const schema_1 = require("@tsed/schema");
/**
* Mounts the specified middleware function or functions at the specified path: the middleware function is executed when
* the base of the requested path matches `path.
*
* ```typescript
* @Controller('/')
* @UseBefore(Middleware1) // called only one time before all endpoint
* export class Ctrl {
*
* @Get('/')
* @UseBefore(Middleware2)
* get() { }
* }
* ```
*
* @returns {function(any, any, any): *}
* @param args
* @decorator
* @operation
*/
function UseBefore(...args) {
return schema_1.JsonEntityFn((entity, parameters) => {
switch (entity.decoratorType) {
case core_1.DecoratorTypes.METHOD:
const endpoint = entity;
endpoint.beforeMiddlewares = args.concat(endpoint.beforeMiddlewares);
break;
case core_1.DecoratorTypes.CLASS:
const middlewares = entity.store.get("middlewares") || {};
entity.store.set("middlewares", {
...middlewares,
useBefore: [...args, ...(middlewares.useBefore || [])]
});
break;
default:
throw new core_1.UnsupportedDecoratorType(UseBefore, parameters);
}
});
}
exports.UseBefore = UseBefore;
//# sourceMappingURL=useBefore.js.map