UNPKG

ts-flex-query

Version:
84 lines 3.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createOperatorForSchema = createOperatorForSchema; exports.querySchema = querySchema; const record_1 = require("../../expressions/record"); const specify_type_1 = require("../../expressions/specify-type"); const pipe_expression_1 = require("../../helpers/pipe-expression"); const utils_1 = require("../../helpers/utils"); const data_type_1 = require("../../types/data-type"); const apply_1 = require("../basic/apply"); const field_1 = require("../basic/field"); const map_1 = require("../basic/map"); const let_if_defined_1 = require("./let-if-defined"); // Specify schema for object. var SpecialObjectSchemaSpec; (function (SpecialObjectSchemaSpec) { /** Expand the object with all its primitive fields. */ SpecialObjectSchemaSpec["expand"] = "expand"; /** Like expand, but when using OData, don't include the field in the $expand clause. */ SpecialObjectSchemaSpec["select"] = "select"; })(SpecialObjectSchemaSpec || (SpecialObjectSchemaSpec = {})); function isPrimitiveSchemaSpec(schema) { return typeof schema === 'boolean'; } function isArraySchemaSpec(schema) { return Array.isArray(schema); } function isObjectSchemaSpec(schema) { return schema === SpecialObjectSchemaSpec.expand || schema === SpecialObjectSchemaSpec.select || (typeof schema === 'object' && !isArraySchemaSpec(schema)); } function isExpressionSchemaSpec(schema) { return typeof schema === 'function'; } function getSchemaDataType(schema) { if (isPrimitiveSchemaSpec(schema)) { return { type: data_type_1.DataTypeType.unknownPrimitive }; } if (isObjectSchemaSpec(schema)) { return { type: data_type_1.DataTypeType.unknownObject }; } if (isArraySchemaSpec(schema)) { return { type: data_type_1.DataTypeType.unknownArray }; } if (isExpressionSchemaSpec(schema)) { return { type: data_type_1.DataTypeType.unknown }; } return (0, utils_1.unexpected)(schema); } function doMap(objectSchema, mapOperator, isArray) { return objectSchema === SpecialObjectSchemaSpec.expand || objectSchema === SpecialObjectSchemaSpec.select ? (0, apply_1.apply)((input) => (0, specify_type_1.specifyType)(input, { type: isArray ? data_type_1.DataTypeType.unknownArray : data_type_1.DataTypeType.unknownObject, isExpandable: objectSchema === SpecialObjectSchemaSpec.expand })) : mapOperator((input) => (0, record_1.record)((0, utils_1.createObjectFromObject)(objectSchema, (subSchema, field) => (0, pipe_expression_1.pipeExpression)(input, new field_1.FieldOperator(field, getSchemaDataType(subSchema)), // eslint-disable-next-line @typescript-eslint/no-use-before-define -- Recursive call. createOperatorForSchema(subSchema, input))))); } function createOperatorForSchema(schema, container) { if (isPrimitiveSchemaSpec(schema)) { return (0, apply_1.apply)((input) => input); } if (isObjectSchemaSpec(schema)) { return doMap(schema, let_if_defined_1.letIfDefined, false); } if (isArraySchemaSpec(schema)) { return doMap(schema[0], map_1.map, true); } if (isExpressionSchemaSpec(schema)) { return (0, apply_1.apply)((input) => schema(input, container)); } return (0, utils_1.unexpected)(schema); } function querySchema(schema) { return createOperatorForSchema(schema, null); } //# sourceMappingURL=query-schema.js.map