@glandjs/core
Version:
Glands is a web framework for Node.js (@core)
77 lines (76 loc) • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Explorer = void 0;
const common_1 = require("@glandjs/common");
const discovery_service_1 = require("./discovery-service");
const scanner_1 = require("./scanner");
class Explorer {
constructor(modulesContainer, logger) {
this.logger = logger?.child('Explorer');
this.metadataScanner = new scanner_1.MetadataScanner();
this.discoveryService = new discovery_service_1.DiscoveryService(modulesContainer, logger);
}
exploreControllers() {
this.logger?.debug('Starting controller exploration...');
const controllers = this.discoveryService.getControllers(common_1.PATH_METADATA);
const result = [];
for (const wrapper of controllers) {
const instance = wrapper.getInstance();
const controllerType = wrapper.token;
const prototype = Object.getPrototypeOf(instance);
const controllerPath = Reflect.getMetadata(common_1.PATH_METADATA, controllerType) || '';
this.logger?.debug(`Scanning controller: ${controllerType.name}, path: "${controllerPath}"`);
this.metadataScanner.scanFromPrototype(prototype, (methodName) => {
const target = prototype[methodName];
const method = Reflect.getMetadata(common_1.METHOD_METADATA, target);
const route = Reflect.getMetadata(common_1.PATH_METADATA, target) || '/';
if (method) {
this.logger?.debug(` - Found route handler: ${method.toUpperCase()} ${controllerPath}${route} -> ${methodName}`);
result.push({
controller: {
instance,
target,
methodName,
path: controllerPath,
},
method,
route,
});
}
});
}
this.logger?.debug(`Completed controller exploration. Total: ${result.length}`);
this.logger?.debug(`- Done.`);
return result;
}
exploreChannels() {
this.logger?.debug('Starting channel exploration...');
const channels = this.discoveryService.getChannels(common_1.PATH_METADATA);
const result = [];
for (const wrapper of channels) {
const instance = wrapper.getInstance();
const metatype = wrapper.token;
const prototype = Object.getPrototypeOf(instance);
const channelName = Reflect.getMetadata(common_1.PATH_METADATA, metatype);
this.logger?.debug(`Scanning channel: ${metatype.name}, namespace: "${channelName}"`);
this.metadataScanner.scanFromPrototype(prototype, (method) => {
const target = prototype[method];
const methodMetadata = Reflect.getMetadata(common_1.METHOD_METADATA, prototype, method);
if (methodMetadata) {
this.logger?.debug(` - Found event handler: ${channelName}.${methodMetadata} -> ${method}`);
result.push({
instance,
token: metatype,
event: methodMetadata,
namespace: channelName,
target,
});
}
});
}
this.logger?.debug(`Completed channel exploration. Total: ${result.length}`);
this.logger?.debug(`- Done.`);
return result;
}
}
exports.Explorer = Explorer;