class-transformer
class-transformer copied to clipboard
Expose properties with different name using a callback
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.