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

feat(export): export defaultMetadataStorage

Open thongxuan opened this issue 1 year ago • 5 comments

Description

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

Checklist

  • [x] the pull request title describes what this PR does (not a vague title like Update index.md)
  • [x] the pull request targets the default branch of the repository (develop)
  • [x] the code follows the established code style of the repository
    • npm run prettier:check passes
    • npm run lint:check passes
  • [x] tests are added for the changes I made (if any source code was modified)
  • [x] documentation added or updated
  • [x] I have run the project locally and verified that there are no errors

Fixes

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

thongxuan avatar Jun 15 '24 03:06 thongxuan

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

diffy0712 avatar Jul 07 '24 13:07 diffy0712

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

scottwday avatar Jul 18 '24 19:07 scottwday

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 🙏

elliot-sabitov avatar Oct 03 '24 14:10 elliot-sabitov

is there any update on the status of this PR?

ahmedwael216 avatar Jan 27 '25 20:01 ahmedwael216

A very useful and necessary feature

AlekseyMelikov avatar Jul 03 '25 05:07 AlekseyMelikov