Skip to content

Commit

Permalink
fix(di): Refactoring interfaces and Codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Romakita committed May 18, 2021
1 parent 79cb46c commit 106b443
Show file tree
Hide file tree
Showing 51 changed files with 145 additions and 155 deletions.
2 changes: 1 addition & 1 deletion docs/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ Logger configuration. See [logger section for more detail](/docs/logger.md).

### resolvers - External DI

- type: @@IDIResolver@@
- type: @@DIResolver@@

Ts.ED has its own DI container, but sometimes you have to work with other DI like Inversify or TypeDI. The version 5.39.0+
now allows you to configure multiple external DI by using the `resolvers` options.
Expand Down
7 changes: 1 addition & 6 deletions packages/core/src/decorators/deprecated.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
/**
* @module common/core
*/
/** */

import {Type} from "../interfaces";
import {Type} from "../domain/Type";

/**
* The `@Deprecated()` decorators wraps the given method in such a way that it is marked as deprecated.
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/domain/Entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Type} from "../interfaces/Type";
import {Type} from "./Type";
import {classOf, decoratorTypeOf, isArrayOrArrayClass, isClass, isDate, isObject, isPrimitiveOrPrimitiveClass, nameOf} from "../utils";
import {DecoratorTypes} from "./DecoratorTypes";

Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions packages/core/src/domain/RegistryKey.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
* the `MyCustomComponent` constructor function.
*/
// tslint:disable-next-line: variable-name
export const Type = Function;

/**
*
*/
export interface Type<T = any> extends Function {
new (...args: any[]): T;
}

export const Type = Function;

// @ts-ignore
global.Type = Type;
9 changes: 5 additions & 4 deletions packages/core/src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * from "./Metadata";
export * from "./DecoratorTypes";
export * from "./Entity";
export * from "./Env";
export * from "./Hooks";
export * from "./RegistryKey";
export * from "./Metadata";
export * from "./Store";
export * from "./Entity";
export * from "./DecoratorTypes";
export * from "./Type";
7 changes: 1 addition & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
if (process.env.NODE_ENV === "development") {
require("source-map-support").install();
}
import "reflect-metadata";

export * from "./interfaces";
export type {DecoratorParameters, DecoratorMethodParameters, StaticMethodDecorator, MetadataTypes, ValueOf, HashOf} from "./interfaces";
export * from "./utils";
export * from "./domain";
export * from "./errors/UnsupportedDecoratorType";

// decorators
export * from "./decorators";
2 changes: 1 addition & 1 deletion packages/core/src/interfaces/MetadataTypes.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Type} from "./Type";
import type {Type} from "../domain/Type";

export interface MetadataTypes<T = any, C = any> {
type?: Type<T> | T;
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
export * from "./Type";
export * from "./Env";
export * from "./DecoratorParameters";
export * from "./MetadataTypes";
export * from "./ValueOf";
export * from "./HashOf";
export type {DecoratorParameters, DecoratorMethodParameters, StaticMethodDecorator} from "./DecoratorParameters";
export type {MetadataTypes} from "./MetadataTypes";
export type {ValueOf} from "./ValueOf";
export type {HashOf} from "./HashOf";
2 changes: 1 addition & 1 deletion packages/core/src/utils/objects/constructorOf.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Type} from "../../interfaces/Type";
import type {Type} from "../../domain/Type";

