Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

odata-query

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

odata-query - npm Package Compare versions

Comparing version
7.0.9
to
8.0.0
+253
dist/index.js.mjs
const S = ["eq", "ne", "gt", "ge", "lt", "le"], w = ["and", "or", "not"], L = ["any", "all"], g = ["startswith", "endswith", "contains", "matchespattern"], q = [
"expand",
"levels",
"select",
"skip",
"top",
"count",
"orderby",
"filter"
], N = /\((.*)\)/, F = /(?!indexof)\((\w+)\)/;
var I = /* @__PURE__ */ ((e) => (e.sum = "sum", e.min = "min", e.max = "max", e.average = "average", e.countdistinct = "countdistinct", e))(I || {});
const X = (e) => ({ type: "raw", value: e }), P = (e) => ({ type: "guid", value: e }), J = (e) => ({ type: "duration", value: e }), x = (e) => ({ type: "binary", value: e }), z = (e) => ({ type: "json", value: e }), H = (e, o) => ({ type: "alias", name: e, value: o }), Q = (e) => ({ type: "decimal", value: e }), j = "";
function W({
select: e,
search: o,
skiptoken: r,
format: n,
top: p,
skip: $,
filter: u,
transform: l,
orderBy: O,
key: b,
count: s,
expand: y,
action: i,
func: f
} = {}) {
let a = "", t = [];
const c = {};
if (b != null && (a += `(${h(b, t)})`), (u || typeof s == "object") && (c.$filter = m(typeof s == "object" ? s : u, t)), l && (c.$apply = _(l)), y && (c.$expand = E(y)), O && (c.$orderby = R(O)), s && (typeof s == "boolean" ? c.$count = !0 : a += "/$count"), typeof p == "number" && (c.$top = p), typeof $ == "number" && (c.$skip = $), i && (a += `/${i}`), f) {
if (typeof f == "string")
a += `/${f}`;
else if (typeof f == "object") {
const [d] = Object.keys(f), A = h(f[d], t);
a += `/${d}`, A !== "" && (a += `(${A})`);
}
}
return t.length > 0 && Object.assign(c, t.reduce(
(d, A) => Object.assign(d, { [`@${A.name}`]: h(A.value) }),
{}
)), G(a, { $select: e, $search: o, $skiptoken: r, $format: n, ...c });
}
function C(e, o, r = []) {
return `${e} eq ${h(o, r)}`;
}
function m(e = {}, o = [], r = "") {
return (Array.isArray(e) ? e : [e]).reduce(($, u) => {
if (u) {
const l = n(u, o, r);
l && $.push(l);
}
return $;
}, []).join(" and ");
function n($ = {}, u = [], l = "") {
let O = "";
return typeof $ == "string" ? O = $ : $ && typeof $ == "object" && (O = Object.keys($).reduce(
(s, y) => {
const i = $[y];
if (i === void 0)
return s;
let f = "";
if (l ? y === j ? f = l : F.test(y) ? f = y.replace(F, (a, t) => t.trim() === j ? `(${l})` : `(${l}/${t.trim()})`) : N.test(y) ? f = y.replace(N, (a, t) => t.trim() === j ? `(${l})` : `(${l}/${t.trim()})`) : f = `${l}/${y}` : f = y, y === j && Array.isArray(i))
return s.concat(
i.map((a) => C(f, a))
);
if (["number", "string", "boolean"].indexOf(typeof i) !== -1 || i instanceof Date || i === null)
s.push(C(f, i, u));
else if (Array.isArray(i)) {
const a = y, t = i.map((c) => m(c, u, l)).filter((c) => c).map((c) => w.indexOf(a) !== -1 ? `(${c})` : c);
t.length && (w.indexOf(a) !== -1 ? t.length && (a === "not" ? s.push(T(t)) : s.push(`(${t.join(` ${a} `)})`)) : s.push(t.join(` ${a} `)));
} else if (w.indexOf(f) !== -1) {
const a = f, t = Object.keys(i).map(
(c) => n({ [c]: i[c] })
);
t.length && (a === "not" ? s.push(T(t)) : s.push(`(${t.join(` ${a} `)})`));
} else if (typeof i == "object")
"type" in i ? s.push(C(f, i, u)) : Object.keys(i).forEach((t) => {
if (i[t] !== void 0)
if (S.indexOf(t) !== -1)
s.push(`${f} ${t} ${h(i[t], u)}`);
else if (w.indexOf(t) !== -1)
Array.isArray(i[t]) ? s.push(
i[t].map((c) => "(" + n(c, u, f) + ")").join(` ${t} `)
) : s.push("(" + n(i[t], u, f) + ")");
else if (L.indexOf(t) !== -1) {
const c = p(y.toLowerCase(), i[t], t, f);
c && s.push(c);
} else if (t === "has")
s.push(`${f} ${t} ${h(i[t], u)}`);
else if (t === "in") {
const c = Array.isArray(i[t]) ? i[t] : i[t].value.map((d) => ({
type: i[t].type,
value: d
}));
s.push(
f + " in (" + c.map((d) => h(d, u)).join(",") + ")"
);
} else if (g.indexOf(t) !== -1)
s.push(`${t}(${f},${h(i[t], u)})`);
else {
const c = n({ [t]: i[t] }, u, f);
c && s.push(c);
}
});
else
throw new Error(`Unexpected value type: ${i}`);
return s;
},
[]
).join(" and ")), O;
}
function p($, u, l, O) {
let b = "";
if (typeof u == "string" || u instanceof String)
b = U($, u, l, O);
else if (u) {
const s = Array.isArray(u) ? u.reduce((i, f) => f.hasOwnProperty(j) ? (i.hasOwnProperty(j) || (i[j] = []), i[j].push(f[j]), i) : { ...i, ...f }, {}) : u, y = n(s, o, $);
b = `${O}/${l}(${y ? `${$}:${y}` : ""})`;
}
return b;
}
}
function U(e, o, r, n) {
let p = "";
const $ = r == "all" ? "ne" : "eq";
return p = `${n}/${r}(${e}: ${e} ${$} '${o}')`, p;
}
function D(e) {
return e = e.replace(/%/g, "%25"), e = e.replace(/\+/g, "%2B"), e = e.replace(/\//g, "%2F"), e = e.replace(/\?/g, "%3F"), e = e.replace(/#/g, "%23"), e = e.replace(/&/g, "%26"), e = e.replace(/'/g, "''"), e;
}
function h(e, o) {
if (typeof e == "string")
return `'${D(e)}'`;
if (e instanceof Date)
return e.toISOString();
if (typeof e == "number")
return e;
if (Array.isArray(e))
return `[${e.map((r) => h(r)).join(",")}]`;
if (e === null)
return e;
if (typeof e == "object")
switch (e.type) {
case "raw":
case "guid":
return e.value;
case "duration":
return `duration'${e.value}'`;
case "binary":
return `binary'${e.value}'`;
case "alias":
return Array.isArray(o) && o.push(e), `@${e.name}`;
case "json":
return escape(JSON.stringify(e.value));
case "decimal":
return `${e.value}M`;
default:
return Object.entries(e).filter(([, r]) => r !== void 0).map(([r, n]) => `${r}=${h(n, o)}`).join(",");
}
return e;
}
function E(e) {
if (typeof e == "number")
return e;
if (typeof e == "string")
return e.indexOf("/") === -1 ? e : e.split("/").reverse().reduce((o, r, n, p) => n === 0 ? `$expand=${r}` : n === p.length - 1 ? `${r}(${o})` : `$expand=${r}(${o})`, "");
if (Array.isArray(e))
return `${e.map((o) => E(o)).join(",")}`;
if (typeof e == "object") {
const o = Object.keys(e);
return o.some(
(r) => q.indexOf(r.toLowerCase()) !== -1
) ? o.map((r) => {
let n;
switch (r) {
case "filter":
n = m(e[r]);
break;
case "orderBy":
n = R(e[r]);
break;
case "levels":
case "count":
case "skip":
case "top":
n = `${e[r]}`;
break;
default:
n = E(e[r]);
}
return `$${r.toLowerCase()}=${n}`;
}).join(";") : o.map((r) => {
const n = E(e[r]);
return n ? `${r}(${n})` : r;
}).join(",");
}
return "";
}
function _(e) {
return (Array.isArray(e) ? e : [e]).reduce((n, p) => {
const { aggregate: $, filter: u, groupBy: l, ...O } = p, b = Object.keys(O);
if (b.length)
throw new Error(`Unsupported transform(s): ${b}`);
if ($ && n.push(`aggregate(${V($)})`), u) {
const s = m(u);
s && n.push(`filter(${m(s)})`);
}
return l && n.push(`groupby(${k(l)})`), n;
}, []).join("/") || void 0;
}
function V(e) {
return (Array.isArray(e) ? e : [e]).map((r) => typeof r == "string" ? r : Object.keys(r).map((n) => {
const p = r[n];
if (!p.with && p.as)
return `${n} as ${p.as}`;
if (!p.with)
throw new Error(`'with' property required for '${n}'`);
if (!p.as)
throw new Error(`'as' property required for '${n}'`);
return `${n} with ${p.with} as ${p.as}`;
})).join(",");
}
function k(e) {
if (!e.properties)
throw new Error("'properties' property required for groupBy");
let o = `(${e.properties.join(",")})`;
return e.transform && (o += `,${_(e.transform)}`), o;
}
function R(e, o = "") {
return Array.isArray(e) ? e.map(
(r) => Array.isArray(r) && r.length === 2 && ["asc", "desc"].indexOf(r[1]) !== -1 ? r.join(" ") : r
).map((r) => `${o}${String(r)}`).join(",") : typeof e == "object" ? Object.entries(e).map(([r, n]) => R(n, `${r}/`)).map((r) => `${o}${r}`).join(",") : `${o}${String(e)}`;
}
function G(e, o) {
const r = Object.getOwnPropertyNames(o).filter((n) => o[n] !== void 0 && o[n] !== "").map((n) => `${n}=${o[n]}`);
return r.length ? `${e}?${r.join("&")}` : e;
}
function T(e) {
return `not (${e.join(" and ")})`;
}
export {
j as ITEM_ROOT,
I as StandardAggregateMethods,
H as alias,
x as binary,
Q as decimal,
W as default,
J as duration,
P as guid,
z as json,
X as raw
};
+17
-19
{
"name": "odata-query",
"description": "OData v4 query builder that uses a simple object-based syntax similar to MongoDB and js-data",
"version": "7.0.9",
"version": "8.0.0",
"author": "Sean Lynch <techniq35@gmail.com>",
"license": "MIT",
"repository": "techniq/odata-query",
"main": "dist/commonjs/index.js",
"module": "dist/esm/index.js",
"typings": "dist/index.d.ts",
"main": "dist/index.js.mjs",
"module": "dist/index.js.mjs",
"exports": {
"import": "./dist/index.js.mjs",
"types": "./dist/index.d.ts"
},
"types": "./dist/index.d.ts",
"files": [

@@ -20,22 +24,16 @@ "dist"

"@svitejs/changesets-changelog-github-compact": "^1.2.0",
"@types/jest": "^28.1.6",
"browserify": "^17.0.0",
"jest": "^28.1.3",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.7",
"tsify": "^5.0.4",
"typescript": "^4.7.4"
"typescript": "^4.7.4",
"vite": "^6.3.5",
"vitest": "^3.1.3"
},
"scripts": {
"prebuild": "rimraf dist",
"prepare": "npm run build",
"build": "tsc -p tsconfig.cjs.json & tsc -p tsconfig.esm.json & tsc -p tsconfig.dts.json & browserify -e src/index-browser.ts -p [ tsify ] -o dist/browser/index.js",
"test": "jest",
"test-watch": "jest --watchAll",
"build": "tsc -p tsconfig.esm.json & npx vite build",
"test": "vitest run",
"test-watch": "vitest",
"preversion": "npm run build",
"prepublishOnly": "npm run build",
"changeset": "changeset",
"changeset:version": "changeset version",
"changeset:release": "changeset publish"
"changeset:release": "changeset publish",
"up-deps": "pnpm update -r -i --latest"
}
}
}
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
(function (global){(function (){
/******************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global global, define, System, Reflect, Promise */
var __extends;
var __assign;
var __rest;
var __decorate;
var __param;
var __metadata;
var __awaiter;
var __generator;
var __exportStar;
var __values;
var __read;
var __spread;
var __spreadArrays;
var __spreadArray;
var __await;
var __asyncGenerator;
var __asyncDelegator;
var __asyncValues;
var __makeTemplateObject;
var __importStar;
var __importDefault;
var __classPrivateFieldGet;
var __classPrivateFieldSet;
var __classPrivateFieldIn;
var __createBinding;
(function (factory) {
var root = typeof global === "object" ? global : typeof self === "object" ? self : typeof this === "object" ? this : {};
if (typeof define === "function" && define.amd) {
define("tslib", ["exports"], function (exports) { factory(createExporter(root, createExporter(exports))); });
}
else if (typeof module === "object" && typeof module.exports === "object") {
factory(createExporter(root, createExporter(module.exports)));
}
else {
factory(createExporter(root));
}
function createExporter(exports, previous) {
if (exports !== root) {
if (typeof Object.create === "function") {
Object.defineProperty(exports, "__esModule", { value: true });
}
else {
exports.__esModule = true;
}
}
return function (id, v) { return exports[id] = previous ? previous(id, v) : v; };
}
})
(function (exporter) {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
__extends = function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
__assign = Object.assign || function (t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
}
return t;
};
__rest = function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
__decorate = function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
__param = function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
__metadata = function (metadataKey, metadataValue) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(metadataKey, metadataValue);
};
__awaiter = function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
__generator = function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
__exportStar = function(m, o) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(o, p)) __createBinding(o, m, p);
};
__createBinding = Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
});
__values = function (o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
__read = function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
/** @deprecated */
__spread = function () {
for (var ar = [], i = 0; i < arguments.length; i++)
ar = ar.concat(__read(arguments[i]));
return ar;
};
/** @deprecated */
__spreadArrays = function () {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
for (var r = Array(s), k = 0, i = 0; i < il; i++)
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
r[k] = a[j];
return r;
};
__spreadArray = function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
__await = function (v) {
return this instanceof __await ? (this.v = v, this) : new __await(v);
};
__asyncGenerator = function (thisArg, _arguments, generator) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var g = generator.apply(thisArg, _arguments || []), i, q = [];
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
function fulfill(value) { resume("next", value); }
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
__asyncDelegator = function (o) {
var i, p;
return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: n === "return" } : f ? f(v) : v; } : f; }
};
__asyncValues = function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
__makeTemplateObject = function (cooked, raw) {
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
return cooked;
};
var __setModuleDefault = Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
};
__importStar = function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
__importDefault = function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
__classPrivateFieldGet = function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
__classPrivateFieldSet = function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
__classPrivateFieldIn = function (state, receiver) {
if (receiver === null || (typeof receiver !== "object" && typeof receiver !== "function")) throw new TypeError("Cannot use 'in' operator on non-object");
return typeof state === "function" ? receiver === state : state.has(receiver);
};
exporter("__extends", __extends);
exporter("__assign", __assign);
exporter("__rest", __rest);
exporter("__decorate", __decorate);
exporter("__param", __param);
exporter("__metadata", __metadata);
exporter("__awaiter", __awaiter);
exporter("__generator", __generator);
exporter("__exportStar", __exportStar);
exporter("__createBinding", __createBinding);
exporter("__values", __values);
exporter("__read", __read);
exporter("__spread", __spread);
exporter("__spreadArrays", __spreadArrays);
exporter("__spreadArray", __spreadArray);
exporter("__await", __await);
exporter("__asyncGenerator", __asyncGenerator);
exporter("__asyncDelegator", __asyncDelegator);
exporter("__asyncValues", __asyncValues);
exporter("__makeTemplateObject", __makeTemplateObject);
exporter("__importStar", __importStar);
exporter("__importDefault", __importDefault);
exporter("__classPrivateFieldGet", __classPrivateFieldGet);
exporter("__classPrivateFieldSet", __classPrivateFieldSet);
exporter("__classPrivateFieldIn", __classPrivateFieldIn);
});
}).call(this)}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}],2:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var index_1 = tslib_1.__importDefault(require("./index"));
window.buildQuery = index_1.default;
},{"./index":3,"tslib":1}],3:[function(require,module,exports){
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ITEM_ROOT = exports.decimal = exports.alias = exports.json = exports.binary = exports.duration = exports.guid = exports.raw = exports.StandardAggregateMethods = void 0;
var tslib_1 = require("tslib");
var COMPARISON_OPERATORS = ['eq', 'ne', 'gt', 'ge', 'lt', 'le'];
var LOGICAL_OPERATORS = ['and', 'or', 'not'];
var COLLECTION_OPERATORS = ['any', 'all'];
var BOOLEAN_FUNCTIONS = ['startswith', 'endswith', 'contains', 'matchespattern'];
var SUPPORTED_EXPAND_PROPERTIES = [
'expand',
'levels',
'select',
'skip',
'top',
'count',
'orderby',
'filter',
];
var FUNCTION_REGEX = /\((.*)\)/;
var INDEXOF_REGEX = /(?!indexof)\((\w+)\)/;
var StandardAggregateMethods;
(function (StandardAggregateMethods) {
StandardAggregateMethods["sum"] = "sum";
StandardAggregateMethods["min"] = "min";
StandardAggregateMethods["max"] = "max";
StandardAggregateMethods["average"] = "average";
StandardAggregateMethods["countdistinct"] = "countdistinct";
})(StandardAggregateMethods = exports.StandardAggregateMethods || (exports.StandardAggregateMethods = {}));
var raw = function (value) { return ({ type: 'raw', value: value }); };
exports.raw = raw;
var guid = function (value) { return ({ type: 'guid', value: value }); };
exports.guid = guid;
var duration = function (value) { return ({ type: 'duration', value: value }); };
exports.duration = duration;
var binary = function (value) { return ({ type: 'binary', value: value }); };
exports.binary = binary;
var json = function (value) { return ({ type: 'json', value: value }); };
exports.json = json;
var alias = function (name, value) { return ({ type: 'alias', name: name, value: value }); };
exports.alias = alias;
var decimal = function (value) { return ({ type: 'decimal', value: value }); };
exports.decimal = decimal;
exports.ITEM_ROOT = "";
function default_1(_a) {
var _b = _a === void 0 ? {} : _a, $select = _b.select, $search = _b.search, $skiptoken = _b.skiptoken, $format = _b.format, top = _b.top, skip = _b.skip, filter = _b.filter, transform = _b.transform, orderBy = _b.orderBy, key = _b.key, count = _b.count, expand = _b.expand, action = _b.action, func = _b.func;
var path = '';
var aliases = [];
var params = {};
// key is not (null, undefined)
if (key != undefined) {
path += "(".concat(handleValue(key, aliases), ")");
}
if (filter || typeof count === 'object')
params.$filter = buildFilter(typeof count === 'object' ? count : filter, aliases);
if (transform)
params.$apply = buildTransforms(transform);
if (expand)
params.$expand = buildExpand(expand);
if (orderBy)
params.$orderby = buildOrderBy(orderBy);
if (count) {
if (typeof count === 'boolean') {
params.$count = true;
}
else {
path += '/$count';
}
}
if (typeof top === 'number') {
params.$top = top;
}
if (typeof skip === 'number') {
params.$skip = skip;
}
if (action) {
path += "/".concat(action);
}
if (func) {
if (typeof func === 'string') {
path += "/".concat(func);
}
else if (typeof func === 'object') {
var funcName = Object.keys(func)[0];
var funcArgs = handleValue(func[funcName], aliases);
path += "/".concat(funcName);
if (funcArgs !== "") {
path += "(".concat(funcArgs, ")");
}
}
}
if (aliases.length > 0) {
Object.assign(params, aliases.reduce(function (acc, alias) {
var _a;
return Object.assign(acc, (_a = {}, _a["@".concat(alias.name)] = handleValue(alias.value), _a));
}, {}));
}
return buildUrl(path, tslib_1.__assign({ $select: $select, $search: $search, $skiptoken: $skiptoken, $format: $format }, params));
}
exports.default = default_1;
function renderPrimitiveValue(key, val, aliases) {
if (aliases === void 0) { aliases = []; }
return "".concat(key, " eq ").concat(handleValue(val, aliases));
}
function buildFilter(filters, aliases, propPrefix) {
if (filters === void 0) { filters = {}; }
if (aliases === void 0) { aliases = []; }
if (propPrefix === void 0) { propPrefix = ''; }
return (Array.isArray(filters) ? filters : [filters])
.reduce(function (acc, filter) {
if (filter) {
var builtFilter = buildFilterCore(filter, aliases, propPrefix);
if (builtFilter) {
acc.push(builtFilter);
}
}
return acc;
}, []).join(' and ');
function buildFilterCore(filter, aliases, propPrefix) {
if (filter === void 0) { filter = {}; }
if (aliases === void 0) { aliases = []; }
if (propPrefix === void 0) { propPrefix = ''; }
var filterExpr = "";
if (typeof filter === 'string') {
// Use raw filter string
filterExpr = filter;
}
else if (filter && typeof filter === 'object') {
var filtersArray = Object.keys(filter).reduce(function (result, filterKey) {
var value = filter[filterKey];
if (value === undefined) {
return result;
}
var propName = '';
if (propPrefix) {
if (filterKey === exports.ITEM_ROOT) {
propName = propPrefix;
}
else if (INDEXOF_REGEX.test(filterKey)) {
propName = filterKey.replace(INDEXOF_REGEX, function (_, $1) { return $1.trim() === exports.ITEM_ROOT ? "(".concat(propPrefix, ")") : "(".concat(propPrefix, "/").concat($1.trim(), ")"); });
}
else if (FUNCTION_REGEX.test(filterKey)) {
propName = filterKey.replace(FUNCTION_REGEX, function (_, $1) { return $1.trim() === exports.ITEM_ROOT ? "(".concat(propPrefix, ")") : "(".concat(propPrefix, "/").concat($1.trim(), ")"); });
}
else {
propName = "".concat(propPrefix, "/").concat(filterKey);
}
}
else {
propName = filterKey;
}
if (filterKey === exports.ITEM_ROOT && Array.isArray(value)) {
return result.concat(value.map(function (arrayValue) { return renderPrimitiveValue(propName, arrayValue); }));
}
if (['number', 'string', 'boolean'].indexOf(typeof value) !== -1 ||
value instanceof Date ||
value === null) {
// Simple key/value handled as equals operator
result.push(renderPrimitiveValue(propName, value, aliases));
}
else if (Array.isArray(value)) {
var op_1 = filterKey;
var builtFilters = value
.map(function (v) { return buildFilter(v, aliases, propPrefix); })
.filter(function (f) { return f; })
.map(function (f) { return (LOGICAL_OPERATORS.indexOf(op_1) !== -1 ? "(".concat(f, ")") : f); });
if (builtFilters.length) {
if (LOGICAL_OPERATORS.indexOf(op_1) !== -1) {
if (builtFilters.length) {
if (op_1 === 'not') {
result.push(parseNot(builtFilters));
}
else {
result.push("(".concat(builtFilters.join(" ".concat(op_1, " ")), ")"));
}
}
}
else {
result.push(builtFilters.join(" ".concat(op_1, " ")));
}
}
}
else if (LOGICAL_OPERATORS.indexOf(propName) !== -1) {
var op = propName;
var builtFilters = Object.keys(value).map(function (valueKey) {
var _a;
return buildFilterCore((_a = {}, _a[valueKey] = value[valueKey], _a));
});
if (builtFilters.length) {
if (op === 'not') {
result.push(parseNot(builtFilters));
}
else {
result.push("(".concat(builtFilters.join(" ".concat(op, " ")), ")"));
}
}
}
else if (typeof value === 'object') {
if ('type' in value) {
result.push(renderPrimitiveValue(propName, value, aliases));
}
else {
var operators = Object.keys(value);
operators.forEach(function (op) {
var _a;
if (value[op] === undefined) {
return;
}
if (COMPARISON_OPERATORS.indexOf(op) !== -1) {
result.push("".concat(propName, " ").concat(op, " ").concat(handleValue(value[op], aliases)));
}
else if (LOGICAL_OPERATORS.indexOf(op) !== -1) {
if (Array.isArray(value[op])) {
result.push(value[op]
.map(function (v) { return '(' + buildFilterCore(v, aliases, propName) + ')'; })
.join(" ".concat(op, " ")));
}
else {
result.push('(' + buildFilterCore(value[op], aliases, propName) + ')');
}
}
else if (COLLECTION_OPERATORS.indexOf(op) !== -1) {
var collectionClause = buildCollectionClause(filterKey.toLowerCase(), value[op], op, propName);
if (collectionClause) {
result.push(collectionClause);
}
}
else if (op === 'has') {
result.push("".concat(propName, " ").concat(op, " ").concat(handleValue(value[op], aliases)));
}
else if (op === 'in') {
var resultingValues = Array.isArray(value[op])
? value[op]
: value[op].value.map(function (typedValue) { return ({
type: value[op].type,
value: typedValue,
}); });
result.push(propName + ' in (' + resultingValues.map(function (v) { return handleValue(v, aliases); }).join(',') + ')');
}
else if (BOOLEAN_FUNCTIONS.indexOf(op) !== -1) {
// Simple boolean functions (startswith, endswith, contains, matchespattern)
result.push("".concat(op, "(").concat(propName, ",").concat(handleValue(value[op], aliases), ")"));
}
else {
// Nested property
var filter_1 = buildFilterCore((_a = {}, _a[op] = value[op], _a), aliases, propName);
if (filter_1) {
result.push(filter_1);
}
}
});
}
}
else {
throw new Error("Unexpected value type: ".concat(value));
}
return result;
}, []);
filterExpr = filtersArray.join(' and ');
} /* else {
throw new Error(`Unexpected filters type: ${filter}`);
} */
return filterExpr;
}
function buildCollectionClause(lambdaParameter, value, op, propName) {
var clause = '';
if (typeof value === 'string' || value instanceof String) {
clause = getStringCollectionClause(lambdaParameter, value, op, propName);
}
else if (value) {
// normalize {any:[{prop1: 1}, {prop2: 1}]} --> {any:{prop1: 1, prop2: 1}}; same for 'all',
// simple values collection: {any:[{'': 'simpleVal1'}, {'': 'simpleVal2'}]} --> {any:{'': ['simpleVal1', 'simpleVal2']}}; same for 'all',
var filterValue = Array.isArray(value) ?
value.reduce(function (acc, item) {
if (item.hasOwnProperty(exports.ITEM_ROOT)) {
if (!acc.hasOwnProperty(exports.ITEM_ROOT)) {
acc[exports.ITEM_ROOT] = [];
}
acc[exports.ITEM_ROOT].push(item[exports.ITEM_ROOT]);
return acc;
}
return tslib_1.__assign(tslib_1.__assign({}, acc), item);
}, {}) : value;
var filter = buildFilterCore(filterValue, aliases, lambdaParameter);
clause = "".concat(propName, "/").concat(op, "(").concat(filter ? "".concat(lambdaParameter, ":").concat(filter) : '', ")");
}
return clause;
}
}
function getStringCollectionClause(lambdaParameter, value, collectionOperator, propName) {
var clause = '';
var conditionOperator = collectionOperator == 'all' ? 'ne' : 'eq';
clause = "".concat(propName, "/").concat(collectionOperator, "(").concat(lambdaParameter, ": ").concat(lambdaParameter, " ").concat(conditionOperator, " '").concat(value, "')");
return clause;
}
function escapeIllegalChars(string) {
string = string.replace(/%/g, '%25');
string = string.replace(/\+/g, '%2B');
string = string.replace(/\//g, '%2F');
string = string.replace(/\?/g, '%3F');
string = string.replace(/#/g, '%23');
string = string.replace(/&/g, '%26');
string = string.replace(/'/g, "''");
return string;
}
function handleValue(value, aliases) {
if (typeof value === 'string') {
return "'".concat(escapeIllegalChars(value), "'");
}
else if (value instanceof Date) {
return value.toISOString();
}
else if (typeof value === 'number') {
return value;
}
else if (Array.isArray(value)) {
return "[".concat(value.map(function (d) { return handleValue(d); }).join(','), "]");
}
else if (value === null) {
return value;
}
else if (typeof value === 'object') {
switch (value.type) {
case 'raw':
case 'guid':
return value.value;
case 'duration':
return "duration'".concat(value.value, "'");
case 'binary':
return "binary'".concat(value.value, "'");
case 'alias':
// Store
if (Array.isArray(aliases))
aliases.push(value);
return "@".concat(value.name);
case 'json':
return escape(JSON.stringify(value.value));
case 'decimal':
return "".concat(value.value, "M");
default:
return Object.entries(value)
.filter(function (_a) {
var v = _a[1];
return v !== undefined;
})
.map(function (_a) {
var k = _a[0], v = _a[1];
return "".concat(k, "=").concat(handleValue(v, aliases));
}).join(',');
}
}
return value;
}
function buildExpand(expands) {
if (typeof expands === 'number') {
return expands;
}
else if (typeof expands === 'string') {
if (expands.indexOf('/') === -1) {
return expands;
}
// Change `Foo/Bar/Baz` to `Foo($expand=Bar($expand=Baz))`
return expands
.split('/')
.reverse()
.reduce(function (results, item, index, arr) {
if (index === 0) {
// Inner-most item
return "$expand=".concat(item);
}
else if (index === arr.length - 1) {
// Outer-most item, don't add `$expand=` prefix (added above)
return "".concat(item, "(").concat(results, ")");
}
else {
// Other items
return "$expand=".concat(item, "(").concat(results, ")");
}
}, '');
}
else if (Array.isArray(expands)) {
return "".concat(expands.map(function (e) { return buildExpand(e); }).join(','));
}
else if (typeof expands === 'object') {
var expandKeys = Object.keys(expands);
if (expandKeys.some(function (key) { return SUPPORTED_EXPAND_PROPERTIES.indexOf(key.toLowerCase()) !== -1; })) {
return expandKeys
.map(function (key) {
var value;
switch (key) {
case 'filter':
value = buildFilter(expands[key]);
break;
case 'orderBy':
value = buildOrderBy(expands[key]);
break;
case 'levels':
case 'count':
case 'skip':
case 'top':
value = "".concat(expands[key]);
break;
default:
value = buildExpand(expands[key]);
}
return "$".concat(key.toLowerCase(), "=").concat(value);
})
.join(';');
}
else {
return expandKeys
.map(function (key) {
var builtExpand = buildExpand(expands[key]);
return builtExpand ? "".concat(key, "(").concat(builtExpand, ")") : key;
})
.join(',');
}
}
return "";
}
function buildTransforms(transforms) {
// Wrap single object an array for simplified processing
var transformsArray = Array.isArray(transforms) ? transforms : [transforms];
var transformsResult = transformsArray.reduce(function (result, transform) {
var aggregate = transform.aggregate, filter = transform.filter, groupBy = transform.groupBy, rest = tslib_1.__rest(transform, ["aggregate", "filter", "groupBy"]);
// TODO: support as many of the following:
// topcount, topsum, toppercent,
// bottomsum, bottomcount, bottompercent,
// identity, concat, expand, search, compute, isdefined
var unsupportedKeys = Object.keys(rest);
if (unsupportedKeys.length) {
throw new Error("Unsupported transform(s): ".concat(unsupportedKeys));
}
if (aggregate) {
result.push("aggregate(".concat(buildAggregate(aggregate), ")"));
}
if (filter) {
var builtFilter = buildFilter(filter);
if (builtFilter) {
result.push("filter(".concat(buildFilter(builtFilter), ")"));
}
}
if (groupBy) {
result.push("groupby(".concat(buildGroupBy(groupBy), ")"));
}
return result;
}, []);
return transformsResult.join('/') || undefined;
}
function buildAggregate(aggregate) {
// Wrap single object in an array for simplified processing
var aggregateArray = Array.isArray(aggregate) ? aggregate : [aggregate];
return aggregateArray
.map(function (aggregateItem) {
return typeof aggregateItem === "string"
? aggregateItem
: Object.keys(aggregateItem).map(function (aggregateKey) {
var aggregateValue = aggregateItem[aggregateKey];
if (!aggregateValue.with && aggregateValue.as) {
return "".concat(aggregateKey, " as ").concat(aggregateValue.as);
}
if (!aggregateValue.with) {
throw new Error("'with' property required for '".concat(aggregateKey, "'"));
}
if (!aggregateValue.as) {
throw new Error("'as' property required for '".concat(aggregateKey, "'"));
}
return "".concat(aggregateKey, " with ").concat(aggregateValue.with, " as ").concat(aggregateValue.as);
});
})
.join(',');
}
function buildGroupBy(groupBy) {
if (!groupBy.properties) {
throw new Error("'properties' property required for groupBy");
}
var result = "(".concat(groupBy.properties.join(','), ")");
if (groupBy.transform) {
result += ",".concat(buildTransforms(groupBy.transform));
}
return result;
}
function buildOrderBy(orderBy, prefix) {
if (prefix === void 0) { prefix = ''; }
if (Array.isArray(orderBy)) {
return orderBy
.map(function (value) {
return (Array.isArray(value) && value.length === 2 && ['asc', 'desc'].indexOf(value[1]) !== -1) ? value.join(' ') : value;
})
.map(function (v) { return "".concat(prefix).concat(String(v)); }).join(',');
}
else if (typeof orderBy === 'object') {
return Object.entries(orderBy)
.map(function (_a) {
var k = _a[0], v = _a[1];
return buildOrderBy(v, "".concat(k, "/"));
})
.map(function (v) { return "".concat(prefix).concat(v); }).join(',');
}
return "".concat(prefix).concat(String(orderBy));
}
function buildUrl(path, params) {
// This can be refactored using URL API. But IE does not support it.
var queries = Object.getOwnPropertyNames(params)
.filter(function (key) { return params[key] !== undefined && params[key] !== ''; })
.map(function (key) { return "".concat(key, "=").concat(params[key]); });
return queries.length ? "".concat(path, "?").concat(queries.join('&')) : path;
}
function parseNot(builtFilters) {
return "not (".concat(builtFilters.join(' and '), ")");
}
},{"tslib":1}]},{},[2]);
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ITEM_ROOT = exports.decimal = exports.alias = exports.json = exports.binary = exports.duration = exports.guid = exports.raw = exports.StandardAggregateMethods = void 0;
var tslib_1 = require("tslib");
var COMPARISON_OPERATORS = ['eq', 'ne', 'gt', 'ge', 'lt', 'le'];
var LOGICAL_OPERATORS = ['and', 'or', 'not'];
var COLLECTION_OPERATORS = ['any', 'all'];
var BOOLEAN_FUNCTIONS = ['startswith', 'endswith', 'contains', 'matchespattern'];
var SUPPORTED_EXPAND_PROPERTIES = [
'expand',
'levels',
'select',
'skip',
'top',
'count',
'orderby',
'filter',
];
var FUNCTION_REGEX = /\((.*)\)/;
var INDEXOF_REGEX = /(?!indexof)\((\w+)\)/;
var StandardAggregateMethods;
(function (StandardAggregateMethods) {
StandardAggregateMethods["sum"] = "sum";
StandardAggregateMethods["min"] = "min";
StandardAggregateMethods["max"] = "max";
StandardAggregateMethods["average"] = "average";
StandardAggregateMethods["countdistinct"] = "countdistinct";
})(StandardAggregateMethods = exports.StandardAggregateMethods || (exports.StandardAggregateMethods = {}));
var raw = function (value) { return ({ type: 'raw', value: value }); };
exports.raw = raw;
var guid = function (value) { return ({ type: 'guid', value: value }); };
exports.guid = guid;
var duration = function (value) { return ({ type: 'duration', value: value }); };
exports.duration = duration;
var binary = function (value) { return ({ type: 'binary', value: value }); };
exports.binary = binary;
var json = function (value) { return ({ type: 'json', value: value }); };
exports.json = json;
var alias = function (name, value) { return ({ type: 'alias', name: name, value: value }); };
exports.alias = alias;
var decimal = function (value) { return ({ type: 'decimal', value: value }); };
exports.decimal = decimal;
exports.ITEM_ROOT = "";
function default_1(_a) {
var _b = _a === void 0 ? {} : _a, $select = _b.select, $search = _b.search, $skiptoken = _b.skiptoken, $format = _b.format, top = _b.top, skip = _b.skip, filter = _b.filter, transform = _b.transform, orderBy = _b.orderBy, key = _b.key, count = _b.count, expand = _b.expand, action = _b.action, func = _b.func;
var path = '';
var aliases = [];
var params = {};
// key is not (null, undefined)
if (key != undefined) {
path += "(".concat(handleValue(key, aliases), ")");
}
if (filter || typeof count === 'object')
params.$filter = buildFilter(typeof count === 'object' ? count : filter, aliases);
if (transform)
params.$apply = buildTransforms(transform);
if (expand)
params.$expand = buildExpand(expand);
if (orderBy)
params.$orderby = buildOrderBy(orderBy);
if (count) {
if (typeof count === 'boolean') {
params.$count = true;
}
else {
path += '/$count';
}
}
if (typeof top === 'number') {
params.$top = top;
}
if (typeof skip === 'number') {
params.$skip = skip;
}
if (action) {
path += "/".concat(action);
}
if (func) {
if (typeof func === 'string') {
path += "/".concat(func);
}
else if (typeof func === 'object') {
var funcName = Object.keys(func)[0];
var funcArgs = handleValue(func[funcName], aliases);
path += "/".concat(funcName);
if (funcArgs !== "") {
path += "(".concat(funcArgs, ")");
}
}
}
if (aliases.length > 0) {
Object.assign(params, aliases.reduce(function (acc, alias) {
var _a;
return Object.assign(acc, (_a = {}, _a["@".concat(alias.name)] = handleValue(alias.value), _a));
}, {}));
}
return buildUrl(path, tslib_1.__assign({ $select: $select, $search: $search, $skiptoken: $skiptoken, $format: $format }, params));
}
exports.default = default_1;
function renderPrimitiveValue(key, val, aliases) {
if (aliases === void 0) { aliases = []; }
return "".concat(key, " eq ").concat(handleValue(val, aliases));
}
function buildFilter(filters, aliases, propPrefix) {
if (filters === void 0) { filters = {}; }
if (aliases === void 0) { aliases = []; }
if (propPrefix === void 0) { propPrefix = ''; }
return (Array.isArray(filters) ? filters : [filters])
.reduce(function (acc, filter) {
if (filter) {
var builtFilter = buildFilterCore(filter, aliases, propPrefix);
if (builtFilter) {
acc.push(builtFilter);
}
}
return acc;
}, []).join(' and ');
function buildFilterCore(filter, aliases, propPrefix) {
if (filter === void 0) { filter = {}; }
if (aliases === void 0) { aliases = []; }
if (propPrefix === void 0) { propPrefix = ''; }
var filterExpr = "";
if (typeof filter === 'string') {
// Use raw filter string
filterExpr = filter;
}
else if (filter && typeof filter === 'object') {
var filtersArray = Object.keys(filter).reduce(function (result, filterKey) {
var value = filter[filterKey];
if (value === undefined) {
return result;
}
var propName = '';
if (propPrefix) {
if (filterKey === exports.ITEM_ROOT) {
propName = propPrefix;
}
else if (INDEXOF_REGEX.test(filterKey)) {
propName = filterKey.replace(INDEXOF_REGEX, function (_, $1) { return $1.trim() === exports.ITEM_ROOT ? "(".concat(propPrefix, ")") : "(".concat(propPrefix, "/").concat($1.trim(), ")"); });
}
else if (FUNCTION_REGEX.test(filterKey)) {
propName = filterKey.replace(FUNCTION_REGEX, function (_, $1) { return $1.trim() === exports.ITEM_ROOT ? "(".concat(propPrefix, ")") : "(".concat(propPrefix, "/").concat($1.trim(), ")"); });
}
else {
propName = "".concat(propPrefix, "/").concat(filterKey);
}
}
else {
propName = filterKey;
}
if (filterKey === exports.ITEM_ROOT && Array.isArray(value)) {
return result.concat(value.map(function (arrayValue) { return renderPrimitiveValue(propName, arrayValue); }));
}
if (['number', 'string', 'boolean'].indexOf(typeof value) !== -1 ||
value instanceof Date ||
value === null) {
// Simple key/value handled as equals operator
result.push(renderPrimitiveValue(propName, value, aliases));
}
else if (Array.isArray(value)) {
var op_1 = filterKey;
var builtFilters = value
.map(function (v) { return buildFilter(v, aliases, propPrefix); })
.filter(function (f) { return f; })
.map(function (f) { return (LOGICAL_OPERATORS.indexOf(op_1) !== -1 ? "(".concat(f, ")") : f); });
if (builtFilters.length) {
if (LOGICAL_OPERATORS.indexOf(op_1) !== -1) {
if (builtFilters.length) {
if (op_1 === 'not') {
result.push(parseNot(builtFilters));
}
else {
result.push("(".concat(builtFilters.join(" ".concat(op_1, " ")), ")"));
}
}
}
else {
result.push(builtFilters.join(" ".concat(op_1, " ")));
}
}
}
else if (LOGICAL_OPERATORS.indexOf(propName) !== -1) {
var op = propName;
var builtFilters = Object.keys(value).map(function (valueKey) {
var _a;
return buildFilterCore((_a = {}, _a[valueKey] = value[valueKey], _a));
});
if (builtFilters.length) {
if (op === 'not') {
result.push(parseNot(builtFilters));
}
else {
result.push("(".concat(builtFilters.join(" ".concat(op, " ")), ")"));
}
}
}
else if (typeof value === 'object') {
if ('type' in value) {
result.push(renderPrimitiveValue(propName, value, aliases));
}
else {
var operators = Object.keys(value);
operators.forEach(function (op) {
var _a;
if (value[op] === undefined) {
return;
}
if (COMPARISON_OPERATORS.indexOf(op) !== -1) {
result.push("".concat(propName, " ").concat(op, " ").concat(handleValue(value[op], aliases)));
}
else if (LOGICAL_OPERATORS.indexOf(op) !== -1) {
if (Array.isArray(value[op])) {
result.push(value[op]
.map(function (v) { return '(' + buildFilterCore(v, aliases, propName) + ')'; })
.join(" ".concat(op, " ")));
}
else {
result.push('(' + buildFilterCore(value[op], aliases, propName) + ')');
}
}
else if (COLLECTION_OPERATORS.indexOf(op) !== -1) {
var collectionClause = buildCollectionClause(filterKey.toLowerCase(), value[op], op, propName);
if (collectionClause) {
result.push(collectionClause);
}
}
else if (op === 'has') {
result.push("".concat(propName, " ").concat(op, " ").concat(handleValue(value[op], aliases)));
}
else if (op === 'in') {
var resultingValues = Array.isArray(value[op])
? value[op]
: value[op].value.map(function (typedValue) { return ({
type: value[op].type,
value: typedValue,
}); });
result.push(propName + ' in (' + resultingValues.map(function (v) { return handleValue(v, aliases); }).join(',') + ')');
}
else if (BOOLEAN_FUNCTIONS.indexOf(op) !== -1) {
// Simple boolean functions (startswith, endswith, contains, matchespattern)
result.push("".concat(op, "(").concat(propName, ",").concat(handleValue(value[op], aliases), ")"));
}
else {
// Nested property
var filter_1 = buildFilterCore((_a = {}, _a[op] = value[op], _a), aliases, propName);
if (filter_1) {
result.push(filter_1);
}
}
});
}
}
else {
throw new Error("Unexpected value type: ".concat(value));
}
return result;
}, []);
filterExpr = filtersArray.join(' and ');
} /* else {
throw new Error(`Unexpected filters type: ${filter}`);
} */
return filterExpr;
}
function buildCollectionClause(lambdaParameter, value, op, propName) {
var clause = '';
if (typeof value === 'string' || value instanceof String) {
clause = getStringCollectionClause(lambdaParameter, value, op, propName);
}
else if (value) {
// normalize {any:[{prop1: 1}, {prop2: 1}]} --> {any:{prop1: 1, prop2: 1}}; same for 'all',
// simple values collection: {any:[{'': 'simpleVal1'}, {'': 'simpleVal2'}]} --> {any:{'': ['simpleVal1', 'simpleVal2']}}; same for 'all',
var filterValue = Array.isArray(value) ?
value.reduce(function (acc, item) {
if (item.hasOwnProperty(exports.ITEM_ROOT)) {
if (!acc.hasOwnProperty(exports.ITEM_ROOT)) {
acc[exports.ITEM_ROOT] = [];
}
acc[exports.ITEM_ROOT].push(item[exports.ITEM_ROOT]);
return acc;
}
return tslib_1.__assign(tslib_1.__assign({}, acc), item);
}, {}) : value;
var filter = buildFilterCore(filterValue, aliases, lambdaParameter);
clause = "".concat(propName, "/").concat(op, "(").concat(filter ? "".concat(lambdaParameter, ":").concat(filter) : '', ")");
}
return clause;
}
}
function getStringCollectionClause(lambdaParameter, value, collectionOperator, propName) {
var clause = '';
var conditionOperator = collectionOperator == 'all' ? 'ne' : 'eq';
clause = "".concat(propName, "/").concat(collectionOperator, "(").concat(lambdaParameter, ": ").concat(lambdaParameter, " ").concat(conditionOperator, " '").concat(value, "')");
return clause;
}
function escapeIllegalChars(string) {
string = string.replace(/%/g, '%25');
string = string.replace(/\+/g, '%2B');
string = string.replace(/\//g, '%2F');
string = string.replace(/\?/g, '%3F');
string = string.replace(/#/g, '%23');
string = string.replace(/&/g, '%26');
string = string.replace(/'/g, "''");
return string;
}
function handleValue(value, aliases) {
if (typeof value === 'string') {
return "'".concat(escapeIllegalChars(value), "'");
}
else if (value instanceof Date) {
return value.toISOString();
}
else if (typeof value === 'number') {
return value;
}
else if (Array.isArray(value)) {
return "[".concat(value.map(function (d) { return handleValue(d); }).join(','), "]");
}
else if (value === null) {
return value;
}
else if (typeof value === 'object') {
switch (value.type) {
case 'raw':
case 'guid':
return value.value;
case 'duration':
return "duration'".concat(value.value, "'");
case 'binary':
return "binary'".concat(value.value, "'");
case 'alias':
// Store
if (Array.isArray(aliases))
aliases.push(value);
return "@".concat(value.name);
case 'json':
return escape(JSON.stringify(value.value));
case 'decimal':
return "".concat(value.value, "M");
default:
return Object.entries(value)
.filter(function (_a) {
var v = _a[1];
return v !== undefined;
})
.map(function (_a) {
var k = _a[0], v = _a[1];
return "".concat(k, "=").concat(handleValue(v, aliases));
}).join(',');
}
}
return value;
}
function buildExpand(expands) {
if (typeof expands === 'number') {
return expands;
}
else if (typeof expands === 'string') {
if (expands.indexOf('/') === -1) {
return expands;
}
// Change `Foo/Bar/Baz` to `Foo($expand=Bar($expand=Baz))`
return expands
.split('/')
.reverse()
.reduce(function (results, item, index, arr) {
if (index === 0) {
// Inner-most item
return "$expand=".concat(item);
}
else if (index === arr.length - 1) {
// Outer-most item, don't add `$expand=` prefix (added above)
return "".concat(item, "(").concat(results, ")");
}
else {
// Other items
return "$expand=".concat(item, "(").concat(results, ")");
}
}, '');
}
else if (Array.isArray(expands)) {
return "".concat(expands.map(function (e) { return buildExpand(e); }).join(','));
}
else if (typeof expands === 'object') {
var expandKeys = Object.keys(expands);
if (expandKeys.some(function (key) { return SUPPORTED_EXPAND_PROPERTIES.indexOf(key.toLowerCase()) !== -1; })) {
return expandKeys
.map(function (key) {
var value;
switch (key) {
case 'filter':
value = buildFilter(expands[key]);
break;
case 'orderBy':
value = buildOrderBy(expands[key]);
break;
case 'levels':
case 'count':
case 'skip':
case 'top':
value = "".concat(expands[key]);
break;
default:
value = buildExpand(expands[key]);
}
return "$".concat(key.toLowerCase(), "=").concat(value);
})
.join(';');
}
else {
return expandKeys
.map(function (key) {
var builtExpand = buildExpand(expands[key]);
return builtExpand ? "".concat(key, "(").concat(builtExpand, ")") : key;
})
.join(',');
}
}
return "";
}
function buildTransforms(transforms) {
// Wrap single object an array for simplified processing
var transformsArray = Array.isArray(transforms) ? transforms : [transforms];
var transformsResult = transformsArray.reduce(function (result, transform) {
var aggregate = transform.aggregate, filter = transform.filter, groupBy = transform.groupBy, rest = tslib_1.__rest(transform, ["aggregate", "filter", "groupBy"]);
// TODO: support as many of the following:
// topcount, topsum, toppercent,
// bottomsum, bottomcount, bottompercent,
// identity, concat, expand, search, compute, isdefined
var unsupportedKeys = Object.keys(rest);
if (unsupportedKeys.length) {
throw new Error("Unsupported transform(s): ".concat(unsupportedKeys));
}
if (aggregate) {
result.push("aggregate(".concat(buildAggregate(aggregate), ")"));
}
if (filter) {
var builtFilter = buildFilter(filter);
if (builtFilter) {
result.push("filter(".concat(buildFilter(builtFilter), ")"));
}
}
if (groupBy) {
result.push("groupby(".concat(buildGroupBy(groupBy), ")"));
}
return result;
}, []);
return transformsResult.join('/') || undefined;
}
function buildAggregate(aggregate) {
// Wrap single object in an array for simplified processing
var aggregateArray = Array.isArray(aggregate) ? aggregate : [aggregate];
return aggregateArray
.map(function (aggregateItem) {
return typeof aggregateItem === "string"
? aggregateItem
: Object.keys(aggregateItem).map(function (aggregateKey) {
var aggregateValue = aggregateItem[aggregateKey];
if (!aggregateValue.with && aggregateValue.as) {
return "".concat(aggregateKey, " as ").concat(aggregateValue.as);
}
if (!aggregateValue.with) {
throw new Error("'with' property required for '".concat(aggregateKey, "'"));
}
if (!aggregateValue.as) {
throw new Error("'as' property required for '".concat(aggregateKey, "'"));
}
return "".concat(aggregateKey, " with ").concat(aggregateValue.with, " as ").concat(aggregateValue.as);
});
})
.join(',');
}
function buildGroupBy(groupBy) {
if (!groupBy.properties) {
throw new Error("'properties' property required for groupBy");
}
var result = "(".concat(groupBy.properties.join(','), ")");
if (groupBy.transform) {
result += ",".concat(buildTransforms(groupBy.transform));
}
return result;
}
function buildOrderBy(orderBy, prefix) {
if (prefix === void 0) { prefix = ''; }
if (Array.isArray(orderBy)) {
return orderBy
.map(function (value) {
return (Array.isArray(value) && value.length === 2 && ['asc', 'desc'].indexOf(value[1]) !== -1) ? value.join(' ') : value;
})
.map(function (v) { return "".concat(prefix).concat(String(v)); }).join(',');
}
else if (typeof orderBy === 'object') {
return Object.entries(orderBy)
.map(function (_a) {
var k = _a[0], v = _a[1];
return buildOrderBy(v, "".concat(k, "/"));
})
.map(function (v) { return "".concat(prefix).concat(v); }).join(',');
}
return "".concat(prefix).concat(String(orderBy));
}
function buildUrl(path, params) {
// This can be refactored using URL API. But IE does not support it.
var queries = Object.getOwnPropertyNames(params)
.filter(function (key) { return params[key] !== undefined && params[key] !== ''; })
.map(function (key) { return "".concat(key, "=").concat(params[key]); });
return queries.length ? "".concat(path, "?").concat(queries.join('&')) : path;
}
function parseNot(builtFilters) {
return "not (".concat(builtFilters.join(' and '), ")");
}
import { __assign, __rest } from "tslib";
var COMPARISON_OPERATORS = ['eq', 'ne', 'gt', 'ge', 'lt', 'le'];
var LOGICAL_OPERATORS = ['and', 'or', 'not'];
var COLLECTION_OPERATORS = ['any', 'all'];
var BOOLEAN_FUNCTIONS = ['startswith', 'endswith', 'contains', 'matchespattern'];
var SUPPORTED_EXPAND_PROPERTIES = [
'expand',
'levels',
'select',
'skip',
'top',
'count',
'orderby',
'filter',
];
var FUNCTION_REGEX = /\((.*)\)/;
var INDEXOF_REGEX = /(?!indexof)\((\w+)\)/;
export var StandardAggregateMethods;
(function (StandardAggregateMethods) {
StandardAggregateMethods["sum"] = "sum";
StandardAggregateMethods["min"] = "min";
StandardAggregateMethods["max"] = "max";
StandardAggregateMethods["average"] = "average";
StandardAggregateMethods["countdistinct"] = "countdistinct";
})(StandardAggregateMethods || (StandardAggregateMethods = {}));
export var raw = function (value) { return ({ type: 'raw', value: value }); };
export var guid = function (value) { return ({ type: 'guid', value: value }); };
export var duration = function (value) { return ({ type: 'duration', value: value }); };
export var binary = function (value) { return ({ type: 'binary', value: value }); };
export var json = function (value) { return ({ type: 'json', value: value }); };
export var alias = function (name, value) { return ({ type: 'alias', name: name, value: value }); };
export var decimal = function (value) { return ({ type: 'decimal', value: value }); };
export var ITEM_ROOT = "";
export default function (_a) {
var _b = _a === void 0 ? {} : _a, $select = _b.select, $search = _b.search, $skiptoken = _b.skiptoken, $format = _b.format, top = _b.top, skip = _b.skip, filter = _b.filter, transform = _b.transform, orderBy = _b.orderBy, key = _b.key, count = _b.count, expand = _b.expand, action = _b.action, func = _b.func;
var path = '';
var aliases = [];
var params = {};
// key is not (null, undefined)
if (key != undefined) {
path += "(".concat(handleValue(key, aliases), ")");
}
if (filter || typeof count === 'object')
params.$filter = buildFilter(typeof count === 'object' ? count : filter, aliases);
if (transform)
params.$apply = buildTransforms(transform);
if (expand)
params.$expand = buildExpand(expand);
if (orderBy)
params.$orderby = buildOrderBy(orderBy);
if (count) {
if (typeof count === 'boolean') {
params.$count = true;
}
else {
path += '/$count';
}
}
if (typeof top === 'number') {
params.$top = top;
}
if (typeof skip === 'number') {
params.$skip = skip;
}
if (action) {
path += "/".concat(action);
}
if (func) {
if (typeof func === 'string') {
path += "/".concat(func);
}
else if (typeof func === 'object') {
var funcName = Object.keys(func)[0];
var funcArgs = handleValue(func[funcName], aliases);
path += "/".concat(funcName);
if (funcArgs !== "") {
path += "(".concat(funcArgs, ")");
}
}
}
if (aliases.length > 0) {
Object.assign(params, aliases.reduce(function (acc, alias) {
var _a;
return Object.assign(acc, (_a = {}, _a["@".concat(alias.name)] = handleValue(alias.value), _a));
}, {}));
}
return buildUrl(path, __assign({ $select: $select, $search: $search, $skiptoken: $skiptoken, $format: $format }, params));
}
function renderPrimitiveValue(key, val, aliases) {
if (aliases === void 0) { aliases = []; }
return "".concat(key, " eq ").concat(handleValue(val, aliases));
}
function buildFilter(filters, aliases, propPrefix) {
if (filters === void 0) { filters = {}; }
if (aliases === void 0) { aliases = []; }
if (propPrefix === void 0) { propPrefix = ''; }
return (Array.isArray(filters) ? filters : [filters])
.reduce(function (acc, filter) {
if (filter) {
var builtFilter = buildFilterCore(filter, aliases, propPrefix);
if (builtFilter) {
acc.push(builtFilter);
}
}
return acc;
}, []).join(' and ');
function buildFilterCore(filter, aliases, propPrefix) {
if (filter === void 0) { filter = {}; }
if (aliases === void 0) { aliases = []; }
if (propPrefix === void 0) { propPrefix = ''; }
var filterExpr = "";
if (typeof filter === 'string') {
// Use raw filter string
filterExpr = filter;
}
else if (filter && typeof filter === 'object') {
var filtersArray = Object.keys(filter).reduce(function (result, filterKey) {
var value = filter[filterKey];
if (value === undefined) {
return result;
}
var propName = '';
if (propPrefix) {
if (filterKey === ITEM_ROOT) {
propName = propPrefix;
}
else if (INDEXOF_REGEX.test(filterKey)) {
propName = filterKey.replace(INDEXOF_REGEX, function (_, $1) { return $1.trim() === ITEM_ROOT ? "(".concat(propPrefix, ")") : "(".concat(propPrefix, "/").concat($1.trim(), ")"); });
}
else if (FUNCTION_REGEX.test(filterKey)) {
propName = filterKey.replace(FUNCTION_REGEX, function (_, $1) { return $1.trim() === ITEM_ROOT ? "(".concat(propPrefix, ")") : "(".concat(propPrefix, "/").concat($1.trim(), ")"); });
}
else {
propName = "".concat(propPrefix, "/").concat(filterKey);
}
}
else {
propName = filterKey;
}
if (filterKey === ITEM_ROOT && Array.isArray(value)) {
return result.concat(value.map(function (arrayValue) { return renderPrimitiveValue(propName, arrayValue); }));
}
if (['number', 'string', 'boolean'].indexOf(typeof value) !== -1 ||
value instanceof Date ||
value === null) {
// Simple key/value handled as equals operator
result.push(renderPrimitiveValue(propName, value, aliases));
}
else if (Array.isArray(value)) {
var op_1 = filterKey;
var builtFilters = value
.map(function (v) { return buildFilter(v, aliases, propPrefix); })
.filter(function (f) { return f; })
.map(function (f) { return (LOGICAL_OPERATORS.indexOf(op_1) !== -1 ? "(".concat(f, ")") : f); });
if (builtFilters.length) {
if (LOGICAL_OPERATORS.indexOf(op_1) !== -1) {
if (builtFilters.length) {
if (op_1 === 'not') {
result.push(parseNot(builtFilters));
}
else {
result.push("(".concat(builtFilters.join(" ".concat(op_1, " ")), ")"));
}
}
}
else {
result.push(builtFilters.join(" ".concat(op_1, " ")));
}
}
}
else if (LOGICAL_OPERATORS.indexOf(propName) !== -1) {
var op = propName;
var builtFilters = Object.keys(value).map(function (valueKey) {
var _a;
return buildFilterCore((_a = {}, _a[valueKey] = value[valueKey], _a));
});
if (builtFilters.length) {
if (op === 'not') {
result.push(parseNot(builtFilters));
}
else {
result.push("(".concat(builtFilters.join(" ".concat(op, " ")), ")"));
}
}
}
else if (typeof value === 'object') {
if ('type' in value) {
result.push(renderPrimitiveValue(propName, value, aliases));
}
else {
var operators = Object.keys(value);
operators.forEach(function (op) {
var _a;
if (value[op] === undefined) {
return;
}
if (COMPARISON_OPERATORS.indexOf(op) !== -1) {
result.push("".concat(propName, " ").concat(op, " ").concat(handleValue(value[op], aliases)));
}
else if (LOGICAL_OPERATORS.indexOf(op) !== -1) {
if (Array.isArray(value[op])) {
result.push(value[op]
.map(function (v) { return '(' + buildFilterCore(v, aliases, propName) + ')'; })
.join(" ".concat(op, " ")));
}
else {
result.push('(' + buildFilterCore(value[op], aliases, propName) + ')');
}
}
else if (COLLECTION_OPERATORS.indexOf(op) !== -1) {
var collectionClause = buildCollectionClause(filterKey.toLowerCase(), value[op], op, propName);
if (collectionClause) {
result.push(collectionClause);
}
}
else if (op === 'has') {
result.push("".concat(propName, " ").concat(op, " ").concat(handleValue(value[op], aliases)));
}
else if (op === 'in') {
var resultingValues = Array.isArray(value[op])
? value[op]
: value[op].value.map(function (typedValue) { return ({
type: value[op].type,
value: typedValue,
}); });
result.push(propName + ' in (' + resultingValues.map(function (v) { return handleValue(v, aliases); }).join(',') + ')');
}
else if (BOOLEAN_FUNCTIONS.indexOf(op) !== -1) {
// Simple boolean functions (startswith, endswith, contains, matchespattern)
result.push("".concat(op, "(").concat(propName, ",").concat(handleValue(value[op], aliases), ")"));
}
else {
// Nested property
var filter_1 = buildFilterCore((_a = {}, _a[op] = value[op], _a), aliases, propName);
if (filter_1) {
result.push(filter_1);
}
}
});
}
}
else {
throw new Error("Unexpected value type: ".concat(value));
}
return result;
}, []);
filterExpr = filtersArray.join(' and ');
} /* else {
throw new Error(`Unexpected filters type: ${filter}`);
} */
return filterExpr;
}
function buildCollectionClause(lambdaParameter, value, op, propName) {
var clause = '';
if (typeof value === 'string' || value instanceof String) {
clause = getStringCollectionClause(lambdaParameter, value, op, propName);
}
else if (value) {
// normalize {any:[{prop1: 1}, {prop2: 1}]} --> {any:{prop1: 1, prop2: 1}}; same for 'all',
// simple values collection: {any:[{'': 'simpleVal1'}, {'': 'simpleVal2'}]} --> {any:{'': ['simpleVal1', 'simpleVal2']}}; same for 'all',
var filterValue = Array.isArray(value) ?
value.reduce(function (acc, item) {
if (item.hasOwnProperty(ITEM_ROOT)) {
if (!acc.hasOwnProperty(ITEM_ROOT)) {
acc[ITEM_ROOT] = [];
}
acc[ITEM_ROOT].push(item[ITEM_ROOT]);
return acc;
}
return __assign(__assign({}, acc), item);
}, {}) : value;
var filter = buildFilterCore(filterValue, aliases, lambdaParameter);
clause = "".concat(propName, "/").concat(op, "(").concat(filter ? "".concat(lambdaParameter, ":").concat(filter) : '', ")");
}
return clause;
}
}
function getStringCollectionClause(lambdaParameter, value, collectionOperator, propName) {
var clause = '';
var conditionOperator = collectionOperator == 'all' ? 'ne' : 'eq';
clause = "".concat(propName, "/").concat(collectionOperator, "(").concat(lambdaParameter, ": ").concat(lambdaParameter, " ").concat(conditionOperator, " '").concat(value, "')");
return clause;
}
function escapeIllegalChars(string) {
string = string.replace(/%/g, '%25');
string = string.replace(/\+/g, '%2B');
string = string.replace(/\//g, '%2F');
string = string.replace(/\?/g, '%3F');
string = string.replace(/#/g, '%23');
string = string.replace(/&/g, '%26');
string = string.replace(/'/g, "''");
return string;
}
function handleValue(value, aliases) {
if (typeof value === 'string') {
return "'".concat(escapeIllegalChars(value), "'");
}
else if (value instanceof Date) {
return value.toISOString();
}
else if (typeof value === 'number') {
return value;
}
else if (Array.isArray(value)) {
return "[".concat(value.map(function (d) { return handleValue(d); }).join(','), "]");
}
else if (value === null) {
return value;
}
else if (typeof value === 'object') {
switch (value.type) {
case 'raw':
case 'guid':
return value.value;
case 'duration':
return "duration'".concat(value.value, "'");
case 'binary':
return "binary'".concat(value.value, "'");
case 'alias':
// Store
if (Array.isArray(aliases))
aliases.push(value);
return "@".concat(value.name);
case 'json':
return escape(JSON.stringify(value.value));
case 'decimal':
return "".concat(value.value, "M");
default:
return Object.entries(value)
.filter(function (_a) {
var v = _a[1];
return v !== undefined;
})
.map(function (_a) {
var k = _a[0], v = _a[1];
return "".concat(k, "=").concat(handleValue(v, aliases));
}).join(',');
}
}
return value;
}
function buildExpand(expands) {
if (typeof expands === 'number') {
return expands;
}
else if (typeof expands === 'string') {
if (expands.indexOf('/') === -1) {
return expands;
}
// Change `Foo/Bar/Baz` to `Foo($expand=Bar($expand=Baz))`
return expands
.split('/')
.reverse()
.reduce(function (results, item, index, arr) {
if (index === 0) {
// Inner-most item
return "$expand=".concat(item);
}
else if (index === arr.length - 1) {
// Outer-most item, don't add `$expand=` prefix (added above)
return "".concat(item, "(").concat(results, ")");
}
else {
// Other items
return "$expand=".concat(item, "(").concat(results, ")");
}
}, '');
}
else if (Array.isArray(expands)) {
return "".concat(expands.map(function (e) { return buildExpand(e); }).join(','));
}
else if (typeof expands === 'object') {
var expandKeys = Object.keys(expands);
if (expandKeys.some(function (key) { return SUPPORTED_EXPAND_PROPERTIES.indexOf(key.toLowerCase()) !== -1; })) {
return expandKeys
.map(function (key) {
var value;
switch (key) {
case 'filter':
value = buildFilter(expands[key]);
break;
case 'orderBy':
value = buildOrderBy(expands[key]);
break;
case 'levels':
case 'count':
case 'skip':
case 'top':
value = "".concat(expands[key]);
break;
default:
value = buildExpand(expands[key]);
}
return "$".concat(key.toLowerCase(), "=").concat(value);
})
.join(';');
}
else {
return expandKeys
.map(function (key) {
var builtExpand = buildExpand(expands[key]);
return builtExpand ? "".concat(key, "(").concat(builtExpand, ")") : key;
})
.join(',');
}
}
return "";
}
function buildTransforms(transforms) {
// Wrap single object an array for simplified processing
var transformsArray = Array.isArray(transforms) ? transforms : [transforms];
var transformsResult = transformsArray.reduce(function (result, transform) {
var aggregate = transform.aggregate, filter = transform.filter, groupBy = transform.groupBy, rest = __rest(transform, ["aggregate", "filter", "groupBy"]);
// TODO: support as many of the following:
// topcount, topsum, toppercent,
// bottomsum, bottomcount, bottompercent,
// identity, concat, expand, search, compute, isdefined
var unsupportedKeys = Object.keys(rest);
if (unsupportedKeys.length) {
throw new Error("Unsupported transform(s): ".concat(unsupportedKeys));
}
if (aggregate) {
result.push("aggregate(".concat(buildAggregate(aggregate), ")"));
}
if (filter) {
var builtFilter = buildFilter(filter);
if (builtFilter) {
result.push("filter(".concat(buildFilter(builtFilter), ")"));
}
}
if (groupBy) {
result.push("groupby(".concat(buildGroupBy(groupBy), ")"));
}
return result;
}, []);
return transformsResult.join('/') || undefined;
}
function buildAggregate(aggregate) {
// Wrap single object in an array for simplified processing
var aggregateArray = Array.isArray(aggregate) ? aggregate : [aggregate];
return aggregateArray
.map(function (aggregateItem) {
return typeof aggregateItem === "string"
? aggregateItem
: Object.keys(aggregateItem).map(function (aggregateKey) {
var aggregateValue = aggregateItem[aggregateKey];
if (!aggregateValue.with && aggregateValue.as) {
return "".concat(aggregateKey, " as ").concat(aggregateValue.as);
}
if (!aggregateValue.with) {
throw new Error("'with' property required for '".concat(aggregateKey, "'"));
}
if (!aggregateValue.as) {
throw new Error("'as' property required for '".concat(aggregateKey, "'"));
}
return "".concat(aggregateKey, " with ").concat(aggregateValue.with, " as ").concat(aggregateValue.as);
});
})
.join(',');
}
function buildGroupBy(groupBy) {
if (!groupBy.properties) {
throw new Error("'properties' property required for groupBy");
}
var result = "(".concat(groupBy.properties.join(','), ")");
if (groupBy.transform) {
result += ",".concat(buildTransforms(groupBy.transform));
}
return result;
}
function buildOrderBy(orderBy, prefix) {
if (prefix === void 0) { prefix = ''; }
if (Array.isArray(orderBy)) {
return orderBy
.map(function (value) {
return (Array.isArray(value) && value.length === 2 && ['asc', 'desc'].indexOf(value[1]) !== -1) ? value.join(' ') : value;
})
.map(function (v) { return "".concat(prefix).concat(String(v)); }).join(',');
}
else if (typeof orderBy === 'object') {
return Object.entries(orderBy)
.map(function (_a) {
var k = _a[0], v = _a[1];
return buildOrderBy(v, "".concat(k, "/"));
})
.map(function (v) { return "".concat(prefix).concat(v); }).join(',');
}
return "".concat(prefix).concat(String(orderBy));
}
function buildUrl(path, params) {
// This can be refactored using URL API. But IE does not support it.
var queries = Object.getOwnPropertyNames(params)
.filter(function (key) { return params[key] !== undefined && params[key] !== ''; })
.map(function (key) { return "".concat(key, "=").concat(params[key]); });
return queries.length ? "".concat(path, "?").concat(queries.join('&')) : path;
}
function parseNot(builtFilters) {
return "not (".concat(builtFilters.join(' and '), ")");
}
export declare type PlainObject = {
[property: string]: any;
};
export declare type Select<T> = string | string[] | keyof T | Array<keyof T>;
export declare type NestedOrderBy<T> = {
[P in keyof T]?: T[P] extends Array<infer E> ? OrderBy<E> : OrderBy<T[P]>;
};
export declare type OrderBy<T> = string | OrderByOptions<T> | Array<OrderByOptions<T>> | NestedOrderBy<T>;
export declare type TypedFilter<T> = {
[P in keyof T]?: any;
};
export declare type Filter<T> = string | PlainObject | TypedFilter<T> | Array<string | PlainObject | TypedFilter<T>>;
export declare type NestedExpandOptions<T> = {
[P in keyof T]?: (T[P] extends Array<infer E> ? Partial<ExpandOptions<E>> : Partial<ExpandOptions<T[P]>>);
};
export declare type Expand<T> = string | keyof T | NestedExpandOptions<T> | Array<keyof T | NestedExpandOptions<T>> | Array<string | NestedExpandOptions<T>>;
export declare enum StandardAggregateMethods {
sum = "sum",
min = "min",
max = "max",
average = "average",
countdistinct = "countdistinct"
}
export declare type Aggregate = string | {
[propertyName: string]: {
with: StandardAggregateMethods;
as: string;
};
};
export declare type OrderByOptions<T> = keyof T | [keyof T, 'asc' | 'desc'];
export declare type ExpandOptions<T> = {
select: Select<T>;
filter: Filter<T>;
orderBy: OrderBy<T>;
skip: number;
top: number;
levels: number | 'max';
count: boolean | Filter<T>;
expand: Expand<T>;
};
export declare type Transform<T> = {
aggregate?: Aggregate | Array<Aggregate>;
filter?: Filter<T>;
groupBy?: GroupBy<T>;
};
export declare type GroupBy<T> = {
properties: Array<keyof T>;
transform?: Transform<T>;
};
export declare type Raw = {
type: 'raw';
value: any;
};
export declare type Guid = {
type: 'guid';
value: any;
};
export declare type Duration = {
type: 'duration';
value: any;
};
export declare type Binary = {
type: 'binary';
value: any;
};
export declare type Json = {
type: 'json';
value: any;
};
export declare type Alias = {
type: 'alias';
name: string;
value: any;
};
export declare type Decimal = {
type: 'decimal';
value: any;
};
export declare type Value = string | Date | number | boolean | Raw | Guid | Duration | Binary | Json | Alias | Decimal;
export declare const raw: (value: string) => Raw;
export declare const guid: (value: string) => Guid;
export declare const duration: (value: string) => Duration;
export declare const binary: (value: string) => Binary;
export declare const json: (value: PlainObject) => Json;
export declare const alias: (name: string, value: PlainObject) => Alias;
export declare const decimal: (value: string) => Decimal;
export declare type QueryOptions<T> = ExpandOptions<T> & {
search: string;
transform: PlainObject | PlainObject[];
skip: number;
skiptoken: string;
key: string | number | PlainObject;
count: boolean | Filter<T>;
action: string;
func: string | {
[functionName: string]: {
[parameterName: string]: any;
};
};
format: string;
aliases: Alias[];
};
export declare const ITEM_ROOT = "";
export default function <T>({ select: $select, search: $search, skiptoken: $skiptoken, format: $format, top, skip, filter, transform, orderBy, key, count, expand, action, func }?: Partial<QueryOptions<T>>): string;