UNPKG

@tsed/json-mapper

Version:
77 lines (76 loc) 3.12 kB
import { Type } from "@tsed/core"; import { JsonSchema } from "@tsed/schema"; /** * Signature exposed by compiled mappers. They accept an input value plus contextual options * and return the serialized/deserialized result. */ export type JsonMapperCallback<Options> = (input: any, options?: Options) => any; /** * Executable mapper plus its unique identifier stored in the compiler cache. */ export type CachedJsonMapper<Options> = { id: string; fn: JsonMapperCallback<Options>; }; /** * Cached mapper registry keyed by the groups fingerprint generated for a schema. */ export type CachedGroupsJsonMapper<Options> = Map<string, CachedJsonMapper<Options>>; /** * Base compiler that turns schema metadata into executable (de)serialization functions. * Subclasses supply `map`, `alterValue`, and `createMapper` implementations to specialize * the pipeline for serialization or deserialization. */ export declare abstract class JsonMapperCompiler<Options extends Record<string, any> = any> { /** * Cached mappers metadata * @protected */ protected cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>; /** * Cached executable mappers by his id * @protected */ protected mappers: Record<string, JsonMapperCallback<Options>>; /** * Cached schemas * @protected */ protected schemes: Record<string, any>; /** * Cached classes by his id * @protected */ protected constructors: Record<string, Type<any>>; /** * Global variables available in the mapper * @protected */ protected globals: Record<string, any>; constructor(); addTypeMapper(model: Type<any> | string, fn: any): this; removeTypeMapper(model: Type<any> | string): void; addGlobal(key: string, value: any): this; compileMapper(mapper: string, { id, groupsId, storeGroups }: { id: string; groupsId: string; model: Type<any> | string; storeGroups: CachedGroupsJsonMapper<Options>; }): CachedJsonMapper<Options>; createContext(options: Options): Options & { cache: Map<string | Type<any>, CachedGroupsJsonMapper<Options>>; }; compile(model: Type<any> | string, groups: false | string[], opts?: { mapper?: any; }): CachedJsonMapper<Options>; protected execMapper(id: string, value: any, options: Options): any; protected abstract map(input: any, options: Options): any; protected abstract alterValue(schemaId: string, value: any, options: Options): any; protected abstract createMapper(model: Type<any>, id: string, groups: false | string[]): string; protected getType(model: Type<any>): Type<any> | MapConstructor | ObjectConstructor | ArrayConstructor | SetConstructor; protected alterIgnore(id: string, options: Options): any; protected alterGroups(schema: JsonSchema, groups: false | string[]): boolean; protected getGroupsId(groups: false | string[]): string; protected getId(model: Type<any> | string, groupsId: string): string; protected getSchemaId(id: string, propertyKey: string): string; }