-
-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(di): add global flag to register correctly provider on GlobalRegi…
…stry vs injector.container
- Loading branch information
Showing
7 changed files
with
122 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import {ControllerProvider} from "../domain/ControllerProvider.js"; | ||
import {ProviderType} from "../domain/ProviderType.js"; | ||
import {injectable} from "../fn/injectable.js"; | ||
import type {ProviderOpts} from "../interfaces/ProviderOpts.js"; | ||
import {GlobalProviders} from "./GlobalProviders.js"; | ||
|
||
GlobalProviders.createRegistry(ProviderType.CONTROLLER, ControllerProvider); | ||
|
||
/** | ||
* Register a provider configuration. | ||
* @param {ProviderOpts<any>} provider | ||
* @param {ProviderOpts<any>} opts | ||
*/ | ||
export function registerProvider<Type = any>(provider: Partial<ProviderOpts<Type>> & Pick<ProviderOpts<Type>, "provide">) { | ||
return GlobalProviders.merge(provider.provide, provider); | ||
export function registerProvider<Type = any>(opts: Partial<ProviderOpts<Type>> & Pick<ProviderOpts<Type>, "provide">) { | ||
return injectable(opts.provide, opts as unknown as Partial<ProviderOpts>).inspect(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import "../registries/ProviderRegistry.js"; | ||
|
||
import {Store, type Type} from "@tsed/core"; | ||
|
||
import {ProviderType} from "../domain/ProviderType.js"; | ||
import {hasInjector, injector} from "../fn/injector.js"; | ||
import type {ProviderOpts} from "../interfaces/ProviderOpts.js"; | ||
import type {TokenProvider} from "../interfaces/TokenProvider.js"; | ||
import {GlobalProviders} from "../registries/GlobalProviders.js"; | ||
|
||
type ProviderBuilder<Token extends TokenProvider, BaseProvider, T extends object> = { | ||
[K in keyof T as T[K] extends (...args: any[]) => any ? never : K]: (value: T[K]) => ProviderBuilder<Token, BaseProvider, T>; | ||
} & { | ||
inspect(): BaseProvider; | ||
store(): Store; | ||
token(): Token; | ||
factory(f: (...args: unknown[]) => unknown): ProviderBuilder<Token, BaseProvider, T>; | ||
asyncFactory(f: (...args: unknown[]) => Promise<unknown>): ProviderBuilder<Token, BaseProvider, T>; | ||
value(v: unknown): ProviderBuilder<Token, BaseProvider, T>; | ||
class(c: Type): ProviderBuilder<Token, BaseProvider, T>; | ||
}; | ||
|
||
export function providerBuilder<Provider, Picked extends keyof Provider>(props: string[], baseOpts: Partial<ProviderOpts<Provider>> = {}) { | ||
return <Token extends TokenProvider>( | ||
token: Token, | ||
options: Partial<ProviderOpts<Type>> = {} | ||
): ProviderBuilder<Token, Provider, Pick<Provider, Picked>> => { | ||
const merged = { | ||
global: !hasInjector() || injector().isLoaded(), | ||
...options, | ||
...baseOpts, | ||
provide: token | ||
}; | ||
|
||
const provider = GlobalProviders.merge(token, merged); | ||
|
||
if (!merged.global) { | ||
injector().setProvider(token, provider); | ||
} | ||
|
||
return props.reduce( | ||
(acc, prop) => { | ||
return { | ||
...acc, | ||
[prop]: function (value: any) { | ||
(provider as any)[prop] = value; | ||
return this; | ||
} | ||
}; | ||
}, | ||
{ | ||
factory(factory: any) { | ||
provider.useFactory = factory; | ||
return this; | ||
}, | ||
asyncFactory(asyncFactory: any) { | ||
provider.useAsyncFactory = asyncFactory; | ||
return this; | ||
}, | ||
value(value: any) { | ||
provider.useValue = value; | ||
provider.type = ProviderType.VALUE; | ||
return this; | ||
}, | ||
class(k: any) { | ||
provider.useClass = k; | ||
return this; | ||
}, | ||
store() { | ||
return provider.store; | ||
}, | ||
inspect() { | ||
return provider; | ||
}, | ||
token() { | ||
return provider.token as Token; | ||
} | ||
} as ProviderBuilder<Token, Provider, Pick<Provider, Picked>> | ||
); | ||
}; | ||
} |