UNPKG

@wocker/core

Version:
62 lines (61 loc) 2.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ModuleWrapper = void 0; const InstanceWrapper_1 = require("./InstanceWrapper"); const ControllerWrapper_1 = require("./ControllerWrapper"); const env_1 = require("../env"); class ModuleWrapper { constructor(container, type) { this.container = container; this.type = type; this.imports = new Map(); this.controllers = new Map(); this.providers = new Map(); this.exports = new Set(); } get(type) { const wrapper = this.getWrapper(type); if (!wrapper) { if (typeof type === "function") { throw new Error(`Provider "${type.prototype.constructor.name}" not found`); } else if (typeof type === "string") { throw new Error(`Provider "${type}" not found`); } throw new Error("Provider not found"); } return wrapper.instance; } getWrapper(type) { const token = typeof type === "function" ? Reflect.getMetadata(env_1.INJECT_TOKEN_METADATA, type) || type : type; const wrapper = this.providers.get(token); if (!wrapper) { return this.container.globalProviders.get(token); } return wrapper; } addProvider(provider) { const wrapper = new InstanceWrapper_1.InstanceWrapper(this, provider); this.providers.set(wrapper.token, wrapper); } addController(type) { const controllerWrapper = new ControllerWrapper_1.ControllerWrapper(this, type); this.controllers.set(type, controllerWrapper); } addExport(type) { const token = typeof type !== "string" ? Reflect.getMetadata(env_1.INJECT_TOKEN_METADATA, type) || type : type; this.exports.add(token); } replace(token, provider) { const wrapper = this.getWrapper(token); if (!wrapper) { return; } wrapper.replace(provider); } } exports.ModuleWrapper = ModuleWrapper;