Skip to content

Commit

Permalink
feature() extract fastify, multer, cors, adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Dec 1, 2018
1 parent bc4a8a5 commit a3fbb88
Show file tree
Hide file tree
Showing 47 changed files with 305 additions and 205 deletions.
10 changes: 9 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const packages = {
microservices: ts.createProject('packages/microservices/tsconfig.json'),
websockets: ts.createProject('packages/websockets/tsconfig.json'),
testing: ts.createProject('packages/testing/tsconfig.json'),
'platform-express': ts.createProject(
'packages/platform-express/tsconfig.json',
),
'platform-fastify': ts.createProject(
'packages/platform-fastify/tsconfig.json',
),
};
const modules = Object.keys(packages);
const source = 'packages';
Expand All @@ -34,7 +40,9 @@ gulp.task('copy-misc', function() {
.pipe(gulp.dest(`${source}/core`))
.pipe(gulp.dest(`${source}/microservices`))
.pipe(gulp.dest(`${source}/websockets`))
.pipe(gulp.dest(`${source}/testing`));
.pipe(gulp.dest(`${source}/testing`))
.pipe(gulp.dest(`${source}/platform-fastify`))
.pipe(gulp.dest(`${source}/platform-express`));
});

gulp.task('clean:output', function() {
Expand Down
35 changes: 32 additions & 3 deletions package-lock.json

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

56 changes: 27 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@
"scripts": {
"coverage": "nyc report --reporter=text-lcov | coveralls",
"precommit": "lint-staged",
"test": "nyc --require ts-node/register mocha packages/**/*.spec.ts --reporter spec --require 'node_modules/reflect-metadata/Reflect.js'",
"integration-test": "mocha integration/**/*.spec.ts --reporter spec --require ts-node/register --require 'node_modules/reflect-metadata/Reflect.js'",
"lint": "tslint -p tsconfig.json -c tslint.json \"packages/**/*.ts\" -e \"*.spec.ts\"",
"format": "prettier **/**/*.ts --ignore-path ./.prettierignore --write && git status",
"test":
"nyc --require ts-node/register mocha packages/**/*.spec.ts --reporter spec --require 'node_modules/reflect-metadata/Reflect.js'",
"integration-test":
"mocha integration/**/*.spec.ts --reporter spec --require ts-node/register --require 'node_modules/reflect-metadata/Reflect.js'",
"lint":
"tslint -p tsconfig.json -c tslint.json \"packages/**/*.ts\" -e \"*.spec.ts\"",
"format":
"prettier **/**/*.ts --ignore-path ./.prettierignore --write && git status",
"clean": "gulp clean:bundle",
"build": "npm run clean && gulp build",
"prebuild:dev": "rm -rf node_modules/@nestjs",
"build:dev": "gulp build --dist node_modules/@nestjs && gulp move",
"postinstall": "opencollective",
"prerelease": "gulp copy-misc && gulp build --dist node_modules/@nestjs",
"publish": "npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --force-publish --exact -m \"chore(@nestjs) publish %s release\"",
"publish:rc": "npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --npm-tag=rc -m \"chore(@nestjs) publish %s release\"",
"publish:next": "npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --npm-tag=next --skip-git -m \"chore(@nestjs) publish %s release\"",
"publish:beta": "npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --npm-tag=beta -m \"chore(@nestjs) publish %s release\"",
"publish:test": "npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --force-publish --npm-tag=test --skip-git -m \"chore(@nestjs) publish %s release\""
"publish":
"npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --force-publish --exact -m \"chore(@nestjs) publish %s release\"",
"publish:rc":
"npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --npm-tag=rc -m \"chore(@nestjs) publish %s release\"",
"publish:next":
"npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --npm-tag=next --skip-git -m \"chore(@nestjs) publish %s release\"",
"publish:beta":
"npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --npm-tag=beta -m \"chore(@nestjs) publish %s release\"",
"publish:test":
"npm run prerelease && npm run build && ./node_modules/.bin/lerna publish --force-publish --npm-tag=test --skip-git -m \"chore(@nestjs) publish %s release\""
},
"engines": {
"node": ">= 8.9.0"
Expand Down Expand Up @@ -52,9 +61,11 @@
"fast-json-stringify": "^1.5.4",
"fast-safe-stringify": "^1.2.0",
"fastify": "^1.1.1",
"fastify-cors": "^0.2.0",
"fastify-formbody": "^2.0.0",
"fastify-multipart": "^0.4.1",
"grpc": "^1.14.1",
"http2": "^3.3.7",
"iterare": "0.0.8",
"json-socket": "^0.2.1",
"mqtt": "^2.16.0",
Expand Down Expand Up @@ -114,7 +125,7 @@
"mocha": "^3.2.0",
"nodemon": "^1.18.4",
"nyc": "^10.1.2",
"prettier": "^1.9.2",
"prettier": "^1.15.3",
"sinon": "^2.1.0",
"sinon-chai": "^2.8.0",
"socket.io-client": "^2.0.4",
Expand All @@ -131,14 +142,11 @@
}
},
"nyc": {
"include": [
"packages/**/*.ts"
],
"include": ["packages/**/*.ts"],
"exclude": [
"node_modules/",
"packages/**/*.spec.ts",
"packages/core/adapters/*.ts",
"packages/websockets/adapters/*.ts",
"packages/**/adapters/*.ts",
"packages/**/nest-*.ts",
"packages/core/errors/**/*",
"packages/common/exceptions/*.ts",
Expand All @@ -153,23 +161,13 @@
"packages/common/serializer/**/*",
"packages/common/services/logger.service.ts"
],
"extension": [
".ts"
],
"require": [
"ts-node/register"
],
"reporter": [
"text-summary",
"html"
],
"extension": [".ts"],
"require": ["ts-node/register"],
"reporter": ["text-summary", "html"],
"sourceMap": true,
"instrument": true
},
"lint-staged": {
"packages/**/*.{ts,json}": [
"npm run format",
"git add"
]
"packages/**/*.{ts,json}": ["npm run format", "git add"]
}
}
3 changes: 1 addition & 2 deletions packages/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export * from './cache';
export * from './decorators';
export * from './enums';
export * from './exceptions';
export * from './files';
export * from './http';
export {
ArgumentMetadata,
Expand Down Expand Up @@ -42,5 +41,5 @@ export {
} from './interfaces';
export * from './pipes';
export * from './serializer';
export * from './services/logger.service';
export * from './services';
export * from './utils';
2 changes: 2 additions & 0 deletions packages/common/interfaces/http/http-server.interface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestMethod } from '../../enums';
import { CorsOptions } from './../../interfaces/external/cors-options.interface';
import { NestApplicationOptions } from './../../interfaces/nest-application-options.interface';

