atomic-di 0.9.1-beta.4 → 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 +144 -159
- package/dist/index.d.ts +144 -159
- 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,222 +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 an **optional** resolution context that will be propagated
|
89
|
-
* throughout a dependency tree.
|
90
|
-
* ```ts
|
91
|
-
* provider({ scope, mocks })
|
92
|
-
* ```
|
93
|
-
*
|
94
|
-
* When passing a scope it will try to get an instance from it
|
95
|
-
* or create a new one and put it there.
|
96
|
-
*
|
97
|
-
* When passing mocks, it will try to get its own mock version,
|
98
|
-
* and if there is one, it will use it instead of itself.
|
99
|
-
*/
|
100
|
-
type Provider<T> = (context?: ResolutionContext) => T;
|
101
|
-
/**
|
102
|
-
* A resolution lifetime.
|
103
|
-
*
|
104
|
-
* Passed when creating a provider to determine its behavior.
|
105
|
-
*
|
106
|
-
* `"transient"` doesn't provide any modifications to a resolver behaviour,
|
107
|
-
* so the resolver will create a new instance on each request.
|
108
|
-
*
|
109
|
-
* `"singleton"` forces the resolver to create an instance once
|
110
|
-
* and return it in subsequent requests.
|
111
|
-
*
|
112
|
-
* `"scoped"` forces the resolver to take its instance from a provided scope
|
113
|
-
* or create a new one and save it if there is none.
|
114
|
-
* 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.
|
115
66
|
*/
|
116
|
-
type
|
67
|
+
type Resolver<T> = (context?: ResolutionContext) => T;
|
117
68
|
/**
|
118
|
-
* 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.
|
119
71
|
*/
|
120
|
-
type
|
72
|
+
type Provider<T> = Resolver<T>;
|
121
73
|
/**
|
122
|
-
*
|
123
|
-
*
|
124
|
-
* 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.
|
125
76
|
*/
|
126
77
|
type ResolutionContext = {
|
127
78
|
scope?: Scope;
|
128
79
|
mocks?: MockMap;
|
129
80
|
};
|
130
81
|
/**
|
131
|
-
* Creates a provider instance
|
132
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution.
|
133
|
-
* ```ts
|
134
|
-
* const getInstance = provide("transient", () =>
|
135
|
-
* createInstance(
|
136
|
-
* getOtherInstance(context)
|
137
|
-
* )
|
138
|
-
* )
|
139
|
-
* ```
|
140
|
-
*
|
141
|
-
* @param lifetime
|
142
|
-
* A resolution lifetime.
|
143
|
-
*
|
144
|
-
* `"transient"` doesn't provide any modifications to a resolver behaviour,
|
145
|
-
* so the resolver will create a new instance on each request.
|
146
|
-
*
|
147
|
-
* `"singleton"` forces the resolver to create an instance once
|
148
|
-
* and return it in subsequent requests.
|
149
|
-
*
|
150
|
-
* `"scoped"` forces the resolver to take its resolution from a provided scope
|
151
|
-
* or create a new one and save it if there is none.
|
152
|
-
* If no scope is passed, it will create a new instance on each request.
|
82
|
+
* Creates a transient provider that will resolve a new instance on each call.
|
153
83
|
*
|
154
|
-
* @
|
155
|
-
* The function that creates an instance using a resolution context.
|
156
|
-
* If the function calls other providers,
|
157
|
-
* the context **must** be passed to their calls.
|
158
|
-
*
|
159
|
-
* @returns The provider instance.
|
160
|
-
*/
|
161
|
-
declare const provide: <T>(lifetime: Lifetime, resolver: Resolver<T>) => Provider<T>;
|
162
|
-
/**
|
163
|
-
* An alias for `provide("transient", ...)`.
|
164
|
-
* Creates a transient provider instance,
|
165
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution
|
166
|
-
* that will create a new instance on each request.
|
84
|
+
* @example
|
167
85
|
* ```ts
|
168
|
-
* const
|
169
|
-
*
|
170
|
-
* )
|
171
|
-
*
|
172
|
-
* getInstance() !== getInstance()
|
86
|
+
* const getThing = transient(() => createThing())
|
87
|
+
* getThing() !== getThing()
|
173
88
|
* ```
|
174
89
|
*
|
175
90
|
* @param resolver
|
176
|
-
*
|
91
|
+
* A function that returns a value of a particular type
|
92
|
+
* with a resolution context being passed to it.
|
177
93
|
*
|
178
|
-
* @returns The transient provider
|
94
|
+
* @returns The transient provider.
|
179
95
|
*/
|
180
96
|
declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
|
181
97
|
/**
|
182
|
-
*
|
183
|
-
*
|
184
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution
|
185
|
-
* that will create an instance once and return it in subsequent requests.
|
186
|
-
* ```ts
|
187
|
-
* const getInstance = singleton((context) =>
|
188
|
-
* createInstance(...)
|
189
|
-
* )
|
98
|
+
* Creates a singleton provider that will resolve an instance once
|
99
|
+
* and return it on every call.
|
190
100
|
*
|
191
|
-
*
|
101
|
+
* @example
|
102
|
+
* ```ts
|
103
|
+
* const getThing = singleton(() => createThing())
|
104
|
+
* getThing() === getThing()
|
192
105
|
* ```
|
193
106
|
*
|
194
107
|
* @param resolver
|
195
|
-
*
|
108
|
+
* A function that returns a value of a particular type
|
109
|
+
* with a resolution context being passed to it.
|
196
110
|
*
|
197
|
-
* @returns The singleton provider
|
111
|
+
* @returns The singleton provider.
|
198
112
|
*/
|
199
113
|
declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
|
200
114
|
/**
|
201
|
-
*
|
202
|
-
* Creates a transient provider instance,
|
203
|
-
* a wrapper around a resolver(factory) for contextual dependency resolution
|
204
|
-
* that will take its resolution from a provided scope
|
115
|
+
* Creates a scoped provider that will take its resolution from a passed scope
|
205
116
|
* or create a new one and save it if there is none.
|
206
|
-
* 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
|
207
120
|
* ```ts
|
208
|
-
* const
|
209
|
-
*
|
210
|
-
*
|
121
|
+
* const getThing = scoped(() => createThing())
|
122
|
+
* getThing() !== getThing()
|
123
|
+
* ```
|
211
124
|
*
|
212
|
-
*
|
213
|
-
*
|
125
|
+
* @example
|
126
|
+
* ```ts
|
127
|
+
* const getThing = scoped(() => createThing())
|
128
|
+
* const scope = createScope()
|
129
|
+
* getThing({ scope }) === getThing({ scope })
|
214
130
|
* ```
|
215
131
|
*
|
216
132
|
* @param resolver
|
217
|
-
*
|
133
|
+
* A function that returns a value of a particular type
|
134
|
+
* with a resolution context being passed to it.
|
218
135
|
*
|
219
|
-
* @returns The scoped provider
|
136
|
+
* @returns The scoped provider.
|
220
137
|
*/
|
221
138
|
declare const scoped: <T>(resolver: Resolver<T>) => Provider<T>;
|
222
139
|
|
@@ -230,18 +147,52 @@ type InferProviderCollectionResolutions<Providers extends ProviderList | Provide
|
|
230
147
|
* if there'ss at least one `Promise` in the collection,
|
231
148
|
* otherwise returns an untouched type.
|
232
149
|
*/
|
233
|
-
type
|
150
|
+
type AwaitValuesInCollection<T extends any[] | Record<any, any>> = Promise<any> extends T[keyof T] ? Promise<{
|
234
151
|
[I in keyof T]: T[I] extends Promise<infer T> ? T : T[I];
|
235
152
|
}> : T;
|
236
153
|
/**
|
237
154
|
* Calls every provider in a list with a provided resolution context
|
238
|
-
* and returns a list of resolutions. Returns a
|
239
|
-
* 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:
|
240
160
|
* ```ts
|
161
|
+
* const getA = scoped(() => createA())
|
162
|
+
* const getB = scoped(() => createB())
|
163
|
+
* const getC = scoped(() => createC())
|
164
|
+
*
|
165
|
+
* const scope = createScope()
|
241
166
|
* const resolutions = resolveList(
|
242
167
|
* [getA, getB, getC],
|
243
|
-
* { scope
|
168
|
+
* { scope }
|
244
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
|
+
* ]
|
245
196
|
* ```
|
246
197
|
*
|
247
198
|
* @param providers - The list of providers.
|
@@ -249,17 +200,51 @@ type AwaitAllValuesInCollection<T extends any[] | Record<any, any>> = Promise<an
|
|
249
200
|
*
|
250
201
|
* @returns The list of resolutions.
|
251
202
|
*/
|
252
|
-
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>>;
|
253
204
|
/**
|
254
205
|
* Calls every provider in a map with a provided resolution context
|
255
206
|
* and returns a map with identical keys but with resolutions in values instead.
|
256
|
-
* Returns a
|
257
|
-
*
|
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:
|
258
212
|
* ```ts
|
259
|
-
* const
|
213
|
+
* const getA = scoped(() => createA())
|
214
|
+
* const getB = scoped(() => createB())
|
215
|
+
* const getC = scoped(() => createC())
|
216
|
+
*
|
217
|
+
* const scope = createScope()
|
218
|
+
* const resolutions = resolveMap(
|
260
219
|
* { a: getA, b: getB, c: getC },
|
261
|
-
* { scope
|
220
|
+
* { scope }
|
262
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
|
+
* }
|
263
248
|
* ```
|
264
249
|
*
|
265
250
|
* @param providers - The map of providers.
|
@@ -267,6 +252,6 @@ declare const resolveList: <const Providers extends ProviderList>(providers: Pro
|
|
267
252
|
*
|
268
253
|
* @returns The map of resolutions.
|
269
254
|
*/
|
270
|
-
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>>;
|
271
256
|
|
272
|
-
export { type
|
257
|
+
export { type MockMap, type Provider, type ResolutionContext, type Resolver, type Scope, createMockMap, createScope, resolveList, resolveMap, scoped, singleton, transient };
|