@cognigy/rest-api-client
Version:
Cognigy REST-Client
144 lines • 5.72 kB
JavaScript
/* 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] === "