Comments (5)
Ok, interesting.
With reference to both use cases, the converter could be access to the request:
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
@Convert(Car, CarDto)
@Injectable({ scope: Scope.REQUEST })
export class CarToCarDtoConverter implements Converter<Car, CarDto>{
constructor(@Inject(REQUEST) private readonly request: Request) {}
public convert(source: Car): CarDto {
const target = new CarDto();
... HERE THE CONVERSION BEHAVIOUR CAN BE AFFECTED BY THE ACTUAL REQUEST AND REQ PARAMS
for instance current user, API version, etc...
return target;
}
}
What do you think?
from metamorphosis-nestjs.
I actually thought about this approach, but It would result in implementing whatever logic you wish for both in the interception/controller layer and the convectors - especially in cases were the conversion is not direct request based it wouldn't be helpful,
For example I have an application that communicate with multiple version back-end - in this case the conversion is not request based
from metamorphosis-nestjs.
Usually the version of the API is in the URL or in a http header. You can get these info from the Request.
Futhermore, I believe it's better using different DTOs if different API versions expose different fields.
Anyway, I've got your point. It's true, class-trasform
has recently added some options to the conversion method like versioning or roles. Instead Spring Framework (java)
still holds the same signature: Target convert(Source source)
and is responsability of the converter to get context data (like current user, request based data) to change the conversion behaviour.
Metamorphosis
could adopt to the class-trasform
choice with some simple optional params.
Let me think about.
from metamorphosis-nestjs.
Related Issues (13)
- ConversionService: naming clash HOT 1
- Upgrade doc at typegoose support HOT 1
- async convertion method HOT 1
- Could conversion service return the exact type instead of any for convertAll? HOT 2
- Extract typegoose support in another lib
- Add a test with TypeORM
- Create Changelog
- with node 12.18.4 Converter not registered HOT 10
- Debug Mode flag
- Inject a custom logger
- Jest Config: exclude dist folder from coverage
- New method to convert array
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from metamorphosis-nestjs.