Coder Social home page Coder Social logo

Comments (5)

fabioformosa avatar fabioformosa commented on June 12, 2024

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.

galwa avatar galwa commented on June 12, 2024

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.

fabioformosa avatar fabioformosa commented on June 12, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.