@tsed/common
Version:
A TypeScript Framework on top of Express
58 lines • 2.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AuthOptions = void 0;
const core_1 = require("@tsed/core");
const endpointFn_1 = require("./endpointFn");
/**
* Change authentication options.
*
* ```typescript
* @Controller('/mypath')
* @UseAuth(MyAuthStrategy, {role: ''})
* class MyCtrl {
*
* @Get('/')
* @AuthOptions(MyAuthStrategy, {role: 'admin'})
* public getResource(){}
* }
* ```
*
* @param guardAuth
* @param options {Object} Object passed to the customer auth strategy
* @returns {Function}
* @decorator
* @decorator
* @operation
*/
function AuthOptions(guardAuth, options = {}) {
return (...args) => {
switch (core_1.decoratorTypeOf(args)) {
case core_1.DecoratorTypes.METHOD:
return endpointFn_1.EndpointFn((endpoint) => {
const store = endpoint.store;
if (options.responses) {
const { responses } = options;
store.merge("responses", responses, true);
delete options.responses;
}
if (options.security) {
const { security } = options;
[].concat(security).forEach((security) => {
Object.entries(security).forEach(([name, scopes]) => {
endpoint.operation.addSecurityScopes(name, scopes);
});
});
delete options.security;
}
store.merge(guardAuth, options, true);
})(...args);
case core_1.DecoratorTypes.CLASS:
core_1.decorateMethodsOf(args[0], AuthOptions(guardAuth, options));
break;
default:
throw new core_1.UnsupportedDecoratorType(AuthOptions, args);
}
};
}
exports.AuthOptions = AuthOptions;
//# sourceMappingURL=authOptions.js.map