@tanstack/angular-query-experimental 5.34.2 → 5.35.0

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.
Files changed (59) hide show
  1. package/build/rollup.d.ts +591 -0
  2. package/package.json +9 -5
  3. package/build/create-base-query.d.ts +0 -6
  4. package/build/index.d.ts +0 -14
  5. package/build/infinite-query-options.d.ts +0 -34
  6. package/build/inject-infinite-query.d.ts +0 -31
  7. package/build/inject-is-fetching.d.ts +0 -13
  8. package/build/inject-is-mutating.d.ts +0 -12
  9. package/build/inject-mutation-state.d.ts +0 -21
  10. package/build/inject-mutation.d.ts +0 -13
  11. package/build/inject-queries.d.ts +0 -76
  12. package/build/inject-query-client.d.ts +0 -30
  13. package/build/inject-query.d.ts +0 -106
  14. package/build/providers.d.ts +0 -42
  15. package/build/query-options.d.ts +0 -66
  16. package/build/signal-proxy.d.ts +0 -11
  17. package/build/types.d.ts +0 -95
  18. package/build/util/assert-injector/assert-injector.d.ts +0 -54
  19. package/build/util/create-injection-token/create-injection-token.d.ts +0 -52
  20. package/build/util/index.d.ts +0 -2
  21. package/build/util/lazy-init/lazy-init.d.ts +0 -1
  22. package/build/util/lazy-signal-initializer/lazy-signal-initializer.d.ts +0 -4
  23. package/src/__tests__/inject-infinite-query.test.ts +0 -64
  24. package/src/__tests__/inject-is-fetching.test.ts +0 -35
  25. package/src/__tests__/inject-is-mutating.test.ts +0 -39
  26. package/src/__tests__/inject-mutation-state.test-d.ts +0 -22
  27. package/src/__tests__/inject-mutation-state.test.ts +0 -175
  28. package/src/__tests__/inject-mutation.test-d.ts +0 -71
  29. package/src/__tests__/inject-mutation.test.ts +0 -458
  30. package/src/__tests__/inject-query.test-d.ts +0 -59
  31. package/src/__tests__/inject-query.test.ts +0 -349
  32. package/src/__tests__/query-options.test-d.ts +0 -127
  33. package/src/__tests__/signal-proxy.test.ts +0 -27
  34. package/src/__tests__/test-utils.ts +0 -131
  35. package/src/__tests__/util/lazy-init/lazy-init.test.ts +0 -126
  36. package/src/__tests__/util/lazy-signal-initializer/lazy-signal-initializer.test.ts +0 -130
  37. package/src/create-base-query.ts +0 -116
  38. package/src/index.ts +0 -24
  39. package/src/infinite-query-options.ts +0 -118
  40. package/src/inject-infinite-query.ts +0 -125
  41. package/src/inject-is-fetching.ts +0 -49
  42. package/src/inject-is-mutating.ts +0 -48
  43. package/src/inject-mutation-state.ts +0 -107
  44. package/src/inject-mutation.ts +0 -118
  45. package/src/inject-queries.ts +0 -265
  46. package/src/inject-query-client.ts +0 -25
  47. package/src/inject-query.ts +0 -200
  48. package/src/providers.ts +0 -65
  49. package/src/query-options.ts +0 -122
  50. package/src/signal-proxy.ts +0 -46
  51. package/src/test-setup.ts +0 -12
  52. package/src/types.ts +0 -311
  53. package/src/util/assert-injector/assert-injector.test.ts +0 -74
  54. package/src/util/assert-injector/assert-injector.ts +0 -81
  55. package/src/util/create-injection-token/create-injection-token.test.ts +0 -32
  56. package/src/util/create-injection-token/create-injection-token.ts +0 -185
  57. package/src/util/index.ts +0 -13
  58. package/src/util/lazy-init/lazy-init.ts +0 -34
  59. package/src/util/lazy-signal-initializer/lazy-signal-initializer.ts +0 -28
