Skip to content
This repository was archived by the owner on Apr 21, 2024. It is now read-only.

Commit 54bdbfd

Browse files
committed
feat: export relevant "symbols" through index
1 parent e99237c commit 54bdbfd

File tree

6 files changed

+146
-50
lines changed

6 files changed

+146
-50
lines changed

lambda-ioc/deno/combinators.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Container, ContainerKey, DependencyFactory } from './container.ts';
22
import { ParamsToResolverKeys, TupleO, Zip } from './util.ts';
33

44
/**
5-
*
5+
* Given a dependency factory, returns a new factory that will always resolve
6+
* the same instance of the dependency.
67
*/
78
export function singleton<
89
TVal,
@@ -20,14 +21,11 @@ export function singleton<
2021
}
2122
}
2223

23-
type FuncContainer<
24-
TParams extends readonly unknown[],
25-
TDependencies extends ParamsToResolverKeys<TParams>,
26-
> = Container<
27-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28-
TupleO<Extract<Zip<TDependencies, TParams>, readonly [ContainerKey, any][]>>
29-
>
30-
24+
/**
25+
* Given a function, and a list of named dependencies, creates a new dependency
26+
* factory that will resolve a parameterless function wrapping the original
27+
* function and its resolved dependencies.
28+
*/
3129
export function func<
3230
TParams extends readonly unknown[],
3331
TReturn,
@@ -51,6 +49,10 @@ export function func<
5149
}
5250
}
5351

52+
/**
53+
* Given a class constructor, and a list of named dependencies, creates a new
54+
* dependency factory that will resolve a new instance of the class.
55+
*/
5456
export function constructor<
5557
TParams extends readonly unknown[],
5658
TClass,
@@ -73,3 +75,14 @@ export function constructor<
7375
return new constructor(...resolvedArgs)
7476
}
7577
}
78+
79+
// -----------------------------------------------------------------------------
80+
// Private Types
81+
// -----------------------------------------------------------------------------
82+
type FuncContainer<
83+
TParams extends readonly unknown[],
84+
TDependencies extends ParamsToResolverKeys<TParams>,
85+
> = Container<
86+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87+
TupleO<Extract<Zip<TDependencies, TParams>, readonly [ContainerKey, any][]>>
88+
>

lambda-ioc/deno/container.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,50 @@
33
export type ContainerKey = string | symbol
44

55
/**
6-
*
6+
* Represents a dependency factory: a function that, given an IoC container, it
7+
* is able to instantiate a specific dependency.
78
*/
89
export type DependencyFactory<
910
T,
10-
TContainer extends Container<Record<ContainerKey, unknown>>,
11+
TContainer extends ReadableContainer<Record<ContainerKey, unknown>>,
1112
> = (container: TContainer) => T | Promise<T>
1213

1314
/**
14-
*
15+
* Represents a read-only version of a type-safe IoC container with "auto-wired"
16+
* dependencies resolution.
1517
*/
16-
export interface Container<
18+
export interface ReadableContainer<
1719
TDependencies extends Record<ContainerKey, unknown>,
1820
> {
1921
/**
20-
* Register a new dependency.
22+
* Resolve a dependency from the container.
23+
*
24+
* @param name The "name" of the dependency (can be a symbol).
25+
*/
26+
resolve<TName extends keyof TDependencies>(
27+
name: TName,
28+
): Promise<TDependencies[TName]>
29+
}
30+
31+
/**
32+
* Represents a write-only version of a type-safe IoC container with
33+
* "auto-wired" dependencies resolution.
34+
*/
35+
export interface WritableContainer<
36+
TDependencies extends Record<ContainerKey, unknown>,
37+
> {
38+
/**
39+
* Register a new dependency factory.
2140
*
2241
* @param name The "name" of the dependency (can be a symbol).
2342
* @param dependency A dependency factory.
2443
*/
2544
register<TName extends ContainerKey, TDependency>(
2645
name: TName,
27-
dependency: DependencyFactory<TDependency, Container<TDependencies>>,
46+
dependency: DependencyFactory<
47+
TDependency,
48+
ReadableContainer<TDependencies>
49+
>,
2850
): Container<{
2951
[TK in keyof TDependencies | TName]: TK extends keyof TDependencies
3052
? TName extends TK
@@ -33,6 +55,12 @@ export interface Container<
3355
: TDependency
3456
}>
3557

58+
/**
59+
* Register an already instantiated dependency.
60+
*
61+
* @param name The "name" of the dependency (can be a symbol).
62+
* @param dependency An already instantiated value.
63+
*/
3664
registerValue<TName extends ContainerKey, TDependency>(
3765
name: TName,
3866
dependency: TDependency,
@@ -43,19 +71,18 @@ export interface Container<
4371
: TDependencies[TK]
4472
: TDependency
4573
}>
46-
47-
/**
48-
* Resolve a dependency from the container.
49-
*
50-
* @param name The "name" of the dependency (can be a symbol).
51-
*/
52-
resolve<TName extends keyof TDependencies>(
53-
name: TName,
54-
): Promise<TDependencies[TName]>
5574
}
5675

