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

120 lines 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GroqBuilder = void 0; const public_types_1 = require("./types/public-types"); const validate_utils_1 = require("./commands/validate-utils"); const validation_errors_1 = require("./validation/validation-errors"); const query_error_1 = require("./types/query-error"); class GroqBuilder { /** * 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) { Object.assign(GroqBuilder.prototype, methods); } /** * 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) { Object.defineProperties(GroqBuilder.prototype, properties); } constructor(internal) { this.internal = internal; } /** * The GROQ query as a string */ get query() { return this.internal.query; } /** * The parser function that should be used to parse result data */ get parser() { return this.internal.parser; } /** * Parses and validates the query results, passing all data through the parsers. */ parse(data) { const parser = this.internal.parser; if (!parser) { return data; } try { return parser(data); } catch (err) { // Ensure we throw a ValidationErrors instance: if (err instanceof validation_errors_1.ValidationErrors) { throw err.withMessage(); } const v = new validation_errors_1.ValidationErrors(); v.add(null, data, err); throw v.withMessage(); } } /** * Returns a new GroqBuilder, extending the current one. * * @internal */ chain(query, parser) { if (query && this.internal.parser) { throw new query_error_1.QueryError("You cannot chain a new query once you've specified a parser, " + "since this changes the result type.", { existingQuery: this.internal.query, newQuery: query, }); } return new GroqBuilder({ query: this.internal.query + query, parser: (0, validate_utils_1.normalizeValidationFunction)(parser), options: this.internal.options, }); } /** * Returns an empty GroqBuilder */ get root() { let options = this.internal.options; // Make the query pretty, if needed: if (options.indent) { options = { ...options, indent: options.indent + " " }; } return new GroqBuilder({ query: "", parser: null, options: options, }); } /** * Returns a GroqBuilder, overriding the result type. */ as() { return this; } /** * Returns a GroqBuilder, overriding the result type * with the specified document type. */ asType() { return this; } /** * This utility returns whitespace, if 'indent' is enabled. */ get indentation() { const indent = this.internal.options.indent; return { newLine: indent ? `\n${indent}` : " ", space: indent ? " " : "", }; } } exports.GroqBuilder = GroqBuilder; //# sourceMappingURL=groq-builder.js.map