Skip to content

Commit e73afb7

Browse files
feature(@nejsts/core) create nest application context feature nestjs#201
1 parent ee66faf commit e73afb7

6 files changed

Lines changed: 17 additions & 59 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 4.4.0 @soon
2+
- **core**: possibility to create the `NestApplicationContext` using `NestFactory.createApplicationContext()` (Nest application without HTTP server / microservice in the background)
3+
- **core**: create custom params decorators feature (`createRouteParamDecorator()`)
4+
15
## 4.3.3
26
- **common**: `ParseIntPipe` is now available out-of-the-box (`@nestjs/common`)
37
- **common**: package contains a set of useful HTTP exceptions now, such as `ForbiddenException`, `UnauthorizedException`, `BadRequestException` etc

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nestjs",
3-
"version": "4.3.4",
3+
"version": "4.4.0",
44
"description": "Modern, fast, powerful node.js web framework",
55
"main": "index.js",
66
"scripts": {

src/core/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ export { MiddlewareBuilder } from './middlewares/builder';
1010
export { ModuleRef } from './injector/module-ref';
1111
export * from './services/reflector.service';
1212
export * from './nest-factory';
13-
export * from './nest-application';
13+
export * from './nest-application';
14+
export * from './nest-application-context';

src/core/nest-application-context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { Metatype } from '@nestjs/common/interfaces';
66
import { isFunction } from '@nestjs/common/utils/shared.utils';
77
import { INestApplicationContext } from '@nestjs/common';
88

9-
export class NestApplicationContext {
9+
export class NestApplicationContext implements INestApplicationContext {
1010
private readonly moduleTokenFactory = new ModuleTokenFactory();
1111

1212
constructor(
13-
private readonly container: NestContainer,
13+
protected readonly container: NestContainer,
1414
private readonly scope: NestModuleMetatype[],
1515
private readonly contextModule) {}
1616

@@ -34,6 +34,7 @@ export class NestApplicationContext {
3434
const dependencies = new Map([
3535
...this.contextModule.components,
3636
...this.contextModule.routes,
37+
...this.contextModule.injectables,
3738
]);
3839
const name = isFunction(metatypeOrToken) ? (metatypeOrToken as any).name : metatypeOrToken;
3940
const instanceWrapper = dependencies.get(name);

src/testing/errors/unknown-module.exception.ts

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

src/testing/testing-module.ts

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
import * as optional from 'optional';
2-
import { NestContainer, InstanceWrapper } from '@nestjs/core/injector/container';
3-
import { DependenciesScanner } from '@nestjs/core/scanner';
4-
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
5-
import { Metatype } from '@nestjs/common/interfaces';
6-
import { isFunction } from '@nestjs/common/utils/shared.utils';
7-
import { ModuleTokenFactory } from '@nestjs/core/injector/module-token-factory';
2+
import { NestContainer } from '@nestjs/core/injector/container';
83
import { NestModuleMetatype } from '@nestjs/common/interfaces/modules/module-metatype.interface';
9-
import { UnknownModuleException } from './errors/unknown-module.exception';
10-
import { NestApplication } from '@nestjs/core';
4+
import { NestApplication, NestApplicationContext } from '@nestjs/core';
115
import { INestApplication, INestMicroservice } from '@nestjs/common';
126
import { MicroserviceConfiguration } from '@nestjs/common/interfaces/microservices/microservice-configuration.interface';
137
import { MicroservicesPackageNotFoundException } from '@nestjs/core/errors/exceptions/microservices-package-not-found.exception';
148

159
const { NestMicroservice } = optional('@nestjs/microservices/nest-microservice') || {} as any;
1610

17-
export class TestingModule {
18-
private readonly moduleTokenFactory = new ModuleTokenFactory();
19-
20-
constructor(
21-
private readonly container: NestContainer,
22-
private readonly scope: NestModuleMetatype[],
23-
private readonly contextModule) {}
11+
export class TestingModule extends NestApplicationContext {
12+
constructor(container: NestContainer, scope: NestModuleMetatype[], contextModule) {
13+
super(container, scope, contextModule);
14+
}
2415

2516
public createNestApplication(express?): INestApplication {
2617
return new NestApplication(this.container, express);
@@ -32,36 +23,4 @@ export class TestingModule {
3223
}
3324
return new NestMicroservice(this.container, config);
3425
}
35-
36-
public select<T>(module: Metatype<T>): TestingModule {
37-
const modules = this.container.getModules();
38-
const moduleMetatype = this.contextModule.metatype;
39-
const scope = this.scope.concat(moduleMetatype);
40-
41-
const token = this.moduleTokenFactory.create(module as any, scope);
42-
const selectedModule = modules.get(token);
43-
if (!selectedModule) {
44-
throw new UnknownModuleException();
45-
}
46-
return new TestingModule(this.container, scope, selectedModule);
47-
}
48-
49-
public get<T>(metatypeOrToken: Metatype<T> | string): T {
50-
return this.findInstanceByPrototypeOrToken<T>(metatypeOrToken);
51-
}
52-
53-
private findInstanceByPrototypeOrToken<T>(metatypeOrToken: Metatype<T> | string) {
54-
const dependencies = new Map([
55-
...this.contextModule.components,
56-
...this.contextModule.routes,
57-
...this.contextModule.injectables,
58-
]);
59-
const name = isFunction(metatypeOrToken) ? (metatypeOrToken as any).name : metatypeOrToken;
60-
const instanceWrapper = dependencies.get(name);
61-
if (!instanceWrapper) {
62-
return null;
63-
}
64-
return (instanceWrapper as InstanceWrapper<any>).instance;
65-
}
66-
}
67-
26+
}

0 commit comments

Comments
 (0)