UNPKG

@backland/schema

Version:

TypeScript schema declaration and validation library with static type inference

130 lines (129 loc) 6.57 kB
import { IsKnown } from '@backland/utils'; import { Serializable } from '@backland/utils'; import type { GraphQLInterfaceType, GraphQLObjectType } from 'graphql'; import { BacklandModules } from './CircularDeps'; import type { GraphQLParserResult, ParseInputTypeOptions, ParseInterfaceOptions, ParseTypeOptions } from './GraphType/GraphQLParser'; import { GraphQLParseMiddleware } from './GraphType/GraphQLParser'; import type { ObjectDefinitionInput } from './TObjectConfig'; import { FieldParserOptionsObject, ValidationCustomMessage } from './applyValidator'; import { ExtendObjectDefinition } from './extendObjectDefinition'; import { ObjectLike } from './fields/IObjectLike'; import { InferObjectDefinition } from './fields/Infer'; import { MetaFieldDef } from './fields/MetaFieldField'; import { FieldTypeName } from './fields/_fieldDefinitions'; import type { FieldAsString } from './fields/_parseFields'; import { ObjectHelpers } from './getObjectHelpers'; import { ImplementObject } from './implementObject'; import type { ObjectToTypescriptOptions } from './objectToTypescript'; import { WithCache } from './withCache'; export * from './parseObjectDefinition'; export * from './objectInferenceUtils'; export * from './implementObject'; export * from './fields/_parseFields'; export * from './fields/_fieldDefinitions'; export * from './fields/_parseFields'; export declare class ObjectType<Input, HandledInput extends _HandleInput<Input> = _HandleInput<Input>> { get __isBacklandObject(): true; static __isBacklandObject: boolean; __withCache: WithCache<{ helpers: ObjectHelpers; }>; inputDefinition: ObjectDefinitionInput | ((modules: BacklandModules) => ObjectDefinitionInput); constructor(objectDef: HandledInput | ((modules: BacklandModules) => HandledInput)); private __definitionCache; get definition(): HandledInput; get description(): string | undefined; private __hidden; set hidden(value: boolean); get hidden(): boolean; cleanDefinition(): HandledInput; edit(): ExtendObjectDefinition<{ type: 'object'; def: HandledInput; }, { type: 'object'; def: HandledInput; }>; get meta(): MetaFieldDef; __setMetaData(k: keyof MetaFieldDef, value: Serializable): void; parse(input: any, options?: { customMessage?: ValidationCustomMessage; } & FieldParserOptionsObject): InferObjectDefinition<HandledInput>; parse(input: any, options?: { partial: true; } & FieldParserOptionsObject): Partial<InferObjectDefinition<HandledInput>>; parse<Fields extends (keyof HandledInput)[]>(input: any, options: { customMessage?: ValidationCustomMessage; fields: Fields; } & FieldParserOptionsObject): { [K in keyof InferObjectDefinition<HandledInput> as K extends Fields[number] ? K : never]: InferObjectDefinition<HandledInput>[K]; }; parse<Fields extends (keyof HandledInput)[]>(input: any, options: { exclude: Fields; } & FieldParserOptionsObject): { [K in keyof InferObjectDefinition<HandledInput> as K extends Fields[number] ? never : K]: InferObjectDefinition<HandledInput>[K]; }; softParse: <T = any>(input: any, options?: FieldParserOptionsObject) => InferObjectDefinition<HandledInput> & { [K: string]: T; }; validate(input: any): input is InferObjectDefinition<HandledInput>; safeParse(input: any, options?: { customMessage?: ValidationCustomMessage; excludeInvalidListItems?: boolean; fields?: keyof HandledInput[]; partial?: boolean; } & FieldParserOptionsObject): { errors: string[]; parsed: unknown; }; describe(...descriptions: [comment: string] | [{ [K in keyof HandledInput]?: string; }]): ObjectType<HandledInput>; clone<T>(handler: (input: ExtendObjectDefinition<{ object: HandledInput; }, { object: HandledInput; }>) => T): T; get id(): string | null; get nonNullId(): string; identify<ID extends string>(id: ID): this & { id: ID; }; helpers: () => ObjectHelpers; toGraphQL: (name?: string) => GraphQLParserResult; graphqlType: (options?: ParseTypeOptions) => GraphQLObjectType; graphqlInterfaceType: (options?: ParseInterfaceOptions) => GraphQLInterfaceType; graphqlPrint: () => string; typescriptPrint: (options?: ObjectToTypescriptOptions) => Promise<string>; graphqlTypeToString: () => string; graphqlInputType: (options?: ParseInputTypeOptions) => import("graphql").GraphQLInputObjectType; implement: <Parents extends readonly ObjectLike[]>(name: string, ...parents: Parents) => ImplementObject<ObjectType<HandledInput, _HandleInput<HandledInput>>, Parents>; static reset(): Promise<void>; static register: import("@backland/utils").Store<Record<string, ObjectLike>, string, ObjectLike>; /** * Get an Object with the provided id * or set a new Object in the register if not found. * @param id * @param def */ static getOrSet: <T extends ObjectDefinitionInput>(id: string, def: T | (() => T)) => ObjectType<T, _HandleInput<T>>; graphQLMiddleware: GraphQLParseMiddleware[]; addGraphQLMiddleware: (middleware: GraphQLParseMiddleware[] | GraphQLParseMiddleware) => void; static is(input: any): input is ObjectType<ObjectDefinitionInput>; } export declare const BacklandObject: typeof ObjectType; export type ObjectTypeFromInput<DefinitionInput extends Readonly<ObjectDefinitionInput>> = IsKnown<DefinitionInput> extends 1 ? [DefinitionInput] extends [ObjectDefinitionInput] ? ObjectType<DefinitionInput> : never : any; export declare function createObjectType<DefinitionInput extends Readonly<ObjectDefinitionInput>>(fields: Readonly<DefinitionInput>): ObjectTypeFromInput<DefinitionInput>; export declare function createObjectType<DefinitionInput extends Readonly<ObjectDefinitionInput>>(name: string, fields: DefinitionInput): ObjectTypeFromInput<DefinitionInput>; export declare const createBacklandObject: typeof createObjectType; export declare const createSchema: typeof createObjectType; export declare const resetTypesCache: typeof ObjectType.reset; type _HandleInput<T> = [IsKnown<T>] extends [1] ? { [K in keyof T as T[K] extends { parse(...args: any): any; } | any[] | Readonly<any[]> | { [K in FieldTypeName]?: any; } | FieldAsString | { type: any; } ? K : never]: T[K]; } extends infer R ? T extends R ? T : T extends Readonly<R> ? T : {} : {} : {};