Skip to content

Commit

Permalink
fix(di): rename IProvider by ProviderOpts and deprecated IProvider in…
Browse files Browse the repository at this point in the history
…terface
  • Loading branch information
Romakita committed Apr 4, 2022
1 parent c322373 commit 4351ba3
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/di/src/decorators/controller.ts
Original file line number Diff line number Diff line change
@@ -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)[];
Expand All @@ -11,7 +11,7 @@ export interface ControllerMiddlewares {
useAfter: any[];
}

export interface ControllerOptions extends Partial<IProvider<any>> {
export interface ControllerOptions extends Partial<ProviderOpts<any>> {
path?: PathType;
children?: Type<any>[];
routerOptions?: any;
Expand Down
4 changes: 2 additions & 2 deletions packages/di/src/decorators/injectable.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {IProvider} from "../interfaces/IProvider";
import type {ProviderOpts} from "../interfaces/ProviderOpts";
import {registerProvider} from "../registries/ProviderRegistry";

/**
Expand All @@ -19,7 +19,7 @@ import {registerProvider} from "../registries/ProviderRegistry";
* @returns {Function}
* @decorator
*/
export function Injectable(options: Partial<IProvider> = {}): ClassDecorator {
export function Injectable(options: Partial<ProviderOpts> = {}): ClassDecorator {
return (target: any) => {
registerProvider({
...options,
Expand Down
6 changes: 3 additions & 3 deletions packages/di/src/domain/Container.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -10,7 +10,7 @@ export class Container extends Map<TokenProvider, Provider> {
* @param token
* @param settings
*/
public add(token: TokenProvider, settings: Partial<IProvider> = {}): this {
public add(token: TokenProvider, settings: Partial<ProviderOpts> = {}): this {
const provider = GlobalProviders.get(token)?.clone() || new Provider(token);

Object.assign(provider, settings);
Expand All @@ -23,7 +23,7 @@ export class Container extends Map<TokenProvider, Provider> {
* @param token
* @param settings
*/
public addProvider(token: TokenProvider, settings: Partial<IProvider> = {}): this {
public addProvider(token: TokenProvider, settings: Partial<ProviderOpts> = {}): this {
return this.add(token, settings);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/di/src/domain/Provider.ts
Original file line number Diff line number Diff line change
@@ -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<T = any> implements IProvider<T> {
export class Provider<T = any> implements ProviderOpts<T> {
public type: ProviderType | any = ProviderType.PROVIDER;
/**
* @deprecated Use injector.get(provider.token) instead.
Expand Down
2 changes: 1 addition & 1 deletion packages/di/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any> {

export interface ProviderOpts<T = any> {
/**
* An injection token. (Typically an instance of `Type` or `InjectionToken`, but can be `any`).
*/
Expand Down Expand Up @@ -53,3 +51,8 @@ export interface IProvider<T = any> {
*/
[key: string]: any;
}

/**
* @deprecated
*/
export interface IProvider<T = any> extends ProviderOpts<T> {}
8 changes: 4 additions & 4 deletions packages/di/src/registries/GlobalProviders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenProvider, Provider> {
#settings: Map<string, RegistrySettings> = new Map();
Expand Down Expand Up @@ -44,7 +44,7 @@ export class GlobalProviderRegistry extends Map<TokenProvider, Provider> {
* @param target
* @param options
*/
merge(target: TokenProvider, options: Partial<IProvider>): void {
merge(target: TokenProvider, options: Partial<ProviderOpts>): void {
const meta = this.createIfNotExists(target, options);

Object.keys(options).forEach((key) => {
Expand Down Expand Up @@ -104,7 +104,7 @@ export class GlobalProviderRegistry extends Map<TokenProvider, Provider> {
}

createRegisterFn(type: string) {
return (provider: any | IProvider, instance?: any): void => {
return (provider: any | ProviderOpts, instance?: any): void => {
// istanbul ignore next
if (!provider.provide) {
provider = {
Expand All @@ -122,7 +122,7 @@ export class GlobalProviderRegistry extends Map<TokenProvider, Provider> {
* @param key
* @param options
*/
protected createIfNotExists(key: TokenProvider, options: Partial<IProvider>): Provider {
protected createIfNotExists(key: TokenProvider, options: Partial<ProviderOpts>): Provider {
const type = options.type || ProviderType.PROVIDER;

if (!this.has(key)) {
Expand Down
8 changes: 4 additions & 4 deletions packages/di/src/registries/ProviderRegistry.ts
Original file line number Diff line number Diff line change
@@ -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";

/**
Expand All @@ -11,9 +11,9 @@ GlobalProviders.createRegistry(ProviderType.CONTROLLER, Provider);

/**
* Register a provider configuration.
* @param {IProvider<any>} provider
* @param {ProviderOpts<any>} provider
*/
export function registerProvider(provider: Partial<IProvider>): void {
export function registerProvider(provider: Partial<ProviderOpts>): void {
if (!provider.provide) {
throw new Error("Provider.provide is required");
}
Expand Down Expand Up @@ -107,7 +107,7 @@ export const registerFactory = registerProvider;
* }
* ```
*/
export const registerValue = (provider: any | IProvider<any>, value?: any): void => {
export const registerValue = (provider: any | ProviderOpts<any>, value?: any): void => {
if (!provider.provide) {
provider = {
provide: provider
Expand Down
4 changes: 2 additions & 2 deletions packages/di/src/services/InjectorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -93,7 +93,7 @@ export class InjectorService extends Container {
* @param settings
* @deprecated
*/
public forkProvider(token: TokenProvider, settings: Partial<IProvider<any>> = {}): Provider {
public forkProvider(token: TokenProvider, settings: Partial<ProviderOpts<any>> = {}): Provider {
if (!this.hasProvider(token)) {
this.addProvider(token);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/common/src/builder/PlatformBuilder.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -269,7 +269,7 @@ export class PlatformBuilder<App = TsED.Application, Router = TsED.Router> {
});
}

useProvider(token: Type<any>, settings?: Partial<IProvider>) {
useProvider(token: Type<any>, settings?: Partial<ProviderOpts>) {
this.injector.addProvider(token, settings);

return this;
Expand Down
4 changes: 2 additions & 2 deletions packages/platform/common/src/interfaces/PlatformAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {Type} from "@tsed/core";
import {IProvider} from "@tsed/di";
import {ProviderOpts} from "@tsed/di";

export interface PlatformAdapter<App = TsED.Application, Router = TsED.Router> {
/**
* Load providers in top priority
*/
readonly providers: IProvider[];
readonly providers: ProviderOpts[];
/**
* Called after the injector instantiation
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/platform/common/src/utils/createInjector.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -21,7 +21,7 @@ const DEFAULT_PROVIDERS = [
];

interface CreateInjectorOptions {
providers?: IProvider[];
providers?: ProviderOpts[];
settings?: Partial<TsED.Configuration>;
}

Expand All @@ -37,7 +37,7 @@ export function createInjector({providers = [], settings = {}}: CreateInjectorOp

providers = [...DEFAULT_PROVIDERS, ...providers];

toMap<any, IProvider>(providers, "provide").forEach((provider, token) => {
toMap<any, ProviderOpts>(providers, "provide").forEach((provider, token) => {
injector.addProvider(token, provider);
});

Expand Down

0 comments on commit 4351ba3

Please sign in to comment.