Crud: ParsedRequest is broken with newest changes in NestJS

Created on 14 Mar 2020  路  2Comments  路  Source: nestjsx/crud

NestJs version: 7.0.1
Package version: 4.4.1

There is a breaking change in the way how we create custom decorators in NestJS v7:
https://docs.nestjs.com/migration-guide#custom-route-decorators
And that will break this decorator: https://github.com/nestjsx/crud/blob/master/packages/crud/src/decorators/parsed-request.decorator.ts
Fix should be easy but it is breaking change like in nestjs migration above. :)

Also there is a problem with getManyBase and getOneBase. Request here:
https://github.com/nestjsx/crud/blob/master/packages/crud/src/crud/crud-routes.factory.ts#L208 is undefined, so i get this message from typeorm-crud.service.js: Cannot destructure propertyparsedof 'undefined' or 'null'.

Most helpful comment

hello, because of nestjs/core modified, we need change these files:

https://github.com/nestjsx/crud/blob/76dc6c2bc841a9b6fdc68b730aa87043f327117b/packages/crud/src/crud/reflection.helper.ts#L57

changed like this:

factory: (_, req) => req.switchToHttp().getRequest()[paramtype],

https://github.com/nestjsx/crud/blob/76dc6c2bc841a9b6fdc68b730aa87043f327117b/packages/crud/src/decorators/parsed-request.decorator.ts#L7
changed like this:

req.switchToHttp().getRequest()[PARSED_CRUD_REQUEST_KEY]

So I wrote the following code to temporarily fix the problem

create file: crud-fix.js

const fs = require('fs');
const files = ['node_modules/@nestjsx/crud/lib/decorators/parsed-request.decorator.js', 'node_modules/@nestjsx/crud/lib/crud/reflection.helper.js'];
fs.writeFileSync(files[0], fs.readFileSync(files[0]).toString().replace(/return req\[([^\]]*)]/, 'return req.switchToHttp().getRequest()[$1]'));
fs.writeFileSync(files[1], fs.readFileSync(files[1]).toString().replace('req[paramtype]', 'req.switchToHttp().getRequest()[paramtype]'));

then run:

node crud-fix.js

All 2 comments

hello, because of nestjs/core modified, we need change these files:

https://github.com/nestjsx/crud/blob/76dc6c2bc841a9b6fdc68b730aa87043f327117b/packages/crud/src/crud/reflection.helper.ts#L57

changed like this:

factory: (_, req) => req.switchToHttp().getRequest()[paramtype],

https://github.com/nestjsx/crud/blob/76dc6c2bc841a9b6fdc68b730aa87043f327117b/packages/crud/src/decorators/parsed-request.decorator.ts#L7
changed like this:

req.switchToHttp().getRequest()[PARSED_CRUD_REQUEST_KEY]

So I wrote the following code to temporarily fix the problem

create file: crud-fix.js

const fs = require('fs');
const files = ['node_modules/@nestjsx/crud/lib/decorators/parsed-request.decorator.js', 'node_modules/@nestjsx/crud/lib/crud/reflection.helper.js'];
fs.writeFileSync(files[0], fs.readFileSync(files[0]).toString().replace(/return req\[([^\]]*)]/, 'return req.switchToHttp().getRequest()[$1]'));
fs.writeFileSync(files[1], fs.readFileSync(files[1]).toString().replace('req[paramtype]', 'req.switchToHttp().getRequest()[paramtype]'));

then run:

node crud-fix.js

Fixed in v4.4.2

Was this page helpful?
0 / 5 - 0 ratings