chore() resolve conflicts

This commit is contained in:
Kamil Myśliwiec
2018-12-10 22:03:06 +01:00
216 changed files with 1484 additions and 1448 deletions

View File

@@ -18,7 +18,7 @@
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"subscriptions-transport-ws": "^0.9.5",
"typescript": "^2.8.0",
"typescript": "^3.1.0",
"ws": "^4.0.0"
},
"devDependencies": {

View File

@@ -17,7 +17,7 @@
"fastify": "^1.1.1",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typescript": "^2.8.0"
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/node": "^7.0.41",

View File

@@ -16,7 +16,7 @@
"class-validator": "^0.7.2",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typescript": "^2.8.0"
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/node": "^7.0.41",

View File

@@ -17,7 +17,7 @@
"class-validator": "^0.7.2",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typescript": "^2.8.0"
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/node": "^7.0.41",

View File

@@ -15,7 +15,7 @@
"mongoose": "^5.0.1",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typescript": "^2.8.0"
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/mongoose": "^4.7.23",

View File

@@ -16,7 +16,7 @@
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typeorm": "^0.1.16",
"typescript": "^2.8.0"
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/node": "^7.0.41",

View File

@@ -16,7 +16,7 @@
"class-validator": "^0.7.2",
"reflect-metadata": "^0.1.12",
"rxjs": "^6.0.0",
"typescript": "^2.8.0"
"typescript": "^3.1.0"
},
"devDependencies": {
"@types/node": "^7.0.41",

101
package-lock.json generated
View File

@@ -1,6 +1,6 @@
{
"name": "nestjs",
"version": "5.4.0",
"version": "5.4.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@@ -122,9 +122,9 @@
}
},
"@nestjs/core": {
"version": "5.3.15",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-5.3.15.tgz",
"integrity": "sha512-pbF+e7JTBxiR+vu6VmrXsP9UQqBkQrH7o6HHQdovCP1Q2/NEBhpFvzMC3mWo0/4J11qTCN06vc6sWjstpX1fMg==",
"version": "5.4.1",
"resolved": "https://registry.npmjs.org/@nestjs/core/-/core-5.4.1.tgz",
"integrity": "sha512-PAlSEsycXPIdVm1GOeghs0co8cUmMX65FPjzBLtRJKKcPS8YM7Xo3WKd2f2oGMACYNXbulLk8rWN0xTO2HTgww==",
"requires": {
"@nuxtjs/opencollective": "0.1.0",
"body-parser": "1.18.3",
@@ -243,16 +243,16 @@
"integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4="
},
"mime-db": {
"version": "1.36.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.36.0.tgz",
"integrity": "sha512-L+xvyD9MkoYMXb1jAmzI/lWYAxAMCPvIBSWur0PZ5nOf5euahRLVqH//FKW9mWp2lkqUgYiXPgkzfMUFi4zVDw=="
"version": "1.37.0",
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.37.0.tgz",
"integrity": "sha512-R3C4db6bgQhlIhPU48fUtdVmKnflq+hRdad7IyKhtFj06VPNVdk2RhiYL3UjQIlso8L+YxAtFkobT0VK+S/ybg=="
},
"mime-types": {
"version": "2.1.20",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.20.tgz",
"integrity": "sha512-HrkrPaP9vGuWbLK1B1FfgAkbqNjIuy4eHlIYnFi7kamZyLLrGlo2mpcx0bBmNpKqBtYtAfGbodDddIgddSJC2A==",
"version": "2.1.21",
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.21.tgz",
"integrity": "sha512-3iL6DbwpyLzjR3xHSFNFeb9Nz/M8WDkX33t1GFQnFOllWk8pOrh/LSrB5OXlnlW5P9LH73X6loW/eogc+F5lJg==",
"requires": {
"mime-db": "~1.36.0"
"mime-db": "~1.37.0"
}
},
"proxy-addr": {
@@ -15568,9 +15568,9 @@
"integrity": "sha512-f/qGG2tUkrISBlQZEjEqoZ3B2+npJjIf04H1wuAv9iA8i04Icp+61KRXxFdha22670NJopsZCIjhC3SnjPRKrQ=="
},
"tslint": {
"version": "5.9.1",
"resolved": "https://registry.npmjs.org/tslint/-/tslint-5.9.1.tgz",
"integrity": "sha1-ElX4ej/1frCw4fDmEKi0dIBGya4=",
"version": "5.11.0",
"resolved": "https://registry.npmjs.org/tslint/-/tslint-5.11.0.tgz",
"integrity": "sha1-mPMMAurjzecAYgHkwzywi0hYHu0=",
"dev": true,
"requires": {
"babel-code-frame": "^6.22.0",
@@ -15584,7 +15584,7 @@
"resolve": "^1.3.2",
"semver": "^5.3.0",
"tslib": "^1.8.0",
"tsutils": "^2.12.1"
"tsutils": "^2.27.2"
},
"dependencies": {
"ansi-styles": {
@@ -15597,9 +15597,9 @@
}
},
"chalk": {
"version": "2.4.0",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.0.tgz",
"integrity": "sha512-Wr/w0f4o9LuE7K53cD0qmbAMM+2XNLzR29vFn5hqko4sxGlUsyy363NvmyGIyk5tpe9cjTr9SJYbysEyPkRnFw==",
"version": "2.4.1",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz",
"integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==",
"dev": true,
"requires": {
"ansi-styles": "^3.2.1",
@@ -15608,31 +15608,17 @@
}
},
"commander": {
"version": "2.15.1",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.15.1.tgz",
"integrity": "sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag==",
"version": "2.19.0",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.19.0.tgz",
"integrity": "sha512-6tvAOO+D6OENvRAh524Dh9jcfKTYDQAqvqezbCW82xj5X0pSrcpxtvRKHLG0yBY6SD7PSDrJaj+0AiOcKVd1Xg==",
"dev": true
},
"esprima": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.0.tgz",
"integrity": "sha512-oftTcaMu/EGrEIu904mWteKIv8vMuOgGYo7EhVJJN00R/EED9DCua/xxHRdYnKtcECzVg7xOWhflvJMnqcFZjw==",
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz",
"integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==",
"dev": true
},
"glob": {
"version": "7.1.2",
"resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz",
"integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==",
"dev": true,
"requires": {
"fs.realpath": "^1.0.0",
"inflight": "^1.0.4",
"inherits": "2",
"minimatch": "^3.0.4",
"once": "^1.3.0",
"path-is-absolute": "^1.0.0"
}
},
"has-flag": {
"version": "3.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
@@ -15640,34 +15626,25 @@
"dev": true
},
"js-yaml": {
"version": "3.11.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.11.0.tgz",
"integrity": "sha512-saJstZWv7oNeOyBh3+Dx1qWzhW0+e6/8eDzo7p5rDFqxntSztloLtuKu+Ejhtq82jsilwOIZYsCz+lIjthg1Hw==",
"version": "3.12.0",
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.12.0.tgz",
"integrity": "sha512-PIt2cnwmPfL4hKNwqeiuz4bKfnzHTBv6HyVgjahA6mPLwPDzjDWrplJBMjHUFxku/N3FlmrbyPclad+I+4mJ3A==",
"dev": true,
"requires": {
"argparse": "^1.0.7",
"esprima": "^4.0.0"
}
},
"minimatch": {
"version": "3.0.4",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz",
"integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==",
"dev": true,
"requires": {
"brace-expansion": "^1.1.7"
}
},
"semver": {
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz",
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==",
"version": "5.6.0",
"resolved": "https://registry.npmjs.org/semver/-/semver-5.6.0.tgz",
"integrity": "sha512-RS9R6R35NYgQn++fkDWaOmqGoj4Ek9gGs+DPxNUZKuwE183xjJroKvyo1IzVFeXvUrvmALy6FWD5xrdJT25gMg==",
"dev": true
},
"supports-color": {
"version": "5.4.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.4.0.tgz",
"integrity": "sha512-zjaXglF5nnWpsq470jSv6P9DwPvgLkuapYmfDm3JWOm0vkNTVF2tI4UrN2r6jH1qM/uc/WtxYY1hYoA2dOKj5w==",
"version": "5.5.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz",
"integrity": "sha512-QjVjwdXIt408MIiAqCX4oUKsgU2EqAGzs2Ppkm4aQYbjm+ZEWEcW4SfFNTr4uMNZma0ey4f5lgLrkB0aX0QMow==",
"dev": true,
"requires": {
"has-flag": "^3.0.0"
@@ -15676,9 +15653,9 @@
}
},
"tsutils": {
"version": "2.26.1",
"resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.26.1.tgz",
"integrity": "sha512-bnm9bcjOqOr1UljleL94wVCDlpa6KjfGaTkefeLch4GRafgDkROxPizbB/FxTEdI++5JqhxczRy/Qub0syNqZA==",
"version": "2.29.0",
"resolved": "https://registry.npmjs.org/tsutils/-/tsutils-2.29.0.tgz",
"integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==",
"dev": true,
"requires": {
"tslib": "^1.8.1"
@@ -15727,9 +15704,9 @@
"integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c="
},
"typescript": {
"version": "2.8.1",
"resolved": "http://registry.npmjs.org/typescript/-/typescript-2.8.1.tgz",
"integrity": "sha512-Ao/f6d/4EPLq0YwzsQz8iXflezpTkQzqAyenTiw4kCUGr1uPiFLC3+fZ+gMZz6eeI/qdRUqvC+HxIJzUAzEFdg==",
"version": "3.2.1",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.2.1.tgz",
"integrity": "sha512-jw7P2z/h6aPT4AENXDGjcfHTu5CSqzsbZc6YlUIebTyBAq8XaKp78x7VcSh30xwSCcsu5irZkYZUSFP1MrAMbg==",
"dev": true
},
"uglify-js": {

View File

@@ -5,30 +5,21 @@
"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"
@@ -56,7 +47,6 @@
"cli-color": "^1.1.0",
"connect": "^3.6.6",
"cors": "^2.8.4",
"deprecate": "^1.0.0",
"engine.io-client": "^3.1.1",
"express": "^4.16.2",
"fast-json-stringify": "^1.5.4",
@@ -130,8 +120,8 @@
"socket.io-client": "^2.0.4",
"supertest": "^3.0.0",
"ts-node": "^6.0.0",
"tslint": "^5.9.1",
"typescript": "^2.8.1"
"tslint": "^5.11.0",
"typescript": "^3.1.6"
},
"collective": {
"type": "opencollective",
@@ -141,7 +131,9 @@
}
},
"nyc": {
"include": ["packages/**/*.ts"],
"include": [
"packages/**/*.ts"
],
"exclude": [
"node_modules/",
"packages/**/*.spec.ts",
@@ -161,13 +153,23 @@
"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"
]
}
}

View File

