@micro.ts/core
Version:
Microservice framework with Typescript
201 lines (200 loc) • 9.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.attachControllerAuthorization = exports.attachControllerMiddleware = exports.attachControllerErrorHandlers = exports.registerControllerMetadata = exports.attachHandlerErrorHandler = exports.attachHandlerBrokersFitler = exports.attachHandlerRedirect = exports.attachHandlerMiddleware = exports.attachHandlerAuthorization = exports.registerParamMetadata = exports.registerHandlerMetadata = void 0;
const GlobalMetadata_1 = require("./GlobalMetadata");
const DiDecorators_1 = require("../di/DiDecorators");
const DiOptionsTypes_1 = require("../di/types/DiOptionsTypes");
/**
* Registers or appends metadata to the method
* If the parameters of that method are not registered (from decorations) it saves only the types of the parameters
* @param target Target controller
* @param propertyKey method name
* @param _descriptor method descriptor
* @param options Options to attach
*/
function registerHandlerMetadata(target, propertyKey, _descriptor, options) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
// Get registered or default
let controller = metadata.methods.get(target);
controller = controller || {};
controller[propertyKey] = controller[propertyKey] || { params: [] };
controller[propertyKey].name = propertyKey;
controller[propertyKey].metadata = controller[propertyKey].metadata || {};
// Attach to existing options, necessary if method is decorated with more than 1 annotation
controller[propertyKey].metadata = Object.assign(Object.assign({}, (controller[propertyKey].metadata || {})), (options || {}));
controller[propertyKey].params = [];
// If the parameters of the method are registered copy save them to the method method metadata ...
const existingParams = metadata.parameters.get(target);
if (existingParams && existingParams[propertyKey]) {
controller[propertyKey].params = existingParams[propertyKey];
}
else {
// ... or get only the parameter types if they are not registered
const paramtypes = Reflect.getOwnMetadata('design:paramtypes', target, propertyKey);
if (paramtypes && paramtypes.length) {
controller[propertyKey].params.push(...paramtypes.map((x) => {
return { type: x };
}));
}
}
// Save the new metadata to the global metadata structure
metadata.methods.set(target, controller);
}
exports.registerHandlerMetadata = registerHandlerMetadata;
/**
* Register metadata for a single parameter
* @param target Target controller
* @param propertyKey Method name
* @param index Parameter Index
* @param options Parameter options
*/
function registerParamMetadata(target, propertyKey, index, options) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
// Get existing method parameters or default
let controller = metadata.parameters.get(target);
controller = controller || {};
controller[propertyKey] = controller[propertyKey] || [];
// Get types for the parameters of the method
const paramtypes = Reflect.getOwnMetadata('design:paramtypes', target, propertyKey);
// If no metadata registered save only the parameter types
if (paramtypes &&
paramtypes.length > 0 &&
controller[propertyKey].length === 0) {
controller[propertyKey].push(...paramtypes.map((x) => {
return { type: x };
}));
}
// Set the metadata for the current parameter using index
controller[propertyKey][index].options = options;
metadata.parameters.set(target, controller);
}
exports.registerParamMetadata = registerParamMetadata;
/**
* Sets or unsets the authorized property for the method
* attaches authorization options and enabled flag to the method
* @param target Target controller
* @param propertyKey Method name
* @param _descriptor Method descriptor
* @param options Authorization options
* @param active Used to disable in case it's enabled by its controller
*/
function attachHandlerAuthorization(target, propertyKey, _descriptor, options, active = true) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
// Get registered metadata or default
let controller = metadata.methods.get(target);
controller = controller || {};
controller[propertyKey] = controller[propertyKey] || { params: [] };
const handlerObject = controller[propertyKey] || {};
// Sets authorization options
handlerObject.authorize = active;
handlerObject.authorization = handlerObject.authorization || [];
handlerObject.authorization = options;
controller[propertyKey] = handlerObject;
// Saves appended metadata
metadata.methods.set(target, controller);
}
exports.attachHandlerAuthorization = attachHandlerAuthorization;
/**
* Attaches middlewares to the method
* @param target
* @param propertyKey
* @param _descriptor
* @param options
*/
function attachHandlerMiddleware(target, propertyKey, _descriptor, options) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
let controller = metadata.methods.get(target);
controller = controller || {};
controller[propertyKey] = controller[propertyKey] || { params: [] };
const handlerObject = controller[propertyKey] || {};
handlerObject.middlewares = handlerObject.middlewares || [];
handlerObject.middlewares.push(...options);
controller[propertyKey] = handlerObject;
metadata.methods.set(target, controller);
}
exports.attachHandlerMiddleware = attachHandlerMiddleware;
function attachHandlerRedirect(target, propertyKey, _descriptor, url) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
let controller = metadata.methods.get(target);
controller = controller || {};
controller[propertyKey] = controller[propertyKey] || { params: [] };
const handlerObject = controller[propertyKey] || {};
handlerObject.redirect = handlerObject.redirect || url;
controller[propertyKey] = handlerObject;
metadata.methods.set(target, controller);
}
exports.attachHandlerRedirect = attachHandlerRedirect;
function attachHandlerBrokersFitler(target, propertyKey, _descriptor, options) {
registerHandlerMetadata(target, propertyKey, _descriptor, {
brokers: options,
});
}
exports.attachHandlerBrokersFitler = attachHandlerBrokersFitler;
function attachHandlerErrorHandler(target, propertyKey, _descriptor, options) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
let controller = metadata.methods.get(target);
controller = controller || {};
controller[propertyKey] = controller[propertyKey] || { params: [] };
const handlerObject = controller[propertyKey] || {};
handlerObject.errorHandlers = handlerObject.errorHandlers || [];
handlerObject.errorHandlers.push(...options);
controller[propertyKey] = handlerObject;
metadata.methods.set(target, controller);
}
exports.attachHandlerErrorHandler = attachHandlerErrorHandler;
function registerControllerMetadata(target, options) {
const metadata = (0, GlobalMetadata_1.getGlobalMetadata)();
metadata.controllers =
metadata.controllers || new Map();
const name = target.name;
// let found = metadata.controllers.find(x => x.ctor === target);
let found = metadata.controllers.get(target);
const isFound = !!found;
const paramtypes = Reflect.getOwnMetadata('design:paramtypes', target);
found = found || { name, ctor: target };
found.constructorParams = [];
if (paramtypes && paramtypes.length) {
found.constructorParams.push(...paramtypes.map((x) => {
return { type: x };
}));
}
found.options = found.options || {};
if (options && options.errorHandlers) {
const currentErrorHandlers = found.options.errorHandlers || [];
options.errorHandlers = [
...currentErrorHandlers,
...options.errorHandlers,
];
options.timeout = options.timeout || found.options.timeout;
}
if (options && options.middlewares) {
const currentMiddlewares = found.options.middlewares || [];
options.middlewares = [...currentMiddlewares, ...options.middlewares];
}
found.options = Object.assign(Object.assign({}, found.options), (options || {}));
found.handlers = found.handlers || metadata.methods.get(target.prototype);
metadata.methods.delete(target.prototype);
metadata.controllers.set(target, found);
if (!isFound)
Reflect.decorate([(0, DiDecorators_1.Service)({ scope: DiOptionsTypes_1.ServiceScope.Request })], target);
}
exports.registerControllerMetadata = registerControllerMetadata;
function attachControllerErrorHandlers(target, errorHandlers) {
registerControllerMetadata(target, { errorHandlers });
}
exports.attachControllerErrorHandlers = attachControllerErrorHandlers;
function attachControllerMiddleware(target, middlewares, before) {
registerControllerMetadata(target, {
middlewares: middlewares.map((x) => {
return { before, middleware: x };
}),
});
}
exports.attachControllerMiddleware = attachControllerMiddleware;
function attachControllerAuthorization(target, options) {
registerControllerMetadata(target, {
authorize: true,
authorization: options,
});
}
exports.attachControllerAuthorization = attachControllerAuthorization;