/**
* Get the class constructor
Expand Down
6 changes: 3 additions & 3 deletions packages/di/src/decorators/constant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Store} from "@tsed/core";
import {INJECTABLE_PROP} from "../constants";
import {IInjectableProperties} from "../interfaces/IInjectableProperties";
import {InjectablePropertyType} from "../interfaces/InjectablePropertyType";
import type {InjectableProperties} from "../interfaces";
import {InjectablePropertyType} from "../domain/InjectablePropertyType";

/**
* Return value from Configuration.
Expand Down Expand Up @@ -47,6 +47,6 @@ export function Constant(expression: string, defaultValue?: any): any {
expression,
defaultValue
}
} as IInjectableProperties);
} as InjectableProperties);
};
}
13 changes: 13 additions & 0 deletions packages/di/src/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export * from "./scope";
export * from "./service";
export * from "./overrideProvider";
export * from "./inject";
export * from "./injectable";
export * from "./constant";
export * from "./value";
export * from "./intercept";
export * from "./interceptor";
export * from "./configuration";
export * from "./module";
export * from "./opts";
export * from "./useOpts";
3 changes: 1 addition & 2 deletions packages/di/src/decorators/injectable.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {Type} from "@tsed/core";
import {IProvider, ProviderScope} from "../interfaces";
import type {IProvider} from "../interfaces";
import {registerProvider} from "../registries/ProviderRegistry";

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/di/src/decorators/intercept.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Store, Type} from "@tsed/core";
import {INJECTABLE_PROP} from "../constants";
import {IInjectableProperties, InjectablePropertyType, InterceptorMethods} from "../interfaces";
import type {InjectableProperties, InterceptorMethods} from "../interfaces";
import {InjectablePropertyType} from "../domain";

/**
* Attaches interceptor to method call and executes the before and after methods
Expand All @@ -18,7 +19,7 @@ export function Intercept<T extends InterceptorMethods>(interceptor: Type<T>, op
useType: interceptor,
options
}
} as IInjectableProperties);
} as InjectableProperties);

return descriptor;
};
Expand Down
2 changes: 1 addition & 1 deletion packages/di/src/decorators/interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ProviderType} from "../interfaces/ProviderType";
import {ProviderType} from "../domain/ProviderType";
import {Injectable} from "./injectable";

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/di/src/decorators/module.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useDecorators} from "@tsed/core";
import {IDIResolver, ProviderScope, ProviderType, TokenProvider} from "../interfaces";
import {ProviderScope, ProviderType} from "../domain";
import type {DIResolver, TokenProvider} from "../interfaces";
import {Configuration} from "./configuration";
import {Injectable} from "./injectable";

Expand All @@ -19,7 +20,7 @@ export interface ModuleOptions extends Omit<TsED.Configuration, "scopes"> {
/**
* A list of resolvers to inject provider from external DI.
*/
resolvers?: IDIResolver[];
resolvers?: DIResolver[];

/**
* Additional properties are stored as provider configuration.
Expand Down
2 changes: 1 addition & 1 deletion packages/di/src/decorators/opts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {classOf} from "@tsed/core";
import {DI_PARAM_OPTIONS} from "../constants";
import {ProviderScope} from "../interfaces/ProviderScope";
import {ProviderScope} from "../domain/ProviderScope";
import {Inject} from "./inject";
import {Scope} from "./scope";

Expand Down
2 changes: 1 addition & 1 deletion packages/di/src/decorators/scope.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {StoreSet} from "@tsed/core";
import {ProviderScope} from "../interfaces/ProviderScope";
import {ProviderScope} from "../domain/ProviderScope";

/**
*
Expand Down
6 changes: 3 additions & 3 deletions packages/di/src/decorators/value.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Store} from "@tsed/core";
import {INJECTABLE_PROP} from "../constants";
import {IInjectableProperties} from "../interfaces/IInjectableProperties";
import {InjectablePropertyType} from "../interfaces/InjectablePropertyType";
import type {InjectableProperties} from "../interfaces/InjectableProperties";
import {InjectablePropertyType} from "../domain/InjectablePropertyType";

/**
* Return value from Configuration.
Expand Down Expand Up @@ -47,6 +47,6 @@ export function Value(expression: any, defaultValue?: any) {
expression,
defaultValue
}
} as IInjectableProperties);
} as InjectableProperties);
};
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {IProvider} from "../interfaces/IProvider";
import {ProviderType} from "../interfaces/ProviderType";
import {ProviderType} from "./ProviderType";
import {TokenProvider} from "../interfaces/TokenProvider";
import {GlobalProviders} from "../registries/GlobalProviders";
import {LocalsContainer} from "./LocalsContainer";
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expect} from "chai";
import {getEnumerableKeys} from "../../../core/src/utils";
import {Provider} from "../../src/class/Provider";
import {Provider} from ".//Provider";
import {ProviderScope} from "../../src/interfaces";

class T1 {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {classOf, Enumerable, getEnumerableKeys, isClass, nameOf, NotEnumerable, Store, Type} from "@tsed/core";
import {ProviderScope} from "../interfaces";
import {IProvider} from "../interfaces/IProvider";
import {ProviderType} from "../interfaces/ProviderType";
import {TokenProvider} from "../interfaces/TokenProvider";
import {IProvider, TokenProvider} from "../interfaces";
import {ProviderScope} from "./ProviderScope";
import {ProviderType} from "./ProviderType";

export class Provider<T = any> implements IProvider<T> {
@Enumerable()
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions packages/di/src/domain/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./Container";
export * from "./InjectablePropertyType";
export * from "./LocalsContainer";
export * from "./Provider";
export * from "./ProviderScope";
export * from "./ProviderType";
21 changes: 5 additions & 16 deletions packages/di/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
export * from "./constants/index";
export * from "./class/Provider";
export * from "./class/Container";
export * from "./class/LocalsContainer";
export * from "./domain";
export * from "./interfaces";
export * from "./decorators/scope";
export * from "./decorators/service";
export * from "./decorators/overrideProvider";
export * from "./decorators/inject";
export * from "./decorators/injectable";
export * from "./decorators/constant";
export * from "./decorators/value";
export * from "./decorators/intercept";
export * from "./decorators/interceptor";
export * from "./decorators/configuration";
export * from "./decorators/module";
export * from "./decorators/opts";
export * from "./decorators/useOpts";
export * from "./decorators";
export * from "./registries/ProviderRegistry";
export * from "./registries/GlobalProviders";

export * from "./services/InjectorService";
export * from "./services/DILogger";
export * from "./services/DIConfiguration";
export * from "./services/DITest";

export * from "./errors/InjectionError";
export * from "./errors/UndefinedTokenError";

export * from "./utils/createContainer";
export * from "./utils/importFiles";
export * from "./utils/importComponents";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {IDIResolver} from "./IDIResolver";
import {ProviderScope} from "./ProviderScope";
import {TokenProvider} from "./TokenProvider";
import type {DIResolver} from "./DIResolver";
import type {ProviderScope} from "../domain/ProviderScope";
import type {TokenProvider} from "./TokenProvider";

declare global {
namespace TsED {
Expand All @@ -9,7 +9,7 @@ declare global {
/**
* Define a list of resolvers (it can be an external DI).
*/
resolvers: IDIResolver[];
resolvers: DIResolver[];
/**
* Define dependencies to build the provider
*/
Expand All @@ -20,4 +20,4 @@ declare global {
}
}

