Skip to content

Commit f67c876

Browse files
feature(@nestjs/common) add @Injectable() class, providers property integration
1 parent 375028d commit f67c876

10 files changed

Lines changed: 34 additions & 102 deletions

File tree

src/common/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const metadata = {
22
MODULES: 'modules',
33
IMPORTS: 'imports',
44
COMPONENTS: 'components',
5+
PROVIDERS: 'providers',
56
CONTROLLERS: 'controllers',
67
EXPORTS: 'exports',
78
};

src/common/decorators/core/component.decorator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
/**
2+
* Defines the injectable class. This class can inject dependencies through constructor.
3+
* Those dependencies should belongs to the same module.
4+
*/
5+
export function Injectable(): ClassDecorator {
6+
return (target: object) => {};
7+
}
8+
19
/**
210
* Defines the Component. The component can inject dependencies through constructor.
311
* Those dependencies should belongs to the same module.

src/common/decorators/core/inject.decorator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { SELF_DECLARED_DEPS_METADATA } from '../../constants';
33
import { isFunction } from '../../utils/shared.utils';
44

55
/**
6-
* Injects component, which has to be available in the current injector (module) scope.
7-
* Components are recognized by types / or tokens.
6+
* Injects provider, which has to be available in the current injector (module) scope.
7+
* Providers are recognized by types / or tokens.
88
*/
99
export function Inject(token): ParameterDecorator {
1010
return (target, key, index) => {

src/common/decorators/modules/module.decorator.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ const metadataKeys = [
99
metadata.EXPORTS,
1010
metadata.COMPONENTS,
1111
metadata.CONTROLLERS,
12+
metadata.PROVIDERS,
1213
];
1314

1415
const validateKeys = (keys: string[]) => {
15-
const isKeyValid = key => metadataKeys.findIndex(k => k === key) < 0;
16+
const isKeyInvalid = key => metadataKeys.findIndex(k => k === key) < 0;
1617
const validateKey = key => {
17-
if (isKeyValid(key)) {
18-
throw new InvalidModuleConfigException(key);
18+
if (!isKeyInvalid(key)) {
19+
return;
1920
}
21+
throw new InvalidModuleConfigException(key);
2022
};
2123
keys.forEach(validateKey);
2224
};
@@ -25,16 +27,16 @@ const validateKeys = (keys: string[]) => {
2527
* Defines the module
2628
* - `imports` - the set of the 'imported' modules
2729
* - `controllers` - the list of controllers (e.g. HTTP controllers)
28-
* - `components` - the list of components that belong to this module. They can be injected between themselves.
30+
* - `providers` - the list of providers that belong to this module. They can be injected between themselves.
2931
* - `exports` - the set of components, which should be available for modules, which imports this module
3032
* - `modules` - @deprecated the set of the 'imported' modules
33+
* - `components` - @deprecated the list of components that belong to this module. They can be injected between themselves.
3134
* @param obj {ModuleMetadata} Module metadata
3235
*/
3336
export function Module(obj: ModuleMetadata): ClassDecorator {
3437
const propsKeys = Object.keys(obj);
3538
validateKeys(propsKeys);
36-
37-
obj.modules = obj.imports && !obj.modules ? obj.imports : obj.modules;
39+
overrideModuleMetadata(obj);
3840

3941
return (target: object) => {
4042
for (const property in obj) {
@@ -44,3 +46,13 @@ export function Module(obj: ModuleMetadata): ClassDecorator {
4446
}
4547
};
4648
}
49+
50+
function overrideModuleMetadata(metadata: ModuleMetadata) {
51+
metadata.modules = metadata.imports
52+
? metadata.imports
53+
: metadata.modules;
54+
55+
metadata.components = metadata.providers
56+
? metadata.providers
57+
: metadata.components;
58+
}

src/common/decorators/modules/shared.decorator.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/common/interfaces/modules/module-metadata.interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import { DynamicModule } from './dynamic-module.interface';
44

55
export interface ModuleMetadata {
66
imports?: any[];
7-
components?: any[];
87
controllers?: any[];
8+
providers?: any[];
99
exports?: any[];
1010
modules?: any[];
11+
components?: any[];
1112
}

src/common/utils/bind-resolve-values.util.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Constructor } from './merge-with-values.util';
22
import { NestMiddleware } from '../interfaces/middlewares/nest-middleware.interface';
3-
import { Component } from '../decorators/core/component.decorator';
3+
import { Injectable } from '../decorators/core/component.decorator';
44

55
export const BindResolveMiddlewareValues = <
66
T extends Constructor<NestMiddleware>
@@ -15,7 +15,7 @@ export const BindResolveMiddlewareValues = <
1515
};
1616
const token = Metatype.name + JSON.stringify(data);
1717
Object.defineProperty(type, 'name', { value: token });
18-
Component()(type);
18+
Injectable()(type);
1919
return type;
2020
};
2121
};

src/core/exceptions/exceptions-handler.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { HttpException as DeprecatedHttpException } from './http-exception';
21
import { messages } from '../constants';
32
import { Logger } from '@nestjs/common';
43
import { ExceptionFilterMetadata } from '@nestjs/common/interfaces/exceptions/exception-filter-metadata.interface';
@@ -13,12 +12,7 @@ export class ExceptionsHandler {
1312
public next(exception: Error | HttpException | any, response) {
1413
if (this.invokeCustomFilters(exception, response)) return;
1514

16-
if (
17-
!(
18-
exception instanceof HttpException ||
19-
exception instanceof DeprecatedHttpException
20-
)
21-
) {
15+
if (!(exception instanceof HttpException)) {
2216
response.status(500).json({
2317
statusCode: 500,
2418
message: messages.UNKNOWN_EXCEPTION_MESSAGE,

src/core/exceptions/http-exception.ts

Lines changed: 0 additions & 37 deletions
This file was deleted.

src/core/middlewares/builder.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,6 @@ export class MiddlewareBuilder implements MiddlewaresConsumer {
2323
return new MiddlewareBuilder.ConfigProxy(this, middlewares);
2424
}
2525

26-
/**
27-
* @deprecated
28-
* Since version RC.6 this method is deprecated. Use apply() instead.
29-
*/
30-
public use(configuration: MiddlewareConfiguration) {
31-
this.logger.warn(
32-
'DEPRECATED! Since version RC.6 `use()` method is deprecated. Use `apply()` instead.',
33-
);
34-
35-
const { middlewares, forRoutes } = configuration;
36-
if (isUndefined(middlewares) || isUndefined(forRoutes)) {
37-
throw new InvalidMiddlewareConfigurationException();
38-
}
39-
40-
this.middlewaresCollection.add(configuration);
41-
return this;
42-
}
43-
4426
public build() {
4527
return [...this.middlewaresCollection];
4628
}

0 commit comments

Comments
 (0)