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 for Custom Fields Decorators in Mapped types #3201

Closed
1 task done
Copystrike opened this issue Mar 30, 2024 · 2 comments
Closed
1 task done

Support for Custom Fields Decorators in Mapped types #3201

Copystrike opened this issue Mar 30, 2024 · 2 comments
Labels

Comments

@Copystrike
Copy link

Copystrike commented Mar 30, 2024

Is there an existing issue that is already proposing this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe it

The problem is that when using tools like Automapper, Mapped types lose their functionality because Automapper requires each field to be decorated with @AutoMap().

When looking for a solution, I found a Stack Overflow post that describes the same problem I'm experiencing: https://stackoverflow.com/questions/77825626/automapper-and-mapped-type-in-nest-js

Describe the solution you'd like

Here's the solution I believe would be best and flexible:

export class TodoItem extends PartialType(CreateTodoItemInput, {
  customFields(partialObjectType, item) {
    if (item.name !== 'type') {
      AutoMap()(partialObjectType.prototype, item.name);
    }
  },
}) {}

Developers will have the option to use the customFields parameter, which takes two parameters:

interface PartialTypeOptions {
  decorator?: ClassDecoratorFactory;
  omitDefaultValues?: boolean;
  customFields?: (partialObjectType: typeof PartialObjectType, item: PropertyMetadata) => void;
}

With this solution, users can select which fields to decorate or apply decoration to all fields if needed. This offers greater control and customization in field decoration.

Teachability, documentation, adoption, migration strategy

I've already created a custom mapped type available here: https://gist.github.com/Copystrike/bfc5010100aba362002616f9eca7fa25

This Gist demonstrates the functionality I described in "Describe the solution you'd like." However, I haven't submitted a pull request because I believe this functionality should be added to all Mapped Types and thoroughly tested, which I currently don't have time for. But the gist works effectively for me!

Also worth mentioning, when defining the PartialType second parameter it instantly tries to execute the decorator which is not being used which causes an error, I did fix it in my Gist but I don't know if the fix itself is good since I used AI for this.

What is the motivation / use case for changing the behavior?

The motivation for changing the behavior is to provide users with greater flexibility and control over field decoration when using tools like AutoMap in NestJS mapped types. By allowing users to specify which fields should be decorated, they can tailor the mapping configuration to their specific needs.

@Copystrike Copystrike changed the title Support for Custom Fields in Mapped types Support for Custom Fields Decorators in Mapped types Mar 30, 2024
@kamilmysliwiec
Copy link
Member

Would you like to create a PR for this issue?

@Copystrike
Copy link
Author

Hey @kamilmysliwiec,

Due to time constraints and my shift away from GraphQL, I'm unable to create a PR for this issue. Upon further reflection, I also believe this issue would be better suited for the NestJS core repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants