import { ExtractProjectionResult, ProjectionMap } from "../../types/projection-types"; import { Empty, IntersectionOfValues, Simplify, ValueOf } from "../../types/utils"; import { QueryConfig } from "../../types/query-config"; import { GroqBuilderSubquery } from "../../groq-builder"; import { Expressions } from "../../types/groq-expressions"; import { ExtractDocumentTypes } from "../../types/document-types"; import { IGroqBuilder, InferResultType } from "../../groq-builder"; export type ConditionalProjectionMap = { [P in Expressions.AnyConditional]?: ConditionalQuery; }; /** * The ConditionalQuery can be one of these values: * @example * q.conditional({ * // a Raw Projection: * "condition1": { slug: "slug.current" }, * // a Query (for some edge-cases): * "condition2": q.project({ slug: "slug.current" }), * // a function that returns either a Raw Projection or a Query: * "condition3": (q) => ({ slug: "slug.current" }), * "condition4": (q) => q.project({ slug: "slug.current" }), * }) * * @example * ...q.conditionalByType({ * link: { href: true }, * // The "callback" syntax allows `q` to be strongly-typed: * button: q => ({ href: q.field("url") }) * reference: q => q.field("@").deref().project({ href: true }), * }) */ export type ConditionalQuery = ProjectionMap | IGroqBuilder | ((sub: GroqBuilderSubquery) => ProjectionMap | IGroqBuilder); export type ExtractConditionalProjectionResults, TConfig extends ConditionalConfig> = SpreadableConditionals>; }>>; type ExtractConditionalQueryResult> = ReturnTypeMaybe extends IGroqBuilder ? TResult : ExtractProjectionResult>; type ReturnTypeMaybe = T extends (...params: any) => infer TResult ? TResult : T; export type ExtractConditionalProjectionTypes = Simplify>]: InferResultType>; }>>; export type ConditionalByTypeProjectionMap = { [_type in ExtractDocumentTypes]?: ConditionalQuery, TQueryConfig>; }; export type ExtractConditionalByTypeProjectionResults, TConfig extends ConditionalConfig> = SpreadableConditionals, keyof TConditionalByTypeProjectionMap>; }) | ValueOf<{ [_type in keyof TConditionalByTypeProjectionMap]: { /** * When using conditionalByType, * this _type is automatically added to the query. */ _type: _type; } & ExtractConditionalQueryResult, TQueryConfig, NonNullable>; }>>; export type ConditionalKey = `[CONDITIONAL] ${TKey}`; export declare function isConditionalKey(key: string): key is ConditionalKey; export type SpreadableConditionals = { [UniqueConditionalKey in ConditionalKey]: IGroqBuilder; }; export type ConditionalConfig = { /** * If using multiple conditions in a single projection, * each condition must have a unique key. * This key is not used in the resulting query, and can be anything. */ key: TKey; /** * If the conditional statements cover all possible scenarios, * then setting `isExhaustive` to `true` will ensure stronger types, * and will throw runtime errors if none of the conditions are satisfied. */ isExhaustive: TIsExhaustive; }; export declare function normalizeConditionalQuery(subquery: GroqBuilderSubquery, condition: string, conditionalQuery: ConditionalQuery): IGroqBuilder; export {};