package/src/types.ts DELETED
@@ -1,311 +0,0 @@
1
- /* istanbul ignore file */
2
-
3
- import type {
4
- DefaultError,
5
- DefinedInfiniteQueryObserverResult,
6
- DefinedQueryObserverResult,
7
- InfiniteQueryObserverOptions,
8
- InfiniteQueryObserverResult,
9
- MutateFunction,
10
- MutationObserverOptions,
11
- MutationObserverResult,
12
- OmitKeyof,
13
- QueryKey,
14
- QueryObserverOptions,
15
- QueryObserverResult,
16
- } from '@tanstack/query-core'
17
- import type { MapToSignals } from './signal-proxy'
18
-
19
- /**
20
- * @public
21
- */
22
- export interface CreateBaseQueryOptions<
23
- TQueryFnData = unknown,
24
- TError = DefaultError,
25
- TData = TQueryFnData,
26
- TQueryData = TQueryFnData,
27
- TQueryKey extends QueryKey = QueryKey,
28
- > extends QueryObserverOptions<
29
- TQueryFnData,
30
- TError,
31
- TData,
32
- TQueryData,
33
- TQueryKey
34
- > {}
35
-
36
- /**
37
- * @public
38
- */
39
- export interface CreateQueryOptions<
40
- TQueryFnData = unknown,
41
- TError = DefaultError,
42
- TData = TQueryFnData,
43
- TQueryKey extends QueryKey = QueryKey,
44
- > extends OmitKeyof<
45
- CreateBaseQueryOptions<
46
- TQueryFnData,
47
- TError,
48
- TData,
49
- TQueryFnData,
50
- TQueryKey
51
- >,
52
- 'suspense'
53
- > {}
54
-
55
- /**
56
- * @public
57
- */
58
- type CreateStatusBasedQueryResult<
59
- TStatus extends QueryObserverResult['status'],
60
- TData = unknown,
61
- TError = DefaultError,
62
- > = Extract<QueryObserverResult<TData, TError>, { status: TStatus }>
63
-
64
- /**
65
- * @public
66
- */
67
- export interface BaseQueryNarrowing<TData = unknown, TError = DefaultError> {
68
- isSuccess: (
69
- this: CreateBaseQueryResult<TData, TError>,
70
- ) => this is CreateBaseQueryResult<
71
- TData,
72
- TError,
73
- CreateStatusBasedQueryResult<'success', TData, TError>
74
- >
75
- isError: (
76
- this: CreateBaseQueryResult<TData, TError>,
77
- ) => this is CreateBaseQueryResult<
78
- TData,
79
- TError,
80
- CreateStatusBasedQueryResult<'error', TData, TError>
81
- >
82
- isPending: (
83
- this: CreateBaseQueryResult<TData, TError>,
84
- ) => this is CreateBaseQueryResult<
85
- TData,
86
- TError,
87
- CreateStatusBasedQueryResult<'pending', TData, TError>
88
- >
89
- }
90
-
91
- /**
92
- * @public
93
- */
94
- export interface CreateInfiniteQueryOptions<
95
- TQueryFnData = unknown,
96
- TError = DefaultError,
97
- TData = TQueryFnData,
98
- TQueryData = TQueryFnData,
99
- TQueryKey extends QueryKey = QueryKey,
100
- TPageParam = unknown,
101
- > extends OmitKeyof<
102
- InfiniteQueryObserverOptions<
103
- TQueryFnData,
104
- TError,
105
- TData,
106
- TQueryData,
107
- TQueryKey,
108
- TPageParam
109
- >,
110
- 'suspense'
111
- > {}
112
-
113
- /**
114
- * @public
115
- */
116
- export type CreateBaseQueryResult<
117
- TData = unknown,
118
- TError = DefaultError,
119
- TState = QueryObserverResult<TData, TError>,
120
- > = BaseQueryNarrowing<TData, TError> &
121
- MapToSignals<OmitKeyof<TState, keyof BaseQueryNarrowing, 'safely'>>
122
-
123
- /**
124
- * @public
125
- */
126
- export type CreateQueryResult<
127
- TData = unknown,
128
- TError = DefaultError,
129
- > = CreateBaseQueryResult<TData, TError>
130
-
131
- /**
132
- * @public
133
- */
134
- export type DefinedCreateQueryResult<
135
- TData = unknown,
136
- TError = DefaultError,
137
- TDefinedQueryObserver = DefinedQueryObserverResult<TData, TError>,
138
- > = MapToSignals<TDefinedQueryObserver>
139
-
140
- /**
141
- * @public
142
- */
143
- export type CreateInfiniteQueryResult<
144
- TData = unknown,
145
- TError = DefaultError,
146
- > = MapToSignals<InfiniteQueryObserverResult<TData, TError>>
147
-
148
- /**
149
- * @public
150
- */
151
- export type DefinedCreateInfiniteQueryResult<
152
- TData = unknown,
153
- TError = DefaultError,
154
- TDefinedInfiniteQueryObserver = DefinedInfiniteQueryObserverResult<
155
- TData,
156
- TError
157
- >,
158
- > = MapToSignals<TDefinedInfiniteQueryObserver>
159
-
160
- /**
161
- * @public
162
- */
163
- export interface CreateMutationOptions<
164
- TData = unknown,
165
- TError = DefaultError,
166
- TVariables = void,
167
- TContext = unknown,
168
- > extends OmitKeyof<
169
- MutationObserverOptions<TData, TError, TVariables, TContext>,
170
- '_defaulted'
171
- > {}
172
-
173
- /**
174
- * @public
175
- */
176
- export type CreateMutateFunction<
177
- TData = unknown,
178
- TError = DefaultError,
179
- TVariables = void,
180
- TContext = unknown,
181
- > = (
182
- ...args: Parameters<MutateFunction<TData, TError, TVariables, TContext>>
183
- ) => void
184
-
185
- /**
186
- * @public
187
- */
188
- export type CreateMutateAsyncFunction<
189
- TData = unknown,
190
- TError = DefaultError,
191
- TVariables = void,
192
- TContext = unknown,
193
- > = MutateFunction<TData, TError, TVariables, TContext>
194
-
195
- /**
196
- * @public
197
- */
198
- export type CreateBaseMutationResult<
199
- TData = unknown,
200
- TError = DefaultError,
201
- TVariables = unknown,
202
- TContext = unknown,
203
- > = Override<
204
- MutationObserverResult<TData, TError, TVariables, TContext>,
205
- { mutate: CreateMutateFunction<TData, TError, TVariables, TContext> }
206
- > & {
207
- mutateAsync: CreateMutateAsyncFunction<TData, TError, TVariables, TContext>
208
- }
209
-
210
- /**
211
- * @public
212
- */
213
- type CreateStatusBasedMutationResult<
214
- TStatus extends CreateBaseMutationResult['status'],
215
- TData = unknown,
216
- TError = DefaultError,
217
- TVariables = unknown,
218
- TContext = unknown,
219
- > = Extract<
220
- CreateBaseMutationResult<TData, TError, TVariables, TContext>,
221
- { status: TStatus }
222
- >
223
-
224
- /**
225
- * @public
226
- */
227
- export interface BaseMutationNarrowing<
228
- TData = unknown,
229
- TError = DefaultError,
230
- TVariables = unknown,
231
- TContext = unknown,
232
- > {
233
- isSuccess: (
234
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
235
- ) => this is CreateMutationResult<
236
- TData,
237
- TError,
238
- TVariables,
239
- TContext,
240
- CreateStatusBasedMutationResult<
241
- 'success',
242
- TData,
243
- TError,
244
- TVariables,
245
- TContext
246
- >
247
- >
248
- isError: (
249
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
250
- ) => this is CreateMutationResult<
251
- TData,
252
- TError,
253
- TVariables,
254
- TContext,
255
- CreateStatusBasedMutationResult<
256
- 'error',
257
- TData,
258
- TError,
259
- TVariables,
260
- TContext
261
- >
262
- >
263
- isPending: (
264
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
265
- ) => this is CreateMutationResult<
266
- TData,
267
- TError,
268
- TVariables,
269
- TContext,
270
- CreateStatusBasedMutationResult<
271
- 'pending',
272
- TData,
273
- TError,
274
- TVariables,
275
- TContext
276
- >
277
- >
278
- isIdle: (
279
- this: CreateMutationResult<TData, TError, TVariables, TContext>,
280
- ) => this is CreateMutationResult<
281
- TData,
282
- TError,
283
- TVariables,
284
- TContext,
285
- CreateStatusBasedMutationResult<'idle', TData, TError, TVariables, TContext>
286
- >
287
- }
288
-
289
- /**
290
- * @public
291
- */
292
- export type CreateMutationResult<
293
- TData = unknown,
294
- TError = DefaultError,
295
- TVariables = unknown,
296
- TContext = unknown,
297
- TState = CreateStatusBasedMutationResult<
298
- CreateBaseMutationResult['status'],
299
- TData,
300
- TError,
301
- TVariables,
302
- TContext
303
- >,
304
- > = BaseMutationNarrowing<TData, TError, TVariables, TContext> &
305
- MapToSignals<OmitKeyof<TState, keyof BaseMutationNarrowing, 'safely'>>
306
-
307
- type Override<TTargetA, TTargetB> = {
308
- [AKey in keyof TTargetA]: AKey extends keyof TTargetB
309
- ? TTargetB[AKey]
310
- : TTargetA[AKey]
311
- }
@@ -1,74 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import {
15
- InjectionToken,
16
- Injector,
17
- inject,
18
- runInInjectionContext,
19
- } from '@angular/core'
20
- import { TestBed } from '@angular/core/testing'
21
- import { assertInjector } from './assert-injector'
22
-
23
- describe(assertInjector.name, () => {
24
- const token = new InjectionToken('token', {
25
- factory: () => 1,
26
- })
27
-
28
- function injectDummy(injector?: Injector) {
29
- injector = assertInjector(injectDummy, injector)
30
- return runInInjectionContext(injector, () => inject(token))
31
- }
32
-
33
- function injectDummyTwo(injector?: Injector) {
34
- return assertInjector(injectDummyTwo, injector, () => inject(token) + 1)
35
- }
36
-
37
- it('given no custom injector, when run in injection context, then return value', () => {
38
- TestBed.runInInjectionContext(() => {
39
- const value = injectDummy()
40
- const valueTwo = injectDummyTwo()
41
- expect(value).toEqual(1)
42
- expect(valueTwo).toEqual(2)
43
- })
44
- })
45
-
46
- it('given no custom injector, when run outside injection context, then throw', () => {
47
- expect(() => injectDummy()).toThrowError(
48
- /injectDummy\(\) can only be used within an injection context/i,
49
- )
50
- expect(() => injectDummyTwo()).toThrowError(
51
- /injectDummyTwo\(\) can only be used within an injection context/i,
52
- )
53
- })
54
-
55
- it('given a custom injector, when run in that injector context without providing number, then throw', () => {
56
- expect(() => injectDummy(Injector.create({ providers: [] }))).toThrowError(
57
- /No provider for InjectionToken/i,
58
- )
59
- expect(() =>
60
- injectDummyTwo(Injector.create({ providers: [] })),
61
- ).toThrowError(/No provider for InjectionToken/i)
62
- })
63
-
64
- it('given a custom injector, when run in that injector context and providing number, then return value', () => {
65
- const value = injectDummy(
66
- Injector.create({ providers: [{ provide: token, useValue: 2 }] }),
67
- )
68
- const valueTwo = injectDummyTwo(
69
- Injector.create({ providers: [{ provide: token, useValue: 2 }] }),
70
- )
71
- expect(value).toEqual(2)
72
- expect(valueTwo).toEqual(3)
73
- })
74
- })
@@ -1,81 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import {
15
- Injector,
16
- assertInInjectionContext,
17
- inject,
18
- runInInjectionContext,
19
- } from '@angular/core'
20
-
21
- /**
22
- * `assertInjector` extends `assertInInjectionContext` with an optional `Injector`
23
- * After assertion, `assertInjector` runs the `runner` function with the guaranteed `Injector`
24
- * whether it is the default `Injector` within the current **Injection Context**
25
- * or the custom `Injector` that was passed in.
26
- *
27
- * @template {() => any} Runner - Runner is a function that can return anything
28
- * @param {Function} fn - the Function to pass in `assertInInjectionContext`
29
- * @param {Injector | undefined | null} injector - the optional "custom" Injector
30
- * @param {Runner} runner - the runner fn
31
- * @returns {ReturnType<Runner>} result - returns the result of the Runner
32
- *
33
- * @example
34
- * ```ts
35
- * function injectValue(injector?: Injector) {
36
- * return assertInjector(injectValue, injector, () => 'value');
37
- * }
38
- *
39
- * injectValue(); // string
40
- * ```
41
- */
42
- export function assertInjector<TRunner extends () => any>(
43
- fn: Function,
44
- injector: Injector | undefined | null,
45
- runner: TRunner,
46
- ): ReturnType<TRunner>
47
- /**
48
- * `assertInjector` extends `assertInInjectionContext` with an optional `Injector`
49
- * After assertion, `assertInjector` returns a guaranteed `Injector` whether it is the default `Injector`
50
- * within the current **Injection Context** or the custom `Injector` that was passed in.
51
- *
52
- * @param {Function} fn - the Function to pass in `assertInInjectionContext`
53
- * @param {Injector | undefined | null} injector - the optional "custom" Injector
54
- * @returns Injector
55
- *
56
- * @example
57
- * ```ts
58
- * function injectDestroy(injector?: Injector) {
59
- * injector = assertInjector(injectDestroy, injector);
60
- *
61
- * return runInInjectionContext(injector, () => {
62
- * // code
63
- * })
64
- * }
65
- * ```
66
- */
67
- export function assertInjector(
68
- fn: Function,
69
- injector: Injector | undefined | null,
70
- ): Injector
71
- export function assertInjector(
72
- fn: Function,
73
- injector: Injector | undefined | null,
74
- runner?: () => any,
75
- ) {
76
- !injector && assertInInjectionContext(fn)
77
- const assertedInjector = injector ?? inject(Injector)
78
-
79
- if (!runner) return assertedInjector
80
- return runInInjectionContext(assertedInjector, runner)
81
- }
@@ -1,32 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import { TestBed } from '@angular/core/testing'
15
- import { createNoopInjectionToken } from './create-injection-token'
16
-
17
- describe(createNoopInjectionToken.name, () => {
18
- describe('given an injection token', () => {
19
- const [injectFn, provideFn] = createNoopInjectionToken<number, true>(
20
- 'noop',
21
- { multi: true },
22
- )
23
- it('then work properly', () => {
24
- TestBed.configureTestingModule({
25
- providers: [provideFn(1), provideFn(() => 2)],
26
- }).runInInjectionContext(() => {
27
- const values = injectFn()
28
- expect(values).toEqual([1, 2])
29
- })
30
- })
31
- })
32
- })
@@ -1,185 +0,0 @@
1
- /* eslint-disable @cspell/spellchecker */
2
- /**
3
- * The code in this file is adapted from NG Extension Platform at https://ngxtension.netlify.app.
4
- *
5
- * Original Author: Chau Tran
6
- *
7
- * NG Extension Platform is an open-source project licensed under the MIT license.
8
- *
9
- * For more information about the original code, see
10
- * https://github.com/nartc/ngxtension-platform
11
- */
12
- /* eslint-enable */
13
-
14
- import {
15
- type EnvironmentProviders,
16
- type FactoryProvider,
17
- type Host,
18
- type InjectOptions,
19
- InjectionToken,
20
- type Injector,
21
- type Optional,
22
- type Provider,
23
- type Self,
24
- type SkipSelf,
25
- type Type,
26
- inject,
27
- runInInjectionContext,
28
- } from '@angular/core'
29
- import { assertInjector } from '../assert-injector/assert-injector'
30
-
31
- type CreateInjectionTokenDep<TTokenType> =
32
- | Type<TTokenType>
33
- // NOTE: we don't have an AbstractType
34
- | (abstract new (...args: Array<any>) => TTokenType)
35
- | InjectionToken<TTokenType>
36
-
37
- type CreateInjectionTokenDeps<
38
- TFactory extends (...args: Array<any>) => any,
39
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
40
- > = {
41
- [Index in keyof TFactoryDeps]:
42
- | CreateInjectionTokenDep<TFactoryDeps[Index]>
43
- | [
44
- ...modifiers: Array<Optional | Self | SkipSelf | Host>,
45
- token: CreateInjectionTokenDep<TFactoryDeps[Index]>,
46
- ]
47
- } & { length: TFactoryDeps['length'] }
48
-
49
- type CreateInjectionTokenOptions<
50
- TFactory extends (...args: Array<any>) => any,
51
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
52
- > =
53
- // this means TFunction has no parameters
54
- (TFactoryDeps[0] extends undefined
55
- ? { deps?: never }
56
- : { deps: CreateInjectionTokenDeps<TFactory, TFactoryDeps> }) & {
57
- isRoot?: boolean
58
- multi?: boolean
59
- token?: InjectionToken<ReturnType<TFactory>>
60
- extraProviders?: Provider | EnvironmentProviders
61
- }
62
-
63
- type CreateProvideFnOptions<
64
- TFactory extends (...args: Array<any>) => any,
65
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
66
- > = Pick<
67
- CreateInjectionTokenOptions<TFactory, TFactoryDeps>,
68
- 'deps' | 'extraProviders' | 'multi'
69
- >
70
-
71
- type InjectFn<TFactoryReturn> = {
72
- (): TFactoryReturn
73
- (
74
- injectOptions: InjectOptions & { optional?: false } & {
75
- injector?: Injector
76
- },
77
- ): TFactoryReturn
78
- (
79
- injectOptions: InjectOptions & { injector?: Injector },
80
- ): TFactoryReturn | null
81
- }
82
-
83
- type ProvideFn<
84
- TNoop extends boolean,
85
- TFactoryReturn,
86
- TReturn = TFactoryReturn extends Array<infer Item> ? Item : TFactoryReturn,
87
- > = (TNoop extends true
88
- ? (value: TReturn | (() => TReturn)) => Provider
89
- : () => Provider) &
90
- (TReturn extends Function
91
- ? (value: TReturn | (() => TReturn), isFunctionValue: boolean) => Provider
92
- : (value: TReturn | (() => TReturn)) => Provider)
93
-
94
- type CreateInjectionTokenReturn<
95
- TFactoryReturn,
96
- TNoop extends boolean = false,
97
- > = [
98
- InjectFn<TFactoryReturn>,
99
- ProvideFn<TNoop, TFactoryReturn>,
100
- InjectionToken<TFactoryReturn>,
101
- () => Provider,
102
- ]
103
-
104
- function createInjectFn<TValue>(token: InjectionToken<TValue>) {
105
- return function (
106
- this: Function,
107
- {
108
- injector,
109
- ...injectOptions
110
- }: InjectOptions & { injector?: Injector } = {},
111
- ) {
112
- injector = assertInjector(this, injector)
113
- return runInInjectionContext(injector, () =>
114
- inject(token, injectOptions as InjectOptions),
115
- )
116
- }
117
- }
118
-
119
- function createProvideFn<
120
- TValue,
121
- TFactory extends (...args: Array<any>) => any = (
122
- ...args: Array<any>
123
- ) => TValue,
124
- TFactoryDeps extends Parameters<TFactory> = Parameters<TFactory>,
125
- >(
126
- token: InjectionToken<TValue>,
127
- factory: (...args: Array<any>) => TValue,
128
- opts: CreateProvideFnOptions<TFactory, TFactoryDeps> = {},
129
- ) {
130
- const { deps = [], multi = false, extraProviders = [] } = opts
131
- return (value?: TValue | (() => TValue), isFunctionValue = false) => {
132
- let provider: Provider
133
- if (value !== undefined) {
134
- // eslint-disable-next-line no-shadow
135
- const factory =
136
- typeof value === 'function'
137
- ? isFunctionValue
138
- ? () => value
139
- : value
140
- : () => value
141
-
142
- provider = {
143
- provide: token,
144
- useFactory: factory,
145
- multi,
146
- }
147
- } else {
148
- provider = {
149
- provide: token,
150
- useFactory: factory,
151
- deps: deps as FactoryProvider['deps'],
152
- multi,
153
- }
154
- }
155
-
156
- return [extraProviders, provider]
157
- }
158
- }
159
-
160
- export function createNoopInjectionToken<
161
- TValue,
162
- TMulti extends boolean = false,
163
- TOptions = Pick<
164
- CreateInjectionTokenOptions<() => void, []>,
165
- 'extraProviders'
166
- > &
167
- (TMulti extends true ? { multi: true } : Record<string, never>),
168
- >(description: string, options?: TOptions) {
169
- type TReturn = TMulti extends true ? Array<TValue> : TValue
170
-
171
- const token =
172
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
173
- (options as CreateInjectionTokenOptions<() => void, []>)?.token ||
174
- new InjectionToken<TReturn>(description)
175
- return [
176
- createInjectFn(token) as CreateInjectionTokenReturn<TReturn, true>[0],
177
- createProvideFn(
178
- token,
179
- () => null!,
180
- (options || {}) as CreateProvideFnOptions<() => void, []>,
181
- ) as CreateInjectionTokenReturn<TReturn, true>[1],
182
- token,
183
- () => {},
184
- ] as CreateInjectionTokenReturn<TReturn, true>
185
- }
package/src/util/index.ts DELETED
@@ -1,13 +0,0 @@
1
- export function shouldThrowError<T extends (...args: Array<any>) => boolean>(
2
- throwError: boolean | T | undefined,
3
- params: Parameters<T>,
4
- ): boolean {
5
- // Allow throwError function to override throwing behavior on a per-error basis
6
- if (typeof throwError === 'function') {
7
- return throwError(...params)
8
- }
9
-
10
- return !!throwError
11
- }
12
-
13
- export function noop() {}