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

42 lines 2.03 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const groq_builder_1 = require("../groq-builder"); const utils_1 = require("../types/utils"); const DEFAULT_KEY = "[BY_TYPE]"; groq_builder_1.GroqBuilder.implement({ conditionalByType(conditionalProjections, config) { const typeNames = (0, utils_1.keys)(conditionalProjections); const root = this.root; const conditions = typeNames.map((_type) => { const projectionMap = conditionalProjections[_type]; const conditionQuery = root .chain(`_type == "${_type}" =>`) .project(projectionMap); const { query, parser } = conditionQuery; return { _type, query, parser }; }); const { newLine } = this.indentation; const query = conditions.map((c) => c.query).join(`,${newLine}`); const conditionalParser = !conditions.some((c) => c.parser) ? null : function conditionalByTypeParser(input) { // find the right conditional parser const typeParser = conditions.find((c) => c._type === input._type); if (typeParser === null || typeParser === void 0 ? void 0 : typeParser.parser) { return typeParser.parser(input); } if (!typeParser && (config === null || config === void 0 ? void 0 : config.isExhaustive)) { throw new TypeError(`Unexpected _type "${input._type}"; expected one of: ${typeNames}`); } return {}; }; const conditionalQuery = this.root.chain(query, conditionalParser); const key = (config === null || config === void 0 ? void 0 : config.key) || DEFAULT_KEY; const conditionalKey = `[CONDITIONAL] ${key}`; return { _type: true, // Ensure we request the `_type` parameter [conditionalKey]: conditionalQuery, }; }, }); //# sourceMappingURL=conditionalByType.js.map