typestack/class-transformer

Expose properties with different name using a callback

thewebartisan7 opened this issue · 3 comments

Description

I would like to make a PR to allow pass a function to expose decorator for set different name using a callback. Let me know if you would be interested in this.

Proposed solution

I am not yet sure the best approach to handle this, but quickly checking source code below could be a solution without introducing regression.

import { defaultMetadataStorage } from '../storage';
import { ExposeOptions } from '../interfaces';

// options type can be a function that return ExposeOptions
export function Expose(options: ExposeOptions |  (() => ExposeOptions) = {}): PropertyDecorator & ClassDecorator {

  if (typeof options === 'function') options = options();

  return function (object: any, propertyName?: string | Symbol): void {
    defaultMetadataStorage.addExposeMetadata({
      target: object instanceof Function ? object : object.constructor,
      propertyName: propertyName as string,
      options,
    });
  };
}

Or also only the options.name become a string or a callback. In this case we change ExposeOptions['name'] to string or function.

Hello @thewebartisan7,

before getting into any implementation details, could you please give some details on when would this be useful and what limitations this would solve?

Thank you

Closing as invalid, because of inactivity.
If you want to continue this discussion, feel free to comment on this issue.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.