@shko.online/dataverse-odata 0.1.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +3 -3
- package/CHANGELOG.md +20 -0
- package/azure-pipelines.yml +1 -1
- package/lib/cjs/OData.types.d.js +1 -0
- package/lib/cjs/getAliasedProperty.js +36 -0
- package/lib/cjs/getExpandFromParser.js +122 -0
- package/lib/cjs/getFetchXmlFromParser.js +52 -0
- package/lib/cjs/getFilterFromParser.js +257 -0
- package/lib/cjs/getOrderByFromParser.js +78 -0
- package/lib/cjs/getSelectFromParser.js +27 -0
- package/lib/cjs/getTopFromParser.js +40 -0
- package/lib/cjs/getXQueryFromParser.js +25 -0
- package/lib/cjs/index.js +56 -0
- package/lib/cjs/parseOData.js +25 -0
- package/lib/cjs/validators/atMostOnce.js +24 -0
- package/lib/cjs/validators/differentFromEmptyString.js +23 -0
- package/lib/cjs/validators/hasContent.js +24 -0
- package/lib/cjs/validators/isGuid.js +25 -0
- package/lib/cjs/validators/recognizedGuid.js +23 -0
- package/lib/esm/OData.types.d.js +0 -0
- package/lib/esm/getAliasedProperty.js +29 -0
- package/lib/esm/getExpandFromParser.js +115 -0
- package/lib/esm/getFetchXmlFromParser.js +45 -0
- package/lib/esm/getFilterFromParser.js +250 -0
- package/lib/esm/getOrderByFromParser.js +71 -0
- package/lib/esm/getSelectFromParser.js +20 -0
- package/lib/esm/getTopFromParser.js +33 -0
- package/lib/esm/getXQueryFromParser.js +19 -0
- package/lib/esm/index.js +9 -0
- package/lib/esm/parseOData.js +19 -0
- package/lib/esm/validators/atMostOnce.js +17 -0
- package/lib/esm/validators/differentFromEmptyString.js +16 -0
- package/lib/esm/validators/hasContent.js +17 -0
- package/lib/esm/validators/isGuid.js +18 -0
- package/lib/esm/validators/recognizedGuid.js +16 -0
- package/lib/modern/OData.types.d.js +0 -0
- package/lib/modern/getAliasedProperty.js +29 -0
- package/lib/modern/getExpandFromParser.js +115 -0
- package/lib/modern/getFetchXmlFromParser.js +45 -0
- package/lib/modern/getFilterFromParser.js +255 -0
- package/lib/modern/getOrderByFromParser.js +72 -0
- package/lib/modern/getSelectFromParser.js +20 -0
- package/lib/modern/getTopFromParser.js +33 -0
- package/lib/modern/getXQueryFromParser.js +19 -0
- package/lib/modern/index.js +9 -0
- package/lib/modern/parseOData.js +19 -0
- package/lib/modern/validators/atMostOnce.js +17 -0
- package/lib/modern/validators/differentFromEmptyString.js +16 -0
- package/lib/modern/validators/hasContent.js +17 -0
- package/lib/modern/validators/isGuid.js +18 -0
- package/lib/modern/validators/recognizedGuid.js +16 -0
- package/lib/ts3.4/OData.types.d.ts +172 -0
- package/lib/ts3.4/getAliasedProperty.d.ts +10 -0
- package/lib/ts3.4/getExpandFromParser.d.ts +7 -0
- package/lib/ts3.4/getFetchXmlFromParser.d.ts +7 -0
- package/lib/ts3.4/getFilterFromParser.d.ts +7 -0
- package/lib/ts3.4/getOrderByFromParser.d.ts +7 -0
- package/lib/ts3.4/getSelectFromParser.d.ts +7 -0
- package/lib/ts3.4/getTopFromParser.d.ts +7 -0
- package/lib/ts3.4/getXQueryFromParser.d.ts +8 -0
- package/lib/ts3.4/index.d.ts +11 -0
- package/lib/ts3.4/parseOData.d.ts +8 -0
- package/lib/ts3.4/validators/atMostOnce.d.ts +10 -0
- package/lib/ts3.4/validators/differentFromEmptyString.d.ts +9 -0
- package/lib/ts3.4/validators/hasContent.d.ts +10 -0
- package/lib/ts3.4/validators/isGuid.d.ts +9 -0
- package/lib/ts3.4/validators/recognizedGuid.d.ts +9 -0
- package/lib/ts3.9/OData.types.d.ts +172 -0
- package/lib/ts3.9/getAliasedProperty.d.ts +10 -0
- package/lib/ts3.9/getExpandFromParser.d.ts +7 -0
- package/lib/ts3.9/getFetchXmlFromParser.d.ts +7 -0
- package/lib/ts3.9/getFilterFromParser.d.ts +7 -0
- package/lib/ts3.9/getOrderByFromParser.d.ts +7 -0
- package/lib/ts3.9/getSelectFromParser.d.ts +7 -0
- package/lib/ts3.9/getTopFromParser.d.ts +7 -0
- package/lib/ts3.9/getXQueryFromParser.d.ts +8 -0
- package/lib/ts3.9/index.d.ts +11 -0
- package/lib/ts3.9/parseOData.d.ts +8 -0
- package/lib/ts3.9/validators/atMostOnce.d.ts +10 -0
- package/lib/ts3.9/validators/differentFromEmptyString.d.ts +9 -0
- package/lib/ts3.9/validators/hasContent.d.ts +10 -0
- package/lib/ts3.9/validators/isGuid.d.ts +9 -0
- package/lib/ts3.9/validators/recognizedGuid.d.ts +9 -0
- package/lib/ts4.2/OData.types.d.ts +226 -0
- package/lib/ts4.2/getAliasedProperty.d.ts +10 -0
- package/lib/ts4.2/getAliasedProperty.d.ts.map +1 -0
- package/lib/ts4.2/getExpandFromParser.d.ts +7 -0
- package/lib/ts4.2/getExpandFromParser.d.ts.map +1 -0
- package/lib/ts4.2/getFetchXmlFromParser.d.ts +7 -0
- package/lib/ts4.2/getFetchXmlFromParser.d.ts.map +1 -0
- package/lib/ts4.2/getFilterFromParser.d.ts +7 -0
- package/lib/ts4.2/getFilterFromParser.d.ts.map +1 -0
- package/lib/ts4.2/getOrderByFromParser.d.ts +7 -0
- package/lib/ts4.2/getOrderByFromParser.d.ts.map +1 -0
- package/lib/ts4.2/getSelectFromParser.d.ts +7 -0
- package/lib/ts4.2/getSelectFromParser.d.ts.map +1 -0
- package/lib/ts4.2/getTopFromParser.d.ts +7 -0
- package/lib/ts4.2/getTopFromParser.d.ts.map +1 -0
- package/lib/ts4.2/getXQueryFromParser.d.ts +8 -0
- package/lib/ts4.2/getXQueryFromParser.d.ts.map +1 -0
- package/lib/ts4.2/index.d.ts +11 -0
- package/lib/ts4.2/index.d.ts.map +1 -0
- package/lib/ts4.2/parseOData.d.ts +8 -0
- package/lib/ts4.2/parseOData.d.ts.map +1 -0
- package/lib/ts4.2/validators/atMostOnce.d.ts +10 -0
- package/lib/ts4.2/validators/atMostOnce.d.ts.map +1 -0
- package/lib/ts4.2/validators/differentFromEmptyString.d.ts +9 -0
- package/lib/ts4.2/validators/differentFromEmptyString.d.ts.map +1 -0
- package/lib/ts4.2/validators/hasContent.d.ts +10 -0
- package/lib/ts4.2/validators/hasContent.d.ts.map +1 -0
- package/lib/ts4.2/validators/isGuid.d.ts +9 -0
- package/lib/ts4.2/validators/isGuid.d.ts.map +1 -0
- package/lib/ts4.2/validators/recognizedGuid.d.ts +9 -0
- package/lib/ts4.2/validators/recognizedGuid.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/OData.types.d.ts +48 -5
- package/src/getFilterFromParser.ts +206 -5
package/lib/cjs/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
Object.defineProperty(exports, "getExpandFromParser", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function () {
|
|
10
|
+
return _getExpandFromParser.getExpandFromParser;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "getFetchXmlFromParser", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function () {
|
|
16
|
+
return _getFetchXmlFromParser.getFetchXmlFromParser;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(exports, "getOrderByFromParser", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return _getOrderByFromParser.getOrderByFromParser;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(exports, "getSelectFromParser", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _getSelectFromParser.getSelectFromParser;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "getTopFromParser", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return _getTopFromParser.getTopFromParser;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, "getXQueryFromParser", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _getXQueryFromParser.getXQueryFromParser;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(exports, "parseOData", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _parseOData.parseOData;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
var _getExpandFromParser = require("./getExpandFromParser");
|
|
50
|
+
var _getFetchXmlFromParser = require("./getFetchXmlFromParser");
|
|
51
|
+
var _getOrderByFromParser = require("./getOrderByFromParser");
|
|
52
|
+
var _getSelectFromParser = require("./getSelectFromParser");
|
|
53
|
+
var _getTopFromParser = require("./getTopFromParser");
|
|
54
|
+
var _getXQueryFromParser = require("./getXQueryFromParser");
|
|
55
|
+
var _parseOData = require("./parseOData");
|
|
56
|
+
var _default = exports.default = _parseOData.parseOData;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parseOData = void 0;
|
|
7
|
+
var _getTopFromParser = require("./getTopFromParser");
|
|
8
|
+
var _getSelectFromParser = require("./getSelectFromParser");
|
|
9
|
+
var _getExpandFromParser = require("./getExpandFromParser");
|
|
10
|
+
var _getFetchXmlFromParser = require("./getFetchXmlFromParser");
|
|
11
|
+
var _getXQueryFromParser = require("./getXQueryFromParser");
|
|
12
|
+
var _getOrderByFromParser = require("./getOrderByFromParser");
|
|
13
|
+
var _getFilterFromParser = require("./getFilterFromParser");
|
|
14
|
+
/**
|
|
15
|
+
* parses the OData query and applies some Dataverse validations
|
|
16
|
+
* @param query The OData query
|
|
17
|
+
* @returns The parsed OData query
|
|
18
|
+
*/
|
|
19
|
+
const parseOData = query => {
|
|
20
|
+
const parser = new URLSearchParams(query);
|
|
21
|
+
const result = {};
|
|
22
|
+
(0, _getExpandFromParser.getExpandFromParser)(parser, result) && (0, _getFetchXmlFromParser.getFetchXmlFromParser)(parser, result) && (0, _getFilterFromParser.getFilterFromParser)(parser, result) && (0, _getSelectFromParser.getSelectFromParser)(parser, result) && (0, _getTopFromParser.getTopFromParser)(parser, result) && (0, _getXQueryFromParser.getXQueryFromParser)('savedQuery', parser, result) && (0, _getXQueryFromParser.getXQueryFromParser)('userQuery', parser, result) && (0, _getOrderByFromParser.getOrderByFromParser)(parser, result);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
exports.parseOData = parseOData;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.atMostOnce = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Options of this type must be specified at most once.
|
|
9
|
+
* @param option The option being validated (ex. $top)
|
|
10
|
+
* @param value The result of {@link URLSearchParams.prototype.getAll URLSearchParams.getAll}
|
|
11
|
+
* @param result The {@link ODataQuery} to append the error to
|
|
12
|
+
* @returns {boolean} Returns `false` when the parse has an error
|
|
13
|
+
*/
|
|
14
|
+
const atMostOnce = (option, value, result) => {
|
|
15
|
+
if (value.length > 1) {
|
|
16
|
+
result.error = {
|
|
17
|
+
code: '0x0',
|
|
18
|
+
message: `Query option '${option}' was specified more than once, but it must be specified at most once.`
|
|
19
|
+
};
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
exports.atMostOnce = atMostOnce;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.differentFromEmptyString = void 0;
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param value The result of {@link URLSearchParams.getAll getAll}
|
|
10
|
+
* @param result The {@link ODataQuery} to append the error to
|
|
11
|
+
* @returns {boolean} Returns `false` when the parse has an error
|
|
12
|
+
*/
|
|
13
|
+
const differentFromEmptyString = (value, result) => {
|
|
14
|
+
if (value[0] === '') {
|
|
15
|
+
result.error = {
|
|
16
|
+
code: '0x80040203',
|
|
17
|
+
message: 'Expected non-empty string.'
|
|
18
|
+
};
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
exports.differentFromEmptyString = differentFromEmptyString;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.hasContent = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Options of this type must be specified at most once.
|
|
9
|
+
* @param option The option being validated (ex. $top)
|
|
10
|
+
* @param value The result of {@link URLSearchParams.getAll getAll}
|
|
11
|
+
* @param result The {@link ODataQuery} to append the error to
|
|
12
|
+
* @returns {boolean} Returns `false` when the parse has an error
|
|
13
|
+
*/
|
|
14
|
+
const hasContent = (query, value, result) => {
|
|
15
|
+
if (!value[0].trim()) {
|
|
16
|
+
result.error = {
|
|
17
|
+
code: '0x0',
|
|
18
|
+
message: `The value for OData query '${query}' cannot be empty.`
|
|
19
|
+
};
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
};
|
|
24
|
+
exports.hasContent = hasContent;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isGuid = void 0;
|
|
7
|
+
const guidRegex = /[0-9A-F]{8}\-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/gi;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param value The result of {@link URLSearchParams.getAll getAll}
|
|
12
|
+
* @param result The {@link ODataQuery} to append the error to
|
|
13
|
+
* @returns {boolean} Returns `false` when the parse has an error
|
|
14
|
+
*/
|
|
15
|
+
const isGuid = (value, result) => {
|
|
16
|
+
if (!value[0].match(guidRegex)) {
|
|
17
|
+
result.error = {
|
|
18
|
+
code: '0x0',
|
|
19
|
+
message: 'Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
|
|
20
|
+
};
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return true;
|
|
24
|
+
};
|
|
25
|
+
exports.isGuid = isGuid;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.recognizedGuid = void 0;
|
|
7
|
+
/**
|
|
8
|
+
*
|
|
9
|
+
* @param value The result of {@link URLSearchParams.getAll getAll}
|
|
10
|
+
* @param result The {@link ODataQuery} to append the error to
|
|
11
|
+
* @returns {boolean} Returns `false` when the parse has an error
|
|
12
|
+
*/
|
|
13
|
+
const recognizedGuid = (value, result) => {
|
|
14
|
+
if (!value[0].trim()) {
|
|
15
|
+
result.error = {
|
|
16
|
+
code: '0x0',
|
|
17
|
+
message: 'Unrecognized Guid format.'
|
|
18
|
+
};
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
exports.recognizedGuid = recognizedGuid;
|
|
File without changes
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursively gets the value of an aliased property. For example, if the query is `$orderby=@p1` and `@p1=name`, this function will return `name`
|
|
3
|
+
* @param parser The URLSearchParams object containing the query parameters
|
|
4
|
+
* @param result Will contain the error details in case there is any
|
|
5
|
+
* @param property The property to expand
|
|
6
|
+
* @returns The expanded property or null when there is an error
|
|
7
|
+
*/
|
|
8
|
+
export const getAliasedProperty = (parser, result, property) => {
|
|
9
|
+
let propertyName = parser.get(property);
|
|
10
|
+
if (!propertyName) {
|
|
11
|
+
result.error = {
|
|
12
|
+
code: '0x80060888',
|
|
13
|
+
message: 'Order By Property must be of type EdmProperty'
|
|
14
|
+
};
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
if (!/^[@a-zA-Z]\w+/gi.test(propertyName)) {
|
|
18
|
+
const position = propertyName.length;
|
|
19
|
+
result.error = {
|
|
20
|
+
code: '0x80060888',
|
|
21
|
+
message: `Syntax error at position ${position} in '${propertyName}'.`
|
|
22
|
+
};
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
if (propertyName.startsWith('@')) {
|
|
26
|
+
return getAliasedProperty(parser, result, propertyName);
|
|
27
|
+
}
|
|
28
|
+
return propertyName;
|
|
29
|
+
};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { getSelectFromParser } from './getSelectFromParser';
|
|
2
|
+
import { atMostOnce } from './validators/atMostOnce';
|
|
3
|
+
const option = '$expand';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses the {@link ODataExpand.$expand $expand} query
|
|
7
|
+
* @returns Returns `false` when the parse has an error
|
|
8
|
+
*/
|
|
9
|
+
export const getExpandFromParser = (parser, result) => {
|
|
10
|
+
const value = parser.getAll(option);
|
|
11
|
+
if (value.length === 0) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (!atMostOnce(option, value, result)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
result.$expand = {};
|
|
18
|
+
if (!extractExpand(value[0], result)) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
const extractExpand = (value, $expand) => {
|
|
24
|
+
const match = value.match(/^\s*(\w(\w|\d|_)*)\s*(,|\(|\))?\s*/);
|
|
25
|
+
if (match === null || match[0].length < value.length && match[3] === null || match[0].length === value.length && match[3] !== undefined) {
|
|
26
|
+
$expand.error = {
|
|
27
|
+
code: '0x0',
|
|
28
|
+
message: `Term '${value}' is not valid in a $select or $expand expression.`
|
|
29
|
+
};
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
let matchSeparator = match[3];
|
|
33
|
+
let matchLength = match[0].length;
|
|
34
|
+
if (matchSeparator !== '(') {
|
|
35
|
+
if ($expand.$expand !== undefined) {
|
|
36
|
+
$expand.$expand[match[1]] = {
|
|
37
|
+
$select: []
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
} else {
|
|
41
|
+
const {
|
|
42
|
+
index,
|
|
43
|
+
error
|
|
44
|
+
} = getClosingBracket(value.substring(matchLength));
|
|
45
|
+
if (error) {
|
|
46
|
+
$expand.error = {
|
|
47
|
+
code: '0x0',
|
|
48
|
+
message: error
|
|
49
|
+
};
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
if ($expand.$expand !== undefined) {
|
|
53
|
+
const innerExpand = {};
|
|
54
|
+
const parser = new URLSearchParams('?' + value.substring(matchLength, matchLength + index));
|
|
55
|
+
if (!getSelectFromParser(parser, innerExpand)) {
|
|
56
|
+
$expand.error = innerExpand.error;
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (!getExpandFromParser(parser, innerExpand)) {
|
|
60
|
+
$expand.error = innerExpand.error;
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (innerExpand.$expand === undefined && innerExpand.$select === undefined) {
|
|
64
|
+
$expand.error = {
|
|
65
|
+
code: '0x0',
|
|
66
|
+
message: `Missing expand option on navigation property '${match[1]}'. If a parenthesis expression follows an expanded navigation property, then at least one expand option must be provided.`
|
|
67
|
+
};
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
$expand.$expand[match[1]] = innerExpand;
|
|
71
|
+
}
|
|
72
|
+
matchLength = matchLength + index;
|
|
73
|
+
const secondMatch = new RegExp(/\s*(,?)\s*d/).exec(value.substring(matchLength + 1));
|
|
74
|
+
if (secondMatch !== null) {
|
|
75
|
+
matchLength = matchLength + secondMatch[0].length;
|
|
76
|
+
if (secondMatch[1] !== null) {
|
|
77
|
+
matchSeparator = ',';
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (matchSeparator === ',') {
|
|
82
|
+
if (!extractExpand(value.substring(matchLength), $expand)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
};
|
|
88
|
+
const getClosingBracket = value => {
|
|
89
|
+
let depth = 1;
|
|
90
|
+
let startAt = 0;
|
|
91
|
+
while (depth > 0) {
|
|
92
|
+
const match = value.substring(startAt).match(/\(|\)/);
|
|
93
|
+
if (match === null) {
|
|
94
|
+
return {
|
|
95
|
+
error: 'Found an unbalanced bracket expression.',
|
|
96
|
+
index: -1
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (match[0] === ')') {
|
|
100
|
+
depth -= 1;
|
|
101
|
+
if (depth === 0) {
|
|
102
|
+
return {
|
|
103
|
+
index: match.index || 0
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
} else {
|
|
107
|
+
depth += 1;
|
|
108
|
+
}
|
|
109
|
+
startAt += (match.index || 0) + 1;
|
|
110
|
+
}
|
|
111
|
+
return {
|
|
112
|
+
error: 'Found an unbalanced bracket expression.',
|
|
113
|
+
index: -1
|
|
114
|
+
};
|
|
115
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { atMostOnce } from './validators/atMostOnce';
|
|
2
|
+
import { differentFromEmptyString } from './validators/differentFromEmptyString';
|
|
3
|
+
const option = 'fetchXml';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Parses the {@link ODataFetch.fetchXml fetchXml} query
|
|
7
|
+
* @returns Returns `false` when the parse has an error
|
|
8
|
+
*/
|
|
9
|
+
export const getFetchXmlFromParser = (parser, result) => {
|
|
10
|
+
const value = parser.getAll(option);
|
|
11
|
+
if (value.length === 0) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (!atMostOnce(option, value, result) || !differentFromEmptyString(value, result)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
const fetchXml = value[0];
|
|
18
|
+
const serializer = new DOMParser();
|
|
19
|
+
const fetchXmlDocument = serializer.parseFromString(fetchXml, 'text/xml');
|
|
20
|
+
if (fetchXmlDocument.documentElement.tagName === 'parsererror') {
|
|
21
|
+
result.error = {
|
|
22
|
+
code: '0x80040201',
|
|
23
|
+
message: 'Invalid XML.'
|
|
24
|
+
};
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
const entity = fetchXmlDocument.evaluate('fetch/entity', fetchXmlDocument, null, XPathResult.ANY_TYPE, null).iterateNext();
|
|
28
|
+
if (fetchXmlDocument.documentElement.children.length != 1 || !entity || !entity.getAttribute('name')) {
|
|
29
|
+
result.error = {
|
|
30
|
+
code: '0x80041102',
|
|
31
|
+
message: 'Entity Name was not specified in FetchXml String.'
|
|
32
|
+
};
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const invalidAttribute = fetchXmlDocument.evaluate('fetch/entity/*[not(self::filter or self::order or self::link-entity or self::attribute or self::all-attributes or self::no-attrs)]', fetchXmlDocument, null, XPathResult.ANY_TYPE, null).iterateNext();
|
|
36
|
+
if (invalidAttribute) {
|
|
37
|
+
result.error = {
|
|
38
|
+
code: '0x8004111c',
|
|
39
|
+
message: `Invalid Child Node, valid nodes are filter, order, link-entity, attribute, all-attributes, no-attrs. NodeName = ${invalidAttribute.tagName} NodeXml = ${invalidAttribute.outerHTML}`
|
|
40
|
+
};
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
result.fetchXml = fetchXmlDocument;
|
|
44
|
+
return true;
|
|
45
|
+
};
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { atMostOnce } from './validators/atMostOnce';
|
|
2
|
+
import { hasContent } from './validators/hasContent';
|
|
3
|
+
const option = '$filter';
|
|
4
|
+
const STANDARD_OPERATORS = ['eq', 'ne', 'gt', 'ge', 'lt', 'le'];
|
|
5
|
+
const QUERY_FUNCTION_OPERATORS = ['contains', 'endswith', 'startswith'];
|
|
6
|
+
function isStandardOperator(s) {
|
|
7
|
+
return STANDARD_OPERATORS.includes(s);
|
|
8
|
+
}
|
|
9
|
+
function isQueryFunctionOperator(s) {
|
|
10
|
+
return QUERY_FUNCTION_OPERATORS.includes(s);
|
|
11
|
+
}
|
|
12
|
+
const GUID_REGEX = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/;
|
|
13
|
+
function tokenize(input) {
|
|
14
|
+
const tokens = [];
|
|
15
|
+
let i = 0;
|
|
16
|
+
while (i < input.length) {
|
|
17
|
+
if (/\s/.test(input[i])) {
|
|
18
|
+
i++;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (input[i] === '(') {
|
|
22
|
+
tokens.push({
|
|
23
|
+
type: 'lparen',
|
|
24
|
+
value: '('
|
|
25
|
+
});
|
|
26
|
+
i++;
|
|
27
|
+
} else if (input[i] === ')') {
|
|
28
|
+
tokens.push({
|
|
29
|
+
type: 'rparen',
|
|
30
|
+
value: ')'
|
|
31
|
+
});
|
|
32
|
+
i++;
|
|
33
|
+
} else if (input[i] === ',') {
|
|
34
|
+
tokens.push({
|
|
35
|
+
type: 'comma',
|
|
36
|
+
value: ','
|
|
37
|
+
});
|
|
38
|
+
i++;
|
|
39
|
+
} else if (input[i] === "'") {
|
|
40
|
+
let j = i + 1;
|
|
41
|
+
let str = '';
|
|
42
|
+
while (j < input.length) {
|
|
43
|
+
if (input[j] === "'" && j + 1 < input.length && input[j + 1] === "'") {
|
|
44
|
+
str += "'";
|
|
45
|
+
j += 2;
|
|
46
|
+
} else if (input[j] === "'") {
|
|
47
|
+
break;
|
|
48
|
+
} else {
|
|
49
|
+
str += input[j];
|
|
50
|
+
j++;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
tokens.push({
|
|
54
|
+
type: 'string',
|
|
55
|
+
value: str
|
|
56
|
+
});
|
|
57
|
+
i = j + 1;
|
|
58
|
+
} else if (/[0-9a-fA-F]/.test(input[i]) && GUID_REGEX.test(input.slice(i))) {
|
|
59
|
+
tokens.push({
|
|
60
|
+
type: 'string',
|
|
61
|
+
value: input.slice(i, i + 36)
|
|
62
|
+
});
|
|
63
|
+
i += 36;
|
|
64
|
+
} else if (/[0-9]/.test(input[i]) || input[i] === '-' && /[0-9]/.test(input[i + 1] ?? '')) {
|
|
65
|
+
let j = i;
|
|
66
|
+
if (input[j] === '-') j++;
|
|
67
|
+
while (j < input.length && /[0-9.]/.test(input[j])) j++;
|
|
68
|
+
tokens.push({
|
|
69
|
+
type: 'number',
|
|
70
|
+
value: input.slice(i, j)
|
|
71
|
+
});
|
|
72
|
+
i = j;
|
|
73
|
+
} else if (/[a-zA-Z_]/.test(input[i])) {
|
|
74
|
+
let j = i;
|
|
75
|
+
while (j < input.length && /[a-zA-Z0-9_]/.test(input[j])) j++;
|
|
76
|
+
tokens.push({
|
|
77
|
+
type: 'word',
|
|
78
|
+
value: input.slice(i, j)
|
|
79
|
+
});
|
|
80
|
+
i = j;
|
|
81
|
+
} else {
|
|
82
|
+
i++;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return tokens;
|
|
86
|
+
}
|
|
87
|
+
class FilterParser {
|
|
88
|
+
constructor(tokens) {
|
|
89
|
+
this.tokens = void 0;
|
|
90
|
+
this.pos = 0;
|
|
91
|
+
this.tokens = tokens;
|
|
92
|
+
}
|
|
93
|
+
peek() {
|
|
94
|
+
return this.tokens[this.pos] ?? null;
|
|
95
|
+
}
|
|
96
|
+
consume(expected) {
|
|
97
|
+
const token = this.tokens[this.pos++];
|
|
98
|
+
if (!token) throw new Error(`Unexpected end of filter expression${expected ? `, expected ${expected}` : ''}`);
|
|
99
|
+
return token;
|
|
100
|
+
}
|
|
101
|
+
expect(type, value) {
|
|
102
|
+
const token = this.consume(`${type}${value ? ` '${value}'` : ''}`);
|
|
103
|
+
if (token.type !== type || value !== undefined && token.value !== value) {
|
|
104
|
+
throw new Error(`Expected ${type}${value ? ` '${value}'` : ''} but got ${token.type} '${token.value}'`);
|
|
105
|
+
}
|
|
106
|
+
return token;
|
|
107
|
+
}
|
|
108
|
+
parse() {
|
|
109
|
+
const expr = this.parseOr();
|
|
110
|
+
if (this.pos < this.tokens.length) {
|
|
111
|
+
throw new Error(`Unexpected token '${this.tokens[this.pos].value}'`);
|
|
112
|
+
}
|
|
113
|
+
return expr;
|
|
114
|
+
}
|
|
115
|
+
parseOr() {
|
|
116
|
+
let left = this.parseAnd();
|
|
117
|
+
while (this.peek()?.value === 'or') {
|
|
118
|
+
this.consume();
|
|
119
|
+
const right = this.parseAnd();
|
|
120
|
+
left = {
|
|
121
|
+
operator: 'or',
|
|
122
|
+
left,
|
|
123
|
+
right
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return left;
|
|
127
|
+
}
|
|
128
|
+
parseAnd() {
|
|
129
|
+
let left = this.parseNot();
|
|
130
|
+
while (this.peek()?.value === 'and') {
|
|
131
|
+
this.consume();
|
|
132
|
+
const right = this.parseNot();
|
|
133
|
+
left = {
|
|
134
|
+
operator: 'and',
|
|
135
|
+
left,
|
|
136
|
+
right
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
return left;
|
|
140
|
+
}
|
|
141
|
+
parseNot() {
|
|
142
|
+
if (this.peek()?.value === 'not') {
|
|
143
|
+
this.consume();
|
|
144
|
+
const right = this.parseNot();
|
|
145
|
+
return {
|
|
146
|
+
operator: 'not',
|
|
147
|
+
right
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
return this.parsePrimary();
|
|
151
|
+
}
|
|
152
|
+
parsePrimary() {
|
|
153
|
+
const token = this.peek();
|
|
154
|
+
if (!token) throw new Error('Unexpected end of filter expression');
|
|
155
|
+
if (token.type === 'lparen') {
|
|
156
|
+
this.consume();
|
|
157
|
+
const expr = this.parseOr();
|
|
158
|
+
this.expect('rparen');
|
|
159
|
+
return expr;
|
|
160
|
+
}
|
|
161
|
+
if (token.type === 'word' && isQueryFunctionOperator(token.value.toLowerCase())) {
|
|
162
|
+
const func = this.consume().value.toLowerCase();
|
|
163
|
+
this.expect('lparen');
|
|
164
|
+
const left = this.expect('word').value;
|
|
165
|
+
this.expect('comma');
|
|
166
|
+
const right = this.expect('string').value;
|
|
167
|
+
this.expect('rparen');
|
|
168
|
+
return {
|
|
169
|
+
operator: func,
|
|
170
|
+
left,
|
|
171
|
+
right
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
if (token.type === 'word') {
|
|
175
|
+
const left = this.consume().value;
|
|
176
|
+
const opToken = this.consume(`a comparison operator`);
|
|
177
|
+
if (!isStandardOperator(opToken.value.toLowerCase())) {
|
|
178
|
+
throw new Error(`Invalid operator '${opToken.value}'`);
|
|
179
|
+
}
|
|
180
|
+
const operator = opToken.value.toLowerCase();
|
|
181
|
+
const right = this.consume(`a value or column name`);
|
|
182
|
+
if (right.type === 'string') {
|
|
183
|
+
return {
|
|
184
|
+
operator,
|
|
185
|
+
left,
|
|
186
|
+
right: right.value
|
|
187
|
+
};
|
|
188
|
+
} else if (right.type === 'number') {
|
|
189
|
+
return {
|
|
190
|
+
operator,
|
|
191
|
+
left,
|
|
192
|
+
right: Number(right.value)
|
|
193
|
+
};
|
|
194
|
+
} else if (right.type === 'word') {
|
|
195
|
+
// Constant keywords stay as StandardOperator; bare identifiers are column comparisons
|
|
196
|
+
const BOOL_CONSTANTS = ['true', 'false'];
|
|
197
|
+
if (BOOL_CONSTANTS.includes(right.value.toLowerCase())) {
|
|
198
|
+
return {
|
|
199
|
+
operator,
|
|
200
|
+
left,
|
|
201
|
+
isBooleanOperation: true,
|
|
202
|
+
right: right.value === 'true'
|
|
203
|
+
};
|
|
204
|
+
} else if (right.value.toLowerCase() === 'null') {
|
|
205
|
+
return {
|
|
206
|
+
operator,
|
|
207
|
+
left,
|
|
208
|
+
isNullOperation: true,
|
|
209
|
+
right: null
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
left,
|
|
214
|
+
operator,
|
|
215
|
+
isColumnOperation: true,
|
|
216
|
+
right: right.value
|
|
217
|
+
};
|
|
218
|
+
} else {
|
|
219
|
+
throw new Error(`Invalid right-hand side value of type '${right.type}' in filter expression`);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
throw new Error(`Unexpected token '${token.value}'`);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Parses the {@link ODataFilter.$filter $filter} query
|
|
228
|
+
* @returns {boolean} Returns `false` when the parse has an error
|
|
229
|
+
*/
|
|
230
|
+
export const getFilterFromParser = (parser, result) => {
|
|
231
|
+
const value = parser.getAll(option);
|
|
232
|
+
if (value.length === 0) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
if (!atMostOnce(option, value, result) || !hasContent(option, value, result)) {
|
|
236
|
+
return false;
|
|
237
|
+
}
|
|
238
|
+
try {
|
|
239
|
+
const tokens = tokenize(value[0]);
|
|
240
|
+
const p = new FilterParser(tokens);
|
|
241
|
+
result.$filter = p.parse();
|
|
242
|
+
} catch (e) {
|
|
243
|
+
result.error = {
|
|
244
|
+
code: '0x80060888',
|
|
245
|
+
message: `Syntax error in '$filter': ${e.message}`
|
|
246
|
+
};
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
return true;
|
|
250
|
+
};
|