UNPKG

@data-client/normalizr

Version:

Normalizes and denormalizes JSON according to schema for Redux and Flux applications

121 lines (110 loc) 3.02 kB
export type Schema = | null | string | { [K: string]: any } | Schema[] | SchemaSimple | Serializable; export interface Queryable<Args extends readonly any[] = readonly any[]> { queryKey( args: Args, queryKey: (...args: any) => any, getEntity: GetEntity, getIndex: GetIndex, // Must be non-void ): {}; } export type Serializable< T extends { toJSON(): string } = { toJSON(): string }, > = (value: any) => T; export interface SchemaSimple<T = any, Args extends readonly any[] = any[]> { normalize( input: any, parent: any, key: any, args: any[], visit: (...args: any) => any, addEntity: (...args: any) => any, getEntity: (...args: any) => any, checkLoop: (...args: any) => any, ): any; denormalize( input: {}, args: readonly any[], unvisit: (schema: any, input: any) => any, ): T; queryKey( args: Args, queryKey: (...args: any) => any, getEntity: GetEntity, getIndex: GetIndex, ): any; } export interface SchemaClass<T = any, Args extends readonly any[] = any[]> extends SchemaSimple<T, Args> { // this is not an actual member, but is needed for the recursive NormalizeNullable<> type algo _normalizeNullable(): any; // this is not an actual member, but is needed for the recursive DenormalizeNullable<> type algo _denormalizeNullable(): any; } export interface EntityInterface<T = any> extends SchemaSimple { createIfValid(props: any): any; pk( params: any, parent?: any, key?: string, args?: readonly any[], ): string | number | undefined; readonly key: string; merge(existing: any, incoming: any): any; mergeWithStore( existingMeta: any, incomingMeta: any, existing: any, incoming: any, ): any; mergeMetaWithStore( existingMeta: any, incomingMeta: any, existing: any, incoming: any, ): any; indexes?: any; schema: Record<string, Schema>; cacheWith?: object; prototype: T; } export interface NormalizedIndex { readonly [entityKey: string]: { readonly [indexName: string]: { readonly [lookup: string]: string }; }; } export interface EntityTable { [entityKey: string]: | { [pk: string]: unknown; } | undefined; } /** Visits next data + schema while recurisvely normalizing */ export interface Visit { (schema: any, value: any, parent: any, key: any, args: readonly any[]): any; } /** Returns true if a circular reference is found */ export interface CheckLoop { (entityKey: string, pk: string, input: object): boolean; } /** Get Array of entities with map function applied */ export interface GetEntity { (entityKey: string | symbol): { readonly [pk: string]: any } | undefined; (entityKey: string | symbol, pk: string | number): any; } /** Get PK using an Entity Index */ export interface GetIndex { /** getIndex('User', 'username', 'ntucker') */ ( entityKey: string, field: string, value: string, ): { readonly [indexKey: string]: string | undefined }; }