jsonpath-mapper
Version:
A json to json transformation utility with a few nice features to use when translating for example API responses into a domain object for use in your domain-driven JavaScript applications. Can be used in React applications with the 'useMapper' hook.
44 lines (38 loc) • 2.26 kB
TypeScript
type Path = string | string[];
type MappingElement<S> = Path | ResultFormatter<S> | {
[key: string]: MappingElement<S>;
} | MappingElement<S>[];
type WireFunction<S> = (scopedJson: S | any, root: S) => any;
type ResultFormatter<S> = WireFunction<S> | MapperFunctions<S>;
type DefaultFunction<S> = WireFunction<S> | string | number;
type DisableFunction<S> = (resolvedJson: S | any, root: S) => boolean;
type Partial<T> = {
[P in keyof T]?: T[P];
};
type Deferred<T> = {
[P in keyof T]: Promise<T[P]>;
};
type PureJsFunction<S> = (json: S) => any;
interface MapperFunctions<S> {
$path: string | string[];
$formatting?: ResultFormatter<S>;
$return?: ResultFormatter<S>;
$default?: DefaultFunction<S>;
$disable?: DisableFunction<S>;
}
interface MappingTemplate<S> {
[key: string]: MappingElement<S>;
}
type MapJson = <S, T>(json: S, template: MappingTemplate<S> | PureJsFunction<S>) => T;
type MapJsonAsync = <S, T>(json: S, template: MappingTemplate<S> | PureJsFunction<S>) => Promise<T>;
type MapArrayFunction = <S, T>(json: S, arr: MappingTemplate<S>[], $root: S) => T | Promise<T>;
type FormatResult = <S>(value: S, $formatting: ResultFormatter<S>, $root: S) => any | Promise<any>;
type MapObject = <S, T>(json: S, obj: MappingTemplate<S> | ResultFormatter<S>, $root: S) => T | Promise<T>;
type ElementKeyValue<S> = [string, MappingElement<S>];
type MapperFunctionKeyValue<S> = [string, MapperFunctions<S>];
type MapElement = <S>(keyValue: ElementKeyValue<S>, json: S, $root: S) => ElementKeyValue<S> | Promise<ElementKeyValue<S>>;
type HandleMapperFunctions = <S>(keyValue: MapperFunctionKeyValue<S>, json: S, $root: S) => MappingElement<S> | Promise<MappingElement<S>>;
declare const mapJson$1: MapJsonAsync;
declare const jpath: <S, T>(query: string, json: S) => T | T[];
declare const mapJson: MapJson;
export { type DefaultFunction, type Deferred, type DisableFunction, type FormatResult, type HandleMapperFunctions, type MapArrayFunction, type MapElement, type MapJson, type MapJsonAsync, type MapObject, type MapperFunctions, type MappingElement, type MappingTemplate, type Partial, type Path, type PureJsFunction, type ResultFormatter, type WireFunction, mapJson as default, jpath, mapJson$1 as mapJsonAsync };