@deboxsoft/module-core
Advanced tools
Comparing version 1.6.2-7 to 1.7.0-0
@@ -45,2 +45,65 @@ 'use strict'; | ||
class ServiceNotFoundError extends DeboxError { | ||
constructor(identifier) { | ||
super(); | ||
this.name = "ServiceNotFoundError"; | ||
this.normalizedIdentifier = "<UNKNOWN_IDENTIFIER>"; | ||
if (typeof identifier === "string") { | ||
this.normalizedIdentifier = identifier; | ||
} | ||
} | ||
get message() { | ||
return `Service with "${this.normalizedIdentifier}" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set"`; | ||
} | ||
} | ||
class ContainerDI { | ||
constructor() { | ||
this.metadataMap = new Map(); | ||
this.disposed = false; | ||
} | ||
has(identifier) { | ||
return !!this.metadataMap.has(identifier); | ||
} | ||
get(identifier) { | ||
const value = this.metadataMap.get(identifier); | ||
if (!this.metadataMap.has(identifier)) { | ||
throw new ServiceNotFoundError(identifier); | ||
} | ||
return value; | ||
} | ||
set(id, value) { | ||
this.metadataMap.set(id, value); | ||
return this; | ||
} | ||
remove(identifierOrIdentifierArray) { | ||
if (Array.isArray(identifierOrIdentifierArray)) { | ||
identifierOrIdentifierArray.forEach((id) => this.remove(id)); | ||
} else { | ||
const serviceMetadata = this.metadataMap.get(identifierOrIdentifierArray); | ||
if (serviceMetadata) { | ||
this.metadataMap.delete(identifierOrIdentifierArray); | ||
} | ||
} | ||
return this; | ||
} | ||
reset() { | ||
this.metadataMap = new Map(); | ||
} | ||
initializeParams(target, paramTypes) { | ||
return paramTypes.map((paramType, index) => { | ||
if (paramType && paramType.name && !ContainerDI.isPrimitiveParamType(paramType.name)) { | ||
return this.get(paramType); | ||
} | ||
return void 0; | ||
}); | ||
} | ||
static isPrimitiveParamType(paramTypeName) { | ||
return ["string", "boolean", "number", "object"].includes(paramTypeName.toLowerCase()); | ||
} | ||
} | ||
const container = new ContainerDI(); | ||
const Container = container; | ||
function isObject(val) { | ||
@@ -373,2 +436,3 @@ return val != null && typeof val === "object" && Array.isArray(val) === false; | ||
exports.CONFIG_KEY = CONFIG_KEY; | ||
exports.Container = Container; | ||
exports.DeboxError = DeboxError; | ||
@@ -375,0 +439,0 @@ exports.LOGGER_KEY = LOGGER_KEY; |
65
index.js
@@ -18,2 +18,65 @@ import * as changeCase from 'change-case'; | ||
class ServiceNotFoundError extends DeboxError { | ||
constructor(identifier) { | ||
super(); | ||
this.name = "ServiceNotFoundError"; | ||
this.normalizedIdentifier = "<UNKNOWN_IDENTIFIER>"; | ||
if (typeof identifier === "string") { | ||
this.normalizedIdentifier = identifier; | ||
} | ||
} | ||
get message() { | ||
return `Service with "${this.normalizedIdentifier}" identifier was not found in the container. Register it before usage via explicitly calling the "Container.set"`; | ||
} | ||
} | ||
class ContainerDI { | ||
constructor() { | ||
this.metadataMap = new Map(); | ||
this.disposed = false; | ||
} | ||
has(identifier) { | ||
return !!this.metadataMap.has(identifier); | ||
} | ||
get(identifier) { | ||
const value = this.metadataMap.get(identifier); | ||
if (!this.metadataMap.has(identifier)) { | ||
throw new ServiceNotFoundError(identifier); | ||
} | ||
return value; | ||
} | ||
set(id, value) { | ||
this.metadataMap.set(id, value); | ||
return this; | ||
} | ||
remove(identifierOrIdentifierArray) { | ||
if (Array.isArray(identifierOrIdentifierArray)) { | ||
identifierOrIdentifierArray.forEach((id) => this.remove(id)); | ||
} else { | ||
const serviceMetadata = this.metadataMap.get(identifierOrIdentifierArray); | ||
if (serviceMetadata) { | ||
this.metadataMap.delete(identifierOrIdentifierArray); | ||
} | ||
} | ||
return this; | ||
} | ||
reset() { | ||
this.metadataMap = new Map(); | ||
} | ||
initializeParams(target, paramTypes) { | ||
return paramTypes.map((paramType, index) => { | ||
if (paramType && paramType.name && !ContainerDI.isPrimitiveParamType(paramType.name)) { | ||
return this.get(paramType); | ||
} | ||
return void 0; | ||
}); | ||
} | ||
static isPrimitiveParamType(paramTypeName) { | ||
return ["string", "boolean", "number", "object"].includes(paramTypeName.toLowerCase()); | ||
} | ||
} | ||
const container = new ContainerDI(); | ||
const Container = container; | ||
function isObject(val) { | ||
@@ -345,2 +408,2 @@ return val != null && typeof val === "object" && Array.isArray(val) === false; | ||
export { CONFIG_KEY, DeboxError, LOGGER_KEY, Logger, index as utils }; | ||
export { CONFIG_KEY, Container, DeboxError, LOGGER_KEY, Logger, index as utils }; |
{ | ||
"name": "@deboxsoft/module-core", | ||
"version": "1.6.2-7", | ||
"version": "1.7.0-0", | ||
"license": "SEE LICENSE IN LICENSE", | ||
@@ -5,0 +5,0 @@ "maintainers": [ |
@@ -6,2 +6,3 @@ export { DeboxError } from "./Error"; | ||
export * from "./logger"; | ||
export * from "./di"; | ||
export * as utils from "./utils"; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
44038
33
1185