atomic-di 0.9.1-beta.3 → 1.0.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +468 -102
- package/dist/index.d.mts +160 -126
- package/dist/index.d.ts +160 -126
- package/dist/index.js +41 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +41 -31
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
@@ -1,185 +1,139 @@
|
|
1
1
|
/**
|
2
|
-
* A
|
3
|
-
*
|
4
|
-
* a different one if necessary.
|
5
|
-
*
|
6
|
-
* Passed to a provider call in a resolution context object
|
2
|
+
* A `Map` of providers to providers of the same type
|
3
|
+
* which is then passed to a provider call in a resolution context object
|
7
4
|
* in order to replace providers with their mocks.
|
8
|
-
* ```ts
|
9
|
-
* const otherProvider =
|
10
|
-
* transitive(() => ...)
|
11
|
-
* const otherProviderMock: typeof otherProvider =
|
12
|
-
* scoped(() => ...)
|
13
|
-
*
|
14
|
-
* const mocks = createMockMap()
|
15
|
-
* mocks.set(otherProvider, otherProviderMock)
|
16
|
-
*
|
17
|
-
* provider({ mocks })
|
18
|
-
* ```
|
19
5
|
*/
|
20
|
-
type MockMap = Omit<Map<
|
6
|
+
type MockMap = Omit<Map<Resolver<any>, Resolver<any>>, "set" | "get"> & {
|
21
7
|
/**
|
22
8
|
* Sets a mock for a provider.
|
23
9
|
*
|
24
10
|
* @param provider - The original provider.
|
25
11
|
* @param mock - The mock provider.
|
26
12
|
*/
|
27
|
-
set<T>(provider:
|
13
|
+
set<T>(provider: Resolver<T>, mock: Resolver<T>): MockMap;
|
28
14
|
/**
|
29
15
|
* Retrieves a mock of a provider. Returns undefined if there's none.
|
30
16
|
*
|
31
17
|
* @param provider - The provider.
|
32
18
|
*/
|
33
|
-
get<T>(provider:
|
19
|
+
get<T>(provider: Resolver<T>): Resolver<T> | undefined;
|
34
20
|
};
|
35
21
|
/**
|
36
|
-
* Creates a
|
37
|
-
*
|
38
|
-
* Lifetime is not a part of `Provider` type, so you can use
|
39
|
-
* a different one if necessary.
|
40
|
-
*
|
41
|
-
* Passed to a provider call in a resolution context object
|
22
|
+
* Creates a `Map` of providers to providers of the same type
|
23
|
+
* which is then passed to a provider call in a resolution context object
|
42
24
|
* in order to replace providers with their mocks.
|
43
|
-
* ```ts
|
44
|
-
* const otherProvider =
|
45
|
-
* transitive(() => ...)
|
46
|
-
* const otherProviderMock: typeof otherProvider =
|
47
|
-
* scoped(() => ...)
|
48
25
|
*
|
26
|
+
* @example
|
27
|
+
* ```ts
|
49
28
|
* const mocks = createMockMap()
|
50
|
-
*
|
29
|
+
* .set(getConfig, getTestConfig)
|
51
30
|
*
|
52
|
-
*
|
31
|
+
* getThing({ mocks })
|
53
32
|
* ```
|
54
33
|
*
|
55
|
-
* @returns The
|
34
|
+
* @returns The map instance.
|
56
35
|
*/
|
57
36
|
declare const createMockMap: () => MockMap;
|
58
37
|
|
59
38
|
/**
|
60
|
-
* A
|
61
|
-
*
|
62
|
-
* Passed to a provider call in a resolution context object
|
39
|
+
* A `Map` of providers to their instances
|
40
|
+
* that is then passed to a provider call in a resolution context object
|
63
41
|
* to resolve instances of scoped providers within it.
|
64
|
-
* ```ts
|
65
|
-
* const scope = createScope()
|
66
|
-
* provider({ scope })
|
67
|
-
* ```
|
68
42
|
*/
|
69
|
-
type Scope = Map<
|
43
|
+
type Scope = Map<Resolver<any>, any>;
|
70
44
|
/**
|
71
|
-
* Creates a
|
72
|
-
*
|
73
|
-
* Scope is passed to a provider call in a resolution context object
|
45
|
+
* Creates a `Map` of providers to their instances
|
46
|
+
* that is then passed to a provider call in a resolution context object
|
74
47
|
* to resolve instances of scoped providers within it.
|
48
|
+
*
|
49
|
+
* @example
|
75
50
|
* ```ts
|
76
|
-
* const
|
77
|
-
*
|
51
|
+
* const requestScope = createScope()
|
52
|
+
*
|
53
|
+
* app.use(() => {
|
54
|
+
* const db = getDb({ scope: requestScope })
|
55
|
+
* // ...
|
56
|
+
* })
|
78
57
|
* ```
|
79
58
|
*
|
80
|
-
* @returns The
|
59
|
+
* @returns The map instance.
|
81
60
|
*/
|
82
61
|
declare const createScope: () => Scope;
|
83
62
|
|
84
63
|
/**
|
85
|
-
* A
|
86
|
-
*
|
87
|
-
* Resolves an instance by calling a resolver
|
88
|
-
* with a resolution context that will be propagated
|
89
|
-
* throughout a dependency tree.
|
90
|
-
*
|
91
|
-
* When passing a scope it will try to get an instance from it
|
92
|
-
* or create a new one and put it there.
|
93
|
-
*
|
94
|
-
* When passing mocks, it will try to get its own mock version,
|
95
|
-
* and if there is one, it will use it instead of itself.
|
96
|
-
*/
|
97
|
-
type Provider<T> = (context?: ResolutionContext) => T;
|
98
|
-
/**
|
99
|
-
* A resolution lifetime.
|
100
|
-
*
|
101
|
-
* Passed when creating a provider to determine its behavior.
|
102
|
-
*
|
103
|
-
* `"transient"` doesn't provide any modifications to a resolver behaviour,
|
104
|
-
* so the resolver will create a new instance on each request.
|
105
|
-
*
|
106
|
-
* `"singleton"` forces the resolver to create an instance once
|
107
|
-
* and return it in subsequent requests.
|
108
|
-
*
|
109
|
-
* `"scoped"` forces the resolver to take its instance from a provided scope
|
110
|
-
* or create a new one and save it if there is none.
|
111
|
-
* If no scope is passed, it will create a new instance on each request.
|
64
|
+
* A function that returns a value of a particular type
|
65
|
+
* with a resolution context being passed to it.
|
112
66
|
*/
|
113
|
-
type
|
67
|
+
type Resolver<T> = (context?: ResolutionContext) => T;
|
114
68
|
/**
|
115
|
-
* A function that
|
69
|
+
* A function that resolves an instance or a `Promise` of a particular type
|
70
|
+
* based on a resolution context passed to it.
|
116
71
|
*/
|
117
|
-
type
|
72
|
+
type Provider<T> = Resolver<T>;
|
118
73
|
/**
|
119
|
-
*
|
120
|
-
*
|
121
|
-
* Passed to the provider call to resolve scope instances and mock providers.
|
74
|
+
* A context used by providers to resolve instances
|
75
|
+
* based on current scope and mocks.
|
122
76
|
*/
|
123
77
|
type ResolutionContext = {
|
124
78
|
scope?: Scope;
|
125
79
|
mocks?: MockMap;
|
126
80
|
};
|
127
81
|
/**
|
128
|
-
* Creates a provider instance
|
129
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution.
|
130
|
-
*
|
131
|
-
* @param lifetime
|
132
|
-
* A resolution lifetime.
|
133
|
-
*
|
134
|
-
* `"transient"` doesn't provide any modifications to a resolver behaviour,
|
135
|
-
* so the resolver will create a new instance on each request.
|
82
|
+
* Creates a transient provider that will resolve a new instance on each call.
|
136
83
|
*
|
137
|
-
*
|
138
|
-
*
|
139
|
-
*
|
140
|
-
*
|
141
|
-
*
|
142
|
-
* If no scope is passed, it will create a new instance on each request.
|
143
|
-
*
|
144
|
-
* @param resolver
|
145
|
-
* The function that creates an instance using a resolution context.
|
146
|
-
*
|
147
|
-
* @returns The provider instance.
|
148
|
-
*/
|
149
|
-
declare const provide: <T>(lifetime: Lifetime, resolver: Resolver<T>) => Provider<T>;
|
150
|
-
/**
|
151
|
-
* Creates a transient provider instance,
|
152
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution
|
153
|
-
* that will create a new instance on each request.
|
84
|
+
* @example
|
85
|
+
* ```ts
|
86
|
+
* const getThing = transient(() => createThing())
|
87
|
+
* getThing() !== getThing()
|
88
|
+
* ```
|
154
89
|
*
|
155
90
|
* @param resolver
|
156
|
-
*
|
91
|
+
* A function that returns a value of a particular type
|
92
|
+
* with a resolution context being passed to it.
|
157
93
|
*
|
158
|
-
* @returns The transient provider
|
94
|
+
* @returns The transient provider.
|
159
95
|
*/
|
160
96
|
declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
|
161
97
|
/**
|
162
|
-
* Creates a
|
163
|
-
*
|
164
|
-
*
|
98
|
+
* Creates a singleton provider that will resolve an instance once
|
99
|
+
* and return it on every call.
|
100
|
+
*
|
101
|
+
* @example
|
102
|
+
* ```ts
|
103
|
+
* const getThing = singleton(() => createThing())
|
104
|
+
* getThing() === getThing()
|
105
|
+
* ```
|
165
106
|
*
|
166
107
|
* @param resolver
|
167
|
-
*
|
108
|
+
* A function that returns a value of a particular type
|
109
|
+
* with a resolution context being passed to it.
|
168
110
|
*
|
169
|
-
* @returns The singleton provider
|
111
|
+
* @returns The singleton provider.
|
170
112
|
*/
|
171
113
|
declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
|
172
114
|
/**
|
173
|
-
* Creates a
|
174
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution
|
175
|
-
* that will take its resolution from a provided scope
|
115
|
+
* Creates a scoped provider that will take its resolution from a passed scope
|
176
116
|
* or create a new one and save it if there is none.
|
177
|
-
* If no scope is passed, it will create a new instance on each
|
117
|
+
* If no scope is passed, it will create a new instance on each call.
|
118
|
+
*
|
119
|
+
* @example
|
120
|
+
* ```ts
|
121
|
+
* const getThing = scoped(() => createThing())
|
122
|
+
* getThing() !== getThing()
|
123
|
+
* ```
|
124
|
+
*
|
125
|
+
* @example
|
126
|
+
* ```ts
|
127
|
+
* const getThing = scoped(() => createThing())
|
128
|
+
* const scope = createScope()
|
129
|
+
* getThing({ scope }) === getThing({ scope })
|
130
|
+
* ```
|
178
131
|
*
|
179
132
|
* @param resolver
|
180
|
-
*
|
133
|
+
* A function that returns a value of a particular type
|
134
|
+
* with a resolution context being passed to it.
|
181
135
|
*
|
182
|
-
* @returns The scoped provider
|
136
|
+
* @returns The scoped provider.
|
183
137
|
*/
|
184
138
|
declare const scoped: <T>(resolver: Resolver<T>) => Provider<T>;
|
185
139
|
|
@@ -193,31 +147,111 @@ type InferProviderCollectionResolutions<Providers extends ProviderList | Provide
|
|
193
147
|
* if there'ss at least one `Promise` in the collection,
|
194
148
|
* otherwise returns an untouched type.
|
195
149
|
*/
|
196
|
-
type
|
150
|
+
type AwaitValuesInCollection<T extends any[] | Record<any, any>> = Promise<any> extends T[keyof T] ? Promise<{
|
197
151
|
[I in keyof T]: T[I] extends Promise<infer T> ? T : T[I];
|
198
152
|
}> : T;
|
199
153
|
/**
|
200
154
|
* Calls every provider in a list with a provided resolution context
|
201
|
-
* and returns a list of resolutions. Returns a
|
202
|
-
* of awaited resolutions if there's at least one
|
155
|
+
* and returns a list of resolutions. Returns a `Promise` of a list
|
156
|
+
* of awaited resolutions if there's at least one `Promise` in the resolutions.
|
157
|
+
*
|
158
|
+
* @example
|
159
|
+
* Only sync providers:
|
160
|
+
* ```ts
|
161
|
+
* const getA = scoped(() => createA())
|
162
|
+
* const getB = scoped(() => createB())
|
163
|
+
* const getC = scoped(() => createC())
|
164
|
+
*
|
165
|
+
* const scope = createScope()
|
166
|
+
* const resolutions = resolveList(
|
167
|
+
* [getA, getB, getC],
|
168
|
+
* { scope }
|
169
|
+
* )
|
170
|
+
*
|
171
|
+
* resolutions == [
|
172
|
+
* getA({ scope }),
|
173
|
+
* getB({ scope }),
|
174
|
+
* getC({ scope })
|
175
|
+
* ]
|
176
|
+
* ```
|
177
|
+
*
|
178
|
+
* @example
|
179
|
+
* Some provider is async:
|
180
|
+
* ```ts
|
181
|
+
* const getA = scoped(() => createA())
|
182
|
+
* const getB = scoped(async () => await createB())
|
183
|
+
* const getC = scoped(() => createC())
|
184
|
+
*
|
185
|
+
* const scope = createScope()
|
186
|
+
* const resolutions = resolveList(
|
187
|
+
* [getA, getB, getC],
|
188
|
+
* { scope }
|
189
|
+
* )
|
190
|
+
*
|
191
|
+
* resolutions == [
|
192
|
+
* getA({ scope }),
|
193
|
+
* await getB({ scope }),
|
194
|
+
* getC({ scope })
|
195
|
+
* ]
|
196
|
+
* ```
|
203
197
|
*
|
204
198
|
* @param providers - The list of providers.
|
205
199
|
* @param context - The resolution context.
|
206
200
|
*
|
207
201
|
* @returns The list of resolutions.
|
208
202
|
*/
|
209
|
-
declare const resolveList: <const Providers extends ProviderList>(providers: Providers, context?: ResolutionContext) =>
|
203
|
+
declare const resolveList: <const Providers extends ProviderList>(providers: Providers, context?: ResolutionContext) => AwaitValuesInCollection<InferProviderCollectionResolutions<Providers>>;
|
210
204
|
/**
|
211
205
|
* Calls every provider in a map with a provided resolution context
|
212
206
|
* and returns a map with identical keys but with resolutions in values instead.
|
213
|
-
* Returns a
|
214
|
-
*
|
207
|
+
* Returns a `Promise` of a map of awaited resolutions if there's at least one
|
208
|
+
* `Promise` in the resolutions.
|
209
|
+
*
|
210
|
+
* @example
|
211
|
+
* Only sync providers:
|
212
|
+
* ```ts
|
213
|
+
* const getA = scoped(() => createA())
|
214
|
+
* const getB = scoped(() => createB())
|
215
|
+
* const getC = scoped(() => createC())
|
216
|
+
*
|
217
|
+
* const scope = createScope()
|
218
|
+
* const resolutions = resolveMap(
|
219
|
+
* { a: getA, b: getB, c: getC },
|
220
|
+
* { scope }
|
221
|
+
* )
|
222
|
+
*
|
223
|
+
* resolutions == {
|
224
|
+
* a: getA({ scope }),
|
225
|
+
* b: getB({ scope }),
|
226
|
+
* c: getC({ scope })
|
227
|
+
* }
|
228
|
+
* ```
|
229
|
+
*
|
230
|
+
* @example
|
231
|
+
* Some provider is async:
|
232
|
+
* ```ts
|
233
|
+
* const getA = scoped(() => createA())
|
234
|
+
* const getB = scoped(async () => await createB())
|
235
|
+
* const getC = scoped(() => createC())
|
236
|
+
*
|
237
|
+
* const scope = createScope()
|
238
|
+
* const resolutions = await resolveMap(
|
239
|
+
* { a: getA, b: getB, c: getC },
|
240
|
+
* { scope }
|
241
|
+
* )
|
242
|
+
*
|
243
|
+
* resolutions == {
|
244
|
+
* a: getA({ scope }),
|
245
|
+
* b: await getB({ scope }),
|
246
|
+
* c: getC({ scope })
|
247
|
+
* }
|
248
|
+
* ```
|
215
249
|
*
|
216
250
|
* @param providers - The map of providers.
|
217
251
|
* @param context - The resolution context.
|
218
252
|
*
|
219
253
|
* @returns The map of resolutions.
|
220
254
|
*/
|
221
|
-
declare const resolveMap: <const Providers extends ProviderRecord>(providers: Providers, context?: ResolutionContext) =>
|
255
|
+
declare const resolveMap: <const Providers extends ProviderRecord>(providers: Providers, context?: ResolutionContext) => AwaitValuesInCollection<InferProviderCollectionResolutions<Providers>>;
|
222
256
|
|
223
|
-
export { type
|
257
|
+
export { type MockMap, type Provider, type ResolutionContext, type Resolver, type Scope, createMockMap, createScope, resolveList, resolveMap, scoped, singleton, transient };
|