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**.
46 lines (45 loc) • 1.85 kB
TypeScript
import type { HasRequiredKeys, IsUnknown } from "type-fest";
import type { QueryConfig } from "./types/schema-types";
import { IGroqBuilder } from "./types/public-types";
export type QueryRunnerOptions<TQueryConfig extends QueryConfig = QueryConfig> = IsUnknown<TQueryConfig["parameters"]> extends true ? {
/**
* This query does not have any parameters defined.
* Please use `q.parameters<...>()` to define the required input parameters.
*/
parameters?: never;
} : {
/**
* This query requires the following input parameters.
*/
parameters: TQueryConfig["parameters"];
};
/**
* Utility to create a "query runner" that consumes the result of the `q` chain.
*
* If you need to pass custom options to your `execute` function,
* use the TCustomOptions to ensure they're strongly typed.
*
* @example
* const runner = makeSafeQueryRunner(
* async (query, { parameters }) => {
* return await sanityClient.fetch(query, { params: parameters });
* }
* )
*
* @example
* const runner = makeSafeQueryRunner<{ withAuth: boolean }>(
* async (query, { parameters, withAuth }) => {
* if (withAuth) ...
* }
* )
* */
export declare function makeSafeQueryRunner<TCustomOptions>(execute: (query: string, options: QueryRunnerOptions & TCustomOptions) => Promise<any>): <TResult, TQueryConfig extends QueryConfig>(builder: IGroqBuilder<TResult, TQueryConfig>, ..._options: MaybeRequired<QueryRunnerOptions<TQueryConfig> & TCustomOptions>) => Promise<TResult>;
/**
* If all options are fully optional,
* then this makes the entire options argument optional too.
*
* If the options argument has any required keys,
* then the entire options argument is required too.
*/
type MaybeRequired<TOptions extends object> = HasRequiredKeys<TOptions> extends true ? [TOptions] : [] | [TOptions];
export {};