groq-builder
Version:
A **schema-aware**, strongly-typed GROQ query builder. It enables you to build GROQ queries using **auto-completion**, **type-checking**, and **runtime validation**.
100 lines (99 loc) • 3.62 kB
TypeScript
import { GroqBuilderConfigType, GroqBuilderResultType, IGroqBuilder, Parser, ParserFunction } from "./types/public-types";
import type { ExtractDocumentTypes, QueryConfig } from "./types/schema-types";
import type { Empty } from "./types/utils";
export type RootResult = Empty;
export type GroqBuilderOptions = {
/**
* Enables "pretty printing" for the compiled GROQ string. Useful for debugging.
* @default "" (disabled)
*/
indent?: string;
/**
* If enabled, then runtime validation is always required for all fields.
* If missing, an error will be thrown when the query is created.
*
* This affects the following 3 APIs where validation is normally optional:
*
* q.project({
* example: true, // ⛔️ use a validation function instead
* example: q.string(), // ✅
*
* example: "example.current", // ⛔️ use a tuple instead
* example: ["example.current", q.string()], // ✅
*
* example: q.field("example.current"), // ⛔️ ensure you pass the 2nd validation parameter
* example: q.field("example.current", q.string()), // ✅
* })
*
* @default false
*/
validationRequired?: boolean;
};
export declare class GroqBuilder<TResult = any, TQueryConfig extends QueryConfig = QueryConfig> implements IGroqBuilder<TResult> {
protected readonly internal: {
readonly query: string;
readonly parser: null | ParserFunction;
readonly options: GroqBuilderOptions;
};
readonly [GroqBuilderResultType]: TResult;
readonly [GroqBuilderConfigType]: TQueryConfig;
/**
* Extends the GroqBuilder class by implementing methods.
* This allows for this class to be split across multiple files in the `./commands/` folder.
* @internal
*/
static implement(methods: Partial<GroqBuilder>): void;
/**
* Extends the GroqBuilder class by implementing properties.
* This allows for this class to be split across multiple files in the `./commands/` folder.
* @internal
*/
static implementProperties(properties: {
[P in keyof GroqBuilder]?: PropertyDescriptor;
}): void;
constructor(internal: {
readonly query: string;
readonly parser: null | ParserFunction;
readonly options: GroqBuilderOptions;
});
/**
* The GROQ query as a string
*/
get query(): string;
/**
* The parser function that should be used to parse result data
*/
get parser(): null | ParserFunction<unknown, TResult>;
/**
* Parses and validates the query results, passing all data through the parsers.
*/
parse(data: unknown): TResult;
/**
* Returns a new GroqBuilder, extending the current one.
*
* @internal
*/
protected chain<TResultNew = never>(query: string, parser?: Parser | null): GroqBuilder<TResultNew, TQueryConfig>;
/**
* Returns an empty GroqBuilder
*/
get root(): GroqBuilder<Empty, TQueryConfig>;
/**
* Returns a GroqBuilder, overriding the result type.
*/
as<TResultNew>(): GroqBuilder<TResultNew, TQueryConfig>;
/**
* Returns a GroqBuilder, overriding the result type
* with the specified document type.
*/
asType<_type extends ExtractDocumentTypes<TQueryConfig["schemaTypes"]>>(): GroqBuilder<Extract<TQueryConfig["schemaTypes"], {
_type: _type;
}>, TQueryConfig>;
/**
* This utility returns whitespace, if 'indent' is enabled.
*/
protected get indentation(): {
newLine: string;
space: string;
};
}