class-transformer
- Version 0.5.1
- Published
- 776 kB
- No dependencies
- MIT license
Install
npm i class-transformer
yarn add class-transformer
pnpm add class-transformer
Overview
Proper decorator-based transformation / serialization / deserialization of plain javascript objects to class constructors
Index
Functions
- classToClassFromExist()
- classToPlain()
- classToPlainFromExist()
- deserialize()
- deserializeArray()
- Exclude()
- Expose()
- instanceToInstance()
- instanceToPlain()
- plainToClass()
- plainToClassFromExist()
- plainToInstance()
- serialize()
- Transform()
- TransformInstanceToInstance()
- TransformInstanceToPlain()
- TransformPlainToInstance()
- Type()
Classes
Interfaces
Enums
Type Aliases
Functions
function classToClassFromExist
classToClassFromExist: { <T>(object: T, fromObject: T, options?: ClassTransformOptions): T; <T>(object: T, fromObjects: T[], options?: ClassTransformOptions): T[];};
Converts class (constructor) object to plain (literal) object. Uses given plain object as source object (it means fills given plain object with data from class object). Also works with arrays.
Deprecated
This function is being removed. The current implementation is incorrect as it modifies the source object.
function classToPlain
classToPlain: { <T>(object: T, options?: ClassTransformOptions): Record<string, any>; <T>(object: T[], options?: ClassTransformOptions): Record<string, any>[];};
Converts class (constructor) object to plain (literal) object. Also works with arrays.
Deprecated
Function name changed, use the
instanceToPlain
method instead.
function classToPlainFromExist
classToPlainFromExist: { <T>( object: T, plainObject: Record<string, any>, options?: ClassTransformOptions ): Record<string, any>; <T>( object: T, plainObjects: Record<string, any>[], options?: ClassTransformOptions ): Record<string, any>[];};
Converts class (constructor) object to plain (literal) object. Uses given plain object as source object (it means fills given plain object with data from class object). Also works with arrays.
Deprecated
This function is being removed.
function deserialize
deserialize: <T>( cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions) => T;
Deserializes given JSON string to a object of the given class.
Deprecated
This function is being removed. Please use the following instead:
instanceToClass(cls, JSON.parse(json), options)
function deserializeArray
deserializeArray: <T>( cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions) => T[];
Deserializes given JSON string to an array of objects of the given class.
Deprecated
This function is being removed. Please use the following instead:
JSON.parse(json).map(value => instanceToClass(cls, value, options))
function Exclude
Exclude: (options?: ExcludeOptions) => PropertyDecorator & ClassDecorator;
Marks the given class or property as excluded. By default the property is excluded in both constructorToPlain and plainToConstructor transformations. It can be limited to only one direction via using the
toPlainOnly
ortoClassOnly
option.Can be applied to class definitions and properties.
function Expose
Expose: (options?: ExposeOptions) => PropertyDecorator & ClassDecorator;
Marks the given class or property as included. By default the property is included in both constructorToPlain and plainToConstructor transformations. It can be limited to only one direction via using the
toPlainOnly
ortoClassOnly
option.Can be applied to class definitions and properties.
function instanceToInstance
instanceToInstance: { <T>(object: T, options?: ClassTransformOptions): T; <T>(object: T[], options?: ClassTransformOptions): T[];};
Converts class (constructor) object to new class (constructor) object. Also works with arrays.
function instanceToPlain
instanceToPlain: { <T>(object: T, options?: ClassTransformOptions): Record<string, any>; <T>(object: T[], options?: ClassTransformOptions): Record<string, any>[];};
Converts class (constructor) object to plain (literal) object. Also works with arrays.
function plainToClass
plainToClass: { <T, V>( cls: ClassConstructor<T>, plain: V[], options?: ClassTransformOptions ): T[]; <T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T;};
Converts plain (literal) object to class (constructor) object. Also works with arrays.
Deprecated
Function name changed, use the
plainToInstance
method instead.
function plainToClassFromExist
plainToClassFromExist: { <T, V>(clsObject: T[], plain: V[], options?: ClassTransformOptions): T[]; <T, V>(clsObject: T, plain: V, options?: ClassTransformOptions): T;};
Converts plain (literal) object to class (constructor) object. Uses given object as source object (it means fills given object with data from plain object). Also works with arrays.
Deprecated
This function is being removed. The current implementation is incorrect as it modifies the source object.
function plainToInstance
plainToInstance: { <T, V>( cls: ClassConstructor<T>, plain: V[], options?: ClassTransformOptions ): T[]; <T, V>(cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions): T;};
Converts plain (literal) object to class (constructor) object. Also works with arrays.
function serialize
serialize: { <T>(object: T, options?: ClassTransformOptions): string; <T>(object: T[], options?: ClassTransformOptions): string;};
Serializes given object to a JSON string.
Deprecated
This function is being removed. Please use
JSON.stringify(instanceToPlain(object, options))
function Transform
Transform: ( transformFn: (params: TransformFnParams) => any, options?: TransformOptions) => PropertyDecorator;
Defines a custom logic for value transformation.
Can be applied to properties only.
function TransformInstanceToInstance
TransformInstanceToInstance: (params?: ClassTransformOptions) => MethodDecorator;
Return the class instance only with the exposed properties.
Can be applied to functions and getters/setters only.
function TransformInstanceToPlain
TransformInstanceToPlain: (params?: ClassTransformOptions) => MethodDecorator;
Transform the object from class to plain object and return only with the exposed properties.
Can be applied to functions and getters/setters only.
function TransformPlainToInstance
TransformPlainToInstance: ( classType: ClassConstructor<any>, params?: ClassTransformOptions) => MethodDecorator;
Return the class instance only with the exposed properties.
Can be applied to functions and getters/setters only.
function Type
Type: ( typeFunction?: (type?: TypeHelpOptions) => Function, options?: TypeOptions) => PropertyDecorator;
Specifies a type of the property. The given TypeFunction can return a constructor. A discriminator can be given in the options.
Can be applied to properties only.
Classes
class ClassTransformer
class ClassTransformer {}
method classToClassFromExist
classToClassFromExist: { <T>(object: T, fromObject: T, options?: ClassTransformOptions): T; <T>(object: T, fromObjects: T[], options?: ClassTransformOptions): T[];};
Converts class (constructor) object to plain (literal) object. Uses given plain object as source object (it means fills given plain object with data from class object). Also works with arrays.
method classToPlainFromExist
classToPlainFromExist: { <T extends Record<string, any>, P>( object: T, plainObject: P, options?: ClassTransformOptions ): T; <T extends Record<string, any>, P>( object: T, plainObjects: P[], options?: ClassTransformOptions ): T[];};
Converts class (constructor) object to plain (literal) object. Uses given plain object as source object (it means fills given plain object with data from class object). Also works with arrays.
method deserialize
deserialize: <T>( cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions) => T;
Deserializes given JSON string to a object of the given class.
method deserializeArray
deserializeArray: <T>( cls: ClassConstructor<T>, json: string, options?: ClassTransformOptions) => T[];
Deserializes given JSON string to an array of objects of the given class.
method instanceToInstance
instanceToInstance: { <T>(object: T, options?: ClassTransformOptions): T; <T>(object: T[], options?: ClassTransformOptions): T[];};
Converts class (constructor) object to new class (constructor) object. Also works with arrays.
method instanceToPlain
instanceToPlain: { <T extends Record<string, any>>( object: T, options?: ClassTransformOptions ): Record<string, any>; <T extends Record<string, any>>( object: T[], options?: ClassTransformOptions ): Record<string, any>[];};
Converts class (constructor) object to plain (literal) object. Also works with arrays.
method plainToClassFromExist
plainToClassFromExist: { <T extends Record<string, any>, V extends any[]>( clsObject: T, plain: V, options?: ClassTransformOptions ): T; <T extends Record<string, any>, V>( clsObject: T, plain: V, options?: ClassTransformOptions ): T[];};
Converts plain (literal) object to class (constructor) object. Uses given object as source object (it means fills given object with data from plain object). Also works with arrays.
method plainToInstance
plainToInstance: { <T extends Record<string, any>, V extends any[]>( cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions ): T[]; <T extends Record<string, any>, V>( cls: ClassConstructor<T>, plain: V, options?: ClassTransformOptions ): T;};
Converts plain (literal) object to class (constructor) object. Also works with arrays.
method serialize
serialize: { <T>(object: T, options?: ClassTransformOptions): string; <T>(object: T[], options?: ClassTransformOptions): string;};
Serializes given object to a JSON string.
Interfaces
interface ClassTransformOptions
interface ClassTransformOptions {}
Options to be passed during transformation.
property enableCircularCheck
enableCircularCheck?: boolean;
If set to true then class transformer will perform a circular check. (circular check is turned off by default) This option is useful when you know for sure that your types might have a circular dependency.
property enableImplicitConversion
enableImplicitConversion?: boolean;
If set to true then class transformer will try to convert properties implicitly to their target type based on their typing information.
DEFAULT:
false
property excludeExtraneousValues
excludeExtraneousValues?: boolean;
Indicates if extraneous properties should be excluded from the value when converting a plain value to a class.
This option requires that each property on the target class has at least one
@Expose
or@Exclude
decorator assigned from this library.
property excludePrefixes
excludePrefixes?: string[];
Excludes properties with the given prefixes. For example, if you mark your private properties with "_" and "__" you can set this option's value to ["_", "__"] and all private properties will be skipped. This works only for "exposeAll" strategy.
property exposeDefaultValues
exposeDefaultValues?: boolean;
If set to true then class transformer will take default values for unprovided fields. This is useful when you convert a plain object to a class and have an optional field with a default value.
property exposeUnsetFields
exposeUnsetFields?: boolean;
When set to true, fields with
undefined
as value will be included in class to plain transformation. Otherwise those fields will be omitted from the result.DEFAULT:
true
property groups
groups?: string[];
Only properties with given groups gonna be transformed.
property ignoreDecorators
ignoreDecorators?: boolean;
If set to true then class transformer will ignore the effect of all and decorators. This option is useful if you want to kinda clone your object but do not apply decorators affects.
__NOTE:__ You may still have to add the decorators to make other options work.
property strategy
strategy?: 'excludeAll' | 'exposeAll';
Exclusion strategy. By default exposeAll is used, which means that it will expose all properties are transformed by default.
property targetMaps
targetMaps?: TargetMap[];
Target maps allows to set a Types of the transforming object without using decorator. This is useful when you are transforming external classes, or if you already have type metadata for objects and you don't want to set it up again.
property version
version?: number;
Only properties with "since" > version < "until" gonna be transformed.
interface DiscriminatorDescriptor
interface DiscriminatorDescriptor {}
Discriminator object containing the type information to select a proper type during transformation when a discriminator property is provided.
property property
property: string;
The name of the property which holds the type information in the received object.
property subTypes
subTypes: { /** * Name of the type. */ name: string; /** * A class constructor which can be used to create the object. */ value: ClassConstructor<any>;}[];
List of the available types. The transformer will try to lookup the object with the same key as the value received in the defined discriminator property and create an instance of the defined class.
interface ExcludeMetadata
interface ExcludeMetadata {}
This object represents metadata assigned to a property via the decorator.
property options
options: ExcludeOptions;
Options passed to the operator for this property.
property propertyName
propertyName: string | undefined;
The property name this metadata belongs to on the target (class or property).
Note: If the decorator is applied to a class the propertyName will be undefined.
property target
target: Function;
interface ExcludeOptions
interface ExcludeOptions {}
Possible transformation options for the decorator.
property toClassOnly
toClassOnly?: boolean;
Expose this property only when transforming from plain to class instance.
property toPlainOnly
toPlainOnly?: boolean;
Expose this property only when transforming from class instance to plain object.
interface ExposeMetadata
interface ExposeMetadata {}
This object represents metadata assigned to a property via the decorator.
property options
options: ExposeOptions;
Options passed to the operator for this property.
property propertyName
propertyName: string | undefined;
The property name this metadata belongs to on the target (class or property).
Note: If the decorator is applied to a class the propertyName will be undefined.
property target
target: Function;
interface ExposeOptions
interface ExposeOptions {}
Possible transformation options for the decorator.
property groups
groups?: string[];
List of transformation groups this property belongs to. When set, the property will be exposed only when transform is called with one of the groups specified.
Example:
instanceToPlain(payload, { groups: ['user'] });
property name
name?: string;
Name of property on the target object to expose the value of this property.
property since
since?: number;
First version where this property should be exposed.
Example:
instanceToPlain(payload, { version: 1.0 });
property toClassOnly
toClassOnly?: boolean;
Expose this property only when transforming from plain to class instance.
property toPlainOnly
toPlainOnly?: boolean;
Expose this property only when transforming from class instance to plain object.
property until
until?: number;
Last version where this property should be exposed.
Example:
instanceToPlain(payload, { version: 1.0 });
interface TargetMap
interface TargetMap {}
Allows to specify a map of Types in the object without using decorator. This is useful when you have external classes.
property properties
properties: { [key: string]: Function;};
List of properties and their Types.
property target
target: Function;
Target which Types are being specified.
interface TransformFnParams
interface TransformFnParams {}
interface TransformMetadata
interface TransformMetadata {}
This object represents metadata assigned to a property via the decorator.
property options
options: TransformOptions;
Options passed to the operator for this property.
property propertyName
propertyName: string;
The property name this metadata belongs to on the target (property only).
property target
target: Function;
property transformFn
transformFn: (params: TransformFnParams) => any;
The custom transformation function provided by the user in the decorator.
interface TransformOptions
interface TransformOptions {}
Possible transformation options for the decorator.
property groups
groups?: string[];
List of transformation groups this property belongs to. When set, the property will be exposed only when transform is called with one of the groups specified.
Example:
instanceToPlain(payload, { groups: ['user'] });
property since
since?: number;
First version where this property should be exposed.
Example:
instanceToPlain(payload, { version: 1.0 });
property toClassOnly
toClassOnly?: boolean;
Expose this property only when transforming from plain to class instance.
property toPlainOnly
toPlainOnly?: boolean;
Expose this property only when transforming from class instance to plain object.
property until
until?: number;
Last version where this property should be exposed.
Example:
instanceToPlain(payload, { version: 1.0 });
interface TypeHelpOptions
interface TypeHelpOptions {}
interface TypeMetadata
interface TypeMetadata {}
This object represents metadata assigned to a property via the decorator.
property options
options: TypeOptions;
Options passed to the operator for this property.
property propertyName
propertyName: string;
The property name this metadata belongs to on the target (property only).
property reflectedType
reflectedType: any;
The type guessed from assigned Reflect metadata ('design:type')
property target
target: Function;
property typeFunction
typeFunction: (options?: TypeHelpOptions) => Function;
The custom function provided by the user in the decorator which returns the target type for the transformation.
interface TypeOptions
interface TypeOptions {}
Possible transformation options for the decorator.
property discriminator
discriminator?: DiscriminatorDescriptor;
Optional discriminator object, when provided the property value will be initialized according to the specified object.
property keepDiscriminatorProperty
keepDiscriminatorProperty?: boolean;
Indicates whether to keep the discriminator property on the transformed object or not. Disabled by default.
false
Enums
enum TransformationType
enum TransformationType { PLAIN_TO_CLASS = 0, CLASS_TO_PLAIN = 1, CLASS_TO_CLASS = 2,}
member CLASS_TO_CLASS
CLASS_TO_CLASS = 2
member CLASS_TO_PLAIN
CLASS_TO_PLAIN = 1
member PLAIN_TO_CLASS
PLAIN_TO_CLASS = 0
Type Aliases
type ClassConstructor
type ClassConstructor<T> = { new (...args: any[]): T;};
Package Files (24)
- types/ClassTransformer.d.ts
- types/decorators/exclude.decorator.d.ts
- types/decorators/expose.decorator.d.ts
- types/decorators/transform-instance-to-instance.decorator.d.ts
- types/decorators/transform-instance-to-plain.decorator.d.ts
- types/decorators/transform-plain-to-instance.decorator.d.ts
- types/decorators/transform.decorator.d.ts
- types/decorators/type.decorator.d.ts
- types/enums/transformation-type.enum.d.ts
- types/index.d.ts
- types/interfaces/class-constructor.type.d.ts
- types/interfaces/class-transformer-options.interface.d.ts
- types/interfaces/decorator-options/exclude-options.interface.d.ts
- types/interfaces/decorator-options/expose-options.interface.d.ts
- types/interfaces/decorator-options/transform-options.interface.d.ts
- types/interfaces/decorator-options/type-discriminator-descriptor.interface.d.ts
- types/interfaces/decorator-options/type-options.interface.d.ts
- types/interfaces/metadata/exclude-metadata.interface.d.ts
- types/interfaces/metadata/expose-metadata.interface.d.ts
- types/interfaces/metadata/transform-fn-params.interface.d.ts
- types/interfaces/metadata/transform-metadata.interface.d.ts
- types/interfaces/metadata/type-metadata.interface.d.ts
- types/interfaces/target-map.interface.d.ts
- types/interfaces/type-help-options.interface.d.ts
Dependencies (0)
No dependencies.
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (0)
No peer dependencies.
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/class-transformer
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/class-transformer)
- HTML<a href="https://www.jsdocs.io/package/class-transformer"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 3887 ms. - Missing or incorrect documentation? Open an issue for this package.