export interface IDIConfigurationOptions extends TsED.Configuration {}
export interface DIConfigurationOptions extends TsED.Configuration {}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface IDILogger {
export interface DILogger {
info(...args: any[]): void | any;

warn(...args: any[]): void | any;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {TokenProvider} from "./TokenProvider";
import type {TokenProvider} from "./TokenProvider";

export interface IDIResolver {
export interface DIResolver {
deps?: TokenProvider[];
get<T = any>(type: TokenProvider, options: any): T | undefined;
}
24 changes: 0 additions & 24 deletions packages/di/src/interfaces/IInjectableProperties.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/di/src/interfaces/IProvider.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {Type} from "@tsed/core";
import {IDIResolver} from "./IDIResolver";
import {ProviderScope} from "./ProviderScope";
import {ProviderType} from "./ProviderType";
import {TokenProvider} from "./TokenProvider";
import type {Type} from "@tsed/core";
import type {DIResolver} from "./DIResolver";
import type {ProviderScope} from "../domain/ProviderScope";
import type {ProviderType} from "../domain/ProviderType";
import type {TokenProvider} from "./TokenProvider";
/**
*
*/
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface IProvider<T = any> {
/**
* A list of resolvers which will be used to resolve missing Symbol/Class when injector invoke a Class. This property allow external DI usage.
*/
resolvers?: IDIResolver[];
resolvers?: DIResolver[];

/**
*
Expand Down
24 changes: 24 additions & 0 deletions packages/di/src/interfaces/InjectableProperties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type {InjectablePropertyType} from "../domain/InjectablePropertyType";
import type {TokenProvider} from "./TokenProvider";

export interface InjectableProperty {
propertyKey: string;
}

export interface InjectablePropertyService extends InjectableProperty {
bindingType: InjectablePropertyType.METHOD | InjectablePropertyType.PROPERTY | InjectablePropertyType.INTERCEPTOR;
propertyType: string;
useType: TokenProvider;
onGet?: (bean: any) => any;
options?: any;
}

export interface InjectablePropertyValue extends InjectableProperty {
bindingType: InjectablePropertyType.CONSTANT | InjectablePropertyType.VALUE;
expression: string;
defaultValue?: any;
}

export interface InjectableProperties {
[key: string]: InjectablePropertyService | InjectablePropertyValue;
}
2 changes: 1 addition & 1 deletion packages/di/src/interfaces/InterceptorContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Type} from "@tsed/core";
import type {Type} from "@tsed/core";

export interface InterceptorNext {
<T>(err?: Error): T;
Expand Down
Loading

0 comments on commit 106b443

Please sign in to comment.