Skip to content

Commit

Permalink
bugfix(@nestjs/core) use ApplicationConfig within ExternalContextCreator
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Mar 1, 2018
1 parent deaf3a2 commit ff10b07
Show file tree
Hide file tree
Showing 18 changed files with 6,161 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# dependencies
node_modules/
package-lock.json

# IDE
/.idea
/.awcache
Expand All @@ -9,6 +10,7 @@ package-lock.json
# misc
lerna-debug.log
npm-debug.log
yarn-error.log
/src/common/npm-debug.log
/src/core/npm-debug.log
/src/testing/npm-debug.log
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## 4.6.5
### Bug Fixes
- **common**: `File(s)Interceptor` does not populate thrown exception [#437](https://github.com/nestjs/nest/issues/437)
- **core**: `NestFactory.create()` returns `any`
- **core**: use `ApplicationConfig` within `ExternalContextCreator` [#434](https://github.com/nestjs/nest/issues/434)

### Improvements
- **common**: `HttpException` extends `Error`
- **core**: make `cors` middleware customizable (`enableCors()`, `{ cors }`) [#457](https://github.com/nestjs/nest/issues/437)
- **microservices**: `RpcException` extends `Error`
- **websockets**: `WsException` extends `Error`

Expand Down
6 changes: 3 additions & 3 deletions lib/common/exceptions/http.exception.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export declare class HttpException {
export declare class HttpException extends Error {
private readonly response;
private readonly status;
private readonly message;
readonly message: any;
/**
* The base Nest Application exception, which is handled by the default Exceptions Handler.
* If you throw an exception from your HTTP route handlers, Nest will map them to the appropriate HTTP response and send to the client.
* If you throw an exception from your HTTP route handler, Nest will map them to the appropriate HTTP response and send to the client.
*
* When `response` is an object:
* - object will be stringified and returned to the user as a JSON response,
Expand Down
5 changes: 3 additions & 2 deletions lib/common/exceptions/http.exception.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class HttpException {
class HttpException extends Error {
/**
* The base Nest Application exception, which is handled by the default Exceptions Handler.
* If you throw an exception from your HTTP route handlers, Nest will map them to the appropriate HTTP response and send to the client.
* If you throw an exception from your HTTP route handler, Nest will map them to the appropriate HTTP response and send to the client.
*
* When `response` is an object:
* - object will be stringified and returned to the user as a JSON response,
Expand All @@ -16,6 +16,7 @@ class HttpException {
* ```
*/
constructor(response, status) {
super();
this.response = response;
this.status = status;
this.message = response;
Expand Down
7 changes: 6 additions & 1 deletion lib/common/interceptors/file.interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ function FileInterceptor(fieldName, options) {
}
intercept(request, context, stream$) {
return __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve, reject) => this.upload.single(fieldName)(request, request.res, resolve));
yield new Promise((resolve, reject) => this.upload.single(fieldName)(request, request.res, err => {
if (err) {
return reject(err);
}
resolve();
}));
return stream$;
});
}
Expand Down
7 changes: 6 additions & 1 deletion lib/common/interceptors/files.interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ function FilesInterceptor(fieldName, maxCount, options) {
}
intercept(request, context, stream$) {
return __awaiter(this, void 0, void 0, function* () {
yield new Promise((resolve, reject) => this.upload.array(fieldName, maxCount)(request, request.res, resolve));
yield new Promise((resolve, reject) => this.upload.array(fieldName, maxCount)(request, request.res, err => {
if (err) {
return reject(err);
}
resolve();
}));
return stream$;
});
}
Expand Down
1 change: 1 addition & 0 deletions lib/common/utils/http-exception-body.util.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export declare const createHttpExceptionBody: (message: any, error: string, stat
} | {
statusCode: number;
error: string;
message?: undefined;
};
8 changes: 4 additions & 4 deletions lib/core/nest-factory.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
import { INestMicroservice, INestApplicationContext } from '@nestjs/common';
import { INestApplication, INestMicroservice, INestApplicationContext } from '@nestjs/common';
import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface';
export declare class NestFactoryStatic {
Expand All @@ -8,9 +8,9 @@ export declare class NestFactoryStatic {
* Creates an instance of the NestApplication (returns Promise)
* @returns an `Promise` of the INestApplication instance
*/
create(module: any): any;
create(module: any, options: NestApplicationOptions): any;
create(module: any, express: any, options: NestApplicationOptions): any;
create(module: any): Promise<INestApplication>;
create(module: any, options: NestApplicationOptions): Promise<INestApplication>;
create(module: any, express: any, options: NestApplicationOptions): Promise<INestApplication>;
/**
* Creates an instance of the NestMicroservice (returns Promise)
*
Expand Down
3 changes: 2 additions & 1 deletion lib/microservices/exceptions/rpc-exception.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare class RpcException {
export declare class RpcException extends Error {
private readonly error;
readonly message: any;
constructor(error: string | object);
getError(): string | object;
}
4 changes: 3 additions & 1 deletion lib/microservices/exceptions/rpc-exception.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class RpcException {
class RpcException extends Error {
constructor(error) {
super();
this.error = error;
this.message = error;
}
getError() {
return this.error;
Expand Down
3 changes: 2 additions & 1 deletion lib/websockets/exceptions/ws-exception.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export declare class WsException {
export declare class WsException extends Error {
private readonly error;
readonly message: any;
constructor(error: string | object);
getError(): string | object;
}
4 changes: 3 additions & 1 deletion lib/websockets/exceptions/ws-exception.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class WsException {
class WsException extends Error {
constructor(error) {
super();
this.error = error;
this.message = error;
}
getError() {
return this.error;
Expand Down
11 changes: 10 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"devDependencies": {
"@types/chai": "^3.5.2",
"@types/chai-as-promised": "0.0.31",
"@types/cors": "^2.8.3",
"@types/express": "^4.0.39",
"@types/mocha": "^2.2.38",
"@types/node": "^7.0.5",
Expand Down
9 changes: 9 additions & 0 deletions src/core/injector/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { InvalidModuleException } from './../errors/exceptions/invalid-module.ex
import { DynamicModule } from '@nestjs/common';
import { ModulesContainer } from './modules-container';
import { NestApplicationContext } from './../nest-application-context';
import { ApplicationConfig } from './../application-config';

export class NestContainer {
private readonly globalModules = new Set<Module>();
Expand All @@ -23,6 +24,14 @@ export class NestContainer {
private readonly moduleTokenFactory = new ModuleTokenFactory();
private applicationRef: any;

constructor(
private readonly _applicationConfig: ApplicationConfig = void 0,
) {}

get applicationConfig(): ApplicationConfig | undefined {
return this._applicationConfig;
}

public setApplicationRef(applicationRef: any) {
this.applicationRef = applicationRef;
}
Expand Down
14 changes: 6 additions & 8 deletions src/core/injector/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export class Module {
private _exports = new Set<string>();

constructor(
private _metatype: NestModuleMetatype,
private _scope: NestModuleMetatype[],
private readonly _metatype: NestModuleMetatype,
private readonly _scope: NestModuleMetatype[],
container: NestContainer,
) {
this.addCoreInjectables(container);
Expand Down Expand Up @@ -146,9 +146,9 @@ export class Module {
metatype: ExternalContextCreator,
isResolved: true,
instance: new ExternalContextCreator(
new GuardsContextCreator(container),
new GuardsContextCreator(container, container.applicationConfig),
new GuardsConsumer(),
new InterceptorsContextCreator(container),
new InterceptorsContextCreator(container, container.applicationConfig),
new InterceptorsConsumer(),
container.getModules(),
),
Expand Down Expand Up @@ -272,11 +272,9 @@ export class Module {
) {
if (this.isCustomProvider(exportedComponent as any)) {
return this.addCustomExportedComponent(exportedComponent as any);
}
else if (isString(exportedComponent)) {
} else if (isString(exportedComponent)) {
return this._exports.add(exportedComponent);
}
else if (this.isDynamicModule(exportedComponent)) {
} else if (this.isDynamicModule(exportedComponent)) {
const { module } = exportedComponent;
return this._exports.add(module.name);
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/nest-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ export class NestFactoryStatic {
? [expressOrOptions, options]
: [ExpressAdapter.create(), expressOrOptions];

const container = new NestContainer();
const applicationConfig = new ApplicationConfig();
const container = new NestContainer(applicationConfig);

this.applyLogger(appOptions);
await this.initialize(
Expand Down Expand Up @@ -87,8 +87,8 @@ export class NestFactoryStatic {
if (!NestMicroservice) {
throw new MicroservicesPackageNotFoundException();
}
const container = new NestContainer();
const applicationConfig = new ApplicationConfig();
const container = new NestContainer(applicationConfig);

this.applyLogger(options);
await this.initialize(module, container, applicationConfig);
Expand Down
Loading

0 comments on commit ff10b07

Please sign in to comment.