class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

Expose properties with different name using a callback

Open thewebartisan7 opened this issue 2 years ago • 0 comments
trafficstars

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.

thewebartisan7 avatar Sep 04 '23 16:09 thewebartisan7