UNPKG

prostgles-types

Version:

Shared TypeScript object definitions for prostgles-client and prostgles-server

203 lines 8.08 kB
import type { JSONSchema7 } from "json-schema"; import { AnyObject } from "../filters"; import { StrictUnion } from "../util"; export declare const PrimitiveTypes: readonly ["boolean", "number", "integer", "string", "Date", "time", "timestamp", "any"]; export declare const PrimitiveArrayTypes: ("number[]" | "boolean[]" | "string[]" | "any[]" | "time[]" | "timestamp[]" | "integer[]" | "Date[]")[]; export declare const DATA_TYPES: readonly ["boolean", "number", "integer", "string", "Date", "time", "timestamp", "any", ...("number[]" | "boolean[]" | "string[]" | "any[]" | "time[]" | "timestamp[]" | "integer[]" | "Date[]")[]]; type DataType = (typeof DATA_TYPES)[number]; export declare namespace JSONB { export type BaseOptions = { /** * False by default */ optional?: boolean; /** * False by default */ nullable?: boolean; description?: string; title?: string; }; export type Lookup = BaseOptions & { type?: "Lookup" | "Lookup[]"; lookup: { type: "data" /** * This is used as edit-mode (to generate lookup of type data) */ | "data-def"; table: string; column: string; filter?: AnyObject; isArray?: boolean; isFullRow?: { /** * Columns used to display the selected row in the dropdown */ displayColumns?: string[]; }; /** * Columns used to search */ searchColumns?: string[]; /** * If true then a button will be shown * in the row card footer to access this action */ showInRowCard?: { /** * Action button text. Defaults to the method name */ actionLabel?: string; actionColor?: "danger" | "warn" | "action"; actionStyle?: AnyObject; actionClass?: string; }; } | { type: "schema"; isArray?: boolean; object: "column" | "table"; filter?: { table?: string; tsDataType?: string; udt_name?: string; }; }; allowedValues?: undefined; oneOf?: undefined; oneOfType?: undefined; arrayOf?: undefined; arrayOfType?: undefined; enum?: undefined; }; /** * Provide more info for allowed values */ type AllowedValueInfo = { value: any; label: string; subLabel?: string; /** * URL of the icon */ icon?: string; }; export type BasicType = BaseOptions & { type: DataType; allowedValues?: readonly any[] | any[] | readonly AllowedValueInfo[] | AllowedValueInfo[]; oneOf?: undefined; oneOfType?: undefined; arrayOf?: undefined; arrayOfType?: undefined; enum?: undefined; }; export type ObjectType = BaseOptions & { type: ObjectSchema; allowedValues?: undefined; oneOf?: undefined; oneOfType?: undefined; arrayOf?: undefined; arrayOfType?: undefined; enum?: undefined; }; export type EnumType = BaseOptions & { type?: undefined; enum: readonly any[]; oneOf?: undefined; oneOfType?: undefined; arrayOf?: undefined; arrayOfType?: undefined; allowedValues?: undefined; }; export type OneOf = BaseOptions & { type?: undefined; arrayOf?: undefined; arrayOfType?: undefined; allowedValues?: undefined; enum?: undefined; } & ({ oneOf?: undefined; oneOfType: readonly ObjectSchema[]; } | { oneOf: FieldType[]; oneOfType?: undefined; }); export type ArrayOf = BaseOptions & { type?: undefined; allowedValues?: undefined; oneOf?: undefined; oneOfType?: undefined; enum?: undefined; } & ({ arrayOf?: undefined; arrayOfType: ObjectSchema; } | { arrayOf: FieldType; arrayOfType?: undefined; }); export type RecordType = BaseOptions & { type?: undefined; allowedValues?: undefined; oneOf?: undefined; oneOfType?: undefined; arrayOf?: undefined; arrayOfType?: undefined; enum?: undefined; record: { keysEnum?: readonly string[]; values?: FieldType; partial?: boolean; }; }; export type FieldTypeObj = StrictUnion<BasicType | ObjectType | EnumType | OneOf | ArrayOf | RecordType | Lookup>; export type FieldType = DataType | FieldTypeObj; export type GetType<T extends FieldType | Omit<FieldTypeObj, "optional">> = GetWNullType<T extends DataType ? { type: T; } : T>; type GetWNullType<T extends FieldTypeObj | Omit<FieldTypeObj, "optional">> = T extends { nullable: true; } ? null | _GetType<T> : _GetType<T>; type GetAllowedValues<T extends FieldTypeObj | Omit<FieldTypeObj, "optional">, TType> = T extends { allowedValues: readonly any[]; } ? T["allowedValues"][number] : TType; type _GetType<T extends FieldTypeObj | Omit<FieldTypeObj, "optional">> = T extends { type: infer U; } ? U extends ObjectSchema ? GetObjectType<U> : U extends DataType ? GetPrimitiveType<T, U> : never : T extends { enum: readonly any[]; } ? T["enum"][number] : T extends { arrayOfType: infer U; } ? U extends ObjectSchema ? GetObjectType<U>[] : never : T extends { arrayOf: infer U; } ? U extends FieldType ? GetType<U>[] : never : T extends { oneOf: readonly (infer U)[]; } ? U extends FieldType ? GetType<U> : never : T extends { oneOfType: readonly (infer U)[]; } ? U extends ObjectSchema ? GetObjectType<U> : never : T extends { record: infer R; } ? R extends RecordType["record"] ? Record<R extends { keysEnum: readonly string[]; } ? R["keysEnum"][number] : string, R extends { values: infer V; } ? V extends FieldType ? GetType<V> : any : any> : never : any; type GetPrimitiveType<T extends JSONB.FieldTypeObj | Omit<JSONB.FieldTypeObj, "optional">, U extends DataType> = U extends "number" ? GetAllowedValues<T, number> : U extends "boolean" ? GetAllowedValues<T, boolean> : U extends "integer" ? GetAllowedValues<T, number> : U extends "string" ? GetAllowedValues<T, string> : U extends "time" | "timestamp" | "Date" ? GetAllowedValues<T, string> : U extends "any" ? GetAllowedValues<T, any> : U extends `${infer P}[]` ? P extends "number" | "integer" ? GetAllowedValues<T, number>[] : P extends "boolean" ? GetAllowedValues<T, boolean>[] : P extends "string" | "time" | "timestamp" | "Date" ? GetAllowedValues<T, string>[] : P extends "any" ? GetAllowedValues<T, any>[] : never : never; type IsOptional<F extends FieldType> = F extends DataType ? false : F extends { optional: true; } ? true : false; type ObjectSchema = Record<string, FieldType>; export type JSONBSchema = Omit<FieldTypeObj, "optional"> & { defaultValue?: any; }; export type GetObjectType<S extends ObjectSchema> = { [K in keyof S as IsOptional<S[K]> extends true ? K : never]?: GetType<S[K]>; } & { [K in keyof S as IsOptional<S[K]> extends true ? never : K]: GetType<S[K]>; }; export type GetSchemaType<S extends JSONBSchema> = S["nullable"] extends true ? null | GetType<S> : GetType<S>; export {}; } export declare const getJSONSchemaObject: (rawType: JSONB.FieldType | JSONB.JSONBSchema, rootInfo?: { id: string; }) => JSONSchema7; export declare function getJSONBSchemaAsJSONSchema(tableName: string, colName: string, schema: JSONB.JSONBSchema): JSONSchema7; export {}; //# sourceMappingURL=JSONBSchema.d.ts.map