UNPKG

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**.

36 lines 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.makeSafeQueryRunner = makeSafeQueryRunner; /** * 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) ... * } * ) * */ function makeSafeQueryRunner(execute) { /** * This queryRunner will execute a query and return strongly-typed results. * If the query has any parameters, you can pass them here too. */ return async function queryRunner(builder, ..._options) { const options = _options[0] || {}; const results = await execute(builder.query, options); const parsed = builder.parse(results); return parsed; }; } //# sourceMappingURL=makeSafeQueryRunner.js.map