|
1 |
| -import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from './morphism'; |
| 1 | +import { SCHEMA_OPTIONS_SYMBOL, SchemaOptions } from "./morphism"; |
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * A structure-preserving object from a source data towards a target data.
|
@@ -35,22 +35,38 @@ export type StrictSchema<Target = any, Source = any> = {
|
35 | 35 | /** `destinationProperty` is the name of the property of the target object you want to produce */
|
36 | 36 | [destinationProperty in keyof Target]:
|
37 | 37 | | ActionString<Source>
|
38 |
| - | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] } |
| 38 | + | { |
| 39 | + ( |
| 40 | + iteratee: Source, |
| 41 | + source: Source[], |
| 42 | + target: Target[destinationProperty] |
| 43 | + ): Target[destinationProperty]; |
| 44 | + } |
39 | 45 | | ActionAggregator<Source>
|
40 |
| - | ActionSelector<Source, Target> |
| 46 | + | ActionSelector<Source, Target, destinationProperty> |
41 | 47 | | StrictSchema<Target[destinationProperty], Source>;
|
42 | 48 | } & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target> };
|
43 | 49 | export type Schema<Target = any, Source = any> = {
|
44 | 50 | /** `destinationProperty` is the name of the property of the target object you want to produce */
|
45 | 51 | [destinationProperty in keyof Target]?:
|
46 | 52 | | ActionString<Source>
|
47 |
| - | { (iteratee: Source, source: Source[], target: Target[destinationProperty]): Target[destinationProperty] } |
| 53 | + | { |
| 54 | + ( |
| 55 | + iteratee: Source, |
| 56 | + source: Source[], |
| 57 | + target: Target[destinationProperty] |
| 58 | + ): Target[destinationProperty]; |
| 59 | + } |
48 | 60 | | ActionAggregator<Source>
|
49 |
| - | ActionSelector<Source, Target> |
| 61 | + | ActionSelector<Source, Target, destinationProperty> |
50 | 62 | | Schema<Target[destinationProperty], Source>;
|
51 | 63 | } & { [SCHEMA_OPTIONS_SYMBOL]?: SchemaOptions<Target | any> };
|
52 | 64 |
|
53 |
| -export type Actions<Target, Source> = ActionFunction<Target, Source> | ActionAggregator | ActionString<Target> | ActionSelector<Source>; |
| 65 | +export type Actions<Target, Source> = |
| 66 | + | ActionFunction<Target, Source> |
| 67 | + | ActionAggregator |
| 68 | + | ActionString<Target> |
| 69 | + | ActionSelector<Source>; |
54 | 70 |
|
55 | 71 | /**
|
56 | 72 | * @interface ActionFunction
|
@@ -129,7 +145,9 @@ export type ActionString<Source> = string; // TODO: ActionString should support
|
129 | 145 | * //=> { fooAndBar: { foo: 'foo', bar: 'bar' } }
|
130 | 146 | * ```
|
131 | 147 | */
|
132 |
| -export type ActionAggregator<T extends unknown = unknown> = T extends object ? (keyof T)[] | string[] : string[]; |
| 148 | +export type ActionAggregator<T extends unknown = unknown> = T extends object |
| 149 | + ? (keyof T)[] | string[] |
| 150 | + : string[]; |
133 | 151 | /**
|
134 | 152 | * @interface ActionSelector
|
135 | 153 | * @typeparam Source Source/Input Type
|
@@ -157,21 +175,41 @@ export type ActionAggregator<T extends unknown = unknown> = T extends object ? (
|
157 | 175 | *```
|
158 | 176 | *
|
159 | 177 | */
|
160 |
| -export interface ActionSelector<Source = object, R = any> { |
| 178 | +export interface ActionSelector< |
| 179 | + Source = object, |
| 180 | + Target = any, |
| 181 | + TargetProperty extends keyof Target = any |
| 182 | +> { |
161 | 183 | path: ActionString<Source> | ActionAggregator<Source>;
|
162 |
| - fn: (fieldValue: any, object: Source, items: Source, objectToCompute: R) => R; |
| 184 | + fn: ( |
| 185 | + fieldValue: any, |
| 186 | + object: Source, |
| 187 | + items: Source, |
| 188 | + objectToCompute: Target |
| 189 | + ) => Target[TargetProperty]; |
163 | 190 | }
|
164 | 191 |
|
165 | 192 | export interface Constructable<T> {
|
166 | 193 | new (...args: any[]): T;
|
167 | 194 | }
|
168 | 195 |
|
169 |
| -export type SourceFromSchema<T> = T extends StrictSchema<unknown, infer U> | Schema<unknown, infer U> ? U : never; |
170 |
| -export type DestinationFromSchema<T> = T extends StrictSchema<infer U> | Schema<infer U> ? U : never; |
| 196 | +export type SourceFromSchema<T> = T extends |
| 197 | + | StrictSchema<unknown, infer U> |
| 198 | + | Schema<unknown, infer U> |
| 199 | + ? U |
| 200 | + : never; |
| 201 | +export type DestinationFromSchema<T> = T extends |
| 202 | + | StrictSchema<infer U> |
| 203 | + | Schema<infer U> |
| 204 | + ? U |
| 205 | + : never; |
171 | 206 |
|
172 | 207 | export type ResultItem<TSchema extends Schema> = DestinationFromSchema<TSchema>;
|
173 | 208 |
|
174 |
| -export interface Mapper<TSchema extends Schema | StrictSchema, TResult = ResultItem<TSchema>> { |
| 209 | +export interface Mapper< |
| 210 | + TSchema extends Schema | StrictSchema, |
| 211 | + TResult = ResultItem<TSchema> |
| 212 | +> { |
175 | 213 | (data?: SourceFromSchema<TSchema>[] | null): TResult[];
|
176 | 214 | (data?: SourceFromSchema<TSchema> | null): TResult;
|
177 | 215 | }
|
0 commit comments