atomic-di 0.9.1-beta.4 → 1.0.0-rc.1

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -1,222 +1,139 @@
1
1
  /**
2
- * A map of providers to providers of the same type.
3
- * Lifetime is not a part of `Provider` type, so you can use
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<Provider<any>, Provider<any>>, "set" | "get"> & {
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: Provider<T>, mock: Provider<T>): MockMap;
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: Provider<T>): Provider<T> | undefined;
19
+ get<T>(provider: Resolver<T>): Resolver<T> | undefined;
34
20
  };
35
21
  /**
36
- * Creates a mock map instance,
37
- * a map of providers to providers of the same type.
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
- * mocks.set(otherProvider, otherProviderMock)
29
+ * .set(getConfig, getTestConfig)
51
30
  *
52
- * provider({ mocks })
31
+ * getThing({ mocks })
53
32
  * ```
54
33
  *
55
- * @returns The mock map instance.
34
+ * @returns The map instance.
56
35
  */
57
36
  declare const createMockMap: () => MockMap;
58
37
 
59
38
  /**
60
- * A map of providers to their instances.
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<Provider<any>, any>;
43
+ type Scope = Map<Resolver<any>, any>;
70
44
  /**
71
- * Creates a scope instance.
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 scope = createScope()
77
- * provider({ scope })
51
+ * const requestScope = createScope()
52
+ *
53
+ * app.use(() => {
54
+ * const db = getDb({ scope: requestScope })
55
+ * // ...
56
+ * })
78
57
  * ```
79
58
  *
80
- * @returns The scope instance.
59
+ * @returns The map instance.
81
60
  */
82
61
  declare const createScope: () => Scope;
83
62
 
84
63
  /**
85
- * A wrapper around the resolver(factory) for contextual dependency resolution.
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 Lifetime = "transient" | "singleton" | "scoped";
67
+ type Resolver<T> = (context?: ResolutionContext) => T;
117
68
  /**
118
- * A function that creates an instance using a resolution context.
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 Resolver<T> = (context?: ResolutionContext) => T;
72
+ type Provider<T> = Resolver<T>;
121
73
  /**
122
- * An object that holds information about a scope and provider mocks.
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
- * @param resolver
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 getInstance = transient((context) =>
169
- * createInstance(...)
170
- * )
171
- *
172
- * getInstance() !== getInstance()
86
+ * const getThing = transient(() => createThing())
87
+ * getThing() !== getThing()
173
88
  * ```
174
89
  *
175
90
  * @param resolver
176
- * The function that creates an instance using a resolution context.
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 instance.
94
+ * @returns The transient provider.
179
95
  */
180
96
  declare const transient: <T>(resolver: Resolver<T>) => Provider<T>;
181
97
  /**
182
- * An alias for `provide("singleton", ...)`.
183
- * Creates a transient provider instance,
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
- * getInstance() === getInstance()
101
+ * @example
102
+ * ```ts
103
+ * const getThing = singleton(() => createThing())
104
+ * getThing() === getThing()
192
105
  * ```
193
106
  *
194
107
  * @param resolver
195
- * The function that creates an instance using a resolution context.
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 instance.
111
+ * @returns The singleton provider.
198
112
  */
199
113
  declare const singleton: <T>(resolver: Resolver<T>) => Provider<T>;
200
114
  /**
201
- * An alias for `provide("scoped", ...)`.
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 request.
117
+ * If no scope is passed, it will create a new instance on each call.
118
+ *
119
+ * @example
207
120
  * ```ts
208
- * const getInstance = scoped((context) =>
209
- * createInstance(...)
210
- * )
121
+ * const getThing = scoped(() => createThing())
122
+ * getThing() !== getThing()
123
+ * ```
211
124
  *
212
- * getInstance() !== getInstance()
213
- * getInstance({ scope }) === getInstance({ scope })
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
- * The function that creates an instance using a resolution context.
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 instance.
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 AwaitAllValuesInCollection<T extends any[] | Record<any, any>> = Promise<any> extends T[keyof T] ? Promise<{
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 promise of a list
239
- * of awaited resolutions if there's at least one promise in the resolutions.
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, mocks }
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) => AwaitAllValuesInCollection<InferProviderCollectionResolutions<Providers>>;
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 promise of a map of awaited resolutions if there's at least one
257
- * promise in the resolutions.
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 resolutionMap = resolveMap(
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, mocks }
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) => AwaitAllValuesInCollection<InferProviderCollectionResolutions<Providers>>;
255
+ declare const resolveMap: <const Providers extends ProviderRecord>(providers: Providers, context?: ResolutionContext) => AwaitValuesInCollection<InferProviderCollectionResolutions<Providers>>;
271
256
 
272
- export { type Lifetime, type MockMap, type Provider, type ResolutionContext, type Resolver, type Scope, createMockMap, createScope, provide, resolveList, resolveMap, scoped, singleton, transient };
257
+ export { type MockMap, type Provider, type ResolutionContext, type Resolver, type Scope, createMockMap, createScope, resolveList, resolveMap, scoped, singleton, transient };