export type ErrorHandler<TRequest = any, TResponse = any> = (
Expand Down Expand Up @@ -56,6 +57,7 @@ export interface HttpServer<TRequest = any, TResponse = any> {
getRequestUrl?(request: TResponse): string;
getInstance(): any;
registerParserMiddleware(): any;
enableCors(options: CorsOptions): any;
getHttpServer(): any;
initHttpServer(options: NestApplicationOptions): void;
close(): any;
Expand Down
4 changes: 2 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@nestjs/common",
"version": "5.4.1",
"description": "Nest - modern, fast, powerful node.js web framework (@common)",
"description":
"Nest - modern, fast, powerful node.js web framework (@common)",
"author": "Kamil Mysliwiec",
"repository": {
"type": "git",
Expand All @@ -11,7 +12,6 @@
"dependencies": {
"axios": "0.18.0",
"cli-color": "1.2.0",
"multer": "1.3.0",
"uuid": "3.3.2"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/common/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './logger.service';
5 changes: 3 additions & 2 deletions packages/core/adapters/http-adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { HttpServer, RequestMethod } from '@nestjs/common';
import { RequestHandler } from '@nestjs/common/interfaces';
import { CorsOptions } from '@nestjs/common/interfaces/external/cors-options.interface';
import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';

export abstract class AbstractHttpAdapter<T = any> implements HttpServer {
Expand Down Expand Up @@ -73,8 +74,7 @@ export abstract class AbstractHttpAdapter<T = any> implements HttpServer {

abstract close();
abstract initHttpServer(options: NestApplicationOptions);
abstract useStaticAssets(path: string, options: any);
abstract setBaseViewsDir(path: string);
abstract useStaticAssets(...args: any[]);
abstract setViewEngine(engine: string);
abstract getRequestMethod(request);
abstract getRequestUrl(request);
Expand All @@ -84,6 +84,7 @@ export abstract class AbstractHttpAdapter<T = any> implements HttpServer {
abstract setNotFoundHandler(handler: Function);
abstract setHeader(response, name: string, value: string);
abstract registerParserMiddleware();
abstract enableCors(options: CorsOptions);
abstract createMiddlewareFactory(
requestMethod: RequestMethod,
): (path: string, callback: Function) => any;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/nest-application-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class NestApplicationContext implements INestApplicationContext {

constructor(
protected readonly container: NestContainer,
private readonly scope: Type<any>[],
private contextModule: Module,
private readonly scope: Type<any>[] = [],
private contextModule: Module = null,
) {
this.containerScanner = new ContainerScanner(container);
}
Expand Down
13 changes: 6 additions & 7 deletions packages/core/nest-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-applicati
import { Logger } from '@nestjs/common/services/logger.service';
import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { isObject, validatePath } from '@nestjs/common/utils/shared.utils';
import * as cors from 'cors';
import { Server } from 'http';
import { Server as HttpsServer } from 'https';
import iterate from 'iterare';
Expand Down Expand Up @@ -55,7 +54,7 @@ export class NestApplication extends NestApplicationContext
private readonly config: ApplicationConfig,
private readonly appOptions: NestApplicationOptions = {},
) {
super(container, [], null);
super(container);

this.applyOptions();
this.selectContextModule();
Expand Down Expand Up @@ -90,9 +89,9 @@ export class NestApplication extends NestApplicationContext
this.enableCors(this.appOptions.cors as CorsOptions);
}

public createServer(): any {
public createServer<T = any>(): T {
this.httpAdapter.initHttpServer(this.appOptions);
return this.httpAdapter.getHttpServer();
return this.httpAdapter.getHttpServer() as T;
}

public async registerModules() {
Expand Down Expand Up @@ -188,7 +187,7 @@ export class NestApplication extends NestApplicationContext
}

public enableCors(options?: CorsOptions): this {
this.httpAdapter.use(cors(options) as any);
this.httpAdapter.enableCors(options);
return this;
}

Expand Down Expand Up @@ -276,8 +275,8 @@ export class NestApplication extends NestApplicationContext
return this;
}

private loadPackage(name: string, ctx: string) {
return loadPackage(name, ctx);
private loadPackage<T = any>(name: string, ctx: string): T {
return loadPackage(name, ctx) as T;
}

private async registerMiddleware(instance: any) {
Expand Down
2 changes: 0 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
},
"dependencies": {
"@nuxtjs/opencollective": "0.1.0",
"cors": "2.8.4",
"fast-safe-stringify": "1.2.0",
"iterare": "0.0.8",
"object-hash": "1.3.0",
"optional": "0.1.4",
"path-to-regexp": "2.2.1",
"uuid": "3.3.2"
},
"peerDependencies": {
Expand Down
19 changes: 17 additions & 2 deletions packages/core/test/exceptions/exceptions-handler.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { HttpException } from '@nestjs/common';
import { isNil, isObject } from '@nestjs/common/utils/shared.utils';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { NestEnvironment } from '../../../common/enums/nest-environment.enum';
import { Logger } from '../../../common/services/logger.service';
import { ExpressAdapter } from '../../adapters/express-adapter';
import { AbstractHttpAdapter } from '../../adapters';
import { InvalidExceptionFilterException } from '../../errors/exceptions/invalid-exception-filter.exception';
import { ExceptionsHandler } from '../../exceptions/exceptions-handler';
import { ExecutionContextHost } from '../../helpers/execution-context.host';
import { NoopHttpAdapter } from './../utils/noop-adapter';

describe('ExceptionsHandler', () => {
let adapter: AbstractHttpAdapter;
let handler: ExceptionsHandler;
let statusStub: sinon.SinonStub;
let jsonStub: sinon.SinonStub;
Expand All @@ -17,7 +20,8 @@ describe('ExceptionsHandler', () => {
before(() => Logger.setMode(NestEnvironment.TEST));

beforeEach(() => {
handler = new ExceptionsHandler(new ExpressAdapter({}));
adapter = new NoopHttpAdapter({});
handler = new ExceptionsHandler(adapter);
statusStub = sinon.stub();
jsonStub = sinon.stub();

Expand All @@ -30,6 +34,17 @@ describe('ExceptionsHandler', () => {
});

describe('next', () => {
beforeEach(() => {
sinon
.stub(adapter, 'reply')
.callsFake((responseRef: any, body: any, statusCode: number) => {
const res = responseRef.status(statusCode);
if (isNil(body)) {
return res.send();
}
return isObject(body) ? res.json(body) : res.send(String(body));
});
});
it('should method send expected response status code and message when exception is unknown', () => {
handler.next(new Error(), new ExecutionContextHost([0, response]));

Expand Down
Loading

0 comments on commit a3fbb88

Please sign in to comment.