@dipscope/type-manager
Version:
Transform JSON strings or plain objects into JS class instances.
456 lines • 18.8 kB
TypeScript
import { GenericArgument } from './generic-argument';
import { Logger } from './logger';
import { SerializerContext } from './serializer-context';
import { TypeConfiguration } from './type-configuration';
import { TypeFn } from './type-fn';
import { TypeLike } from './type-like';
import { TypeManagerOptions } from './type-manager-options';
import { TypeMetadata } from './type-metadata';
import { TypeOptions } from './type-options';
import { TypeOptionsBase } from './type-options-base';
import { TypeScope } from './type-scope';
/**
* Type manager class.
*
* @type {TypeManager}
*/
export declare class TypeManager {
/**
* Static shared options of any type which applied to any instance of type manager
* by default.
*
* @type {TypeOptionsBase<any>}
*/
private static readonly defaultTypeOptionsBase;
/**
* Static type options per type which applied to any instance of type manager
* by default.
*
* @type {ReadonlyMap<TypeFn<any>, TypeOptions<any>>}
*/
private static readonly defaultTypeOptionsMap;
/**
* Static type manager instance which is used for decorator based configurations and
* static declarative based configurations.
*
* @type {TypeManager}
*/
static readonly staticTypeManager: TypeManager;
/**
* Type scope to work with registration in static context.
*
* @type {TypeScope}
*/
static readonly typeScope: TypeScope;
/**
* Symbol of current instance which is used to store type metadata within types.
*
* @type {symbol}
*/
readonly symbol: symbol;
/**
* Type manager options.
*
* @type {TypeManagerOptions}
*/
private readonly typeManagerOptions;
/**
* Type function map for types with aliases.
*
* @type {Map<Alias, TypeFn<any>>}
*/
private readonly typeFnMap;
/**
* Type metadata set for the current instance of type manager.
*
* @type {Set<TypeMetadata<any>>}
*/
private readonly typeMetadataSet;
/**
* Constructor.
*
* Creating an instance of type manager allows to have multiple configs. By default only static
* instance of type manager is created on the background and all decorator based and declarative based
* configurations are applied to it. Note that all options which are passed to a constructor are
* mutated and all references are kept. This allow to have different managers with intersected data.
* If such behaviour is not required then use configure methods after creating instance of type manager.
*
* @param {TypeManagerOptions} typeManagerOptions Type manager options.
*/
constructor(typeManagerOptions?: TypeManagerOptions);
/**
* Gets symbol in static context.
*
* @returns {symbol} Type manager symbol.
*/
static get symbol(): symbol;
/**
* Gets logger in static context.
*
* @returns {Logger} Logger.
*/
static get logger(): Logger;
/**
* Gets logger.
*
* @returns {Logger} Logger.
*/
get logger(): Logger;
/**
* Gets type options base in static context.
*
* @returns {Readonly<TypeOptionsBase<any>>} Type options base.
*/
static get typeOptionsBase(): Readonly<TypeOptionsBase<any>>;
/**
* Gets type options base.
*
* @returns {Readonly<TypeOptionsBase<any>>} Type options base.
*/
get typeOptionsBase(): Readonly<TypeOptionsBase<any>>;
/**
* Gets type options map in static context.
*
* @returns {ReadonlyMap<TypeFn<any>, TypeOptions<any>>} Type options map.
*/
static get typeOptionsMap(): ReadonlyMap<TypeFn<any>, TypeOptions<any>>;
/**
* Gets type options map.
*
* @returns {ReadonlyMap<TypeFn<any>, TypeOptions<any>>} Type options map.
*/
get typeOptionsMap(): ReadonlyMap<TypeFn<any>, TypeOptions<any>>;
/**
* Gets type configuration map in static context.
*
* @returns {ReadonlyMap<TypeFn<any>, TypeConfiguration<any>>} Type configuration map.
*/
static get typeConfigurationMap(): ReadonlyMap<TypeFn<any>, TypeConfiguration<any>>;
/**
* Gets type configuration map.
*
* @returns {ReadonlyMap<TypeFn<any>, TypeConfiguration<any>>} Type configuration map.
*/
get typeConfigurationMap(): ReadonlyMap<TypeFn<any>, TypeConfiguration<any>>;
/**
* Constructs initial type manager options by extending passed options
* with default values if they are not overriden. All references are kept.
*
* @param {TypeManagerOptions} typeManagerOptions Type manager options.
*
* @returns {TypeManagerOptions} Constructed type manager options.
*/
private constructTypeManagerOptions;
/**
* Constructs type options base.
*
* @param {Partial<TypeOptionsBase<any>>} typeOptionsBase Type options base.
*
* @returns {Partial<TypeOptionsBase<any>>} Constructed type options base.
*/
private constructTypeOptionsBase;
/**
* Constructs type options map.
*
* @param {Map<TypeFn<any>, TypeOptions<any>>} typeOptionsMap Type options map.
*
* @returns {Map<TypeFn<any>, TypeOptions<any>>} Constructed type options map.
*/
private constructTypeOptionsMap;
/**
* Configures type metadata for provided type function in a static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeOptions<TObject>} typeOptions Type options.
*
* @returns {TypeMetadata<TObject>} Type metadata for provided type function.
*/
static configureTypeMetadata<TObject>(typeFn: TypeFn<TObject>, typeOptions?: TypeOptions<TObject>): TypeMetadata<TObject>;
/**
* Configures type metadata for provided type function.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeOptions<TObject>} typeOptions Type options.
*
* @returns {TypeMetadata<TObject>} Type metadata for provided type function.
*/
configureTypeMetadata<TObject>(typeFn: TypeFn<TObject>, typeOptions?: TypeOptions<TObject>): TypeMetadata<TObject>;
/**
* Declares type metadata for provided type function based on general configuration.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeOptions<TObject>} typeOptions Type options.
*
* @returns {TypeMetadata<TObject>} Type metadata.
*/
private declareTypeMetadata;
/**
* Extracts type metadata from provided type function in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
*
* @returns {TypeMetadata<TObject>} Type metadata for provided type function.
*/
static extractTypeMetadata<TObject>(typeFn: TypeFn<TObject>): TypeMetadata<TObject>;
/**
* Extracts type metadata from provided type function.
*
* @param {TypeFn<TObject>} typeFn Type function.
*
* @returns {TypeMetadata<TObject>} Type metadata for provided type function.
*/
extractTypeMetadata<TObject>(typeFn: TypeFn<TObject>): TypeMetadata<TObject>;
/**
* Applies shared type options in static context.
*
* @param {Partial<TypeOptionsBase<TObject>>} typeOptionsBase Type options base.
*
* @returns {TypeManager} Static instance of type manager.
*/
static applyTypeOptionsBase<TObject>(typeOptionsBase: Partial<TypeOptionsBase<TObject>>): TypeManager;
/**
* Applies shared type options.
*
* @param {Partial<TypeOptionsBase<TObject>>} typeOptionsBase Type options base.
*
* @returns {this} Instance of type manager.
*/
applyTypeOptionsBase<TObject>(typeOptionsBase: Partial<TypeOptionsBase<TObject>>): this;
/**
* Applies type options map in static context.
*
* @param {Map<TypeFn<TObject>, TypeOptions<TObject>>} typeOptionsMap Type options map.
*
* @returns {TypeManager} Static instance of type manager.
*/
static applyTypeOptionsMap<TObject>(typeOptionsMap: Map<TypeFn<TObject>, TypeOptions<TObject>>): TypeManager;
/**
* Applies type options map.
*
* @param {Map<TypeFn<TObject>, TypeOptions<TObject>>} typeOptionsMap Type options map.
*
* @returns {this} Instance of type manager.
*/
applyTypeOptionsMap<TObject>(typeOptionsMap: Map<TypeFn<TObject>, TypeOptions<TObject>>): this;
/**
* Applies type options in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeOptions<TObject>} typeOptions Type options.
*
* @returns {TypeManager} Static instance of type manager.
*/
static applyTypeOptions<TObject>(typeFn: TypeFn<TObject>, typeOptions: TypeOptions<TObject>): TypeManager;
/**
* Applies type options.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeOptions<TObject>} typeOptions Type options.
*
* @returns {this} Instance of type manager.
*/
applyTypeOptions<TObject>(typeFn: TypeFn<TObject>, typeOptions: TypeOptions<TObject>): this;
/**
* Applies type configuration map in static context.
*
* @param {Map<TypeFn<TObject>, TypeConfiguration<TObject>>} typeConfigurationMap Type configuration map.
*
* @returns {TypeManager} Static instance of type manager.
*/
static applyTypeConfigurationMap<TObject>(typeConfigurationMap: Map<TypeFn<TObject>, TypeConfiguration<TObject>>): TypeManager;
/**
* Applies type configuration map.
*
* @param {Map<TypeFn<TObject>, TypeConfiguration<TObject>>} typeConfigurationMap Type configuration map.
*
* @returns {this} Instance of type manager.
*/
applyTypeConfigurationMap<TObject>(typeConfigurationMap: Map<TypeFn<TObject>, TypeConfiguration<TObject>>): this;
/**
* Applies type configuration in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeConfiguration<TObject>} typeConfiguration Type configuration.
*
* @returns {TypeManager} Static instance of type manager.
*/
static applyTypeConfiguration<TObject>(typeFn: TypeFn<TObject>, typeConfiguration: TypeConfiguration<TObject>): TypeManager;
/**
* Applies type configuration.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeConfiguration<TObject>} typeConfiguration Type configuration.
*
* @returns {this} Instance of type manager.
*/
applyTypeConfiguration<TObject>(typeFn: TypeFn<TObject>, typeConfiguration: TypeConfiguration<TObject>): this;
/**
* Configures type manager in static context.
*
* @param {TypeManagerOptions} typeManagerOptions Type manager options.
*
* @returns {TypeManager} Static instance of type manager.
*/
static configure(typeManagerOptions: TypeManagerOptions): TypeManager;
/**
* Configures type manager.
*
* @param {TypeManagerOptions} typeManagerOptions Type manager options.
*
* @returns {this} Instance of type manager.
*/
configure(typeManagerOptions: TypeManagerOptions): this;
/**
* Defines serializer context for x in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {any} x Some value.
* @param {Array<GenericArgument<any>>} genericArguments Generic arguments.
*
* @returns {SerializerContext<TObject>} Serializer context.
*/
static defineSerializerContext<TObject>(typeFn: TypeFn<TObject>, x: any, genericArguments?: Array<GenericArgument<any>>): SerializerContext<TObject>;
/**
* Defines serializer context for x.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {any} x Some value.
* @param {Array<GenericArgument<any>>} genericArguments Generic arguments.
*
* @returns {SerializerContext<TObject>} Serializer context.
*/
defineSerializerContext<TObject>(typeFn: TypeFn<TObject>, x: any, genericArguments?: Array<GenericArgument<any>>): SerializerContext<TObject>;
/**
* Create a new clone of the static type manager. This method returns a shallow
* copy, preserving its current configuration and registered types.
*
* @returns {TypeManager} A new instance that duplicates the static type manager.
*/
static clone(): TypeManager;
/**
* Create a new clone of this type manager instance. The clone will contain
* copies of this instance base options and mappings, but will be completely
* independent: subsequent changes on the clone or the original will not affect
* each other.
*
* @returns {TypeManager} A fresh type manager instance with the same configuration and registered types.
*/
clone(): TypeManager;
/**
* Creates a clone of type options base.
*
* @returns {TypeOptionsBase<any>} Type options base clone.
*/
private cloneTypeOptionsBase;
/**
* Creates a clone of type options map.
*
* @returns {Map<TypeFn<any>, TypeOptions<any>>} Type options map clone.
*/
private cloneTypeOptionsMap;
/**
* Clears all type registrations from the static type manager. After calling this
* method, the static type manager will no longer hold any registered type metadata.
*
* @returns {TypeManager} Static instance of type manager.
*/
static clear(): TypeManager;
/**
* Clears all type registrations from the type manager. After calling this
* method, the type manager will no longer hold any registered type metadata.
*
* @returns {this} Instance of type manager.
*/
clear(): this;
/**
* Serializes provided value based on the type in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeLike<TObject>} x Input value.
*
* @returns {TypeLike<any>} Object created from provided input value or undefined.
*/
static serialize<TObject>(typeFn: TypeFn<TObject>, x: undefined): any;
static serialize<TObject>(typeFn: TypeFn<TObject>, x: null): null;
static serialize<TObject>(typeFn: TypeFn<TObject>, x: Array<TObject>): Array<any>;
static serialize<TObject>(typeFn: TypeFn<TObject>, x: TObject): any;
/**
* Serializes provided value based on the type function.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeLike<TObject>} x Input value.
*
* @returns {TypeLike<any>} Object created from provided input value or undefined.
*/
serialize<TObject>(typeFn: TypeFn<TObject>, x: undefined): any;
serialize<TObject>(typeFn: TypeFn<TObject>, x: null): null;
serialize<TObject>(typeFn: TypeFn<TObject>, x: Array<TObject>): Array<any>;
serialize<TObject>(typeFn: TypeFn<TObject>, x: TObject): any;
/**
* Deserializes provided value based on the type function in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeLike<any>} x Input value.
*
* @returns {TypeLike<TObject>} Type created from provided input value or undefined.
*/
static deserialize<TObject>(typeFn: TypeFn<TObject>, x: undefined): any;
static deserialize<TObject>(typeFn: TypeFn<TObject>, x: null): null;
static deserialize<TObject>(typeFn: TypeFn<TObject>, x: Array<any>): Array<TObject>;
static deserialize<TObject>(typeFn: TypeFn<TObject>, x: any): TObject;
/**
* Deserializes provided value based on the type function.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {TypeLike<any>} x Input value.
*
* @returns {TypeLike<TObject>} Type created from provided input value or undefined.
*/
deserialize<TObject>(typeFn: TypeFn<TObject>, x: undefined): any;
deserialize<TObject>(typeFn: TypeFn<TObject>, x: null): null;
deserialize<TObject>(typeFn: TypeFn<TObject>, x: Array<any>): Array<TObject>;
deserialize<TObject>(typeFn: TypeFn<TObject>, x: any): TObject;
/**
* Converts provided value to a JavaScript Object Notation (JSON) string in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {any} x Input value, usually an object or array, to be converted.
* @param {Function|Array<number>|Array<string>} replacer A function that transforms the results or an array of strings and numbers that acts as an approved list.
* @param {string|number} space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*
* @returns {string} JSON string.
*/
static stringify<TObject>(typeFn: TypeFn<TObject>, x: any, replacer?: (this: any, key: string, value: any) => any | Array<number> | Array<string> | null, space?: string | number): string;
/**
* Converts provided value to a JavaScript Object Notation (JSON) string.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {any} x Input value, usually an object or array, to be converted.
* @param {Function|Array<number>|Array<string>} replacer A function that transforms the results or an array of strings and numbers that acts as an approved list.
* @param {string|number} space Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.
*
* @returns {string} JSON string.
*/
stringify<TObject>(typeFn: TypeFn<TObject>, x: any, replacer?: (this: any, key: string, value: any) => any | Array<number> | Array<string> | null, space?: string | number): string;
/**
* Converts a JavaScript Object Notation (JSON) string into a type in static context.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {string} x A valid JSON string.
* @param {Function} reviver A function that transforms the results. This function is called for each member of the object.
*
* @returns {TypeLike<TObject>} Type created from provided input value or undefined.
*/
static parse<TObject>(typeFn: TypeFn<TObject>, x: string, reviver?: (this: any, key: string, value: any) => any): TypeLike<TObject>;
/**
* Converts a JavaScript Object Notation (JSON) string into a type.
*
* @param {TypeFn<TObject>} typeFn Type function.
* @param {string} x A valid JSON string.
* @param {Function} reviver A function that transforms the results. This function is called for each member of the object.
*
* @returns {TypeLike<TObject>} Type created from provided input value or undefined.
*/
parse<TObject>(typeFn: TypeFn<TObject>, x: string, reviver?: (this: any, key: string, value: any) => any): TypeLike<TObject>;
}
//# sourceMappingURL=type-manager.d.ts.map