@@ -1,6 +1,6 @@
(The MIT License)
Copyright (c) 2017 Kamil Myśliwiec <http://kamilmysliwiec.com>
Copyright (c) 2018 Kamil Myśliwiec <https://kamilmysliwiec.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@@ -1,21 +1,29 @@
import { Observable, of } from 'rxjs';
import { tap } from 'rxjs/operators';
import { Inject, Injectable, Optional } from '../../decorators';
import { ExecutionContext, NestInterceptor } from '../../interfaces';
import {
ExecutionContext,
HttpServer,
NestInterceptor,
} from '../../interfaces';
import { CACHE_KEY_METADATA, CACHE_MANAGER } from '../cache.constants';
const APPLICATION_REFERENCE_HOST = 'ApplicationReferenceHost';
const REFLECTOR = 'Reflector';
export interface ApplicationHost<T extends HttpServer = any> {
applicationRef: T;
}
@Injectable()
export class CacheInterceptor implements NestInterceptor {
@Optional()
@Inject(APPLICATION_REFERENCE_HOST)
protected readonly applicationRefHost: any;
protected readonly applicationRefHost: ApplicationHost;
constructor(
@Inject(CACHE_MANAGER) protected readonly cacheManager: any,
@Inject(REFLECTOR) protected readonly reflector,
@Inject(REFLECTOR) protected readonly reflector: any,
) {}
async intercept(
@@ -31,7 +39,9 @@ export class CacheInterceptor implements NestInterceptor {
if (value) {
return of(value);
}
return call$.pipe(tap(response => this.cacheManager.set(key, response)));
return call$.pipe(
tap((response: any) => this.cacheManager.set(key, response)),
);
} catch {
return call$;
}

View File

@@ -1,14 +1,12 @@
export const METADATA = {
MODULES: 'modules',
IMPORTS: 'imports',
COMPONENTS: 'components',
PROVIDERS: 'providers',
CONTROLLERS: 'controllers',
EXPORTS: 'exports',
};
export const SHARED_MODULE_METADATA = '__sharedModule__';
export const GLOBAL_MODULE_METADATA = '__globalModule__';
export const SHARED_MODULE_METADATA = '__module:shared__';
export const GLOBAL_MODULE_METADATA = '__module:global__';
export const PATH_METADATA = 'path';
export const PARAMTYPES_METADATA = 'design:paramtypes';
export const SELF_DECLARED_DEPS_METADATA = 'self:paramtypes';
@@ -26,7 +24,6 @@ export const GUARDS_METADATA = '__guards__';
export const RENDER_METADATA = '__renderTemplate__';
export const INTERCEPTORS_METADATA = '__interceptors__';
export const HTTP_CODE_METADATA = '__httpCode__';
export const GATEWAY_MIDDLEWARES = '__gatewayMiddleware';
export const MODULE_PATH = '__module_path__';
export const HEADERS_METADATA = '__headers__';
export const REDIRECT_METADATA = '__redirect__';

View File

@@ -1,10 +1,14 @@
/**
* Binds parameters decorators to the particular method
* Binds parameter decorators to the method
* Useful when the language doesn't provide a 'Parameter Decorators' feature (vanilla JavaScript)
* @param {} ...decorators
*/
export function Bind(...decorators: any[]) {
return (target: object, key, descriptor) => {
export function Bind(...decorators: any[]): MethodDecorator {
return <T>(
target: object,
key: string | symbol,
descriptor: TypedPropertyDescriptor<T>,
) => {
decorators.forEach((fn, index) => fn(target, key, index));
return descriptor;
};

View File

@@ -2,7 +2,7 @@ import { FILTER_CATCH_EXCEPTIONS } from '../../constants';
import { Type } from '../../interfaces';
/**
* Defines the Exceptions Filter. Takes set of exception types as an argument which has to be caught by this Filter.
* Defines an exception filter. Takes set of exception types as arguments which have to be caught by this filter.
* The class should implement the `ExceptionFilter` interface.
*/
export function Catch(...exceptions: Type<any>[]): ClassDecorator {

View File

@@ -1,74 +0,0 @@
import * as deprecate from 'deprecate';
import * as uuid from 'uuid/v4';
/**
* Defines the injectable class. This class can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export function Injectable(): ClassDecorator {
return (target: object) => {};
}
/**
* @deprecated
* Defines the Component. The component can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export function Component(): ClassDecorator {
deprecate(
'The @Component() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.',
);
return (target: object) => {};
}
/**
* @deprecated
* Defines the Pipe. The Pipe should implement the `PipeTransform` interface.
*/
export function Pipe(): ClassDecorator {
deprecate(
'The @Pipe() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.',
);
return (target: object) => {};
}
/**
* @deprecated
* Defines the Guard. The Guard should implement the `CanActivate` interface.
*/
export function Guard(): ClassDecorator {
deprecate(
'The @Guard() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.',
);
return (target: object) => {};
}
/**
* @deprecated
* Defines the Middleware. The Middleware should implement the `NestMiddleware` interface.
*/
export function Middleware(): ClassDecorator {
deprecate(
'The @Middleware() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.',
);
return (target: object) => {};
}
/**
* @deprecated
* Defines the Interceptor. The Interceptor should implement `HttpInterceptor`, `RpcInterceptor` or `WsInterceptor` interface.
*/
export function Interceptor(): ClassDecorator {
deprecate(
'The @Interceptor() decorator is deprecated and will be removed within next major release. Use @Injectable() instead.',
);
return (target: object) => {};
}
export function mixin(mixinClass) {
Object.defineProperty(mixinClass, 'name', {
value: uuid(),
});
Injectable()(mixinClass);
return mixinClass;
}

View File

@@ -1,8 +1,8 @@
import { isUndefined } from '../../utils/shared.utils';
import { PATH_METADATA } from '../../constants';
import { isUndefined } from '../../utils/shared.utils';
/**
* Defines the Controller. The controller can inject dependencies through constructor.
* Defines the controller. Controller can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export function Controller(prefix?: string): ClassDecorator {

View File

@@ -1,8 +1,10 @@
import { PARAMTYPES_METADATA } from '../../constants';
export function flatten(arr: any[]) {
export function flatten<T extends any[] = any, R extends any[] = any>(
arr: T,
): R {
const flat = [].concat(...arr);
return flat.some(Array.isArray) ? flatten(flat) : flat;
return (flat.some(Array.isArray) ? flatten(flat) : flat) as R;
}
export const Dependencies = (...dependencies: any[]): ClassDecorator => {

View File

@@ -5,9 +5,12 @@ import { isFunction } from '../../utils/shared.utils';
import { validateEach } from '../../utils/validate-each.util';
const defineFiltersMetadata = (...filters: (Function | ExceptionFilter)[]) => {
return (target: any, key?, descriptor?) => {
const isFilterValid = filter =>
filter && (isFunction(filter) || isFunction(filter.catch));
return (target: any, key?: string, descriptor?: any) => {
const isFilterValid = <T extends Function | Record<string, any>>(
filter: T,
) =>
filter &&
(isFunction(filter) || isFunction((filter as Record<string, any>).catch));
if (descriptor) {
validateEach(
@@ -31,12 +34,12 @@ const defineFiltersMetadata = (...filters: (Function | ExceptionFilter)[]) => {
};
/**
* Setups exception filters to the chosen context.
* Bounds exception filters to the chosen context.
* When the `@UseFilters()` is used on the controller level:
* - Exception Filter will be set up to every handler (every method)
*
* When the `@UseFilters()` is used on the handle level:
* - Exception Filter will be set up only to specified method
* - Exception Filter will be set up only to the specified method
*
* @param {ExceptionFilter[]} ...filters
*/

View File

@@ -1,10 +1,10 @@
export * from './bind.decorator';
export * from './catch.decorator';
export * from './component.decorator';
export * from './controller.decorator';
export * from './dependencies.decorator';
export * from './exception-filters.decorator';
export * from './inject.decorator';
export * from './injectable.decorator';
export * from './optional.decorator';
export * from './reflect-metadata.decorator';
export * from './use-guards.decorator';

View File

@@ -6,7 +6,7 @@ import { isFunction, isUndefined } from '../../utils/shared.utils';
/**
* Injects provider which has to be available in the current injector (module) scope.
* Providers are recognized by types or tokens.
* Providers are recognized by either types or tokens.
*/
export function Inject<T = any>(token?: T) {
return (target: Object, key: string | symbol, index?: number) => {

View File

@@ -0,0 +1,18 @@
import * as uuid from 'uuid/v4';
import { Type } from './../../interfaces/type.interface';
/**
* Defines the injectable class. This class can inject dependencies through constructor.
* Those dependencies have to belong to the same module.
*/
export function Injectable(): ClassDecorator {
return (target: object) => {};
}
export function mixin(mixinClass: Type<any>) {
Object.defineProperty(mixinClass, 'name', {
value: uuid(),
});
Injectable()(mixinClass);
return mixinClass;
}

View File

@@ -2,11 +2,10 @@
* Assigns the metadata to the class/function under specified `key`.
* This metadata can be reflected using `Reflector` class.
*/
export const ReflectMetadata = <K = any, V = any>(metadataKey: K, metadataValue: V) => (
target: object,
key?,
descriptor?,
) => {
export const ReflectMetadata = <K = any, V = any>(
metadataKey: K,
metadataValue: V,
) => (target: object, key?: any, descriptor?: any) => {
if (descriptor) {
Reflect.defineMetadata(metadataKey, metadataValue, descriptor.value);
return descriptor;

View File

@@ -1,8 +1,8 @@
import { GUARDS_METADATA } from '../../constants';
import { extendArrayMetadata } from '../../utils/extend-metadata.util';
import { validateEach } from '../../utils/validate-each.util';
import { isFunction } from '../../utils/shared.utils';
import { CanActivate } from '../../interfaces';
import { extendArrayMetadata } from '../../utils/extend-metadata.util';
import { isFunction } from '../../utils/shared.utils';
import { validateEach } from '../../utils/validate-each.util';
/**
* Binds guards to the particular context.
@@ -10,14 +10,16 @@ import { CanActivate } from '../../interfaces';
* - Guard will be register to each handler (every method)
*
* When the `@UseGuards()` is used on the handler level:
* - Guard will be registered only to specified method
* - Guard will be registered only to the specified method
*
* @param {} ...guards
*/
export function UseGuards(...guards: (CanActivate | Function)[]) {
return (target: any, key?, descriptor?) => {
const isValidGuard = guard =>
guard && (isFunction(guard) || isFunction(guard.canActivate));
return (target: any, key?: string, descriptor?: any) => {
const isValidGuard = <T extends Function | Record<string, any>>(guard: T) =>
guard &&
(isFunction(guard) ||
isFunction((guard as Record<string, any>).canActivate));
if (descriptor) {
validateEach(

View File

@@ -10,17 +10,20 @@ import { validateEach } from '../../utils/validate-each.util';
* - Interceptor will be register to each handler (every method)
*
* When the `@UseInterceptors()` is used on the handle level:
* - Interceptor will be registered only to specified method
* - Interceptor will be registered only to the specified method
*
* @param {} ...interceptors
*/
export function UseInterceptors(
...interceptors: (NestInterceptor | Function)[]
) {
return (target: any, key?, descriptor?) => {
const isValidInterceptor = interceptor =>
return (target: any, key?: string, descriptor?: any) => {
const isValidInterceptor = <T extends Function | Record<string, any>>(
interceptor: T,
) =>
interceptor &&
(isFunction(interceptor) || isFunction(interceptor.intercept));
(isFunction(interceptor) ||
isFunction((interceptor as Record<string, any>).intercept));
if (descriptor) {
validateEach(

View File

@@ -1,8 +1,8 @@
import { PipeTransform } from '../../interfaces/index';
import { PIPES_METADATA } from '../../constants';
import { PipeTransform } from '../../interfaces/index';
import { extendArrayMetadata } from '../../utils/extend-metadata.util';
import { validateEach } from '../../utils/validate-each.util';
import { isFunction } from '../../utils/shared.utils';
import { validateEach } from '../../utils/validate-each.util';
/**
* Binds pipes to the particular context.
@@ -10,14 +10,16 @@ import { isFunction } from '../../utils/shared.utils';
* - Pipe will be register to each handler (every method)
*
* When the `@UsePipes()` is used on the handle level:
* - Pipe will be registered only to specified method
* - Pipe will be registered only to the specified method
*
* @param {PipeTransform[]} ...pipes
*/
export function UsePipes(...pipes: (PipeTransform | Function)[]) {
return (target: any, key?, descriptor?) => {
const isPipeValid = pipe =>
pipe && (isFunction(pipe) || isFunction(pipe.transform));
return (target: any, key?: string, descriptor?: any) => {
const isPipeValid = <T extends Function | Record<string, any>>(pipe: T) =>
pipe &&
(isFunction(pipe) || isFunction((pipe as Record<string, any>).transform));
if (descriptor) {
extendArrayMetadata(PIPES_METADATA, pipes, descriptor.value);
return descriptor;

View File

@@ -1,4 +1,3 @@
import * as deprecate from 'deprecate';
import * as uuid from 'uuid/v4';
import {
CUSTOM_ROUTE_AGRS_METADATA,
@@ -47,7 +46,7 @@ export function createParamDecorator(
const args =
Reflect.getMetadata(ROUTE_ARGS_METADATA, target.constructor, key) || {};
const isPipe = pipe =>
const isPipe = (pipe: any) =>
pipe &&
((isFunction(pipe) && pipe.prototype) || isFunction(pipe.transform));
@@ -71,20 +70,3 @@ export function createParamDecorator(
enhancers.forEach(fn => fn(target, key, index));
};
}
/**
* Defines HTTP route param decorator
* @deprecated
* @param factory
*/
export function createRouteParamDecorator(
factory: CustomParamFactory,
): (
data?: any,
...pipes: (Type<PipeTransform> | PipeTransform)[]
) => ParameterDecorator {
deprecate(
'The "createRouteParamDecorator" function is deprecated and will be removed within next major release. Use "createParamDecorator" instead.',
);
return createParamDecorator(factory);
}

View File

@@ -41,7 +41,7 @@ const createRouteParamDecorator = (paramtype: RouteParamtypes) => {
};
const createPipesRouteParamDecorator = (paramtype: RouteParamtypes) => (
data?,
data?: any,
...pipes: (Type<PipeTransform> | PipeTransform)[]
): ParameterDecorator => (target, key, index) => {
const args =
@@ -80,48 +80,54 @@ export const Headers: (
property?: string,
) => ParameterDecorator = createRouteParamDecorator(RouteParamtypes.HEADERS);
export function Query();
export function Query(...pipes: (Type<PipeTransform> | PipeTransform)[]);
export function Query(): ParameterDecorator;
export function Query(
...pipes: (Type<PipeTransform> | PipeTransform)[]
): ParameterDecorator;
export function Query(
property: string,
...pipes: (Type<PipeTransform> | PipeTransform)[]
);
): ParameterDecorator;
export function Query(
property?: string | (Type<PipeTransform> | PipeTransform),
...pipes: (Type<PipeTransform> | PipeTransform)[]
) {
): ParameterDecorator {
return createPipesRouteParamDecorator(RouteParamtypes.QUERY)(
property,
...pipes,
);
}
export function Body();
export function Body(...pipes: (Type<PipeTransform> | PipeTransform)[]);
export function Body(): ParameterDecorator;
export function Body(
...pipes: (Type<PipeTransform> | PipeTransform)[]
): ParameterDecorator;
export function Body(
property: string,
...pipes: (Type<PipeTransform> | PipeTransform)[]
);
): ParameterDecorator;
export function Body(
property?: string | (Type<PipeTransform> | PipeTransform),
...pipes: (Type<PipeTransform> | PipeTransform)[]
) {
): ParameterDecorator {
return createPipesRouteParamDecorator(RouteParamtypes.BODY)(
property,
...pipes,
);
}
export function Param();
export function Param(...pipes: (Type<PipeTransform> | PipeTransform)[]);
export function Param(): ParameterDecorator;
export function Param(
...pipes: (Type<PipeTransform> | PipeTransform)[]
): ParameterDecorator;
export function Param(
property: string,
...pipes: (Type<PipeTransform> | PipeTransform)[]
);
): ParameterDecorator;
export function Param(
property?: string | (Type<PipeTransform> | PipeTransform),
...pipes: (Type<PipeTransform> | PipeTransform)[]
) {
): ParameterDecorator {
return createPipesRouteParamDecorator(RouteParamtypes.PARAM)(
property,
...pipes,

View File

@@ -1,2 +1,4 @@
export const InvalidModuleConfigMessage = (property: string) =>
`Invalid property '${property}' in @Module() decorator.`;
export const INVALID_MODULE_CONFIG_MESSAGE = (
text: TemplateStringsArray,
property: string,
) => `Invalid property '${property}' in the @Module() decorator.`;

View File

@@ -1,7 +1,7 @@
import { InvalidModuleConfigMessage } from './constants';
import { INVALID_MODULE_CONFIG_MESSAGE } from './constants';
export class InvalidModuleConfigException extends Error {
constructor(property: string) {
super(InvalidModuleConfigMessage(property));
super(INVALID_MODULE_CONFIG_MESSAGE`${property}`);
}
}

View File

@@ -1,4 +1,5 @@
import { GLOBAL_MODULE_METADATA } from '../../constants';
/**
* Makes the module global-scoped.
* Once imported will be available for all existing modules.

View File

@@ -1,20 +1,19 @@
import * as deprecate from 'deprecate';
import { METADATA as metadataConstants } from '../../constants';
import { ModuleMetadata } from '../../interfaces/modules/module-metadata.interface';
import { InvalidModuleConfigException } from './exceptions/invalid-module-config.exception';
const metadataKeys = [
metadataConstants.MODULES,
metadataConstants.IMPORTS,
metadataConstants.EXPORTS,
metadataConstants.COMPONENTS,
metadataConstants.CONTROLLERS,
metadataConstants.PROVIDERS,
];
const validateKeys = (keys: string[]) => {
const isKeyInvalid = key => metadataKeys.findIndex(k => k === key) < 0;
const validateKey = key => {
const isKeyInvalid = (key: string) =>
metadataKeys.findIndex(k => k === key) < 0;
const validateKey = (key: string) => {
if (!isKeyInvalid(key)) {
return;
}
@@ -29,41 +28,17 @@ const validateKeys = (keys: string[]) => {
* - `controllers` - the list of controllers (e.g. HTTP controllers)
* - `providers` - the list of providers that belong to this module. They can be injected between themselves.
* - `exports` - the set of components, which should be available for modules, which imports this module
* - `components` - @deprecated the list of components that belong to this module. They can be injected between themselves.
* @param options {ModuleMetadata} Module metadata
*/
export function Module(metadata: ModuleMetadata): ClassDecorator {
const propsKeys = Object.keys(metadata);
validateKeys(propsKeys);
showDeprecatedWarnings(metadata);
overrideModuleMetadata(metadata);
return (target: object) => {
for (const property in metadata) {
if (metadata.hasOwnProperty(property)) {
Reflect.defineMetadata(property, metadata[property], target);
Reflect.defineMetadata(property, (metadata as any)[property], target);
}
}
};
}
function overrideModuleMetadata(moduleMetadata: ModuleMetadata) {
moduleMetadata.modules = moduleMetadata.imports
? moduleMetadata.imports
: moduleMetadata.modules;
moduleMetadata.components = moduleMetadata.providers
? moduleMetadata.providers
: moduleMetadata.components;
}
function showDeprecatedWarnings(moduleMetadata: ModuleMetadata) {
const MODULES_DEPRECATED_WARNING =
'The "modules" key in the @Module() decorator is deprecated and will be removed within next major release. Use the "imports" key instead.';
const COMPONENTS_DEPRECATED_WARNING =
'The "components" key in the @Module() decorator is deprecated and will be removed within next major release. Use the "providers" key instead.';
moduleMetadata.modules && deprecate(MODULES_DEPRECATED_WARNING);
moduleMetadata.components && deprecate(COMPONENTS_DEPRECATED_WARNING);
}

View File

@@ -1,4 +1,5 @@
import { SHARED_MODULE_METADATA } from '../../constants';
/**
* Makes the module single-scoped (not singleton).
* In this case, Nest will always create a new instance of this particular module when it's imported by another one.
@@ -7,7 +8,7 @@ export function SingleScope(): ClassDecorator {
return (target: any) => {
const Metatype = target as FunctionConstructor;
const Type = class extends Metatype {
constructor(...args) {
constructor(...args: any[]) {
super(...args);
}
};

View File

@@ -1,8 +1,8 @@
import * as multer from 'multer';
import { Observable } from 'rxjs';
import { Inject, Optional } from '../../decorators';
import { mixin } from '../../decorators/core/component.decorator';
import { ExecutionContext } from '../../interfaces';
import { mixin } from '../../decorators/core/injectable.decorator';
import { ExecutionContext, Type } from '../../interfaces';
import {
MulterField,
MulterOptions,
@@ -17,7 +17,7 @@ type MulterInstance = any;
export function FileFieldsInterceptor(
uploadFields: MulterField[],
localOptions?: MulterOptions,
) {
): Type<NestInterceptor> {
class MixinInterceptor implements NestInterceptor {
protected multer: MulterInstance;
@@ -42,7 +42,7 @@ export function FileFieldsInterceptor(
this.multer.fields(uploadFields)(
ctx.getRequest(),
ctx.getResponse(),
err => {
(err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
@@ -55,5 +55,5 @@ export function FileFieldsInterceptor(
}
}
const Interceptor = mixin(MixinInterceptor);
return Interceptor;
return Interceptor as Type<NestInterceptor>;
}

View File

@@ -1,8 +1,8 @@
import * as multer from 'multer';
import { Observable } from 'rxjs';
import { Inject, Optional } from '../../decorators';
import { mixin } from '../../decorators/core/component.decorator';
import { ExecutionContext } from '../../interfaces';
import { mixin } from '../../decorators/core/injectable.decorator';
import { ExecutionContext, Type } from '../../interfaces';
import { MulterOptions } from '../../interfaces/external/multer-options.interface';
import { NestInterceptor } from '../../interfaces/features/nest-interceptor.interface';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
@@ -14,7 +14,7 @@ type MulterInstance = any;
export function FileInterceptor(
fieldName: string,
localOptions?: MulterOptions,
) {
): Type<NestInterceptor> {
class MixinInterceptor implements NestInterceptor {
protected multer: MulterInstance;
@@ -39,7 +39,7 @@ export function FileInterceptor(
this.multer.single(fieldName)(
ctx.getRequest(),
ctx.getResponse(),
err => {
(err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
@@ -52,5 +52,5 @@ export function FileInterceptor(
}
}
const Interceptor = mixin(MixinInterceptor);
return Interceptor;
return Interceptor as Type<NestInterceptor>;
}

View File

@@ -1,8 +1,8 @@
import * as multer from 'multer';
import { Observable } from 'rxjs';
import { Inject, Optional } from '../../decorators';
import { mixin } from '../../decorators/core/component.decorator';
import { ExecutionContext } from '../../interfaces';
import { mixin } from '../../decorators/core/injectable.decorator';
import { ExecutionContext, Type } from '../../interfaces';
import { MulterOptions } from '../../interfaces/external/multer-options.interface';
import { NestInterceptor } from '../../interfaces/features/nest-interceptor.interface';
import { MULTER_MODULE_OPTIONS } from '../files.constants';
@@ -15,7 +15,7 @@ export function FilesInterceptor(
fieldName: string,
maxCount?: number,
localOptions?: MulterOptions,
) {
): Type<NestInterceptor> {
class MixinInterceptor implements NestInterceptor {
protected multer: MulterInstance;
@@ -40,7 +40,7 @@ export function FilesInterceptor(
this.multer.array(fieldName, maxCount)(
ctx.getRequest(),
ctx.getResponse(),
err => {
(err: any) => {
if (err) {
const error = transformException(err);
return reject(error);
@@ -53,5 +53,5 @@ export function FilesInterceptor(
}
}
const Interceptor = mixin(MixinInterceptor);
return Interceptor;
return Interceptor as Type<NestInterceptor>;
}

View File

@@ -36,7 +36,7 @@ export class HttpService {
post<T = any>(
url: string,
data?,
data?: any,
config?: AxiosRequestConfig,
): Observable<AxiosResponse<T>> {
return defer(() => this.instance.post(url, data, config));
@@ -44,7 +44,7 @@ export class HttpService {
put<T = any>(
url: string,
data?,
data?: any,
config?: AxiosRequestConfig,
): Observable<AxiosResponse<T>> {
return defer(() => this.instance.put(url, data, config));
@@ -52,7 +52,7 @@ export class HttpService {
patch<T = any>(
url: string,
data?,
data?: any,
config?: AxiosRequestConfig,
): Observable<AxiosResponse<T>> {
return defer(() => this.instance.patch(url, data, config));

View File

@@ -21,7 +21,6 @@ export {
ExecutionContext,
ForwardReference,
HttpServer,
HttpServerFactory,
INestApplication,
INestApplicationContext,
INestExpressApplication,

View File

@@ -1,5 +1,5 @@
import { ArgumentsHost } from '../features/arguments-host.interface';
export interface ExceptionFilter<T = any> {
catch(exception: T, host: ArgumentsHost);
catch(exception: T, host: ArgumentsHost): any;
}

View File

@@ -1,5 +1,5 @@
import { ArgumentsHost } from '../features/arguments-host.interface';
export interface WsExceptionFilter<T = any> {
catch(exception: T, host: ArgumentsHost);
}
catch(exception: T, host: ArgumentsHost): any;
}

View File

@@ -28,7 +28,7 @@ export interface MulterOptions {
preservePath?: boolean;
};
fileFilter?(
req,
req: any,
file: {
/** Field name specified in the form */
fieldname: string;
@@ -58,4 +58,4 @@ export interface MulterField {
name: string;
/** Optional maximum number of files per field to accept. */
maxCount?: number;
}
}

View File

@@ -65,7 +65,7 @@ export interface ServeStaticOptions {
* path the file path that is being sent
* stat the stat object of the file that is being sent
*/
setHeaders?: (res, path: string, stat: any) => any;
setHeaders?: (res: any, path: string, stat: any) => any;
/**
* Creates a virtual path prefix

View File

@@ -4,7 +4,7 @@ export type Transform<T = any> = (value: T, metadata: ArgumentMetadata) => any;
export interface ArgumentMetadata {
readonly type: Paramtype;
readonly metatype?: new (...args) => any | undefined;
readonly metatype?: new (...args: any[]) => any | undefined;
readonly data?: string | undefined;
}

View File

@@ -1,3 +0,0 @@
export interface HttpServerFactory {
createServer(Function);
}

View File

@@ -1,51 +1,59 @@
import { IncomingMessage, ServerResponse } from 'http';
import { RequestMethod } from '../../enums';
export type ErrorHandler = (
export type ErrorHandler<TRequest = any, TResponse = any> = (
error: any,
req: Partial<IncomingMessage>,
res: ServerResponse | any,
req: TRequest,
res: TResponse,
next?: Function,
) => any;
export type RequestHandler = (
req: Partial<IncomingMessage>,
res: ServerResponse | any,
export type RequestHandler<TRequest = any, TResponse = any> = (
req: TRequest,
res: TResponse,
next?: Function,
) => any;
export interface HttpServer {
use(handler: RequestHandler | ErrorHandler): any;
use(path, handler: RequestHandler | ErrorHandler): any;
get(handler: RequestHandler): any;
get(path, handler: RequestHandler): any;
post(handler: RequestHandler): any;
post(path, handler: RequestHandler): any;
head(handler: RequestHandler): any;
head(path, handler: RequestHandler): any;
delete(handler: RequestHandler): any;
delete(path, handler: RequestHandler): any;
put(handler: RequestHandler): any;
put(path, handler: RequestHandler): any;
patch(handler: RequestHandler): any;
patch(path, handler: RequestHandler): any;
options(handler: RequestHandler): any;
options(path, handler: RequestHandler): any;
listen(port: number | string, callback?: () => void);
listen(port: number | string, hostname: string, callback?: () => void);
reply(response: any, body: any, statusCode: number);
render(response: any, view: string, options: any);
setHeader(response: any, name: string, value: string);
setErrorHandler?(handler: Function);
setNotFoundHandler?(handler: Function);
export interface HttpServer<TRequest = any, TResponse = any> {
use(
handler:
| RequestHandler<TRequest, TResponse>
| ErrorHandler<TRequest, TResponse>,
): any;
use(
path: string,
handler:
| RequestHandler<TRequest, TResponse>
| ErrorHandler<TRequest, TResponse>,
): any;
get(handler: RequestHandler<TRequest, TResponse>): any;
get(path: string, handler: RequestHandler<TRequest, TResponse>): any;
post(handler: RequestHandler<TRequest, TResponse>): any;
post(path: string, handler: RequestHandler<TRequest, TResponse>): any;
head(handler: RequestHandler<TRequest, TResponse>): any;
head(path: string, handler: RequestHandler<TRequest, TResponse>): any;
delete(handler: RequestHandler<TRequest, TResponse>): any;
delete(path: string, handler: RequestHandler<TRequest, TResponse>): any;
put(handler: RequestHandler<TRequest, TResponse>): any;
put(path: string, handler: RequestHandler<TRequest, TResponse>): any;
patch(handler: RequestHandler<TRequest, TResponse>): any;
patch(path: string, handler: RequestHandler<TRequest, TResponse>): any;
options(handler: RequestHandler<TRequest, TResponse>): any;
options(path: string, handler: RequestHandler<TRequest, TResponse>): any;
listen(port: number | string, callback?: () => void): any;
listen(port: number | string, hostname: string, callback?: () => void): any;
reply(response: any, body: any, statusCode: number): any;
render(response: any, view: string, options: any): any;
setHeader(response: any, name: string, value: string): any;
setErrorHandler?(handler: Function): any;
setNotFoundHandler?(handler: Function): any;
useStaticAssets?(...args: any[]): this;
setBaseViewsDir?(path: string): this;
setViewEngine?(engineOrOptions: any): this;
createMiddlewareFactory(
method: RequestMethod,
): (path: string, callback: Function) => any;
getRequestMethod?(request): string;
getRequestUrl?(request): string;
getRequestMethod?(request: TRequest): string;
getRequestUrl?(request: TResponse): string;
getInstance(): any;
getHttpServer(): any;
close();
close(): any;
}

View File

@@ -12,7 +12,6 @@ export * from './features/execution-context.interface';
export * from './features/nest-interceptor.interface';
export * from './features/paramtype.interface';
export * from './features/pipe-transform.interface';
export * from './http/http-server-factory.interface';
export * from './http/http-server.interface';
export * from './injectable.interface';
export * from './middleware';

View File

@@ -1,4 +1,4 @@
export interface CustomTransportStrategy {
listen(callback: () => void);
close();
listen(callback: () => void): any;
close(): any;
}

View File

@@ -23,8 +23,6 @@ export interface GrpcOptions {
credentials?: any;
protoPath: string;
package: string;
/** @deprecated */
root?: string;
loader?: {
keepCase?: boolean;
alternateCommentMode?: boolean;

View File

@@ -19,10 +19,4 @@ export interface ModuleMetadata {
| ForwardReference
| Abstract<any>
>;
/** @deprecated */
modules?: Array<
Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference
>;
/** @deprecated */
components?: Provider[];
}

View File

@@ -1,3 +1,3 @@
export interface OnModuleDestroy {
onModuleDestroy();
onModuleDestroy(): any;
}

View File

@@ -1,3 +1,3 @@
export interface OnModuleInit {
onModuleInit();
onModuleInit(): any;
}

View File

@@ -27,5 +27,5 @@ export interface INestApplicationContext {
* Sets custom logger service
* @returns {void}
*/
useLogger(logger: LoggerService);
useLogger(logger: LoggerService): void;
}

View File

@@ -21,7 +21,7 @@ export interface INestApplication extends INestApplicationContext {
*
* @returns {void}
*/
use(...args): this;
use(...args: any[]): this;
/**
* Enables CORS (Cross-Origin Resource Sharing)

View File

@@ -7,7 +7,7 @@ export interface INestExpressApplication {
*
* @returns {this}
*/
set(...args): this;
set(...args: any[]): this;
/**
* A wrapper function around native `express.engine()` method.
@@ -15,7 +15,7 @@ export interface INestExpressApplication {
*
* @returns {this}
*/
engine(...args): this;
engine(...args: any[]): this;
/**
* A wrapper function around native `express.enable()` method.
@@ -23,7 +23,7 @@ export interface INestExpressApplication {
*
* @returns {this}
*/
enable(...args): this;
enable(...args: any[]): this;
/**
* A wrapper function around native `express.disable()` method.
@@ -31,7 +31,7 @@ export interface INestExpressApplication {
*
* @returns {this}
*/
disable(...args): this;
disable(...args: any[]): this;
/**
* Sets a base directory for public assets.

View File

@@ -5,7 +5,7 @@ export interface INestFastifyApplication {
*
* @returns {this}
*/
register(...args): this;
register(...args: any[]): this;
/**
* Sets a base directory for public assets.

View File

@@ -1,9 +1,9 @@
import { WebSocketAdapter } from './websockets/web-socket-adapter.interface';
import { ExceptionFilter } from './exceptions/exception-filter.interface';
import { PipeTransform } from './features/pipe-transform.interface';
import { NestInterceptor } from './features/nest-interceptor.interface';
import { CanActivate } from './features/can-activate.interface';
import { NestInterceptor } from './features/nest-interceptor.interface';
import { PipeTransform } from './features/pipe-transform.interface';
import { INestApplicationContext } from './nest-application-context.interface';
import { WebSocketAdapter } from './websockets/web-socket-adapter.interface';
export interface INestMicroservice extends INestApplicationContext {
/**
@@ -12,7 +12,7 @@ export interface INestMicroservice extends INestApplicationContext {
* @param {Function} callback
* @returns {Promise}
*/
listen(callback: () => void);
listen(callback: () => void): any;
/**
* Starts the microservice (can be awaited).

View File

@@ -1,3 +1,3 @@
export interface OnApplicationBootstrap {
onApplicationBootstrap();
onApplicationBootstrap(): any;
}

View File

@@ -1,9 +1,9 @@
import { Observable } from 'rxjs';
export interface WebSocketAdapter<T = any> {
create(port: number, options?: T);
bindClientConnect(server: any, callback: (...args) => void);
bindClientDisconnect?(client: any, callback: (...args) => void);
create(port: number, options?: T): any;
bindClientConnect(server: any, callback: (...args: any[]) => void): any;
bindClientDisconnect?(client: any, callback: (...args: any[]) => void): any;
bindMessageHandlers(
client: any,
handlers: Array<{
@@ -11,6 +11,6 @@ export interface WebSocketAdapter<T = any> {
callback: (...args: any[]) => Observable<any> | Promise<any> | any;
}>,
transform: (data: any) => Observable<any>,
);
close(server: any);
): any;
close(server: any): any;
}

View File

@@ -11,7 +11,6 @@
"dependencies": {
"axios": "0.18.0",
"cli-color": "1.2.0",
"deprecate": "1.0.0",
"multer": "1.3.0",
"uuid": "3.3.2"
},

View File

@@ -1,5 +1,5 @@
import { Optional } from '../decorators';
import { Injectable } from '../decorators/core/component.decorator';
import { Injectable } from '../decorators/core';
import {
ArgumentMetadata,
BadRequestException,
@@ -39,12 +39,12 @@ export class ValidationPipe implements PipeTransform<any> {
this.isDetailedOutputDisabled ? undefined : errors,
));
const loadPkg = pkg => loadPackage(pkg, 'ValidationPipe');
const loadPkg = (pkg: any) => loadPackage(pkg, 'ValidationPipe');
classValidator = loadPkg('class-validator');
classTransformer = loadPkg('class-transformer');
}
public async transform(value, metadata: ArgumentMetadata) {
public async transform(value: any, metadata: ArgumentMetadata) {
const { metatype } = metadata;
if (!metatype || !this.toValidate(metadata)) {
return value;
@@ -60,8 +60,8 @@ export class ValidationPipe implements PipeTransform<any> {
return this.isTransformEnabled
? entity
: Object.keys(this.validatorOptions).length > 0
? classTransformer.classToPlain(entity)
: value;
? classTransformer.classToPlain(entity)
: value;
}
private toValidate(metadata: ArgumentMetadata): boolean {

View File

@@ -21,7 +21,8 @@ const REFLECTOR = 'Reflector';
@Injectable()
export class ClassSerializerInterceptor implements NestInterceptor {
constructor(@Inject(REFLECTOR) protected readonly reflector: any) {
const loadPkg = pkg => loadPackage(pkg, 'ClassSerializerInterceptor');
const loadPkg = (pkg: any) =>
loadPackage(pkg, 'ClassSerializerInterceptor');
classTransformer = loadPkg('class-transformer');
}
@@ -53,7 +54,7 @@ export class ClassSerializerInterceptor implements NestInterceptor {
}
transformToPlain(
plainOrClass,
plainOrClass: any,
options: ClassTransformOptions,
): PlainLiteralObject {
return plainOrClass && plainOrClass.constructor !== Object
@@ -70,7 +71,9 @@ export class ClassSerializerInterceptor implements NestInterceptor {
);
}
private reflectSerializeMetadata(obj): ClassTransformOptions | undefined {
private reflectSerializeMetadata(
obj: object | Function,
): ClassTransformOptions | undefined {
return this.reflector.get(CLASS_SERIALIZER_OPTIONS, obj);
}
}

View File

@@ -3,12 +3,12 @@ import { Injectable, Optional } from '../decorators';
import { NestEnvironment } from '../enums/nest-environment.enum';
import { isObject } from '../utils/shared.utils';
declare const process;
declare const process: any;
export interface LoggerService {
log(message: any, context?: string);
error(message: any, trace?: string, context?: string);
warn(message: any, context?: string);
log(message: any, context?: string): any;
error(message: any, trace?: string, context?: string): any;
warn(message: any, context?: string): any;
}
@Injectable()

View File

@@ -1,21 +1,21 @@
import { ParseIntPipe } from '@nestjs/common';
import { ROUTE_ARGS_METADATA } from '@nestjs/common/constants';
import { expect } from 'chai';
import { createRouteParamDecorator } from '../../decorators/http/create-route-param-metadata.decorator';
import { createParamDecorator } from '../../decorators/http/create-route-param-metadata.decorator';
describe('createRouteParamDecorator', () => {
describe('createParamDecorator', () => {
let result;
beforeEach(() => {
const fn = (data, req) => true;
result = createRouteParamDecorator(fn);
result = createParamDecorator(fn);
});
it('should return a function as a first element', () => {
expect(result).to.be.a('function');
});
describe('returned decorator', () => {
const factoryFn = (data, req) => true;
const Decorator = createRouteParamDecorator(factoryFn);
const Decorator = createParamDecorator(factoryFn);
describe('when 0 pipes have been passed', () => {
const data = { data: 'test' };

View File

@@ -1,22 +1,5 @@
import { expect } from 'chai';
import { Component, MiddlewareFunction, Interceptor, mixin, Injectable } from '../../index';
describe('@Component', () => {
@Component()
class TestComponent {
constructor(param: number, test: string) {}
}
it('should enhance component with "design:paramtypes" metadata', () => {
const constructorParams = Reflect.getMetadata(
'design:paramtypes',
TestComponent,
);
expect(constructorParams[0]).to.be.eql(Number);
expect(constructorParams[1]).to.be.eql(String);
});
});
import { Injectable, mixin } from '../../index';
describe('@Injectable', () => {
@Injectable()
@@ -35,25 +18,8 @@ describe('@Injectable', () => {
});
});
describe('@Interceptor', () => {
@Interceptor()
class TestInterceptor {
constructor(param: number, test: string) {}
}
it('should enhance component with "design:paramtypes" metadata', () => {
const constructorParams = Reflect.getMetadata(
'design:paramtypes',
TestInterceptor,
);
expect(constructorParams[0]).to.be.eql(Number);
expect(constructorParams[1]).to.be.eql(String);
});
});
describe('mixin', () => {
@Interceptor()
@Injectable()
class Test {
constructor(param: number, test: string) {}
}

View File

@@ -1,11 +1,11 @@
import { expect } from 'chai';
import { Module } from '../../decorators/modules/module.decorator';
import { InvalidModuleConfigException } from '../../decorators/modules/exceptions/invalid-module-config.exception';
import { Module } from '../../decorators/modules/module.decorator';
describe('@Module', () => {
const moduleProps = {
components: ['Test'],
modules: ['Test'],
providers: ['Test'],
imports: ['Test'],
exports: ['Test'],
controllers: ['Test'],
};
@@ -14,13 +14,13 @@ describe('@Module', () => {
class TestModule {}
it('should enhance class with expected module metadata', () => {
const modules = Reflect.getMetadata('modules', TestModule);
const components = Reflect.getMetadata('components', TestModule);
const imports = Reflect.getMetadata('imports', TestModule);
const providers = Reflect.getMetadata('providers', TestModule);
const exports = Reflect.getMetadata('exports', TestModule);
const controllers = Reflect.getMetadata('controllers', TestModule);
expect(modules).to.be.eql(moduleProps.modules);
expect(components).to.be.eql(moduleProps.components);
expect(imports).to.be.eql(moduleProps.imports);
expect(providers).to.be.eql(moduleProps.providers);
expect(controllers).to.be.eql(moduleProps.controllers);
expect(exports).to.be.eql(moduleProps.exports);
});
@@ -35,48 +35,4 @@ describe('@Module', () => {
InvalidModuleConfigException,
);
});
describe(`when "imports" is used`, () => {
const imports = ['imports'];
@Module({
imports,
} as any)
class TestModule2 {}
it(`should override "modules" metadata when "imports" exists`, () => {
const modules = Reflect.getMetadata('modules', TestModule2);
expect(modules).to.be.eql(imports);
});
@Module({
...moduleProps as any,
imports: null,
} as any)
class TestModule3 {}
it(`should not override "modules" metadata when "imports" does not exist`, () => {
const modules = Reflect.getMetadata('modules', TestModule3);
expect(modules).to.be.eql(moduleProps.modules);
});
});
describe(`when "providers" is used`, () => {
const providers = ['providers'];
@Module({
providers,
} as any)
class TestModule2 {}
it(`should override "components" metadata when "providers" exists`, () => {
const components = Reflect.getMetadata('components', TestModule2);
expect(components).to.be.eql(providers);
});
@Module({
...moduleProps as any,
providers: null,
} as any)
class TestModule3 {}
it(`should not override "components" metadata when "providers" does not exist`, () => {
const components = Reflect.getMetadata('components', TestModule3);
expect(components).to.be.eql(moduleProps.components);
});
});
});

View File

@@ -1,6 +1,6 @@
import { Constructor } from './merge-with-values.util';
import { Injectable } from '../decorators/core';
import { NestMiddleware } from '../interfaces/middleware/nest-middleware.interface';
import { Injectable } from '../decorators/core/component.decorator';
import { Constructor } from './merge-with-values.util';
export const BindResolveMiddlewareValues = <
T extends Constructor<NestMiddleware>

View File

@@ -7,7 +7,7 @@ export const MergeWithValues = <T extends Constructor<{}>>(data: {
}) => {
return (Metatype: T): any => {
const Type = class extends Metatype {
constructor(...args) {
constructor(...args: any[]) {
super(...args);
}
};

View File

@@ -1,13 +1,12 @@
export const isUndefined = (obj): obj is undefined =>
export const isUndefined = (obj: any): obj is undefined =>
typeof obj === 'undefined';
export const isFunction = (fn): boolean => typeof fn === 'function';
export const isObject = (fn): fn is object => !isNil(fn) && typeof fn === 'object';
export const isString = (fn): fn is string => typeof fn === 'string';
export const isConstructor = (fn): boolean => fn === 'constructor';
export const isObject = (fn: any): fn is object =>
!isNil(fn) && typeof fn === 'object';
export const validatePath = (path?: string): string =>
path
? path.charAt(0) !== '/' ? '/' + path : path
: '';
export const isNil = (obj): boolean => isUndefined(obj) || obj === null;
export const isEmpty = (array): boolean => !(array && array.length > 0);
export const isSymbol = (fn): fn is symbol => typeof fn === 'symbol';
path ? (path.charAt(0) !== '/' ? '/' + path : path) : '';
export const isFunction = (fn: any): boolean => typeof fn === 'function';
export const isString = (fn: any): fn is string => typeof fn === 'string';
export const isConstructor = (fn: any): boolean => fn === 'constructor';
export const isNil = (obj: any): boolean => isUndefined(obj) || obj === null;
export const isEmpty = (array: any): boolean => !(array && array.length > 0);
export const isSymbol = (fn: any): fn is symbol => typeof fn === 'symbol';

View File

@@ -1,6 +1,6 @@
(The MIT License)
Copyright (c) 2017 Kamil Myśliwiec <http://kamilmysliwiec.com>
Copyright (c) 2018 Kamil Myśliwiec <https://kamilmysliwiec.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the

View File

@@ -7,9 +7,9 @@ import { RouterMethodFactory } from '../helpers/router-method-factory';
export class ExpressAdapter implements HttpServer {
private readonly routerMethodFactory = new RouterMethodFactory();
private httpServer = null;
private httpServer: any = null;
constructor(private readonly instance) {}
constructor(private readonly instance: any) {}
use(...args: any[]) {
return this.instance.use(...args);
@@ -103,19 +103,19 @@ export class ExpressAdapter implements HttpServer {
return this.instance.close();
}
set(...args) {
set(...args: any[]) {
return this.instance.set(...args);
}
enable(...args) {
enable(...args: any[]) {
return this.instance.enable(...args);
}
disable(...args) {
disable(...args: any[]) {
return this.instance.disable(...args);
}
engine(...args) {
engine(...args: any[]) {
return this.instance.engine(...args);
}

View File

@@ -88,11 +88,11 @@ export class FastifyAdapter {
return this.instance as T;
}
register(...args) {
register(...args: any[]) {
return this.instance.register(...args);
}
inject(...args) {
inject(...args: any[]) {
return this.instance.inject(...args);
}

View File

@@ -12,7 +12,7 @@ import { Module } from '../injector/module';
* @param instance The instance which should get the name from
*/
const getInstanceName = (instance: any) =>
(instance && (instance as Type<any>).name);
instance && (instance as Type<any>).name;
/**
* Returns the name of the dependency
@@ -20,13 +20,15 @@ const getInstanceName = (instance: any) =>
* (= injection token). As fallback it returns '+'
* @param dependency The dependency whichs name should get displayed
*/
const getDependencyName = (dependency: InjectorDependency) => getInstanceName(dependency) || dependency || '+';
const getDependencyName = (dependency: InjectorDependency) =>
getInstanceName(dependency) || dependency || '+';
/**
* Returns the name of the module
* Tries to get the class name. As fallback it returns 'current'.
* @param module The module which should get displayed
*/
const getModuleName = (module: Module) => (module && getInstanceName(module.metatype)) || 'current';
const getModuleName = (module: Module) =>
(module && getInstanceName(module.metatype)) || 'current';
export const UNKNOWN_DEPENDENCIES_MESSAGE = (
type: string,
@@ -45,20 +47,30 @@ export const UNKNOWN_DEPENDENCIES_MESSAGE = (
message += ` (`;
message += dependenciesName.join(', ');
message += `). Please make sure that the argument at index [${index}] is available in the ${getModuleName(module)} context.`;
message += `). Please make sure that the argument at index [${index}] is available in the ${getModuleName(
module,
)} context.`;
return message;
};
export const INVALID_MIDDLEWARE_MESSAGE = (text, name: string) =>
`The middleware doesn't provide the 'resolve' method (${name})`;
export const INVALID_MIDDLEWARE_MESSAGE = (
text: TemplateStringsArray,
name: string,
) => `The middleware doesn't provide the 'resolve' method (${name})`;
export const INVALID_MODULE_MESSAGE = (text, scope: string) =>
export const INVALID_MODULE_MESSAGE = (
text: TemplateStringsArray,
scope: string,
) =>
`Nest cannot create the module instance. Often, this is because of a circular dependency between modules. Use forwardRef() to avoid it. (Read more https://docs.nestjs.com/advanced/circular-dependency.) Scope [${scope}]`;
export const UNKNOWN_EXPORT_MESSAGE = (text, module: string) =>
`Nest cannot export a component/module that is not a part of the currently processed module (${module}). Please verify whether each exported unit is available in this particular context.`;
export const UNKNOWN_EXPORT_MESSAGE = (
text: TemplateStringsArray,
module: string,
) =>
`Nest cannot export a provider/module that is not a part of the currently processed module (${module}). Please verify whether each exported unit is available in this particular context.`;
export const INVALID_CLASS_MESSAGE = (text, value: any) =>
export const INVALID_CLASS_MESSAGE = (text: TemplateStringsArray, value: any) =>
`ModuleRef cannot instantiate class (${value} is not constructable).`;
export const INVALID_MIDDLEWARE_CONFIGURATION = `Invalid middleware configuration passed inside the module 'configure()' method.`;

View File

@@ -1,7 +1,11 @@
import { FILTER_CATCH_EXCEPTIONS } from '@nestjs/common/constants';
import { Type } from '@nestjs/common/interfaces';
import { ExceptionFilter } from '@nestjs/common/interfaces/exceptions/exception-filter.interface';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ContextCreator } from '../helpers/context-creator';
import { NestContainer } from '../injector/container';
@@ -42,7 +46,9 @@ export class BaseExceptionFilterContext extends ContextCreator {
: null;
}
public getInstanceByMetatype(filter): { instance: any } | undefined {
public getInstanceByMetatype<T extends Record<string, any>>(
filter: T,
): { instance: any } | undefined {
if (!this.moduleContext) {
return undefined;
}

View File

@@ -51,7 +51,7 @@ export class BaseExceptionFilter<T = any> implements ExceptionFilter<T> {
applicationRef.reply(host.getArgByIndex(1), message, exception.getStatus());
}
public isExceptionObject(err): err is Error {
public isExceptionObject(err: any): err is Error {
return isObject(err) && !!(err as Error).message;
}
}

View File

@@ -22,7 +22,10 @@ export class ExceptionsHandler extends BaseExceptionFilter {
this.filters = filters;
}
public invokeCustomFilters(exception, response): boolean {
public invokeCustomFilters<T = any>(
exception: T,
ctx: ArgumentsHost,
): boolean {
if (isEmpty(this.filters)) return false;
const filter = this.filters.find(({ exceptionMetatypes }) => {
@@ -33,7 +36,7 @@ export class ExceptionsHandler extends BaseExceptionFilter {
);
return hasMetatype;
});
filter && filter.func(exception, response);
filter && filter.func(exception, ctx);
return !!filter;
}
}

View File

@@ -1,6 +1,6 @@
import { isEmpty } from '@nestjs/common/utils/shared.utils';
import { Controller } from '@nestjs/common/interfaces';
import { CanActivate } from '@nestjs/common';
import { Controller } from '@nestjs/common/interfaces';
import { isEmpty } from '@nestjs/common/utils/shared.utils';
import { Observable } from 'rxjs';
import { ExecutionContextHost } from '../helpers/execution-context.host';
@@ -9,7 +9,7 @@ export class GuardsConsumer {
guards: CanActivate[],
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): Promise<boolean> {
if (!guards || isEmpty(guards)) {
return true;
@@ -28,7 +28,7 @@ export class GuardsConsumer {
public createContext(
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): ExecutionContextHost {
return new ExecutionContextHost(
args,

View File

@@ -2,7 +2,11 @@ import { CanActivate } from '@nestjs/common';
import { GUARDS_METADATA } from '@nestjs/common/constants';
import { Controller } from '@nestjs/common/interfaces';
import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ContextCreator } from '../helpers/context-creator';
import { NestContainer } from '../injector/container';
@@ -19,7 +23,7 @@ export class GuardsContextCreator extends ContextCreator {
public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
module: string,
): CanActivate[] {
this.moduleContext = module;
@@ -50,7 +54,9 @@ export class GuardsContextCreator extends ContextCreator {
: null;
}
public getInstanceByMetatype(guard): { instance: any } | undefined {
public getInstanceByMetatype<T extends Record<string, any>>(
guard: T,
): { instance: any } | undefined {
if (!this.moduleContext) {
return undefined;
}

View File

@@ -1,13 +1,13 @@
import { HttpServer } from '@nestjs/common';
export class ApplicationReferenceHost {
private _applicationRef: HttpServer | any;
export class ApplicationReferenceHost<T extends HttpServer = any> {
private _applicationRef: T;
set applicationRef(applicationRef: any) {
set applicationRef(applicationRef: T) {
this._applicationRef = applicationRef;
}
get applicationRef(): HttpServer | any {
get applicationRef(): T | undefined {
return this._applicationRef;
}
}

View File

@@ -8,7 +8,7 @@ export abstract class ContextCreator {
public createContext<T extends any[], R extends any[]>(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
metadataKey: string,
): R {
const globalMetadata =
@@ -28,7 +28,7 @@ export abstract class ContextCreator {
}
public reflectMethodMetadata<T>(
callback: (...args) => any,
callback: (...args: any[]) => any,
metadataKey: string,
): T {
return Reflect.getMetadata(metadataKey, callback);

View File

@@ -36,7 +36,7 @@ export class ContextUtils {
}
public createNullArray(length: number): any[] {
return Array.apply(null, { length }).fill(undefined);
return Array.apply(null, { length } as any).fill(undefined);
}
public mergeParamsMetatypes(

View File

@@ -1,7 +1,7 @@
import { ForbiddenException, ParamData } from '@nestjs/common';
import { CUSTOM_ROUTE_AGRS_METADATA } from '@nestjs/common/constants';
import { Controller, Transform } from '@nestjs/common/interfaces';
import { isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import { FORBIDDEN_MESSAGE } from '../guards/constants';
import { GuardsConsumer } from '../guards/guards-consumer';
import { GuardsContextCreator } from '../guards/guards-context-creator';
@@ -52,7 +52,7 @@ export class ExternalContextCreator {
public create<T extends ParamsMetadata = ParamsMetadata>(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
methodName: string,
metadataKey?: string,
paramsFactory?: ParamsFactory,
@@ -87,7 +87,7 @@ export class ExternalContextCreator {
: [];
const fnApplyPipes = this.createPipesFn(pipes, paramsOptions);
const handler = (initialArgs, ...args) => async () => {
const handler = (initialArgs: any[], ...args: any[]) => async () => {
if (fnApplyPipes) {
await fnApplyPipes(initialArgs, ...args);
return callback.apply(instance, initialArgs);
@@ -95,7 +95,7 @@ export class ExternalContextCreator {
return callback.apply(instance, args);
};
return async (...args) => {
return async (...args: any[]) => {
const initialArgs = this.contextUtils.createNullArray(argsLength);
const canActivate = await this.guardsConsumer.tryActivate(
guards,
@@ -123,19 +123,19 @@ export class ExternalContextCreator {
return '';
}
for (const [key, module] of [...this.modulesContainer.entries()]) {
if (this.findComponentByClassName(module, className)) {
if (this.findProviderByClassName(module, className)) {
return key;
}
}
return '';
}
public findComponentByClassName(module: Module, className: string): boolean {
const { components } = module;
const hasComponent = [...components.keys()].some(
component => component === className,
public findProviderByClassName(module: Module, className: string): boolean {
const { providers } = module;
const hasProvider = [...providers.keys()].some(
provider => provider === className,
);
return hasComponent;
return hasProvider;
}
public exchangeKeysForValues<TMetadata = any>(
@@ -158,16 +158,19 @@ export class ExternalContextCreator {
return { index, extractValue: customExtractValue, type, data, pipes };
}
const numericType = Number(type);
const extractValue = (...args) =>
const extractValue = (...args: any[]) =>
paramsFactory.exchangeKeyForValue(numericType, data, args);
return { index, extractValue, type: numericType, data, pipes };
});
}
public getCustomFactory(factory: (...args) => void, data): (...args) => any {
public getCustomFactory(
factory: (...args: any[]) => void,
data: any,
): (...args: any[]) => any {
return isFunction(factory)
? (...args) => factory(data, args)
? (...args: any[]) => factory(data, args)
: () => null;
}
@@ -175,7 +178,7 @@ export class ExternalContextCreator {
pipes: any[],
paramsOptions: (ParamProperties & { metatype?: any })[],
) {
const pipesFn = async (args, ...gqlArgs) => {
const pipesFn = async (args: any[], ...params: any[]) => {
await Promise.all(
paramsOptions.map(async param => {
const {
@@ -186,7 +189,7 @@ export class ExternalContextCreator {
metatype,
pipes: paramPipes,
} = param;
const value = extractValue(...gqlArgs);
const value = extractValue(...params);
args[index] = await this.getParamValue(
value,
@@ -201,7 +204,7 @@ export class ExternalContextCreator {
public async getParamValue<T>(
value: T,
{ metatype, type, data },
{ metatype, type, data }: { metatype: any; type: any; data: any },
transforms: Transform<any>[],
): Promise<any> {
return this.pipesConsumer.apply(
@@ -211,7 +214,7 @@ export class ExternalContextCreator {
);
}
public async transformToResult(resultOrDeffered) {
public async transformToResult(resultOrDeffered: any) {
if (resultOrDeffered && isFunction(resultOrDeffered.subscribe)) {
return resultOrDeffered.toPromise();
}

View File

@@ -1,9 +1,11 @@
import { RequestMethod } from '@nestjs/common/enums/request-method.enum';
export const MODULE_INIT_MESSAGE = (text, module: string) =>
`${module} dependencies initialized`;
export const MODULE_INIT_MESSAGE = (
text: TemplateStringsArray,
module: string,
) => `${module} dependencies initialized`;
export const ROUTE_MAPPED_MESSAGE = (path: string, method) =>
export const ROUTE_MAPPED_MESSAGE = (path: string, method: string | number) =>
`Mapped {${path}, ${RequestMethod[method]}} route`;
export const CONTROLLER_MAPPING_MESSAGE = (name: string, path: string) =>

View File

@@ -1,7 +1,8 @@
import { HttpServer } from '@nestjs/common';
import { RequestMethod } from '@nestjs/common/enums/request-method.enum';
export class RouterMethodFactory {
public get(target, requestMethod: RequestMethod) {
public get(target: HttpServer, requestMethod: RequestMethod): Function {
switch (requestMethod) {
case RequestMethod.POST:
return target.post;

View File

@@ -24,8 +24,8 @@ export class ContainerScanner {
contextModule: Partial<Module>,
): TResult {
const dependencies = new Map([
...contextModule.components,
...contextModule.routes,
...contextModule.providers,
...contextModule.controllers,
...contextModule.injectables,
]);
const name = isFunction(metatypeOrToken)
@@ -38,14 +38,14 @@ export class ContainerScanner {
return (instanceWrapper as InstanceWrapper<any>).instance;
}
private initFlatContainer() {
private initFlatContainer(): void {
if (this.flatContainer) {
return undefined;
return;
}
const modules = this.container.getModules();
const initialValue = {
components: [],
routes: [],
const initialValue: any = {
providers: [],
controllers: [],
injectables: [],
};
const merge = <T = any>(
@@ -55,8 +55,8 @@ export class ContainerScanner {
this.flatContainer = ([...modules.values()].reduce(
(current, next) => ({
components: merge(current.components, next.components),
routes: merge(current.routes, next.routes),
providers: merge(current.providers, next.providers),
controllers: merge(current.controllers, next.controllers),
injectables: merge(current.injectables, next.injectables),
}),
initialValue,

View File

@@ -27,7 +27,7 @@ export class NestContainer {
private applicationRef: any;
constructor(
private readonly _applicationConfig: ApplicationConfig = void 0,
private readonly _applicationConfig: ApplicationConfig = undefined,
) {}
get applicationConfig(): ApplicationConfig | undefined {
@@ -72,20 +72,19 @@ export class NestContainer {
token: string,
dynamicModuleMetadata: Partial<DynamicModule>,
scope: Type<any>[],
) {
): void {
if (!dynamicModuleMetadata) {
return undefined;
return;
}
this.dynamicModulesMetadata.set(token, dynamicModuleMetadata);
const { modules, imports } = dynamicModuleMetadata;
this.addDynamicModules(modules, scope);
const { imports } = dynamicModuleMetadata;
this.addDynamicModules(imports, scope);
}
public addDynamicModules(modules: any[], scope: Type<any>[]) {
if (!modules) {
return undefined;
return;
}
modules.forEach(module => this.addModule(module, scope));
}
@@ -120,15 +119,15 @@ export class NestContainer {
module.addRelatedModule(related);
}
public addComponent(component: Type<any>, token: string): string {
if (!component) {
public addProvider(provider: Type<any>, token: string): string {
if (!provider) {
throw new CircularDependencyException();
}
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
return module.addComponent(component);
return module.addProvider(provider);
}
public addInjectable(injectable: Type<any>, token: string) {
@@ -139,12 +138,12 @@ export class NestContainer {
module.addInjectable(injectable);
}
public addExportedComponent(exportedComponent: Type<any>, token: string) {
public addExportedProvider(provider: Type<any>, token: string) {
if (!this.modules.has(token)) {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
module.addExportedComponent(exportedComponent);
module.addExportedProvider(provider);
}
public addController(controller: Type<any>, token: string) {
@@ -152,14 +151,14 @@ export class NestContainer {
throw new UnknownModuleException();
}
const module = this.modules.get(token);
module.addRoute(controller);
module.addController(controller);
}
public clear() {
this.modules.clear();
}
public replace(toReplace, options: any & { scope: any[] | null }) {
public replace(toReplace: any, options: any & { scope: any[] | null }) {
[...this.modules.values()].forEach(module => {
module.replace(toReplace, options);
});
@@ -177,7 +176,7 @@ export class NestContainer {
public bindGlobalModuleToModule(module: Module, globalModule: Module) {
if (module === globalModule) {
return undefined;
return;
}
module.addRelatedModule(globalModule);
}

View File

@@ -84,12 +84,12 @@ export class Injector {
);
}
public async loadInstanceOfRoute(
public async loadInstanceOfController(
wrapper: InstanceWrapper<Controller>,
module: Module,
) {
const routes = module.routes;
await this.loadInstance<Controller>(wrapper, routes, module);
const controllers = module.controllers;
await this.loadInstance<Controller>(wrapper, controllers, module);
}
public async loadInstanceOfInjectable(
@@ -103,7 +103,7 @@ export class Injector {
public loadPrototypeOfInstance<T>(
{ metatype, name }: InstanceWrapper<T>,
collection: Map<string, InstanceWrapper<T>>,
) {
): void {
if (!collection) {
return null;
}
@@ -121,8 +121,8 @@ export class Injector {
wrapper: InstanceWrapper<Injectable>,
module: Module,
) {
const components = module.components;
await this.loadInstance<Injectable>(wrapper, components, module);
const providers = module.providers;
await this.loadInstance<Injectable>(wrapper, providers, module);
}
public applyDoneHook<T>(wrapper: InstanceWrapper<T>): () => void {
@@ -152,7 +152,7 @@ export class Injector {
if (targetWrapper.isResolved) {
return;
}
const callback = async instances => {
const callback = async (instances: any[]) => {
const properties = await this.resolveProperties(wrapper, module, inject);
const instance = await this.instantiateClass(
instances,
@@ -169,7 +169,7 @@ export class Injector {
wrapper: InstanceWrapper<T>,
module: Module,
inject: InjectorDependency[],
callback: (args) => void,
callback: (args: any[]) => void,
) {
const dependencies = isNil(inject)
? this.reflectConstructorParams(wrapper.metatype)
@@ -259,9 +259,9 @@ export class Injector {
dependencyContext: InjectorDependencyContext,
wrapper: InstanceWrapper<T>,
) {
const components = module.components;
const providers = module.providers;
const instanceWrapper = await this.lookupComponent(
components,
providers,
module,
{ ...dependencyContext, name },
wrapper,
@@ -276,7 +276,7 @@ export class Injector {
}
public async lookupComponent<T = any>(
components: Map<string, any>,
providers: Map<string, any>,
module: Module,
dependencyContext: InjectorDependencyContext,
wrapper: InstanceWrapper<T>,
@@ -284,7 +284,7 @@ export class Injector {
const { name } = dependencyContext;
const scanInExports = () =>
this.lookupComponentInExports(dependencyContext, module, wrapper);
return components.has(name) ? components.get(name) : scanInExports();
return providers.has(name) ? providers.get(name) : scanInExports();
}
public async lookupComponentInExports<T = any>(
@@ -309,8 +309,8 @@ export class Injector {
public async lookupComponentInRelatedModules(
module: Module,
name: any,
moduleRegistry = [],
) {
moduleRegistry: any[] = [],
): Promise<any> {
let componentRef = null;
const relatedModules: Set<Module> = module.relatedModules || new Set();
@@ -320,8 +320,8 @@ export class Injector {
continue;
}
moduleRegistry.push(relatedModule.id);
const { components, exports } = relatedModule;
if (!exports.has(name) || !components.has(name)) {
const { providers, exports } = relatedModule;
if (!exports.has(name) || !providers.has(name)) {
const instanceRef = await this.lookupComponentInRelatedModules(
relatedModule,
name,
@@ -332,7 +332,7 @@ export class Injector {
}
continue;
}
componentRef = components.get(name);
componentRef = providers.get(name);
if (!componentRef.isResolved && !componentRef.forwardRef) {
await this.loadInstanceOfComponent(componentRef, relatedModule);
break;
@@ -383,7 +383,7 @@ export class Injector {
const optionalKeys: string[] =
Reflect.getMetadata(OPTIONAL_PROPERTY_DEPS_METADATA, type) || [];
return properties.map(item => ({
return properties.map((item: any) => ({
...item,
name: item.type,
isOptional: optionalKeys.includes(item.key),
@@ -393,7 +393,7 @@ export class Injector {
public applyProperties<T = any>(
instance: T,
properties: PropertyDependency[],
) {
): void {
if (!isObject(instance)) {
return undefined;
}

View File

@@ -21,18 +21,18 @@ export class InstanceLoader {
private createPrototypes(modules: Map<string, Module>) {
modules.forEach(module => {
this.createPrototypesOfComponents(module);
this.createPrototypesOfProviders(module);
this.createPrototypesOfInjectables(module);
this.createPrototypesOfRoutes(module);
this.createPrototypesOfControllers(module);
});
}
private async createInstances(modules: Map<string, Module>) {
await Promise.all(
[...modules.values()].map(async module => {
await this.createInstancesOfComponents(module);
await this.createInstancesOfProviders(module);
await this.createInstancesOfInjectables(module);
await this.createInstancesOfRoutes(module);
await this.createInstancesOfControllers(module);
const { name } = module.metatype;
this.logger.log(MODULE_INIT_MESSAGE`${name}`);
@@ -40,33 +40,36 @@ export class InstanceLoader {
);
}
private createPrototypesOfComponents(module: Module) {
module.components.forEach(wrapper => {
private createPrototypesOfProviders(module: Module) {
module.providers.forEach(wrapper => {
this.injector.loadPrototypeOfInstance<Injectable>(
wrapper,
module.components,
module.providers,
);
});
}
private async createInstancesOfComponents(module: Module) {
private async createInstancesOfProviders(module: Module) {
await Promise.all(
[...module.components.values()].map(async wrapper =>
[...module.providers.values()].map(async wrapper =>
this.injector.loadInstanceOfComponent(wrapper, module),
),
);
}
private createPrototypesOfRoutes(module: Module) {
module.routes.forEach(wrapper => {
this.injector.loadPrototypeOfInstance<Controller>(wrapper, module.routes);
private createPrototypesOfControllers(module: Module) {
module.controllers.forEach(wrapper => {
this.injector.loadPrototypeOfInstance<Controller>(
wrapper,
module.controllers,
);
});
}
private async createInstancesOfRoutes(module: Module) {
private async createInstancesOfControllers(module: Module) {
await Promise.all(
[...module.routes.values()].map(async wrapper =>
this.injector.loadInstanceOfRoute(wrapper, module),
[...module.controllers.values()].map(async wrapper =>
this.injector.loadInstanceOfController(wrapper, module),
),
);
}

View File

@@ -1,5 +1,5 @@
import { Type } from '@nestjs/common';
import { NestContainer } from './container';
import { InstanceWrapper, NestContainer } from './container';
import { ContainerScanner } from './container-scanner';
import { Injector } from './injector';
import { Module } from './module';
@@ -29,7 +29,7 @@ export abstract class ModuleRef {
type: Type<T>,
module: Module,
): Promise<T> {
const wrapper = {
const wrapper: InstanceWrapper<any> = {
name: type.name,
metatype: type,
instance: undefined,
@@ -37,7 +37,7 @@ export abstract class ModuleRef {
};
return new Promise<T>(async (resolve, reject) => {
try {
const callback = async instances => {
const callback = async (instances: any[]) => {
const properties = await this.injector.resolveProperties(
wrapper,
module,

View File

@@ -24,18 +24,18 @@ import { ModuleRef } from './module-ref';
import { ModulesContainer } from './modules-container';
import { HTTP_SERVER_REF } from './tokens';
export interface CustomComponent {
export interface CustomProvider {
provide: any;
name: string;
}
export type OpaqueToken = string | symbol | object | Type<any>;
export type CustomClass = CustomComponent & { useClass: Type<any> };
export type CustomFactory = CustomComponent & {
useFactory: (...args) => any;
export type CustomClass = CustomProvider & { useClass: Type<any> };
export type CustomFactory = CustomProvider & {
useFactory: (...args: any[]) => any;
inject?: OpaqueToken[];
};
export type CustomValue = CustomComponent & { useValue: any };
export type ComponentMetatype =
export type CustomValue = CustomProvider & { useValue: any };
export type ProviderMetatype =
| Type<Injectable>
| CustomFactory
| CustomValue
@@ -44,9 +44,12 @@ export type ComponentMetatype =
export class Module {
private readonly _id: string;
private readonly _relatedModules = new Set<Module>();
private readonly _components = new Map<any, InstanceWrapper<Injectable>>();
private readonly _providers = new Map<any, InstanceWrapper<Injectable>>();
private readonly _injectables = new Map<any, InstanceWrapper<Injectable>>();
private readonly _routes = new Map<string, InstanceWrapper<Controller>>();
private readonly _controllers = new Map<
string,
InstanceWrapper<Controller>
>();
private readonly _exports = new Set<string | symbol>();
constructor(
@@ -54,7 +57,7 @@ export class Module {
private readonly _scope: Type<any>[],
private readonly container: NestContainer,
) {
this.addCoreInjectables(container);
this.addCoreProviders(container);
this._id = randomStringGenerator();
}
@@ -70,16 +73,30 @@ export class Module {
return this._relatedModules;
}
get providers(): Map<string, InstanceWrapper<Injectable>> {
return this._providers;
}
/**
* Left for backward-compatibility reasons
*/
get components(): Map<string, InstanceWrapper<Injectable>> {
return this._components;
return this._providers;
}
/**
* Left for backward-compatibility reasons
*/
get routes(): Map<string, InstanceWrapper<Controller>> {
return this._controllers;
}
get injectables(): Map<string, InstanceWrapper<Injectable>> {
return this._injectables;
}
get routes(): Map<string, InstanceWrapper<Controller>> {
return this._routes;
get controllers(): Map<string, InstanceWrapper<Controller>> {
return this._controllers;
}
get exports(): Set<string | symbol> {
@@ -87,10 +104,10 @@ export class Module {
}
get instance(): NestModule {
if (!this._components.has(this._metatype.name)) {
if (!this._providers.has(this._metatype.name)) {
throw new RuntimeException();
}
const module = this._components.get(this._metatype.name);
const module = this._providers.get(this._metatype.name);
return module.instance as NestModule;
}
@@ -98,8 +115,8 @@ export class Module {
return this._metatype;
}
public addCoreInjectables(container: NestContainer) {
this.addModuleAsComponent();
public addCoreProviders(container: NestContainer) {
this.addModuleAsProvider();
this.addModuleRef();
this.addReflector(container.getReflector());
this.addApplicationRef(container.getApplicationRef());
@@ -110,7 +127,7 @@ export class Module {
public addModuleRef() {
const moduleRef = this.createModuleRefMetatype();
this._components.set(ModuleRef.name, {
this._providers.set(ModuleRef.name, {
name: ModuleRef.name,
metatype: ModuleRef as any,
isResolved: true,
@@ -118,8 +135,8 @@ export class Module {
});
}
public addModuleAsComponent() {
this._components.set(this._metatype.name, {
public addModuleAsProvider() {
this._providers.set(this._metatype.name, {
name: this._metatype.name,
metatype: this._metatype,
isResolved: false,
@@ -128,7 +145,7 @@ export class Module {
}
public addReflector(reflector: Reflector) {
this._components.set(Reflector.name, {
this._providers.set(Reflector.name, {
name: Reflector.name,
metatype: Reflector,
isResolved: true,
@@ -137,7 +154,7 @@ export class Module {
}
public addApplicationRef(applicationRef: any) {
this._components.set(HTTP_SERVER_REF, {
this._providers.set(HTTP_SERVER_REF, {
name: HTTP_SERVER_REF,
metatype: {} as any,
isResolved: true,
@@ -148,7 +165,7 @@ export class Module {
public addExternalContextCreator(
externalContextCreator: ExternalContextCreator,
) {
this._components.set(ExternalContextCreator.name, {
this._providers.set(ExternalContextCreator.name, {
name: ExternalContextCreator.name,
metatype: ExternalContextCreator,
isResolved: true,
@@ -157,7 +174,7 @@ export class Module {
}
public addModulesContainer(modulesContainer: ModulesContainer) {
this._components.set(ModulesContainer.name, {
this._providers.set(ModulesContainer.name, {
name: ModulesContainer.name,
metatype: ModulesContainer,
isResolved: true,
@@ -166,7 +183,7 @@ export class Module {
}
public addApplicationRefHost(applicationRefHost: ApplicationReferenceHost) {
this._components.set(ApplicationReferenceHost.name, {
this._providers.set(ApplicationReferenceHost.name, {
name: ApplicationReferenceHost.name,
metatype: ApplicationReferenceHost,
isResolved: true,
@@ -174,7 +191,7 @@ export class Module {
});
}
public addInjectable(injectable: Type<Injectable>) {
public addInjectable<T = Injectable>(injectable: Type<T>) {
if (this.isCustomProvider(injectable)) {
return this.addCustomProvider(injectable, this._injectables);
}
@@ -186,63 +203,63 @@ export class Module {
});
}
public addComponent(component: ComponentMetatype): string {
if (this.isCustomProvider(component)) {
return this.addCustomProvider(component, this._components);
public addProvider(provider: ProviderMetatype): string {
if (this.isCustomProvider(provider)) {
return this.addCustomProvider(provider, this._providers);
}
this._components.set((component as Type<Injectable>).name, {
name: (component as Type<Injectable>).name,
metatype: component as Type<Injectable>,
this._providers.set((provider as Type<Injectable>).name, {
name: (provider as Type<Injectable>).name,
metatype: provider as Type<Injectable>,
instance: null,
isResolved: false,
});
return (component as Type<Injectable>).name;
return (provider as Type<Injectable>).name;
}
public isCustomProvider(
component: ComponentMetatype,
): component is CustomClass | CustomFactory | CustomValue {
return !isNil((component as CustomComponent).provide);
provider: ProviderMetatype,
): provider is CustomClass | CustomFactory | CustomValue {
return !isNil((provider as CustomProvider).provide);
}
public addCustomProvider(
component: CustomFactory | CustomValue | CustomClass,
provider: CustomFactory | CustomValue | CustomClass,
collection: Map<string, any>,
): string {
const { provide } = component;
const { provide } = provider;
const name = isFunction(provide) ? provide.name : provide;
const componentWithName = {
...component,
const providerNamePair = {
...provider,
name,
};
if (this.isCustomClass(componentWithName))
this.addCustomClass(componentWithName, collection);
else if (this.isCustomValue(componentWithName))
this.addCustomValue(componentWithName, collection);
else if (this.isCustomFactory(componentWithName))
this.addCustomFactory(componentWithName, collection);
if (this.isCustomClass(providerNamePair))
this.addCustomClass(providerNamePair, collection);
else if (this.isCustomValue(providerNamePair))
this.addCustomValue(providerNamePair, collection);
else if (this.isCustomFactory(providerNamePair))
this.addCustomFactory(providerNamePair, collection);
return name;
}
public isCustomClass(component): component is CustomClass {
return !isUndefined((component as CustomClass).useClass);
public isCustomClass(provider: any): provider is CustomClass {
return !isUndefined((provider as CustomClass).useClass);
}
public isCustomValue(component): component is CustomValue {
return !isUndefined((component as CustomValue).useValue);
public isCustomValue(provider: any): provider is CustomValue {
return !isUndefined((provider as CustomValue).useValue);
}
public isCustomFactory(component): component is CustomFactory {
return !isUndefined((component as CustomFactory).useFactory);
public isCustomFactory(provider: any): provider is CustomFactory {
return !isUndefined((provider as CustomFactory).useFactory);
}
public isDynamicModule(exported): exported is DynamicModule {
public isDynamicModule(exported: any): exported is DynamicModule {
return exported && exported.module;
}
public addCustomClass(component: CustomClass, collection: Map<string, any>) {
const { name, useClass } = component;
public addCustomClass(provider: CustomClass, collection: Map<string, any>) {
const { name, useClass } = provider;
collection.set(name, {
name,
metatype: useClass,
@@ -251,8 +268,8 @@ export class Module {
});
}
public addCustomValue(component: CustomValue, collection: Map<string, any>) {
const { name, useValue: value } = component;
public addCustomValue(provider: CustomValue, collection: Map<string, any>) {
const { name, useValue: value } = provider;
collection.set(name, {
name,
metatype: null,
@@ -264,10 +281,10 @@ export class Module {
}
public addCustomFactory(
component: CustomFactory,
provider: CustomFactory,
collection: Map<string, any>,
) {
const { name, useFactory: factory, inject } = component;
const { name, useFactory: factory, inject } = provider;
collection.set(name, {
name,
metatype: factory as any,
@@ -278,27 +295,27 @@ export class Module {
});
}
public addExportedComponent(
exportedComponent: ComponentMetatype | string | symbol | DynamicModule,
public addExportedProvider(
provider: ProviderMetatype | string | symbol | DynamicModule,
) {
const addExportedUnit = (token: string | symbol) =>
this._exports.add(this.validateExportedProvider(token));
if (this.isCustomProvider(exportedComponent as any)) {
return this.addCustomExportedComponent(exportedComponent as any);
} else if (isString(exportedComponent) || isSymbol(exportedComponent)) {
return addExportedUnit(exportedComponent);
} else if (this.isDynamicModule(exportedComponent)) {
const { module } = exportedComponent;
if (this.isCustomProvider(provider as any)) {
return this.addCustomExportedProvider(provider as any);
} else if (isString(provider) || isSymbol(provider)) {
return addExportedUnit(provider);
} else if (this.isDynamicModule(provider)) {
const { module } = provider;
return addExportedUnit(module.name);
}
addExportedUnit(exportedComponent.name);
addExportedUnit(provider.name);
}
public addCustomExportedComponent(
exportedComponent: CustomFactory | CustomValue | CustomClass,
public addCustomExportedProvider(
provider: CustomFactory | CustomValue | CustomClass,
) {
const provide = exportedComponent.provide;
const provide = provider.provide;
if (isString(provide) || isSymbol(provide)) {
return this._exports.add(this.validateExportedProvider(provide));
}
@@ -306,7 +323,7 @@ export class Module {
}
public validateExportedProvider(token: string | symbol) {
if (this._components.has(token)) {
if (this._providers.has(token)) {
return token;
}
const importedArray = [...this._relatedModules.values()];
@@ -323,22 +340,22 @@ export class Module {
return token;
}
public addRoute(route: Type<Controller>) {
this._routes.set(route.name, {
name: route.name,
metatype: route,
public addController(controller: Type<Controller>) {
this._controllers.set(controller.name, {
name: controller.name,
metatype: controller,
instance: null,
isResolved: false,
});
}
public addRelatedModule(relatedModule) {
this._relatedModules.add(relatedModule);
public addRelatedModule(module: any) {
this._relatedModules.add(module);
}
public replace(toReplace, options) {
if (options.isComponent) {
return this.addComponent({ provide: toReplace, ...options });
public replace(toReplace: string | symbol | Type<any>, options: any) {
if (options.isProvider) {
return this.addProvider({ provide: toReplace, ...options });
}
this.addInjectable({
provide: toReplace,

View File

@@ -10,7 +10,7 @@ export class InterceptorsConsumer {
interceptors: NestInterceptor[],
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
next: () => Promise<any>,
): Promise<any> {
if (isEmpty(interceptors)) {
@@ -27,7 +27,8 @@ export class InterceptorsConsumer {
};
*/
const result$ = await interceptors.reduce(
async (stream$, interceptor) => interceptor.intercept(context, await stream$),
async (stream$, interceptor) =>
interceptor.intercept(context, await stream$),
Promise.resolve(start$),
);
return result$.toPromise();
@@ -36,7 +37,7 @@ export class InterceptorsConsumer {
public createContext(
args: any[],
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
): ExecutionContextHost {
return new ExecutionContextHost(
args,

View File

@@ -1,7 +1,11 @@
import { INTERCEPTORS_METADATA } from '@nestjs/common/constants';
import { Controller, NestInterceptor } from '@nestjs/common/interfaces';
import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ContextCreator } from '../helpers/context-creator';
import { NestContainer } from '../injector/container';
@@ -18,7 +22,7 @@ export class InterceptorsContextCreator extends ContextCreator {
public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
module: string,
): NestInterceptor[] {
this.moduleContext = module;
@@ -55,7 +59,9 @@ export class InterceptorsContextCreator extends ContextCreator {
: null;
}
public getInstanceByMetatype(metatype): { instance: any } | undefined {
public getInstanceByMetatype<T extends Record<string, any> = any>(
metatype: T,
): { instance: any } | undefined {
if (!this.moduleContext) {
return undefined;
}

View File

@@ -1,15 +1,15 @@
import iterate from 'iterare';
import { Injectable } from '@nestjs/common/interfaces/injectable.interface';
import {
isConstructor,
isFunction,
isNil,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
export class MetadataScanner {
public scanFromPrototype<T extends Injectable, R>(
instance: T,
prototype,
prototype: any,
callback: (name: string) => R,
): R[] {
return iterate([...this.getAllFilteredMethodNames(prototype)])
@@ -18,7 +18,7 @@ export class MetadataScanner {
.toArray();
}
*getAllFilteredMethodNames(prototype): IterableIterator<string> {
*getAllFilteredMethodNames(prototype: any): IterableIterator<string> {
do {
yield* iterate(Object.getOwnPropertyNames(prototype))
.filter(prop => {

View File

@@ -38,11 +38,14 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
}
private static readonly ConfigProxy = class implements MiddlewareConfigProxy {
private contextParameters = null;
private contextParameters: any = null;
private excludedRoutes: RouteInfo[] = [];
private readonly includedRoutes: any[];
constructor(private readonly builder: MiddlewareBuilder, middleware) {
constructor(
private readonly builder: MiddlewareBuilder,
middleware: any[],
) {
this.includedRoutes = filterMiddleware(middleware);
}
@@ -50,7 +53,7 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
return this.excludedRoutes;
}
public with(...args): MiddlewareConfigProxy {
public with(...args: any[]): MiddlewareConfigProxy {
this.contextParameters = args;
return this;
}
@@ -88,7 +91,7 @@ export class MiddlewareBuilder implements MiddlewareConsumer {
return this.builder;
}
private mapRoutesToFlatList(forRoutes): RouteInfo[] {
private mapRoutesToFlatList(forRoutes: RouteInfo[][]): RouteInfo[] {
return forRoutes.reduce((a, b) => a.concat(b));
}

View File

@@ -1,6 +1,9 @@
import { HttpServer } from '@nestjs/common';
import { RequestMethod } from '@nestjs/common/enums/request-method.enum';
import { MiddlewareConfiguration, RouteInfo } from '@nestjs/common/interfaces/middleware/middleware-configuration.interface';
import {
MiddlewareConfiguration,
RouteInfo,
} from '@nestjs/common/interfaces/middleware/middleware-configuration.interface';
import { NestMiddleware } from '@nestjs/common/interfaces/middleware/nest-middleware.interface';
import { NestModule } from '@nestjs/common/interfaces/modules/nest-module.interface';
import { Type } from '@nestjs/common/interfaces/type.interface';
@@ -63,13 +66,15 @@ export class MiddlewareModule {
instance: NestModule,
module: string,
) {
if (!instance.configure) return;
if (!instance.configure) {
return;
}
const middlewareBuilder = new MiddlewareBuilder(this.routesMapper);
instance.configure(middlewareBuilder);
if (!(middlewareBuilder instanceof MiddlewareBuilder)) return;
if (!(middlewareBuilder instanceof MiddlewareBuilder)) {
return;
}
const config = middlewareBuilder.build();
middlewareContainer.addConfig(config, module);
}
@@ -163,7 +168,13 @@ export class MiddlewareModule {
undefined,
);
const router = applicationRef.createMiddlewareFactory(method);
const bindWithProxy = middlewareInstance =>
const bindWithProxy = (
middlewareInstance: <TRequest, TResponse>(
req: TRequest,
res: TResponse,
next: Function,
) => void,
) =>
this.bindHandlerWithProxy(
exceptionsHandler,
router,
@@ -171,15 +182,18 @@ export class MiddlewareModule {
path,
);
const resolve = instance.resolve();
const middleware = await resolve;
bindWithProxy(middleware);
}
private bindHandlerWithProxy(
exceptionsHandler: ExceptionsHandler,
router: (...args) => void,
middleware: (req, res, next) => void,
router: (...args: any[]) => void,
middleware: <TRequest, TResponse>(
req: TRequest,
res: TResponse,
next: Function,
) => void,
path: string,
) {
const proxy = this.routerProxy.createProxy(middleware, exceptionsHandler);

View File

@@ -2,20 +2,21 @@ import { Type } from '@nestjs/common/interfaces';
import { isFunction } from '@nestjs/common/utils/shared.utils';
import * as uuid from 'uuid/v4';
export const filterMiddleware = middleware => {
export const filterMiddleware = <T>(middleware: T[]) => {
return []
.concat(middleware)
.filter(isFunction)
.map(mapToClass);
};
export const mapToClass = middleware => {
export const mapToClass = <T extends Function | Type<any>>(middleware: T) => {
if (isClass(middleware)) {
return middleware;
}
return assignToken(
class {
resolve = (...args) => (...params) => middleware(...params);
resolve = (...args: any[]) => (...params: any[]) =>
(middleware as Function)(...params)
},
);
};
@@ -24,7 +25,7 @@ export function isClass(middleware: any) {
return middleware.toString().substring(0, 5) === 'class';
}
export function assignToken(metatype): Type<any> {
export function assignToken(metatype: Type<any>): Type<any> {
Object.defineProperty(metatype, 'name', { value: uuid() });
return metatype;
}

View File

@@ -80,11 +80,11 @@ export class NestApplicationContext implements INestApplicationContext {
}
protected async callModuleInitHook(module: Module): Promise<any> {
const components = [...module.components];
// The Module (class) instance is the first element of the components array
const providers = [...module.providers];
// Module (class) instance is the first element of the providers array
// Lifecycle hook has to be called once all classes are properly initialized
const [_, { instance: moduleClassInstance }] = components.shift();
const instances = [...module.routes, ...components];
const [_, { instance: moduleClassInstance }] = providers.shift();
const instances = [...module.controllers, ...providers];
await Promise.all(
iterate(instances)
@@ -110,11 +110,11 @@ export class NestApplicationContext implements INestApplicationContext {
}
protected async callModuleDestroyHook(module: Module): Promise<any> {
const components = [...module.components];
// The Module (class) instance is the first element of the components array
const providers = [...module.providers];
// Module (class) instance is the first element of the providers array
// Lifecycle hook has to be called once all classes are properly destroyed
const [_, { instance: moduleClassInstance }] = components.shift();
const instances = [...module.routes, ...components];
const [_, { instance: moduleClassInstance }] = providers.shift();
const instances = [...module.controllers, ...providers];
await Promise.all(
iterate(instances)
@@ -131,7 +131,7 @@ export class NestApplicationContext implements INestApplicationContext {
}
}
protected hasOnModuleDestroyHook(instance): instance is OnModuleDestroy {
protected hasOnModuleDestroyHook(instance: any): instance is OnModuleDestroy {
return !isUndefined((instance as OnModuleDestroy).onModuleDestroy);
}
@@ -143,9 +143,9 @@ export class NestApplicationContext implements INestApplicationContext {
}
protected async callModuleBootstrapHook(module: Module): Promise<any> {
const components = [...module.components];
const [_, { instance: moduleClassInstance }] = components.shift();
const instances = [...module.routes, ...components];
const providers = [...module.providers];
const [_, { instance: moduleClassInstance }] = providers.shift();
const instances = [...module.controllers, ...providers];
await Promise.all(
iterate(instances)

View File

@@ -57,7 +57,7 @@ export class NestApplication extends NestApplicationContext
: null;
private readonly socketModule = SocketModule ? new SocketModule() : null;
private readonly routesResolver: Resolver;
private readonly microservices = [];
private readonly microservices: any[] = [];
private httpServer: http.Server;
private isInitialized = false;
@@ -181,7 +181,7 @@ export class NestApplication extends NestApplicationContext
!!app._router.stack &&
isFunction(app._router.stack.filter) &&
app._router.stack.some(
layer => layer && layer.handle && layer.handle.name === name,
(layer: any) => layer && layer.handle && layer.handle.name === name,
)
);
}
@@ -242,7 +242,7 @@ export class NestApplication extends NestApplicationContext
return this;
}
public engine(...args): this {
public engine(...args: any[]): this {
if (!this.isExpress()) {
return this;
}
@@ -250,7 +250,7 @@ export class NestApplication extends NestApplicationContext
return this;
}
public set(...args): this {
public set(...args: any[]): this {
if (!this.isExpress()) {
return this;
}
@@ -258,7 +258,7 @@ export class NestApplication extends NestApplicationContext
return this;
}
public disable(...args): this {
public disable(...args: any[]): this {
if (!this.isExpress()) {
return this;
}
@@ -266,7 +266,7 @@ export class NestApplication extends NestApplicationContext
return this;
}
public enable(...args): this {
public enable(...args: any[]): this {
if (!this.isExpress()) {
return this;
}
@@ -274,13 +274,13 @@ export class NestApplication extends NestApplicationContext
return this;
}
public register(...args): this {
public register(...args: any[]): this {
const adapter = this.httpAdapter as FastifyAdapter;
adapter.register && adapter.register(...args);
return this;
}
public inject(...args) {
public inject(...args: any[]) {
const adapter = this.httpAdapter as FastifyAdapter;
return adapter.inject && adapter.inject(...args);
}
@@ -290,13 +290,16 @@ export class NestApplication extends NestApplicationContext
return this;
}
public async listen(port: number | string, callback?: () => void);
public async listen(
port: number | string,
callback?: () => void,
): Promise<any>;
public async listen(
port: number | string,
hostname: string,
callback?: () => void,
);
public async listen(port: number | string, ...args) {
): Promise<any>;
public async listen(port: number | string, ...args: any[]): Promise<any> {
!this.isInitialized && (await this.init());
this.httpServer.listen(port, ...args);
@@ -305,7 +308,7 @@ export class NestApplication extends NestApplicationContext
public listenAsync(port: number | string, hostname?: string): Promise<any> {
return new Promise(resolve => {
const server = this.listen(port, hostname, () => resolve(server));
const server: any = this.listen(port, hostname, () => resolve(server));
});
}
@@ -353,7 +356,7 @@ export class NestApplication extends NestApplicationContext
}
public useStaticAssets(options: any): this;
public useStaticAssets(path: string, options?: ServeStaticOptions);
public useStaticAssets(path: string, options?: ServeStaticOptions): this;
public useStaticAssets(
pathOrOptions: any,
options?: ServeStaticOptions,
@@ -378,7 +381,7 @@ export class NestApplication extends NestApplicationContext
return loadPackage(name, ctx);
}
private async registerMiddleware(instance) {
private async registerMiddleware(instance: any) {
await this.middlewareModule.registerMiddleware(
this.middlewareContainer,
instance,

View File

@@ -78,7 +78,7 @@ export class NestFactoryStatic {
* @returns {Promise}
*/
public async createMicroservice(
module,
module: any,
options?: NestMicroserviceOptions & MicroserviceOptions,
): Promise<INestMicroservice> {
const { NestMicroservice } = loadPackage(
@@ -104,7 +104,7 @@ export class NestFactoryStatic {
* @returns {Promise}
*/
public async createApplicationContext(
module,
module: any,
options?: NestApplicationContextOptions,
): Promise<INestApplicationContext> {
const container = new NestContainer();
@@ -125,7 +125,7 @@ export class NestFactoryStatic {
}
private async initialize(
module,
module: any,
container: NestContainer,
config = new ApplicationConfig(),
httpServer: HttpServer = null,
@@ -149,7 +149,7 @@ export class NestFactoryStatic {
}
}
private createProxy(target) {
private createProxy(target: any) {
const proxy = this.createExceptionProxy();
return new Proxy(target, {
get: proxy,
@@ -158,11 +158,11 @@ export class NestFactoryStatic {
}
private createExceptionProxy() {
return (receiver, prop) => {
return (receiver: Record<string, any>, prop: string) => {
if (!(prop in receiver)) return;
if (isFunction(receiver[prop])) {
return (...args) => {
return (...args: any[]) => {
let result;
ExceptionsZone.run(() => {
result = receiver[prop](...args);

View File

@@ -1,25 +1,24 @@
import { Transform } from '@nestjs/common/interfaces';
import { RouteParamtypes } from '@nestjs/common/enums/route-paramtypes.enum';
import { ArgumentMetadata, Transform } from '@nestjs/common/interfaces';
import { ParamsTokenFactory } from './params-token-factory';
export class PipesConsumer {
private readonly paramsTokenFactory = new ParamsTokenFactory();
public async apply(
value,
{ metatype, type, data },
public async apply<TInput = any>(
value: TInput,
{ metatype, type, data }: ArgumentMetadata,
transforms: Transform<any>[],
) {
const token = this.paramsTokenFactory.exchangeEnumForString(type);
return this.applyPipes(
value,
{ metatype, type: token, data },
transforms,
const token = this.paramsTokenFactory.exchangeEnumForString(
(type as any) as RouteParamtypes,
);
return this.applyPipes(value, { metatype, type: token, data }, transforms);
}
public async applyPipes(
value,
{ metatype, type, data }: { metatype; type?; data? },
public async applyPipes<TInput = any>(
value: TInput,
{ metatype, type, data }: { metatype: any; type?: any; data?: any },
transforms: Transform<any>[],
) {
return transforms.reduce(async (defferedValue, fn) => {

View File

@@ -1,6 +1,14 @@
import { PIPES_METADATA } from '@nestjs/common/constants';
import { Controller, PipeTransform, Transform } from '@nestjs/common/interfaces';
import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
import {
Controller,
PipeTransform,
Transform,
} from '@nestjs/common/interfaces';
import {
isEmpty,
isFunction,
isUndefined,
} from '@nestjs/common/utils/shared.utils';
import iterate from 'iterare';
import { ApplicationConfig } from '../application-config';
import { ContextCreator } from '../helpers/context-creator';
@@ -18,7 +26,7 @@ export class PipesContextCreator extends ContextCreator {
public create(
instance: Controller,
callback: (...args) => any,
callback: (...args: any[]) => any,
module: string,
): Transform<any>[] {
this.moduleContext = module;
@@ -44,13 +52,15 @@ export class PipesContextCreator extends ContextCreator {
if (isObject) {
return pipe;
}
const instanceWrapper = this.getInstanceByMetatype(pipe);
const instanceWrapper = this.getInstanceByMetatype(pipe as Function);
return instanceWrapper && instanceWrapper.instance
? instanceWrapper.instance
: null;
}
public getInstanceByMetatype(metatype): { instance: any } | undefined {
public getInstanceByMetatype<T extends { name: string } = any>(
metatype: T,
): { instance: any } | undefined {
if (!this.moduleContext) {
return undefined;
}

View File

@@ -2,5 +2,9 @@ import { Controller } from '@nestjs/common/interfaces/controllers/controller.int
import { ExceptionsHandler } from '../../exceptions/exceptions-handler';
export interface ExceptionsFilter {
create(instance: Controller, callback, module: string): ExceptionsHandler;
create(
instance: Controller,
callback: Function,
module: string,
): ExceptionsHandler;
}

Some files were not shown because too many files have changed in this diff Show More