UNPKG

@elsikora/nestjs-crud-automator

Version:

A library for automating the creation of CRUD operations in NestJS.

86 lines (82 loc) 4.77 kB
'use strict'; var wrapper_class = require('./wrapper.class.js'); var logger_utility = require('../../../utility/logger.utility.js'); const subscriberRegistryLogger = logger_utility.LoggerUtility.getLogger("ApiSubscriberRegistry"); class ApiSubscriberRegistry { FUNCTION_SUBSCRIBER_REGISTRATION_ORDER; FUNCTION_SUBSCRIBERS; nextFunctionSubscriberRegistrationOrder; ROUTE_SUBSCRIBERS; constructor() { this.FUNCTION_SUBSCRIBER_REGISTRATION_ORDER = new WeakMap(); this.FUNCTION_SUBSCRIBERS = new Map(); this.ROUTE_SUBSCRIBERS = new Map(); this.nextFunctionSubscriberRegistrationOrder = 0; } getFunctionSubscriberProperties(subscriber) { for (const wrapper of this.FUNCTION_SUBSCRIBERS.values()) { const entry = wrapper.subscribers.find((registeredSubscriber) => registeredSubscriber.subscriber === subscriber); if (entry) { return entry.properties; } } return undefined; } getFunctionSubscriberRegistrationOrder(subscriber) { return this.FUNCTION_SUBSCRIBER_REGISTRATION_ORDER.get(subscriber) ?? Number.MAX_SAFE_INTEGER; } getFunctionSubscribers(entityName, functionType, action) { return (this.FUNCTION_SUBSCRIBERS.get(entityName)?.subscribers ?? []).filter((entry) => this.isFunctionSubscriberMatching(entry.properties, functionType, action)).map((s) => s.subscriber); } getRouteSubscribers(entityName, controller, routeType, action) { return (this.ROUTE_SUBSCRIBERS.get(entityName)?.subscribers ?? []).filter((entry) => this.isRouteSubscriberMatching(entry.properties, controller, routeType, action)).map((s) => s.subscriber); } registerFunctionSubscriber(properties, subscriber) { const entityName = properties.entity.name; let wrapper = this.FUNCTION_SUBSCRIBERS.get(entityName); if (!wrapper) { wrapper = new wrapper_class.SubscriberWrapper(entityName); this.FUNCTION_SUBSCRIBERS.set(entityName, wrapper); } if (!this.FUNCTION_SUBSCRIBER_REGISTRATION_ORDER.has(subscriber)) { this.FUNCTION_SUBSCRIBER_REGISTRATION_ORDER.set(subscriber, this.nextFunctionSubscriberRegistrationOrder); this.nextFunctionSubscriberRegistrationOrder += 1; } wrapper.addSubscriber(subscriber, properties.priority, properties); subscriberRegistryLogger.debug(`Total function subscribers for "${entityName}": ${wrapper.getSubscriberCount()}`); subscriberRegistryLogger.debug(`Registered function subscriber entities: [${[...this.FUNCTION_SUBSCRIBERS.values()].map((registeredWrapper) => registeredWrapper.getName()).join(", ")}]`); } registerRouteSubscriber(properties, subscriber) { const entityName = properties.entity.name; let wrapper = this.ROUTE_SUBSCRIBERS.get(entityName); if (!wrapper) { wrapper = new wrapper_class.SubscriberWrapper(entityName); this.ROUTE_SUBSCRIBERS.set(entityName, wrapper); } wrapper.addSubscriber(subscriber, properties.priority, properties); subscriberRegistryLogger.debug(`Total route subscribers for "${entityName}": ${wrapper.getSubscriberCount()}`); subscriberRegistryLogger.debug(`Registered route subscriber entities: [${[...this.ROUTE_SUBSCRIBERS.values()].map((registeredWrapper) => registeredWrapper.getName()).join(", ")}]`); } isFunctionSubscriberMatching(properties, functionType, action) { if (!properties?.functions?.length) { return true; } return properties.functions.some((filter) => filter.type === functionType && (filter.action === undefined || filter.action === action)); } isRouteSubscriberMatching(properties, controller, routeType, action) { if (!properties) { return true; } const isControllerMatching = !properties.controllers?.length || properties.controllers.some((controllerReference) => { const resolvedController = typeof controllerReference === "function" && "prototype" in controllerReference ? controllerReference : controllerReference(); return resolvedController === controller; }); const isRouteMatching = !properties.routes?.length || (routeType !== undefined && properties.routes.includes(routeType)); const isActionMatching = !properties.actions?.length || (action !== undefined && properties.actions.includes(action)); return isControllerMatching && isRouteMatching && isActionMatching; } } const apiSubscriberRegistry = new ApiSubscriberRegistry(); exports.apiSubscriberRegistry = apiSubscriberRegistry; //# sourceMappingURL=registry.class.js.map