@deepkit/injector
Advanced tools
Comparing version 1.0.1-alpha.52 to 1.0.1-alpha.53
@@ -348,18 +348,34 @@ "use strict"; | ||
const token = options.token; | ||
let of = `${ofName}.${options.name}`; | ||
//regarding configuration values: the attached module is not necessarily in resolveDependenciesFrom[0] | ||
//if the parent module overwrites its, then the parent module is at 0th position. | ||
if (token instanceof config_1.ConfigDefinition) { | ||
const module = module_1.findModuleForConfig(token, resolveDependenciesFrom); | ||
return compiler.reserveVariable('fullConfig', module.getConfig()); | ||
try { | ||
const module = module_1.findModuleForConfig(token, resolveDependenciesFrom); | ||
return compiler.reserveVariable('fullConfig', module.getConfig()); | ||
} | ||
catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} | ||
else if (token instanceof config_1.ConfigToken) { | ||
const module = module_1.findModuleForConfig(token.config, resolveDependenciesFrom); | ||
const config = module.getConfig(); | ||
return compiler.reserveVariable(token.name, config[token.name]); | ||
try { | ||
const module = module_1.findModuleForConfig(token.config, resolveDependenciesFrom); | ||
const config = module.getConfig(); | ||
return compiler.reserveVariable(token.name, config[token.name]); | ||
} | ||
catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} | ||
else if (core_1.isClass(token) && (Object.getPrototypeOf(Object.getPrototypeOf(token)) === config_1.ConfigSlice || Object.getPrototypeOf(token) === config_1.ConfigSlice)) { | ||
const value = new token; | ||
const module = module_1.findModuleForConfig(value.config, resolveDependenciesFrom); | ||
value.bag = module.getConfig(); | ||
return compiler.reserveVariable('configSlice', value); | ||
try { | ||
const value = new token; | ||
const module = module_1.findModuleForConfig(value.config, resolveDependenciesFrom); | ||
value.bag = module.getConfig(); | ||
return compiler.reserveVariable('configSlice', value); | ||
} | ||
catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} | ||
@@ -380,3 +396,2 @@ else if (token === provider_1.TagRegistry) { | ||
else { | ||
let of = `${ofName}.${options.name}`; | ||
if (token === undefined) { | ||
@@ -383,0 +398,0 @@ if (argPosition >= 0) { |
@@ -101,6 +101,11 @@ import { ConfigDefinition } from './config'; | ||
/** | ||
* Modifies this module and adds a new import, returning the same module. | ||
* Adds a new import at the end. | ||
*/ | ||
addImport(...modules: InjectorModule<any>[]): this; | ||
/** | ||
* Adds a new import at the beginning. Since import order matters, it might be useful to import a module first | ||
* so its exported providers can be overwritten by imports following this module. | ||
*/ | ||
addImportAtBeginning(...modules: InjectorModule<any>[]): this; | ||
/** | ||
* Allows to register additional setup calls for a provider in this module. | ||
@@ -107,0 +112,0 @@ * The injector token needs to be available in the local module providers. |
@@ -57,3 +57,3 @@ "use strict"; | ||
} | ||
throw new Error(`No module found for configuration ${config.schema.toString()}. Did you attach it to a module?`); | ||
throw new Error(`No module found for configuration ${config.schema.toString().replace(/\n/g, ' ').replace(/\s+/g, ' ')}. Did you attach the configuration to a module?`); | ||
} | ||
@@ -204,3 +204,3 @@ exports.findModuleForConfig = findModuleForConfig; | ||
/** | ||
* Modifies this module and adds a new import, returning the same module. | ||
* Adds a new import at the end. | ||
*/ | ||
@@ -215,2 +215,14 @@ addImport(...modules) { | ||
/** | ||
* Adds a new import at the beginning. Since import order matters, it might be useful to import a module first | ||
* so its exported providers can be overwritten by imports following this module. | ||
*/ | ||
addImportAtBeginning(...modules) { | ||
this.assertInjectorNotBuilt(); | ||
for (const module of modules) { | ||
module.parent = this; | ||
this.imports.unshift(module); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Allows to register additional setup calls for a provider in this module. | ||
@@ -217,0 +229,0 @@ * The injector token needs to be available in the local module providers. |
@@ -703,2 +703,14 @@ "use strict"; | ||
}); | ||
globals_1.test('InjectorToken reference with interface', () => { | ||
const serviceToken = new decorator_1.InjectorToken('service'); | ||
const root = new module_1.InjectorModule([{ | ||
provide: serviceToken, useClass: class { | ||
add() { | ||
} | ||
} | ||
}]); | ||
const injector = new injector_1.InjectorContext(root); | ||
const service = injector.get(serviceToken); | ||
service.add(); | ||
}); | ||
globals_1.test('injectorReference from other module', () => { | ||
@@ -705,0 +717,0 @@ class Service { |
@@ -339,18 +339,34 @@ import { isClassProvider, isExistingProvider, isFactoryProvider, isValueProvider, Tag, TagProvider, TagRegistry } from './provider'; | ||
const token = options.token; | ||
let of = `${ofName}.${options.name}`; | ||
//regarding configuration values: the attached module is not necessarily in resolveDependenciesFrom[0] | ||
//if the parent module overwrites its, then the parent module is at 0th position. | ||
if (token instanceof ConfigDefinition) { | ||
const module = findModuleForConfig(token, resolveDependenciesFrom); | ||
return compiler.reserveVariable('fullConfig', module.getConfig()); | ||
try { | ||
const module = findModuleForConfig(token, resolveDependenciesFrom); | ||
return compiler.reserveVariable('fullConfig', module.getConfig()); | ||
} | ||
catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} | ||
else if (token instanceof ConfigToken) { | ||
const module = findModuleForConfig(token.config, resolveDependenciesFrom); | ||
const config = module.getConfig(); | ||
return compiler.reserveVariable(token.name, config[token.name]); | ||
try { | ||
const module = findModuleForConfig(token.config, resolveDependenciesFrom); | ||
const config = module.getConfig(); | ||
return compiler.reserveVariable(token.name, config[token.name]); | ||
} | ||
catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} | ||
else if (isClass(token) && (Object.getPrototypeOf(Object.getPrototypeOf(token)) === ConfigSlice || Object.getPrototypeOf(token) === ConfigSlice)) { | ||
const value = new token; | ||
const module = findModuleForConfig(value.config, resolveDependenciesFrom); | ||
value.bag = module.getConfig(); | ||
return compiler.reserveVariable('configSlice', value); | ||
try { | ||
const value = new token; | ||
const module = findModuleForConfig(value.config, resolveDependenciesFrom); | ||
value.bag = module.getConfig(); | ||
return compiler.reserveVariable('configSlice', value); | ||
} | ||
catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} | ||
@@ -371,3 +387,2 @@ else if (token === TagRegistry) { | ||
else { | ||
let of = `${ofName}.${options.name}`; | ||
if (token === undefined) { | ||
@@ -374,0 +389,0 @@ if (argPosition >= 0) { |
@@ -101,6 +101,11 @@ import { ConfigDefinition } from './config'; | ||
/** | ||
* Modifies this module and adds a new import, returning the same module. | ||
* Adds a new import at the end. | ||
*/ | ||
addImport(...modules: InjectorModule<any>[]): this; | ||
/** | ||
* Adds a new import at the beginning. Since import order matters, it might be useful to import a module first | ||
* so its exported providers can be overwritten by imports following this module. | ||
*/ | ||
addImportAtBeginning(...modules: InjectorModule<any>[]): this; | ||
/** | ||
* Allows to register additional setup calls for a provider in this module. | ||
@@ -107,0 +112,0 @@ * The injector token needs to be available in the local module providers. |
@@ -53,3 +53,3 @@ import { TagProvider } from './provider'; | ||
} | ||
throw new Error(`No module found for configuration ${config.schema.toString()}. Did you attach it to a module?`); | ||
throw new Error(`No module found for configuration ${config.schema.toString().replace(/\n/g, ' ').replace(/\s+/g, ' ')}. Did you attach the configuration to a module?`); | ||
} | ||
@@ -197,3 +197,3 @@ export function isProvided(providers, token) { | ||
/** | ||
* Modifies this module and adds a new import, returning the same module. | ||
* Adds a new import at the end. | ||
*/ | ||
@@ -208,2 +208,14 @@ addImport(...modules) { | ||
/** | ||
* Adds a new import at the beginning. Since import order matters, it might be useful to import a module first | ||
* so its exported providers can be overwritten by imports following this module. | ||
*/ | ||
addImportAtBeginning(...modules) { | ||
this.assertInjectorNotBuilt(); | ||
for (const module of modules) { | ||
module.parent = this; | ||
this.imports.unshift(module); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Allows to register additional setup calls for a provider in this module. | ||
@@ -210,0 +222,0 @@ * The injector token needs to be available in the local module providers. |
@@ -16,3 +16,3 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { | ||
import { InjectorContext } from '../src/injector'; | ||
import { inject, injectable, injectorReference } from '../src/decorator'; | ||
import { inject, injectable, injectorReference, InjectorToken } from '../src/decorator'; | ||
import { getClassSchema, t } from '@deepkit/type'; | ||
@@ -702,2 +702,14 @@ import { Tag } from '../src/provider'; | ||
}); | ||
test('InjectorToken reference with interface', () => { | ||
const serviceToken = new InjectorToken('service'); | ||
const root = new InjectorModule([{ | ||
provide: serviceToken, useClass: class { | ||
add() { | ||
} | ||
} | ||
}]); | ||
const injector = new InjectorContext(root); | ||
const service = injector.get(serviceToken); | ||
service.add(); | ||
}); | ||
test('injectorReference from other module', () => { | ||
@@ -704,0 +716,0 @@ class Service { |
{ | ||
"name": "@deepkit/injector", | ||
"version": "1.0.1-alpha.52", | ||
"version": "1.0.1-alpha.53", | ||
"description": "Deepkit Dependency Injection", | ||
@@ -44,3 +44,3 @@ "type": "commonjs", | ||
}, | ||
"gitHead": "537ea1f1691917da4697c8f31f8ce9f1d7c16679" | ||
"gitHead": "abc9cf44d6ddc2da8dd7fbff9c79db9d0f0201d2" | ||
} |
@@ -422,2 +422,3 @@ import { isClassProvider, isExistingProvider, isFactoryProvider, isValueProvider, NormalizedProvider, ProviderWithScope, Tag, TagProvider, TagRegistry, Token } from './provider'; | ||
const token = options.token; | ||
let of = `${ofName}.${options.name}`; | ||
@@ -427,13 +428,25 @@ //regarding configuration values: the attached module is not necessarily in resolveDependenciesFrom[0] | ||
if (token instanceof ConfigDefinition) { | ||
const module = findModuleForConfig(token, resolveDependenciesFrom); | ||
return compiler.reserveVariable('fullConfig', module.getConfig()); | ||
try { | ||
const module = findModuleForConfig(token, resolveDependenciesFrom); | ||
return compiler.reserveVariable('fullConfig', module.getConfig()); | ||
} catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} else if (token instanceof ConfigToken) { | ||
const module = findModuleForConfig(token.config, resolveDependenciesFrom); | ||
const config = module.getConfig(); | ||
return compiler.reserveVariable(token.name, (config as any)[token.name]); | ||
try { | ||
const module = findModuleForConfig(token.config, resolveDependenciesFrom); | ||
const config = module.getConfig(); | ||
return compiler.reserveVariable(token.name, (config as any)[token.name]); | ||
} catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} else if (isClass(token) && (Object.getPrototypeOf(Object.getPrototypeOf(token)) === ConfigSlice || Object.getPrototypeOf(token) === ConfigSlice)) { | ||
const value: ConfigSlice<any> = new token; | ||
const module = findModuleForConfig(value.config, resolveDependenciesFrom); | ||
value.bag = module.getConfig(); | ||
return compiler.reserveVariable('configSlice', value); | ||
try { | ||
const value: ConfigSlice<any> = new token; | ||
const module = findModuleForConfig(value.config, resolveDependenciesFrom); | ||
value.bag = module.getConfig(); | ||
return compiler.reserveVariable('configSlice', value); | ||
} catch (error) { | ||
throw new DependenciesUnmetError(`Undefined configuration dependency '${options.name}' of ${of}. ${error.message}`); | ||
} | ||
} else if (token === TagRegistry) { | ||
@@ -451,3 +464,2 @@ return compiler.reserveVariable('tagRegistry', this.buildContext.tagRegistry); | ||
} else { | ||
let of = `${ofName}.${options.name}`; | ||
if (token === undefined) { | ||
@@ -454,0 +466,0 @@ if (argPosition >= 0) { |
@@ -87,3 +87,3 @@ import { ConfigDefinition } from './config'; | ||
throw new Error(`No module found for configuration ${config.schema.toString()}. Did you attach it to a module?`); | ||
throw new Error(`No module found for configuration ${config.schema.toString().replace(/\n/g, ' ').replace(/\s+/g, ' ')}. Did you attach the configuration to a module?`); | ||
} | ||
@@ -265,3 +265,3 @@ | ||
/** | ||
* Modifies this module and adds a new import, returning the same module. | ||
* Adds a new import at the end. | ||
*/ | ||
@@ -277,2 +277,15 @@ addImport(...modules: InjectorModule<any>[]): this { | ||
/** | ||
* Adds a new import at the beginning. Since import order matters, it might be useful to import a module first | ||
* so its exported providers can be overwritten by imports following this module. | ||
*/ | ||
addImportAtBeginning(...modules: InjectorModule<any>[]): this { | ||
this.assertInjectorNotBuilt(); | ||
for (const module of modules) { | ||
module.parent = this; | ||
this.imports.unshift(module); | ||
} | ||
return this; | ||
} | ||
/** | ||
* Allows to register additional setup calls for a provider in this module. | ||
@@ -279,0 +292,0 @@ * The injector token needs to be available in the local module providers. |
import 'reflect-metadata'; | ||
import { expect, test } from '@jest/globals'; | ||
import { InjectorContext } from '../src/injector'; | ||
import { inject, injectable, injectorReference } from '../src/decorator'; | ||
import { inject, injectable, injectorReference, InjectorToken } from '../src/decorator'; | ||
import { getClassSchema, t } from '@deepkit/type'; | ||
@@ -830,2 +830,21 @@ import { Tag } from '../src/provider'; | ||
test('InjectorToken reference with interface', () => { | ||
interface Service { | ||
add(): void; | ||
} | ||
const serviceToken = new InjectorToken<Service>('service'); | ||
const root = new InjectorModule([{ | ||
provide: serviceToken, useClass: class { | ||
add() { | ||
} | ||
} | ||
}]); | ||
const injector = new InjectorContext(root); | ||
const service = injector.get(serviceToken); | ||
service.add(); | ||
}); | ||
test('injectorReference from other module', () => { | ||
@@ -832,0 +851,0 @@ class Service { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
569689
8425
0