diff --git a/README.md b/README.md index 30cda38..48e94c2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,6 @@ import { ... } from 'https://deno.land/x/lambda_ioc@[VERSION]/lambda-ioc/deno/in ```ts import { cc2ic, // Stands for "class-constructor to interface-constructor" - constructor, createContainer, func } from '@coderspirit/lambda-ioc' @@ -64,10 +63,8 @@ const container = createContainer() .registerValue('someName', 'Timmy') // We can register functions .register('fn', func(printNameAndAge, 'someName', 'someAge')) - // And constructors too - .register('Person', constructor(Person, 'someAge', 'someName')) - // We can do that directly, without having import `constructor`: - .registerConstructor('AnotherPerson', Person, 'someAge', 'someName') + // And constructors too: + .registerConstructor('Person', Person, 'someAge', 'someName') // In case we want to register a "concrete" constructor to provide an // abstract interface, we'll have to apply a small hack, using `cc2ic`: .registerConstructor('Human', cc2ic()(Person), 'someAge', 'someName') @@ -96,14 +93,6 @@ container.resolveGroup('group2') // ~ [3, 4], not necessarily in the same order // up to date. This is useful if we want to use the container as a factory for // some of your dependencies. const resolvedContainer = container.resolve('$') - -// If you want to indirectly resolve the container itself, it can be done only -// with the methods: -// - registerConstructor -// - registerAsyncConstructor -// This is because they have "privileged" information about the container's -// type, while relying on `register` or `registerAsync` plus "combinators" does -// not allow us to leverage that information. ``` It is also possible to register and resolve asynchronous factories and diff --git a/lambda-ioc/README.md b/lambda-ioc/README.md index 30cda38..48e94c2 100644 --- a/lambda-ioc/README.md +++ b/lambda-ioc/README.md @@ -38,7 +38,6 @@ import { ... } from 'https://deno.land/x/lambda_ioc@[VERSION]/lambda-ioc/deno/in ```ts import { cc2ic, // Stands for "class-constructor to interface-constructor" - constructor, createContainer, func } from '@coderspirit/lambda-ioc' @@ -64,10 +63,8 @@ const container = createContainer() .registerValue('someName', 'Timmy') // We can register functions .register('fn', func(printNameAndAge, 'someName', 'someAge')) - // And constructors too - .register('Person', constructor(Person, 'someAge', 'someName')) - // We can do that directly, without having import `constructor`: - .registerConstructor('AnotherPerson', Person, 'someAge', 'someName') + // And constructors too: + .registerConstructor('Person', Person, 'someAge', 'someName') // In case we want to register a "concrete" constructor to provide an // abstract interface, we'll have to apply a small hack, using `cc2ic`: .registerConstructor('Human', cc2ic()(Person), 'someAge', 'someName') @@ -96,14 +93,6 @@ container.resolveGroup('group2') // ~ [3, 4], not necessarily in the same order // up to date. This is useful if we want to use the container as a factory for // some of your dependencies. const resolvedContainer = container.resolve('$') - -// If you want to indirectly resolve the container itself, it can be done only -// with the methods: -// - registerConstructor -// - registerAsyncConstructor -// This is because they have "privileged" information about the container's -// type, while relying on `register` or `registerAsync` plus "combinators" does -// not allow us to leverage that information. ``` It is also possible to register and resolve asynchronous factories and diff --git a/lambda-ioc/deno/combinators.ts b/lambda-ioc/deno/combinators.ts index 904764b..8f244ff 100644 --- a/lambda-ioc/deno/combinators.ts +++ b/lambda-ioc/deno/combinators.ts @@ -87,32 +87,6 @@ export function func< } } -/** - * Given a class constructor, and a list of named dependencies, creates a new - * dependency factory that will resolve a new instance of the class. - */ -export function constructor< - TParams extends readonly unknown[], - TClass, - TDependencies extends ParamsToResolverKeys, ->( - constructor: new (...args: TParams) => Awaited, - ...args: TDependencies -): SyncDependencyFactory> { - return (container: SyncFuncContainer) => { - const resolvedArgs = args.map((arg) => - container.resolve( - // This is ugly as hell, but I did not want to apply ts-ignore - arg as Parameters< - SyncFuncContainer['resolve'] - >[0], - ), - ) as unknown as TParams - - return new constructor(...resolvedArgs) - } -} - // Class-Constructor to Interface-Constructor // eslint-disable-next-line @typescript-eslint/no-explicit-any export function cc2ic(): I>(cc: CC) => AsInterfaceCtor { diff --git a/lambda-ioc/deno/index.ts b/lambda-ioc/deno/index.ts index 3c82962..1ca439f 100644 --- a/lambda-ioc/deno/index.ts +++ b/lambda-ioc/deno/index.ts @@ -11,7 +11,6 @@ export { export { asyncSingleton, cc2ic, - constructor, func, singleton, } from './combinators.ts'; diff --git a/lambda-ioc/package.json b/lambda-ioc/package.json index 7370e0b..6cb3370 100644 --- a/lambda-ioc/package.json +++ b/lambda-ioc/package.json @@ -1,6 +1,6 @@ { "name": "@coderspirit/lambda-ioc", - "version": "0.8.0", + "version": "1.0.0", "main": "./dist/cjs/index.js", "module": "./dist/esm/index.js", "types": "./dist/cjs/index.d.ts", diff --git a/lambda-ioc/src/__tests__/constructor.test.ts b/lambda-ioc/src/__tests__/constructor.test.ts deleted file mode 100644 index 5a137fd..0000000 --- a/lambda-ioc/src/__tests__/constructor.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { constructor, createContainer, singleton } from '..' - -describe('constructor', () => { - it('can be registered without parameters', () => { - class A {} - const container = createContainer().register('A', constructor(A)) - const a = container.resolve('A') - expect(a).toBeInstanceOf(A) - }) - - it('can be registered with parameters', () => { - class A {} - class B { - constructor(readonly a: A) {} - } - class C { - constructor(readonly b: B) {} - } - - const container = createContainer() - .register('A', constructor(A)) - .register('B', constructor(B, 'A')) - .register('C', singleton(constructor(C, 'B'))) - - // We abuse a bit this test to verify other tangential properties, like - // uniqueness of instances. - const b1 = container.resolve('B') - const b2 = container.resolve('B') - - expect(b1).toBeInstanceOf(B) // That's the real test. - expect(b2).toBeInstanceOf(B) - expect(b1).not.toBe(b2) - - const c1 = container.resolve('C') - const c2 = container.resolve('C') - - expect(c1).toBeInstanceOf(C) - expect(c2).toBeInstanceOf(C) - expect(c1).toBe(c2) - }) -}) diff --git a/lambda-ioc/src/combinators.ts b/lambda-ioc/src/combinators.ts index 89fd3f7..8712f9d 100644 --- a/lambda-ioc/src/combinators.ts +++ b/lambda-ioc/src/combinators.ts @@ -87,32 +87,6 @@ export function func< } } -/** - * Given a class constructor, and a list of named dependencies, creates a new - * dependency factory that will resolve a new instance of the class. - */ -export function constructor< - TParams extends readonly unknown[], - TClass, - TDependencies extends ParamsToResolverKeys, ->( - constructor: new (...args: TParams) => Awaited, - ...args: TDependencies -): SyncDependencyFactory> { - return (container: SyncFuncContainer) => { - const resolvedArgs = args.map((arg) => - container.resolve( - // This is ugly as hell, but I did not want to apply ts-ignore - arg as Parameters< - SyncFuncContainer['resolve'] - >[0], - ), - ) as unknown as TParams - - return new constructor(...resolvedArgs) - } -} - // Class-Constructor to Interface-Constructor // eslint-disable-next-line @typescript-eslint/no-explicit-any export function cc2ic(): I>(cc: CC) => AsInterfaceCtor { diff --git a/lambda-ioc/src/index.ts b/lambda-ioc/src/index.ts index 4ef9e3e..e6cc3eb 100644 --- a/lambda-ioc/src/index.ts +++ b/lambda-ioc/src/index.ts @@ -11,7 +11,6 @@ export { export { asyncSingleton, cc2ic, - constructor, func, singleton, } from './combinators'