Comparing version 9.0.2 to 10.0.0
@@ -33,3 +33,3 @@ const CLASS_PATTERN = /^class[ {]/; | ||
/** | ||
* @typedef {import('./index').InjectAnnotated } InjectAnnotated | ||
* @typedef {import('./index.js').InjectAnnotated } InjectAnnotated | ||
*/ | ||
@@ -104,5 +104,5 @@ | ||
/** | ||
* @typedef { import('./index').ModuleDeclaration } ModuleDeclaration | ||
* @typedef { import('./index').ModuleDefinition } ModuleDefinition | ||
* @typedef { import('./index').InjectorContext } InjectorContext | ||
* @typedef { import('./index.js').ModuleDeclaration } ModuleDeclaration | ||
* @typedef { import('./index.js').ModuleDefinition } ModuleDefinition | ||
* @typedef { import('./index.js').InjectorContext } InjectorContext | ||
*/ | ||
@@ -210,7 +210,16 @@ | ||
function instantiate(Type) { | ||
/** | ||
* Instantiate the given type, injecting dependencies. | ||
* | ||
* @template T | ||
* | ||
* @param { Function | [...string[], Function ]} type | ||
* | ||
* @return T | ||
*/ | ||
function instantiate(type) { | ||
const { | ||
fn, | ||
dependencies | ||
} = fnDef(Type); | ||
} = fnDef(type); | ||
@@ -223,2 +232,13 @@ // instantiate var args constructor | ||
/** | ||
* Invoke the given function, injecting dependencies. Return the result. | ||
* | ||
* @template T | ||
* | ||
* @param { Function | [...string[], Function ]} func | ||
* @param { Object } [context] | ||
* @param { Object } [locals] | ||
* | ||
* @return {T} invocation result | ||
*/ | ||
function invoke(func, context, locals) { | ||
@@ -225,0 +245,0 @@ const { |
@@ -30,14 +30,8 @@ export type ValueType = 'value'; | ||
export type ArrayArgs<T> = | ||
[ T ] | | ||
[ string, T ] | | ||
[ string, string, T ] | | ||
[ string, string, string, T ] | | ||
[ string, string, string, string, T ] | | ||
[ string, string, string, string, string, T ] | | ||
[ string, string, string, string, string, string, T ] | | ||
[ string, string, string, string, string, string, string, T ] | | ||
[ string, string, string, string, string, string, string, string, T ] | | ||
[ string, string, string, string, string, string, string, string, string, T ]; | ||
export type ArrayArgs<T> = [ ...string[], T ]; | ||
export type ArrayFunc<T> = [ ...string[], FactoryFunction<T> ]; | ||
export type ArrayConstructor<T> = [ ...string[], Constructor<T> ]; | ||
export type ServiceProvider<T> = { | ||
@@ -82,7 +76,81 @@ (name: string): T; | ||
export class Injector { | ||
/** | ||
* Create an injector from a set of modules. | ||
*/ | ||
constructor(modules: ModuleDefinition[], parent?: InjectorContext); | ||
get<T>(name: string, strict?: boolean): T; | ||
invoke<T>(func: (...args: any[]) => T, context?: InjectionContext, locals?: LocalsMap): T; | ||
instantiate<T>(constructor: { new (...args: any[]) : T }): T; | ||
/** | ||
* Return a named service, and throws if it is not found. | ||
*/ | ||
get<T>(name: string): T; | ||
/** | ||
* Return a named service. | ||
*/ | ||
get<T>(name: string, strict: true): T; | ||
/** | ||
* Return a named service or `null`. | ||
*/ | ||
get<T>(name: string, strict: false): T | null; | ||
/** | ||
* Invoke the given function, injecting dependencies. Return the result. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* injector.invoke(function(car) { | ||
* console.log(car.started); | ||
* }); | ||
* ``` | ||
*/ | ||
invoke<T>(func: FactoryFunction<T>, context?: InjectionContext, locals?: LocalsMap): T; | ||
/** | ||
* Invoke the given function, injecting dependencies provided in | ||
* array notation. Return the result. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* injector.invoke([ 'car', function(car) { | ||
* console.log(car.started); | ||
* } ]); | ||
* ``` | ||
*/ | ||
invoke<T>(func: ArrayFunc<T>, context?: InjectionContext, locals?: LocalsMap): T; | ||
/** | ||
* Instantiate the given type, injecting dependencies. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* injector.instantiate(Car); | ||
* ``` | ||
*/ | ||
instantiate<T>(constructor: Constructor<T>): T; | ||
/** | ||
* Instantiate the given type, injecting dependencies provided in array notation. | ||
* | ||
* @example | ||
* | ||
* ```javascript | ||
* injector.instantiate([ 'hifi', Car ]); | ||
* ``` | ||
*/ | ||
instantiate<T>(constructor: ArrayConstructor<T>): T; | ||
/** | ||
* Create a child injector. | ||
*/ | ||
createChild(modules: ModuleDefinition[], forceNewInstances?: string[]): Injector; | ||
/** | ||
* Initializes the injector once, calling `__init__` | ||
* hooks on registered injector modules. | ||
*/ | ||
init(): void; | ||
@@ -89,0 +157,0 @@ |
{ | ||
"name": "didi", | ||
"version": "9.0.2", | ||
"version": "10.0.0", | ||
"description": "Dependency Injection for JavaScript", | ||
"main": "dist/index.js", | ||
"main": "dist/index.cjs", | ||
"module": "dist/index.esm.js", | ||
"types": "lib/index.d.ts", | ||
"source": "lib/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"all": "run-s lint test bundle check-types integration-test", | ||
"bundle": "cross-env NODE_ENV=production rollup -c", | ||
"bundle": "cross-env NODE_ENV=production rollup -c --bundleConfigAsCjs", | ||
"lint": "run-s lint:*", | ||
"lint:eslint": "eslint --ext .ts --ext .js .", | ||
"lint:eslint": "eslint --ext ts,js,cjs .", | ||
"lint:tsc": "tsc --pretty --noEmit", | ||
@@ -18,4 +19,4 @@ "check-types": "run-s check-types:*", | ||
"check-types:integration": "tsc --project test/integration --pretty --noEmit", | ||
"test": "nyc --reporter=lcov mocha -r esm test/*.spec.js", | ||
"integration-test": "(cd test/integration && mocha -r ts-node/register *.spec.{js,ts})", | ||
"test": "nyc --reporter=lcov mocha test/*.spec.js", | ||
"integration-test": "(cd test/integration && mocha --loader=ts-node/esm *.spec.{cjs,ts})", | ||
"prepare": "run-s bundle" | ||
@@ -34,18 +35,22 @@ }, | ||
], | ||
"engines": { | ||
"node": ">= 16" | ||
}, | ||
"devDependencies": { | ||
"@types/chai": "^4.3.0", | ||
"@types/mocha": "^8.2.3", | ||
"@typescript-eslint/eslint-plugin": "^5.36.2", | ||
"@typescript-eslint/parser": "^5.36.2", | ||
"chai": "^4.3.6", | ||
"@types/chai": "^4.3.6", | ||
"@types/mocha": "^10.0.1", | ||
"@types/node": "^20.7.0", | ||
"@typescript-eslint/eslint-plugin": "^6.12.0", | ||
"@typescript-eslint/parser": "^6.12.0", | ||
"chai": "^4.3.8", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.23.1", | ||
"eslint-plugin-bpmn-io": "^0.14.1", | ||
"esm": "^3.2.25", | ||
"mocha": "^8.4.0", | ||
"didi": "file:", | ||
"eslint": "^8.54.0", | ||
"eslint-plugin-bpmn-io": "^1.0.0", | ||
"mocha": "^10.2.0", | ||
"npm-run-all": "^4.1.2", | ||
"nyc": "^15.1.0", | ||
"rollup": "^2.75.7", | ||
"ts-node": "^10.8.1", | ||
"typescript": "^4.7.4" | ||
"rollup": "^4.5.1", | ||
"ts-node": "^10.9.1", | ||
"typescript": "~5.2" | ||
}, | ||
@@ -52,0 +57,0 @@ "author": "Nico Rehwaldt <https://github.com/nikku>", |
@@ -15,3 +15,3 @@ # `didi` | ||
```js | ||
```ts | ||
import { Injector } from 'didi'; | ||
@@ -33,9 +33,12 @@ | ||
// define a (didi) module | ||
// it declares available components by name and specifies how these are provided | ||
// define a (didi) module - it declares available | ||
// components by name and specifies how these are provided | ||
const carModule = { | ||
// asked for 'car', the injector will call new Car(...) to produce it | ||
'car': ['type', Car], | ||
// asked for 'engine', the injector will call createPetrolEngine(...) to produce it | ||
'engine': ['factory', createPetrolEngine], | ||
// asked for 'power', the injector will give it number 1184 | ||
@@ -53,6 +56,12 @@ 'power': ['value', 1184] // probably Bugatti Veyron | ||
// ...or invoke a function, injecting the arguments | ||
// alternatively invoke a function, injecting the arguments | ||
injector.invoke(function(car) { | ||
console.log('started', car); | ||
}); | ||
// if you work with a TypeScript code base, retrieve | ||
// a typed instance of a component | ||
const car: Car = injector.get<Car>('car'); | ||
car.start(); | ||
``` | ||
@@ -59,0 +68,0 @@ |
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
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
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
36740
914
239
Yes
16
1