@micro.ts/core
Version:
Microservice framework with Typescript
97 lines (96 loc) • 3.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Redirect = exports.BrokerRouteOptions = exports.FilterBrokers = exports.UseErrorHandlers = exports.AllowAnonymous = exports.Authorize = void 0;
const BaseDecorators_1 = require("./BaseDecorators");
/**
* Use this decorator to guard the method or if used on a controller, guard all controller methods,
* by filtering the request through authorizationChecker server function
* If used on a controller you can bypass this check by usig AllowAnonymous decorator on a method
* Throws NotAuthorized error if the authorizationChecker returns false
* @param options Allows any nested value inside authorize options,
* this object, if exists, will be passed in the authorizationChecker function
*/
function Authorize(options) {
return (target, propertyKey, descriptor) => {
if (propertyKey) {
(0, BaseDecorators_1.attachHandlerAuthorization)(target, propertyKey, descriptor, options);
}
else {
(0, BaseDecorators_1.attachControllerAuthorization)(target, options);
}
};
}
exports.Authorize = Authorize;
/**
* Overrides the controller Authorization guard by disabling it for the methods it decorates
*/
function AllowAnonymous() {
return (target, propertyKey, descriptor) => {
(0, BaseDecorators_1.attachHandlerAuthorization)(target, propertyKey, descriptor, undefined, false);
};
}
exports.AllowAnonymous = AllowAnonymous;
/**
* Register error handler for the handler it decorates
* @param options
*/
function UseErrorHandlers(options) {
return (target, propertyKey, descriptor) => {
if (propertyKey) {
(0, BaseDecorators_1.attachHandlerErrorHandler)(target, propertyKey, descriptor, options);
}
else {
(0, BaseDecorators_1.attachControllerErrorHandlers)(target, options);
}
};
}
exports.UseErrorHandlers = UseErrorHandlers;
/**
* Filter controller brokers for this methods
* @param brokers Filter function, applied to the list of brokers enabled for this method's controller
*/
function FilterBrokers(brokers) {
return (target, propertyKey, descriptor) => {
if (propertyKey) {
(0, BaseDecorators_1.attachHandlerBrokersFitler)(target, propertyKey, descriptor, brokers);
}
else {
(0, BaseDecorators_1.registerControllerMetadata)(target, { brokersFilter: brokers });
}
};
}
exports.FilterBrokers = FilterBrokers;
function BrokerRouteOptions(resolver) {
return (target, propertyKey, descriptor) => {
if (propertyKey) {
(0, BaseDecorators_1.registerHandlerMetadata)(target, propertyKey, descriptor, { brokerRouteOptions: resolver });
}
else {
(0, BaseDecorators_1.registerControllerMetadata)(target, { brokerRouteOptions: resolver });
}
};
}
exports.BrokerRouteOptions = BrokerRouteOptions;
/**
* Use this decorator to redirect to another page. You can replace params starting
* with `:` by returning an object from the handler function that has a key with the
* name of the `:param`.
*
* @param url URL to be redirected to when the function handler executes successfully
*
* Example: `url: https://github.com/repos/:owner/:repo`. Returned object from handler:
*
* ```
* {
* owner: "john",
* repo: "doe"
* }
* ```
* Final URL: `https://github.com/repos/john/doe`
*/
function Redirect(url) {
return function (target, propertyKey, descriptor) {
(0, BaseDecorators_1.attachHandlerRedirect)(target, propertyKey, descriptor, url);
};
}
exports.Redirect = Redirect;