import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from "./morphism"; /** * A structure-preserving object from a source data towards a target data. * * The keys of the schema match the desired destination structure. * Each value corresponds to an Action applied by Morphism when iterating over the input data * @example * ```typescript * * const input = { * foo: { * baz: 'value1' * } * }; * * const schema: Schema = { * bar: 'foo', // ActionString * qux: ['foo', 'foo.baz'], // ActionAggregator * quux: (iteratee, source, destination) => { // ActionFunction * return iteratee.foo; * }, * corge: { // ActionSelector * path: 'foo.baz', * fn: (propertyValue, source) => { * return propertyValue; * } * } * }; * * morphism(schema, input); * ``` */ export declare type StrictSchema = { [destinationProperty in keyof Target]: ActionString | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty]; } | ActionAggregator | ActionSelector | StrictSchema; } & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions; }; export declare type Schema = { [destinationProperty in keyof Target]?: ActionString | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty]; } | ActionAggregator | ActionSelector | Schema; } & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions; }; export declare type Actions = ActionFunction | ActionAggregator | ActionString | ActionSelector; /** * @interface ActionFunction * @description A Function invoked per iteration * @param {S} iteratee The current element to transform * @param {S|S[]} source The source input to transform * @param {D} target The current element transformed * @typeparam D Destination / Target type * @typeparam S Source / Input type * @typeparam R Inferred result type * @example * ```typescript * * const source = { * foo: { * bar: 'bar' * } * }; * let schema = { * bar: iteratee => { * // Apply a function over the source propery * return iteratee.foo.bar; * } * }; * * morphism(schema, source); * //=> { bar: 'bar' } * ``` * */ export interface ActionFunction { (iteratee: S, source: S[], target: D): R; } /** * @description A String path that indicates where to find the property in the source input * * @example * ```typescript * * const source = { * foo: 'baz', * bar: ['bar', 'foo'], * baz: { * qux: 'bazqux' * } * }; * const schema = { * foo: 'foo', // Simple Projection * bar: 'bar[0]', // Grab a value from an array * bazqux: 'baz.qux' // Grab a value from a nested property, * }; * * morphism(schema, source); * //=> { foo: 'baz', bar: 'bar', bazqux: 'bazqux' } * ``` * */ export declare type ActionString = string; /** * An Array of String that allows to perform a function over source property * * @example * ```typescript * * const source = { * foo: 'foo', * bar: 'bar' * }; * let schema = { * fooAndBar: ['foo', 'bar'] // Grab these properties into fooAndBar * }; * * morphism(schema, source); * //=> { fooAndBar: { foo: 'foo', bar: 'bar' } } * ``` */ export declare type ActionAggregator = T extends object ? (keyof T)[] | string[] : string[]; /** * @interface ActionSelector * @typeparam Source Source/Input Type * @typeparam R Result Type * * @description An Object that allows to perform a function over a source property's value * * @example * ```typescript * * const source = { * foo: { * bar: 'bar' * } * }; * let schema = { * barqux: { * path: 'foo.bar', * fn: value => `${value}qux` // Apply a function over the source property's value * } * }; * * morphism(schema, source); * //=> { barqux: 'barqux' } *``` * */ export interface ActionSelector { path: ActionString | ActionAggregator; fn: (fieldValue: any, object: Source, items: Source, objectToCompute: Target) => Target[TargetProperty]; } export interface Constructable { new (...args: any[]): T; } export declare type SourceFromSchema = T extends StrictSchema | Schema ? U : never; export declare type DestinationFromSchema = T extends StrictSchema | Schema ? U : never; export declare type ResultItem = DestinationFromSchema; export interface Mapper> { (data?: SourceFromSchema[] | null): TResult[]; (data?: SourceFromSchema | null): TResult; }