Skip to content

Commit

Permalink
Merge pull request #581 from nestjs/master
Browse files Browse the repository at this point in the history
Create a new pull request by comparing changes across two
  • Loading branch information
GulajavaMinistudio committed Mar 13, 2021
2 parents eb0b600 + 58735f9 commit 611284d
Show file tree
Hide file tree
Showing 89 changed files with 513 additions and 204 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"packages": [
"packages/*"
],
"version": "7.6.13"
"version": "7.6.14"
}
9 changes: 5 additions & 4 deletions packages/common/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Nest is a framework for building efficient, scalable <a href="http://nodejs.org"

* To check out the [guide](https://docs.nestjs.com), visit [docs.nestjs.com](https://docs.nestjs.com). :books:
* 要查看中文 [指南](readme_zh.md), 请访问 [docs.nestjs.cn](https://docs.nestjs.cn). :books:
* [가이드](readme_kr.md)를 확인하려면, [docs.nestjs.com](https://docs.nestjs.com)를 방문하세요.:books:

## Questions

Expand Down Expand Up @@ -95,11 +96,11 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
<td align="center" valign="middle">
<a href="https://genuinebee.com/" target="_blank"><img src="https://nestjs.com/img/genuinebee.svg" width="97" valign="middle" /></a> </td>
<td align="center" valign="middle"><a href="https://sanyodigital.com/" target="_blank"><img src="https://nestjs.com/img/sanyo-digital.png" width="130" valign="middle" /></a></td></tr><tr><td align="center" valign="middle"><a href="https://vpn-review.com/vpn-for-torrenting" target="_blank"><img src="https://nestjs.com/img/vpn-review-logo.png" width="85" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://lambda-it.ch/" target="_blank"><img src="https://nestjs.com/img/lambda-it-logo.svg" width="115" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://pickwriters.com/top-10-translation-services" target="_blank"><img src="https://nestjs.com/img/pickwriters-logo.png" width="40" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://thewordpoint.com/services/localization" target="_blank"><img src="https://nestjs.com/img/thewordpoint-logo.png" width="40" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://streamat.se/" target="_blank"><img src="https://nestjs.com/img/streamat-logo.png" width="120" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://filmen.nu/" target="_blank"><img src="https://nestjs.com/img/filmen-logo.png" width="120" valign="middle" /></a></td></tr><tr>
<td align="center" valign="middle"><a href="https://meercode.io/" target="_blank"><img src="https://nestjs.com/img/meercode-logo.png" width="60" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://www.najlepszeplatformyforex.pl/blog/broker-xtb/" target="_blank"><img src="https://nestjs.com/img/npf-logo.jpg" width="200" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://thestandarddaily.com/" target="_blank"><img src="https://nestjs.com/img/the-standard-daily-logo.png" width="180" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://www.najlepszeplatformyforex.pl/blog/broker-xtb/" target="_blank"><img src="https://nestjs.com/img/npf-logo.jpg" width="200" valign="middle" /></a></td></tr><tr>
<td align="center" valign="middle"><a href="https://thestandarddaily.com/" target="_blank"><img src="https://nestjs.com/img/the-standard-daily-logo.png" width="180" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://houseofangular.io/" target="_blank"><img src="https://nestjs.com/img/house-of-angular.png" width="100" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://rocketech.it/cases/?utm_source=google&utm_medium=badge&utm_campaign=nestjs" target="_blank"><img src="https://nestjs.com/img/rocketech-logo.svg" width="110" valign="middle" /></a></td>
</tr></table>

## Backers
Expand Down
3 changes: 1 addition & 2 deletions packages/common/cache/interceptors/cache.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export class CacheInterceptor implements NestInterceptor {
next: CallHandler,
): Promise<Observable<any>> {
const key = this.trackBy(context);
const ttlValueOrFactory =
this.reflector.get(CACHE_TTL_METADATA, context.getHandler()) || null;
const ttlValueOrFactory = this.reflector.get(CACHE_TTL_METADATA, context.getHandler()) ?? null;

if (!key) {
return next.handle();
Expand Down
6 changes: 4 additions & 2 deletions packages/common/interfaces/external/cors-options.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
type StaticOrigin = boolean | string | RegExp | (string | RegExp)[];

/**
* Set origin to a function implementing some custom logic. The function takes the
* request origin as the first parameter and a callback (which expects the signature
Expand All @@ -9,7 +11,7 @@
*/
export type CustomOrigin = (
requestOrigin: string,
callback: (err: Error | null, allow?: boolean) => void,
callback: (err: Error | null, origin?: StaticOrigin) => void,
) => void;

/**
Expand All @@ -22,7 +24,7 @@ export interface CorsOptions {
/**
* Configures the `Access-Control-Allow-Origins` CORS header. See [here for more detail.](https://github.com/expressjs/cors#configuration-options)
*/
origin?: boolean | string | RegExp | (string | RegExp)[] | CustomOrigin;
origin?: StaticOrigin | CustomOrigin;
/**
* Configures the Access-Control-Allow-Methods CORS header.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/common",
"version": "7.6.13",
"version": "7.6.14",
"description": "Nest - modern, fast, powerful node.js web framework (@common)",
"author": "Kamil Mysliwiec",
"homepage": "https://nestjs.com",
Expand Down
3 changes: 2 additions & 1 deletion packages/common/pipes/default-value.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ArgumentMetadata, Injectable, PipeTransform } from '../index';
import { Injectable } from '../decorators/core/injectable.decorator';
import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { isNil } from '../utils/shared.utils';

/**
Expand Down
6 changes: 4 additions & 2 deletions packages/common/pipes/parse-array.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { ArgumentMetadata, HttpStatus, Injectable, Optional } from '../index';
import { Injectable } from '../decorators/core/injectable.decorator';
import { Optional } from '../decorators/core/optional.decorator';
import { HttpStatus } from '../enums/http-status.enum';
import { Type } from '../interfaces';
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { HttpErrorByCode } from '../utils/http-error-by-code.util';
import { isNil, isString } from '../utils/shared.utils';
import { ValidationPipe, ValidationPipeOptions } from './validation.pipe';
Expand Down
6 changes: 4 additions & 2 deletions packages/common/pipes/parse-bool.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ArgumentMetadata, HttpStatus, Injectable, Optional } from '../index';
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { Injectable } from '../decorators/core/injectable.decorator';
import { Optional } from '../decorators/core/optional.decorator';
import { HttpStatus } from '../enums/http-status.enum';
import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface';
import {
ErrorHttpStatusCode,
HttpErrorByCode,
Expand Down
6 changes: 4 additions & 2 deletions packages/common/pipes/parse-int.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ArgumentMetadata, HttpStatus, Injectable, Optional } from '../index';
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { Injectable } from '../decorators/core/injectable.decorator';
import { Optional } from '../decorators/core/optional.decorator';
import { HttpStatus } from '../enums/http-status.enum';
import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface';
import {
ErrorHttpStatusCode,
HttpErrorByCode,
Expand Down
7 changes: 4 additions & 3 deletions packages/common/pipes/parse-uuid.pipe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Optional } from '../decorators';
import { ArgumentMetadata, HttpStatus, Injectable } from '../index';
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { Injectable } from '../decorators/core/injectable.decorator';
import { Optional } from '../decorators/core/optional.decorator';
import { HttpStatus } from '../enums/http-status.enum';
import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface';
import {
ErrorHttpStatusCode,
HttpErrorByCode,
Expand Down
4 changes: 2 additions & 2 deletions packages/common/pipes/validation.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { iterate } from 'iterare';
import { Optional } from '../decorators';
import { Injectable } from '../decorators/core';
import { HttpStatus } from '../enums/http-status.enum';
import { ArgumentMetadata, ValidationError } from '../index';
import { ClassTransformOptions } from '../interfaces/external/class-transform-options.interface';
import { ValidationError } from '../interfaces/external/validation-error.interface';
import { ValidatorOptions } from '../interfaces/external/validator-options.interface';
import { PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { ArgumentMetadata, PipeTransform } from '../interfaces/features/pipe-transform.interface';
import { Type } from '../interfaces/type.interface';
import {
ErrorHttpStatusCode,
Expand Down
9 changes: 5 additions & 4 deletions packages/core/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Nest is a framework for building efficient, scalable <a href="http://nodejs.org"

* To check out the [guide](https://docs.nestjs.com), visit [docs.nestjs.com](https://docs.nestjs.com). :books:
* 要查看中文 [指南](readme_zh.md), 请访问 [docs.nestjs.cn](https://docs.nestjs.cn). :books:
* [가이드](readme_kr.md)를 확인하려면, [docs.nestjs.com](https://docs.nestjs.com)를 방문하세요.:books:

## Questions

Expand Down Expand Up @@ -95,11 +96,11 @@ Nest is an MIT-licensed open source project. It can grow thanks to the sponsors
<td align="center" valign="middle">
<a href="https://genuinebee.com/" target="_blank"><img src="https://nestjs.com/img/genuinebee.svg" width="97" valign="middle" /></a> </td>
<td align="center" valign="middle"><a href="https://sanyodigital.com/" target="_blank"><img src="https://nestjs.com/img/sanyo-digital.png" width="130" valign="middle" /></a></td></tr><tr><td align="center" valign="middle"><a href="https://vpn-review.com/vpn-for-torrenting" target="_blank"><img src="https://nestjs.com/img/vpn-review-logo.png" width="85" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://lambda-it.ch/" target="_blank"><img src="https://nestjs.com/img/lambda-it-logo.svg" width="115" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://pickwriters.com/top-10-translation-services" target="_blank"><img src="https://nestjs.com/img/pickwriters-logo.png" width="40" valign="middle" /></a></td><td align="center" valign="middle"><a href="https://thewordpoint.com/services/localization" target="_blank"><img src="https://nestjs.com/img/thewordpoint-logo.png" width="40" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://streamat.se/" target="_blank"><img src="https://nestjs.com/img/streamat-logo.png" width="120" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://filmen.nu/" target="_blank"><img src="https://nestjs.com/img/filmen-logo.png" width="120" valign="middle" /></a></td></tr><tr>
<td align="center" valign="middle"><a href="https://meercode.io/" target="_blank"><img src="https://nestjs.com/img/meercode-logo.png" width="60" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://www.najlepszeplatformyforex.pl/blog/broker-xtb/" target="_blank"><img src="https://nestjs.com/img/npf-logo.jpg" width="200" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://thestandarddaily.com/" target="_blank"><img src="https://nestjs.com/img/the-standard-daily-logo.png" width="180" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://www.najlepszeplatformyforex.pl/blog/broker-xtb/" target="_blank"><img src="https://nestjs.com/img/npf-logo.jpg" width="200" valign="middle" /></a></td></tr><tr>
<td align="center" valign="middle"><a href="https://thestandarddaily.com/" target="_blank"><img src="https://nestjs.com/img/the-standard-daily-logo.png" width="180" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://houseofangular.io/" target="_blank"><img src="https://nestjs.com/img/house-of-angular.png" width="100" valign="middle" /></a></td>
<td align="center" valign="middle"><a href="https://rocketech.it/cases/?utm_source=google&utm_medium=badge&utm_campaign=nestjs" target="_blank"><img src="https://nestjs.com/img/rocketech-logo.svg" width="110" valign="middle" /></a></td>
</tr></table>

## Backers
Expand Down
15 changes: 12 additions & 3 deletions packages/core/middleware/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export const mapToClass = <T extends Function | Type<any>>(
excludedRoutes: RouteInfoRegex[],
httpAdapter: HttpServer,
) => {
if (isClass(middleware)) {
if (isMiddlewareClass(middleware)) {
if (excludedRoutes.length <= 0) {
return middleware;
}
Expand Down Expand Up @@ -59,8 +59,17 @@ export const mapToClass = <T extends Function | Type<any>>(
);
};

export function isClass(middleware: any): middleware is Type<any> {
return middleware.toString().substring(0, 5) === 'class';
export function isMiddlewareClass(middleware: any): middleware is Type<any> {
const middlewareStr = middleware.toString();
if (middlewareStr.substring(0, 5) === 'class') {
return true;
}
const middlewareArr = middlewareStr.split(' ');
return (
middlewareArr[0] === 'function' &&
/[A-Z]/.test(middlewareArr[1]?.[0]) &&
typeof middleware.prototype?.use === 'function'
);
}

export function assignToken(metatype: Type<any>, token = uuid()): Type<any> {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nestjs/core",
"version": "7.6.13",
"version": "7.6.14",
"description": "Nest - modern, fast, powerful node.js web framework (@core)",
"author": "Kamil Mysliwiec",
"license": "MIT",
Expand Down Expand Up @@ -36,7 +36,7 @@
"uuid": "8.3.2"
},
"devDependencies": {
"@nestjs/common": "7.6.13"
"@nestjs/common": "7.6.14"
},
"peerDependencies": {
"@nestjs/common": "^7.0.0",
Expand Down
9 changes: 5 additions & 4 deletions packages/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,23 @@ export class RouterExplorer {
prototype: object,
methodName: string,
): RoutePathProperties {
const targetCallback = prototype[methodName];
const routePath = Reflect.getMetadata(PATH_METADATA, targetCallback);
const instanceCallback = instance[methodName];
const prototypeCallback = prototype[methodName];
const routePath = Reflect.getMetadata(PATH_METADATA, prototypeCallback);
if (isUndefined(routePath)) {
return null;
}
const requestMethod: RequestMethod = Reflect.getMetadata(
METHOD_METADATA,
targetCallback,
prototypeCallback,
);
const path = isString(routePath)
? [addLeadingSlash(routePath)]
: routePath.map(p => addLeadingSlash(p));
return {
path,
requestMethod,
targetCallback,
targetCallback: instanceCallback,
methodName,
};
}
Expand Down
8 changes: 4 additions & 4 deletions packages/core/test/middleware/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as sinon from 'sinon';
import {
assignToken,
filterMiddleware,
isClass,
isMiddlewareClass,
isRouteExcluded,
mapToClass,
} from '../../middleware/utils';
Expand Down Expand Up @@ -63,15 +63,15 @@ describe('middleware utils', () => {
});
});
});
describe('isClass', () => {
describe('isMiddlewareClass', () => {
describe('when middleware is a class', () => {
it('should returns true', () => {
expect(isClass(Test)).to.be.true;
expect(isMiddlewareClass(Test)).to.be.true;
});
});
describe('when middleware is a function', () => {
it('should returns false', () => {
expect(isClass(fnMiddleware)).to.be.false;
expect(isMiddlewareClass(fnMiddleware)).to.be.false;
});
});
});
Expand Down
86 changes: 82 additions & 4 deletions packages/core/test/router/router-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,55 +109,133 @@ describe('RouterExplorer', () => {
const instanceProto = Object.getPrototypeOf(instance);

const route = routerBuilder.exploreMethodMetadata(
new TestRoute(),
instance,
instanceProto,
'getTest',
);

expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTest);
});

it('should method return expected object which represent single route with alias', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);

const route = routerBuilder.exploreMethodMetadata(
new TestRouteAlias(),
instance,
instanceProto,
'getTest',
);

expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTest);
});

it('should method return expected object which represent multiple routes', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);

const route = routerBuilder.exploreMethodMetadata(
new TestRoute(),
instance,
instanceProto,
'getTestUsingArray',
);

expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTestUsingArray);
});

it('should method return expected object which represent multiple routes with alias', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);

const route = routerBuilder.exploreMethodMetadata(
new TestRouteAlias(),
instance,
instanceProto,
'getTestUsingArray',
);

expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
expect(route.targetCallback).to.eq(instance.getTestUsingArray);
});

describe('when new implementation is injected into router', () => {
it('should method return changed impl of single route', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);

const newImpl = function () {};
instance.getTest = newImpl;

const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTest',
);

expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});

it('should method return changed impl of single route which alias applied', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);

const newImpl = function () {};
instance.getTest = newImpl;

const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTest',
);

expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/test']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});

it('should method return changed impl of multiple routes', () => {
const instance = new TestRoute();
const instanceProto = Object.getPrototypeOf(instance);

const newImpl = function () {};
instance.getTestUsingArray = newImpl;

const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTestUsingArray',
);

expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});

it('should method return changed impl of multiple routes which alias applied', () => {
const instance = new TestRouteAlias();
const instanceProto = Object.getPrototypeOf(instance);

const newImpl = function () {};
instance.getTestUsingArray = newImpl;

const route = routerBuilder.exploreMethodMetadata(
instance,
instanceProto,
'getTestUsingArray',
);

expect(route.targetCallback).to.eq(newImpl);
expect(route.path).to.eql(['/foo', '/bar']);
expect(route.requestMethod).to.eql(RequestMethod.GET);
});
});
});

Expand Down

0 comments on commit 611284d

Please sign in to comment.