diff --git a/packages/di/src/decorators/controller.ts b/packages/di/src/decorators/controller.ts index 637c76ba9d9..5a41895319f 100644 --- a/packages/di/src/decorators/controller.ts +++ b/packages/di/src/decorators/controller.ts @@ -1,6 +1,6 @@ import {isArrayOrArrayClass, Type, useDecorators} from "@tsed/core"; import {Children, Path} from "@tsed/schema"; -import {IProvider} from "../interfaces/IProvider"; +import {ProviderOpts} from "../interfaces/ProviderOpts"; import {registerController} from "../registries/ProviderRegistry"; export type PathType = string | RegExp | (string | RegExp)[]; @@ -11,7 +11,7 @@ export interface ControllerMiddlewares { useAfter: any[]; } -export interface ControllerOptions extends Partial> { +export interface ControllerOptions extends Partial> { path?: PathType; children?: Type[]; routerOptions?: any; diff --git a/packages/di/src/decorators/injectable.ts b/packages/di/src/decorators/injectable.ts index 588e9e01765..820595be888 100644 --- a/packages/di/src/decorators/injectable.ts +++ b/packages/di/src/decorators/injectable.ts @@ -1,4 +1,4 @@ -import type {IProvider} from "../interfaces/IProvider"; +import type {ProviderOpts} from "../interfaces/ProviderOpts"; import {registerProvider} from "../registries/ProviderRegistry"; /** @@ -19,7 +19,7 @@ import {registerProvider} from "../registries/ProviderRegistry"; * @returns {Function} * @decorator */ -export function Injectable(options: Partial = {}): ClassDecorator { +export function Injectable(options: Partial = {}): ClassDecorator { return (target: any) => { registerProvider({ ...options, diff --git a/packages/di/src/domain/Container.ts b/packages/di/src/domain/Container.ts index 91f78ccc351..75cf5df8a06 100644 --- a/packages/di/src/domain/Container.ts +++ b/packages/di/src/domain/Container.ts @@ -1,4 +1,4 @@ -import type {IProvider} from "../interfaces/IProvider"; +import type {ProviderOpts} from "../interfaces/ProviderOpts"; import type {ProviderType} from "./ProviderType"; import type {TokenProvider} from "../interfaces/TokenProvider"; import {GlobalProviders} from "../registries/GlobalProviders"; @@ -10,7 +10,7 @@ export class Container extends Map { * @param token * @param settings */ - public add(token: TokenProvider, settings: Partial = {}): this { + public add(token: TokenProvider, settings: Partial = {}): this { const provider = GlobalProviders.get(token)?.clone() || new Provider(token); Object.assign(provider, settings); @@ -23,7 +23,7 @@ export class Container extends Map { * @param token * @param settings */ - public addProvider(token: TokenProvider, settings: Partial = {}): this { + public addProvider(token: TokenProvider, settings: Partial = {}): this { return this.add(token, settings); } diff --git a/packages/di/src/domain/Provider.ts b/packages/di/src/domain/Provider.ts index 7dc2f4665e2..860d121a8b5 100644 --- a/packages/di/src/domain/Provider.ts +++ b/packages/di/src/domain/Provider.ts @@ -1,10 +1,10 @@ import {classOf, getClassOrSymbol, isClass, nameOf, Store, Type} from "@tsed/core"; -import {IProvider} from "../interfaces/IProvider"; +import {ProviderOpts} from "../interfaces/ProviderOpts"; import {TokenProvider} from "../interfaces/TokenProvider"; import {ProviderScope} from "./ProviderScope"; import {ProviderType} from "./ProviderType"; -export class Provider implements IProvider { +export class Provider implements ProviderOpts { public type: ProviderType | any = ProviderType.PROVIDER; /** * @deprecated Use injector.get(provider.token) instead. diff --git a/packages/di/src/index.ts b/packages/di/src/index.ts index 8834a5d2673..5d8936a00ee 100644 --- a/packages/di/src/index.ts +++ b/packages/di/src/index.ts @@ -31,7 +31,7 @@ export * from "./errors/UndefinedTokenError"; export * from "./interfaces/DIConfigurationOptions"; export * from "./interfaces/DILogger"; export * from "./interfaces/DIResolver"; -export * from "./interfaces/IProvider"; +export * from "./interfaces/ProviderOpts"; export * from "./interfaces/InjectableProperties"; export * from "./interfaces/InterceptorContext"; export * from "./interfaces/InterceptorMethods"; diff --git a/packages/di/src/interfaces/IProvider.ts b/packages/di/src/interfaces/ProviderOpts.ts similarity index 91% rename from packages/di/src/interfaces/IProvider.ts rename to packages/di/src/interfaces/ProviderOpts.ts index 69650ebc906..b73fac5c74d 100644 --- a/packages/di/src/interfaces/IProvider.ts +++ b/packages/di/src/interfaces/ProviderOpts.ts @@ -3,10 +3,8 @@ import type {DIResolver} from "./DIResolver"; import type {ProviderScope} from "../domain/ProviderScope"; import type {ProviderType} from "../domain/ProviderType"; import type {TokenProvider} from "./TokenProvider"; -/** - * - */ -export interface IProvider { + +export interface ProviderOpts { /** * An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`). */ @@ -53,3 +51,8 @@ export interface IProvider { */ [key: string]: any; } + +/** + * @deprecated + */ +export interface IProvider extends ProviderOpts {} diff --git a/packages/di/src/registries/GlobalProviders.ts b/packages/di/src/registries/GlobalProviders.ts index 44125bef030..e5c893442c8 100644 --- a/packages/di/src/registries/GlobalProviders.ts +++ b/packages/di/src/registries/GlobalProviders.ts @@ -5,7 +5,7 @@ import type {InjectorService} from "../services/InjectorService"; import {ResolvedInvokeOptions} from "../interfaces/ResolvedInvokeOptions"; import {TokenProvider} from "../interfaces/TokenProvider"; import {RegistrySettings} from "../interfaces/RegistrySettings"; -import {IProvider} from "../interfaces/IProvider"; +import {ProviderOpts} from "../interfaces/ProviderOpts"; export class GlobalProviderRegistry extends Map { #settings: Map = new Map(); @@ -44,7 +44,7 @@ export class GlobalProviderRegistry extends Map { * @param target * @param options */ - merge(target: TokenProvider, options: Partial): void { + merge(target: TokenProvider, options: Partial): void { const meta = this.createIfNotExists(target, options); Object.keys(options).forEach((key) => { @@ -104,7 +104,7 @@ export class GlobalProviderRegistry extends Map { } createRegisterFn(type: string) { - return (provider: any | IProvider, instance?: any): void => { + return (provider: any | ProviderOpts, instance?: any): void => { // istanbul ignore next if (!provider.provide) { provider = { @@ -122,7 +122,7 @@ export class GlobalProviderRegistry extends Map { * @param key * @param options */ - protected createIfNotExists(key: TokenProvider, options: Partial): Provider { + protected createIfNotExists(key: TokenProvider, options: Partial): Provider { const type = options.type || ProviderType.PROVIDER; if (!this.has(key)) { diff --git a/packages/di/src/registries/ProviderRegistry.ts b/packages/di/src/registries/ProviderRegistry.ts index 131f857385f..e1901878c25 100644 --- a/packages/di/src/registries/ProviderRegistry.ts +++ b/packages/di/src/registries/ProviderRegistry.ts @@ -1,7 +1,7 @@ import {Provider} from "../domain/Provider"; import {ProviderScope} from "../domain/ProviderScope"; import {ProviderType} from "../domain/ProviderType"; -import type {IProvider} from "../interfaces/IProvider"; +import type {ProviderOpts} from "../interfaces/ProviderOpts"; import {GlobalProviders} from "./GlobalProviders"; /** @@ -11,9 +11,9 @@ GlobalProviders.createRegistry(ProviderType.CONTROLLER, Provider); /** * Register a provider configuration. - * @param {IProvider} provider + * @param {ProviderOpts} provider */ -export function registerProvider(provider: Partial): void { +export function registerProvider(provider: Partial): void { if (!provider.provide) { throw new Error("Provider.provide is required"); } @@ -107,7 +107,7 @@ export const registerFactory = registerProvider; * } * ``` */ -export const registerValue = (provider: any | IProvider, value?: any): void => { +export const registerValue = (provider: any | ProviderOpts, value?: any): void => { if (!provider.provide) { provider = { provide: provider diff --git a/packages/di/src/services/InjectorService.ts b/packages/di/src/services/InjectorService.ts index 706e4487c59..2ba44c6d928 100644 --- a/packages/di/src/services/InjectorService.ts +++ b/packages/di/src/services/InjectorService.ts @@ -27,7 +27,7 @@ import {ResolvedInvokeOptions} from "../interfaces/ResolvedInvokeOptions"; import {ProviderScope} from "../domain/ProviderScope"; import {DILogger} from "../interfaces/DILogger"; import {TokenProvider} from "../interfaces/TokenProvider"; -import {IProvider} from "../interfaces/IProvider"; +import {ProviderOpts} from "../interfaces/ProviderOpts"; import {InvokeOptions} from "../interfaces/InvokeOptions"; import {InjectableProperties, InjectablePropertyOptions, InjectablePropertyValue} from "../interfaces/InjectableProperties"; import {InjectablePropertyType} from "../domain/InjectablePropertyType"; @@ -93,7 +93,7 @@ export class InjectorService extends Container { * @param settings * @deprecated */ - public forkProvider(token: TokenProvider, settings: Partial> = {}): Provider { + public forkProvider(token: TokenProvider, settings: Partial> = {}): Provider { if (!this.hasProvider(token)) { this.addProvider(token); } diff --git a/packages/platform/common/src/builder/PlatformBuilder.ts b/packages/platform/common/src/builder/PlatformBuilder.ts index 01ced8e1c9e..fd9dbeb0652 100644 --- a/packages/platform/common/src/builder/PlatformBuilder.ts +++ b/packages/platform/common/src/builder/PlatformBuilder.ts @@ -1,5 +1,5 @@ import {nameOf, Type} from "@tsed/core"; -import {colors, createContainer, InjectorService, IProvider, ProviderScope, setLoggerLevel} from "@tsed/di"; +import {colors, createContainer, InjectorService, ProviderOpts, ProviderScope, setLoggerLevel} from "@tsed/di"; import {getMiddlewaresForHook} from "@tsed/platform-middlewares"; import {Platform} from "../services/Platform"; import {PlatformApplication} from "../services/PlatformApplication"; @@ -269,7 +269,7 @@ export class PlatformBuilder { }); } - useProvider(token: Type, settings?: Partial) { + useProvider(token: Type, settings?: Partial) { this.injector.addProvider(token, settings); return this; diff --git a/packages/platform/common/src/interfaces/PlatformAdapter.ts b/packages/platform/common/src/interfaces/PlatformAdapter.ts index 6ee069dc55f..88da5bb5ada 100644 --- a/packages/platform/common/src/interfaces/PlatformAdapter.ts +++ b/packages/platform/common/src/interfaces/PlatformAdapter.ts @@ -1,11 +1,11 @@ import {Type} from "@tsed/core"; -import {IProvider} from "@tsed/di"; +import {ProviderOpts} from "@tsed/di"; export interface PlatformAdapter { /** * Load providers in top priority */ - readonly providers: IProvider[]; + readonly providers: ProviderOpts[]; /** * Called after the injector instantiation */ diff --git a/packages/platform/common/src/utils/createInjector.ts b/packages/platform/common/src/utils/createInjector.ts index 3a71b7c48be..90ba15d4a5a 100644 --- a/packages/platform/common/src/utils/createInjector.ts +++ b/packages/platform/common/src/utils/createInjector.ts @@ -1,4 +1,4 @@ -import {InjectorService, IProvider, setLoggerLevel} from "@tsed/di"; +import {InjectorService, ProviderOpts, setLoggerLevel} from "@tsed/di"; import {$log} from "@tsed/logger"; import {toMap} from "@tsed/core"; import {PlatformConfiguration} from "../config/services/PlatformConfiguration"; @@ -21,7 +21,7 @@ const DEFAULT_PROVIDERS = [ ]; interface CreateInjectorOptions { - providers?: IProvider[]; + providers?: ProviderOpts[]; settings?: Partial; } @@ -37,7 +37,7 @@ export function createInjector({providers = [], settings = {}}: CreateInjectorOp providers = [...DEFAULT_PROVIDERS, ...providers]; - toMap(providers, "provide").forEach((provider, token) => { + toMap(providers, "provide").forEach((provider, token) => { injector.addProvider(token, provider); });