Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support request for user options to be passed to converter #28

Open
galwa opened this issue Jan 3, 2021 · 5 comments
Open

Support request for user options to be passed to converter #28

galwa opened this issue Jan 3, 2021 · 5 comments
Labels
discussion enhancement New feature or request

Comments

@galwa
Copy link

galwa commented Jan 3, 2021

Current convert function AK convert(source: S): T Does not enable passing user options to the convert function

For example when using class-transformer groups & version are part of the optional fields that may be very useful especially "version" -> the convert function behavior should change as per Version ( as DTO or Entity might change )

@fabioformosa fabioformosa added the enhancement New feature or request label Jan 4, 2021
@fabioformosa
Copy link
Owner

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?

@galwa
Copy link
Author

galwa commented Jan 4, 2021

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

@fabioformosa
Copy link
Owner

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.

@revskill10
Copy link

Any news about this ?

@fabioformosa
Copy link
Owner

Nope, still an open point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants