typestack / class-transformer

Decorator-based transformation, serialization, and deserialization between objects and classes.
MIT License
6.81k stars 500 forks source link

feat(export): export defaultMetadataStorage #1715

Open thongxuan opened 3 months ago

thongxuan commented 3 months ago

Description

This PR exports default metadata storage which allow picking & subclassing class decorated with class-transfomer.

Checklist

Fixes

fixes #[issue number], fixes #[issue number]

diffy0712 commented 2 months ago

Hello @thongxuan,

did you create any issue (or could you find any open issue) for this pr detailing the need for this? If not, could you please describe when would this export be used?

Thank you

scottwday commented 2 months ago

This is related to this issue: https://github.com/typestack/class-transformer/issues/815 This is needed for https://github.com/typestack/class-transformer/issues/563

elliot-sabitov commented 14 hours ago

Hello @thongxuan,

did you create any issue (or could you find any open issue) for this pr detailing the need for this? If not, could you please describe when would this export be used?

Thank you

We use defaultMetadataStorage to get typeMetadata like so:

const typeMeta = classTransformerMetadataStorage.findTypeMetadata(
            meta.target,
            meta.propertyName,
          )

Which is convenient in cases where you have a @Type(() => MyClass) annotation on a class property, and you want to get the MyClass for instantiation or other purposes, we then can call typeMeta.typeFunction()to get the MyClass in this case.

We have generic components that parse a schema maintained in a set of class-validator and class-transformer annotated classes, and these components utilize these annotation values to render Vite/React UI components or handle micro-service endpoint logic utilizing routing-controllers.

Having this default metadata storage being exportable would be super helpful! Right now, we have to have it like this:

import { defaultMetadataStorage } from 'class-transformer/cjs/storage.js';

but this may require additional configuration and it would be much better if we can simply just do:

import { defaultMetadataStorage } from 'class-transformer';

and be able to utilize defaultMetadataStorage where applicable 🙏