UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

144 lines 5.72 kB
/* Node Modules */ import { stringify } from "./query-string"; /* Custom Modules */ import { BadRequestError } from "../../errors"; import { stringifySort } from "./sort"; export function parseQuery(query, entitySchema) { if (typeof query !== "object" || Object.keys(query).length === 0) { return undefined; } const result = {}; for (const param of Object.keys(query)) { const splitParams = param.split("."); let property = JSON.parse(JSON.stringify(entitySchema)); for (const param of splitParams) { if (!property) { throw new BadRequestError(`The requested field ${param} does not exist`); } property = (property === null || property === void 0 ? void 0 : property.properties) && (property === null || property === void 0 ? void 0 : property.properties[param]); } if (!property) { throw new BadRequestError(`The requested field ${param} does not exist`); } let type = (property === null || property === void 0 ? void 0 : property.type) === "array" ? property === null || property === void 0 ? void 0 : property.items.type : property === null || property === void 0 ? void 0 : property.type; let format = (property === null || property === void 0 ? void 0 : property.type) === "array" ? property === null || property === void 0 ? void 0 : property.items.format : property === null || property === void 0 ? void 0 : property.format; if (typeof property === "object" && ["oneOf", "anyOf", "allOf"].includes(Object.keys(property)[0])) { for (const entry of property[Object.keys(property)[0]]) { if (typeof (entry === null || entry === void 0 ? void 0 : entry.type) !== "undefined" && (entry === null || entry === void 0 ? void 0 : entry.type) !== "null") { type = entry.type; break; } if (entry === null || entry === void 0 ? void 0 : entry.format) { format = entry.format; break; } } } type = Array.isArray(type) ? type.find(value => value !== "null") : type; if (!type && format) { type = "string"; } if (query[param] === "" || query[param] === "") { result[param] = [null]; continue; } switch (type) { case "boolean": { result[param] = query[param] === "true"; } break; case "number": { if (/^\[[\-0-9e\.]+,[\-0-9e\.]+\]$/.test(query[param])) { const values = query[param] .replace(/[\[\]]/, "") .split(",") .map((value) => Number(value)) .sort(); if (values.length === 2) { result[param] = { $gte: values[0], $lte: values[1], }; continue; } } result[param] = query[param] .split(",") .map((value) => Number(value)); } break; case "integer": { if (/^\[[\-0-9e\.]+,[\-0-9e\.]+\]$/.test(query[param])) { const values = query[param] .replace(/[\[\]]/, "") .split(",") .map((value) => parseInt(value, 10)) .sort(); if (values.length === 2) { result[param] = { $gte: values[0], $lte: values[1], }; continue; } } result[param] = query[param] .split(",") .map((value) => parseInt(value, 10)); } break; case "string": { result[param] = query[param].split(","); } break; } } return result; } ; export const stringifyQuery = (args, params, path) => { if (!args) { return ""; } path = path || []; params = params || {}; for (const [key, value] of Object.entries(args)) { if (value instanceof Object && key === "query" && value !== null) { stringifyQuery(value, params, path); continue; } if (value instanceof Object && value !== null && key === "sort") { params.sort = stringifySort(value); continue; } if (typeof value === "object" && value !== null && value["type"] === "NumberRange") { params[[...path, key].join(".")] = value.toString(); continue; } if (value instanceof Object && key !== "sort" && value !== null && !Array.isArray(value)) { stringifyQuery(value, params, [...path, key]); continue; } if (value !== "") { params[[...path, key].join(".")] = value; } } return stringify(params, { arrayFormat: "bracket" }); }; //# sourceMappingURL=query.js.map