5776
/**
58-
*
77+
* Represents a type-safe IoC container with "auto-wired" dependencies
78+
* resolution
79+
*/
80+
export interface Container<TDependencies extends Record<ContainerKey, unknown>>
81+
extends ReadableContainer<TDependencies>,
82+
WritableContainer<TDependencies> {}
83+
84+
/**
85+
* Creates a new type-safe IoC container.
5986
*/
6087
export function createContainer(): Container<{}> {
6188
return __createContainer({})

lambda-ioc/deno/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export {
2+
type Container,
3+
type DependencyFactory,
4+
type ReadableContainer,
5+
type WritableContainer,
6+
createContainer,
7+
} from './container.ts';
8+
export { constructor, func, singleton } from './combinators.ts';

lambda-ioc/src/combinators.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { Container, ContainerKey, DependencyFactory } from './container'
22
import { ParamsToResolverKeys, TupleO, Zip } from './util'
33

44
/**
5-
*
5+
* Given a dependency factory, returns a new factory that will always resolve
6+
* the same instance of the dependency.
67
*/
78
export function singleton<
89
TVal,
@@ -20,14 +21,11 @@ export function singleton<
2021
}
2122
}
2223

23-
type FuncContainer<
24-
TParams extends readonly unknown[],
25-
TDependencies extends ParamsToResolverKeys<TParams>,
26-
> = Container<
27-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28-
TupleO<Extract<Zip<TDependencies, TParams>, readonly [ContainerKey, any][]>>
29-
>
30-
24+
/**
25+
* Given a function, and a list of named dependencies, creates a new dependency
26+
* factory that will resolve a parameterless function wrapping the original
27+
* function and its resolved dependencies.
28+
*/
3129
export function func<
3230
TParams extends readonly unknown[],
3331
TReturn,
@@ -51,6 +49,10 @@ export function func<
5149
}
5250
}
5351

52+
/**
53+
* Given a class constructor, and a list of named dependencies, creates a new
54+
* dependency factory that will resolve a new instance of the class.
55+
*/
5456
export function constructor<
5557
TParams extends readonly unknown[],
5658
TClass,
@@ -73,3 +75,14 @@ export function constructor<
7375
return new constructor(...resolvedArgs)
7476
}
7577
}
78+
79+
// -----------------------------------------------------------------------------
80+
// Private Types
81+
// -----------------------------------------------------------------------------
82+
type FuncContainer<
83+
TParams extends readonly unknown[],
84+
TDependencies extends ParamsToResolverKeys<TParams>,
85+
> = Container<
86+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
87+
TupleO<Extract<Zip<TDependencies, TParams>, readonly [ContainerKey, any][]>>
88+
>

lambda-ioc/src/container.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,50 @@
33
export type ContainerKey = string | symbol
44

55
/**
6-
*
6+
* Represents a dependency factory: a function that, given an IoC container, it
7+
* is able to instantiate a specific dependency.
78
*/
89
export type DependencyFactory<
910
T,
10-
TContainer extends Container<Record<ContainerKey, unknown>>,
11+
TContainer extends ReadableContainer<Record<ContainerKey, unknown>>,
1112
> = (container: TContainer) => T | Promise<T>
1213

1314
/**
14-
*
15+
* Represents a read-only version of a type-safe IoC container with "auto-wired"
16+
* dependencies resolution.
1517
*/
16-
export interface Container<
18+
export interface ReadableContainer<
1719
TDependencies extends Record<ContainerKey, unknown>,
1820
> {
1921
/**
20-
* Register a new dependency.
22+
* Resolve a dependency from the container.
23+
*
24+
* @param name The "name" of the dependency (can be a symbol).
25+
*/
26+
resolve<TName extends keyof TDependencies>(
27+
name: TName,
28+
): Promise<TDependencies[TName]>
29+
}
30+
31+
/**
32+
* Represents a write-only version of a type-safe IoC container with
33+
* "auto-wired" dependencies resolution.
34+
*/
35+
export interface WritableContainer<
36+
TDependencies extends Record<ContainerKey, unknown>,
37+
> {
38+
/**
39+
* Register a new dependency factory.
2140
*
2241
* @param name The "name" of the dependency (can be a symbol).
2342
* @param dependency A dependency factory.
2443
*/
2544
register<TName extends ContainerKey, TDependency>(
2645
name: TName,
27-
dependency: DependencyFactory<TDependency, Container<TDependencies>>,
46+
dependency: DependencyFactory<
47+
TDependency,
48+
ReadableContainer<TDependencies>
49+
>,
2850
): Container<{
2951
[TK in keyof TDependencies | TName]: TK extends keyof TDependencies
3052
? TName extends TK
@@ -33,6 +55,12 @@ export interface Container<
3355
: TDependency
3456
}>
3557

58+
/**
59+
* Register an already instantiated dependency.
60+
*
61+
* @param name The "name" of the dependency (can be a symbol).
62+
* @param dependency An already instantiated value.
63+
*/
3664
registerValue<TName extends ContainerKey, TDependency>(
3765
name: TName,
3866
dependency: TDependency,
@@ -43,19 +71,18 @@ export interface Container<
4371
: TDependencies[TK]
4472
: TDependency
4573
}>
46-
47-
/**
48-
* Resolve a dependency from the container.
49-
*
50-
* @param name The "name" of the dependency (can be a symbol).
51-
*/
52-
resolve<TName extends keyof TDependencies>(
53-
name: TName,
54-
): Promise<TDependencies[TName]>
5574
}
5675

5776
/**
58-
*
77+
* Represents a type-safe IoC container with "auto-wired" dependencies
78+
* resolution
79+
*/
80+
export interface Container<TDependencies extends Record<ContainerKey, unknown>>
81+
extends ReadableContainer<TDependencies>,
82+
WritableContainer<TDependencies> {}
83+
84+
/**
85+
* Creates a new type-safe IoC container.
5986
*/
6087
export function createContainer(): Container<{}> {
6188
return __createContainer({})

lambda-ioc/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export {
2+
type Container,
3+
type DependencyFactory,
4+
type ReadableContainer,
5+
type WritableContainer,
6+
createContainer,
7+
} from './container'
8+
export { constructor, func, singleton } from './combinators'

0 commit comments

Comments
 (0)