Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

create-injection-token.ts

186 lines5.2 KB

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​
14import {
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'
29import { assertInjector } from '../assert-injector/assert-injector'
30​
31type CreateInjectionTokenDep<TTokenType> =
32 | Type<TTokenType>
33 // NOTE: we don't have an AbstractType
34 | (abstract new (...args: Array<any>) => TTokenType)
35 | InjectionToken<TTokenType>
36​
37type 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​
49type 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​
63type 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'
70​
71type 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
82​
83type 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​
94type CreateInjectionTokenReturn<
95 TFactoryReturn,
96 TNoop extends boolean = false,
97> = [
98 InjectFn<TFactoryReturn>,
99 ProvideFn<TNoop, TFactoryReturn>,
100 InjectionToken<TFactoryReturn>,
101 () => Provider,
103​
104function 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 }
118​
119function 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 (typeof 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 }
159​
160export 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 // eslint-disable-next-line @typescript-eslint/no-empty-function
184 () => {},
185 ] as CreateInjectionTokenReturn<TReturn, true>