Skip to content

Commit

Permalink
fix(crud): fixed custom routes params caused by NestJs v7 breaking ch…
Browse files Browse the repository at this point in the history
…anges

fixed custom routes params caused by NestJs v7 breaking changes

fix #443
  • Loading branch information
michaelyali committed Mar 17, 2020
1 parent 76dc6c2 commit 78ac985
Show file tree
Hide file tree
Showing 8 changed files with 1,049 additions and 752 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [4.4.2] - 2020-03-17

### Bug Fixes

- **crud** fixed custom routes params caused by NestJs v7 breaking changes ([#443](https://github.com/nestjsx/crud/issues/443))

## [4.4.1] - 2019-12-28

### Bug Fixes
Expand Down Expand Up @@ -111,6 +117,7 @@

- several fixes

[4.4.2]: https://github.com/nestjsx/crud/compare/v4.4.1...v4.4.2
[4.4.1]: https://github.com/nestjsx/crud/compare/v4.4.0...v4.4.1
[4.4.0]: https://github.com/nestjsx/crud/compare/v4.3.0...v4.4.0
[4.3.0]: https://github.com/nestjsx/crud/compare/v4.2.0...v4.3.0
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@
"peerDependencies": {},
"optionalDependencies": {},
"dependencies": {
"@nestjs/common": "6.10.10",
"@nestjs/core": "6.10.10",
"@nestjs/platform-express": "6.10.10",
"@nestjs/swagger": "4.0.9",
"@nestjs/testing": "6.10.10",
"@nestjs/typeorm": "6.2.0",
"@nestjs/common": "7.0.3",
"@nestjs/core": "7.0.3",
"@nestjs/platform-express": "7.0.3",
"@nestjs/swagger": "4.4.0",
"@nestjs/testing": "7.0.3",
"@nestjs/typeorm": "7.0.0",
"@nuxtjs/opencollective": "0.2.2",
"@types/jest": "24.0.18",
"@types/node": "12.7.5",
"@types/qs": "^6.5.3",
"@types/qs": "6.5.3",
"@types/supertest": "2.0.8",
"class-transformer": "0.2.3",
"class-validator": "0.10.0",
Expand All @@ -80,7 +80,7 @@
"pg": "7.12.1",
"prettier": "1.18.2",
"pretty-quick": "1.11.1",
"qs": "^6.8.0",
"qs": "6.8.0",
"redis": "2.8.0",
"reflect-metadata": "0.1.13",
"rimraf": "3.0.0",
Expand Down
10 changes: 9 additions & 1 deletion packages/crud/src/crud/reflection.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
PATH_METADATA,
ROUTE_ARGS_METADATA,
} from '@nestjs/common/constants';
import { ArgumentsHost } from '@nestjs/common';
import { isFunction } from '@nestjsx/util';

import { BaseRoute, MergedCrudOptions, AuthOptions } from '../interfaces';
import { BaseRouteName } from '../types';
Expand Down Expand Up @@ -54,7 +56,7 @@ export class R {
return {
[`${paramtype}${CUSTOM_ROUTE_AGRS_METADATA}:${index}`]: {
index,
factory: (_, req) => req[paramtype],
factory: (_, ctx) => R.getContextRequest(ctx)[paramtype],
data,
pipes,
},
Expand Down Expand Up @@ -171,4 +173,10 @@ export class R {
static getParsedBody(func: Function): any {
return R.get(PARSED_BODY_METADATA, func);
}

static getContextRequest(ctx: ArgumentsHost): any {
return isFunction(ctx.switchToHttp)
? ctx.switchToHttp().getRequest()
: /* istanbul ignore next */ ctx;
}
}
5 changes: 3 additions & 2 deletions packages/crud/src/decorators/parsed-request.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createParamDecorator } from '@nestjs/common';

import { PARSED_CRUD_REQUEST_KEY } from '../constants';
import { R } from '../crud/reflection.helper';

export const ParsedRequest = createParamDecorator(
(_, req): ParameterDecorator => {
return req[PARSED_CRUD_REQUEST_KEY];
(_, ctx): ParameterDecorator => {
return R.getContextRequest(ctx)[PARSED_CRUD_REQUEST_KEY];
},
);
4 changes: 0 additions & 4 deletions packages/crud/test/crud.decorator.base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('age');
done();
});
});
Expand Down Expand Up @@ -147,7 +146,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('bulk');
done();
});
});
Expand Down Expand Up @@ -178,7 +176,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('id');
done();
});
});
Expand Down Expand Up @@ -209,7 +206,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('id');
done();
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/crud/test/crud.decorator.override.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('bulk');
done();
});
});
Expand Down
2 changes: 0 additions & 2 deletions packages/crud/test/crud.dto.options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('age');
done();
});
});
Expand Down Expand Up @@ -99,7 +98,6 @@ describe('#crud', () => {
.send(send)
.end((_, res) => {
expect(res.status).toEqual(400);
expect(res.body.message[0].property).toBe('email');
done();
});
});
Expand Down
Loading

0 comments on commit 78ac985

Please sign in to comment.