UNPKG

lincd

Version:

LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)

90 lines (89 loc) 3.83 kB
import { LinkedQuery } from './SelectQuery.js'; import { NodeShape, PropertyShape } from '../shapes/SHACL.js'; import { Shape } from '../shapes/Shape.js'; import { ShapeSet } from '../collections/ShapeSet.js'; export type Prettify<T> = T extends infer R ? { [K in keyof R]: R[K]; } : never; type WithId<U> = U & { id: string; }; type IsPlainObject<T> = T extends object ? T extends any[] ? false : T extends Function ? false : T extends Date ? false : T extends RegExp ? false : T extends Error ? false : T extends null ? false : T extends undefined ? false : T extends object ? true : false : false; type RecursiveTransform<T, IsCreate = false> = T extends string | number | boolean | Date | null | undefined ? T : T extends Array<infer U> ? UpdatedSet<Prettify<RecursiveTransform<U>>, IsCreate> : IsSetModification<T> extends true ? ModifiedSet<T, IsCreate> : IsPlainObject<T> extends true ? WithId<{ [K in keyof T]: Prettify<RecursiveTransform<T[K], IsCreate>>; }> : T; type UpdatedSet<U, IsCreate> = IsCreate extends true ? U[] : { updatedTo: U[]; }; type IsSetModification<T> = T extends { add?: any; remove?: any; } ? true : false; type AddedType<T> = T extends { add: (infer U)[]; } ? U : T extends { add: infer U; } ? U : never; type RemovedType<T> = T extends { remove: (infer U)[]; } ? U : T extends { remove: infer U; } ? U : never; type ModifiedSet<T, IsCreate = false> = { added: AddId<AddedType<T>, IsCreate>[]; removed: AddId<RemovedType<T>, IsCreate>[]; }; export type AddId<T, IsCreate = false> = Prettify<RecursiveTransform<T, IsCreate>>; export type UpdatePartial<S = Shape> = UpdateNodeDescription<S> | NodeReferenceValue; type UpdateNodeDescription<Shape> = Partial<Omit<{ [P in KeysWithoutFunctions<Shape>]: ShapePropValueToUpdatePartial<Shape[P]>; }, 'node' | 'nodeShape' | 'namedNode' | 'targetClass'>>; type KeysWithoutFunctions<T> = { [K in keyof T]: T[K] extends Function ? never : K; }[keyof T]; type ShapePropValueToUpdatePartial<ShapeProperty> = ShapeProperty extends Shape ? UpdatePartial<ShapeProperty> : ShapeProperty extends ShapeSet<infer SSType> ? SetUpdateValue<SSType> : ShapeProperty; type SetUpdateValue<SSType> = UpdatePartial<SSType>[] | SetModification<SSType>; export type SetModification<SSType> = { add?: UpdatePartial<SSType>[] | UpdatePartial<SSType>; remove?: UpdatePartial<SSType>[] | UpdatePartial<SSType>; }; export type SetModificationValue = { $add?: UpdatePartial[]; $remove?: NodeReferenceValue[]; }; type UnsetValue = undefined; export type LiteralUpdateValue = string | number | boolean | Date; export type PropUpdateValue = SinglePropertyUpdateValue | SinglePropertyUpdateValue[] | SetModificationValue; export type SinglePropertyUpdateValue = NodeDescriptionValue | NodeReferenceValue | LiteralUpdateValue | UnsetValue; export type NodeDescriptionValue = { shape: NodeShape; fields: UpdateNodePropertyValue[]; /** * The id of the node to be created. * Optional, if not provided a new id will be generated. */ __id?: string; }; export type UpdateNodePropertyValue = { prop: PropertyShape; val: PropUpdateValue; }; export type NodeReferenceValue = { id: string; }; export type ShapeReferenceValue = { id: string; shape: NodeReferenceValue; }; export declare abstract class QueryFactory { getQueryObject(): LinkedQuery | Promise<LinkedQuery>; } export declare function isSetModificationValue(value: any): value is SetModificationValue; /** * Checks if the new count of values for a property is within the min and max count of the property shape. * Throws an error if the count is not within the range. * @param propShape * @param numValues */ export declare function checkNewCount(propShape: PropertyShape, numValues: number): void; export {};