@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.
Files changed (117) hide show
  1. package/.github/workflows/publish.yml +3 -3
  2. package/CHANGELOG.md +20 -0
  3. package/azure-pipelines.yml +1 -1
  4. package/lib/cjs/OData.types.d.js +1 -0
  5. package/lib/cjs/getAliasedProperty.js +36 -0
  6. package/lib/cjs/getExpandFromParser.js +122 -0
  7. package/lib/cjs/getFetchXmlFromParser.js +52 -0
  8. package/lib/cjs/getFilterFromParser.js +257 -0
  9. package/lib/cjs/getOrderByFromParser.js +78 -0
  10. package/lib/cjs/getSelectFromParser.js +27 -0
  11. package/lib/cjs/getTopFromParser.js +40 -0
  12. package/lib/cjs/getXQueryFromParser.js +25 -0
  13. package/lib/cjs/index.js +56 -0
  14. package/lib/cjs/parseOData.js +25 -0
  15. package/lib/cjs/validators/atMostOnce.js +24 -0
  16. package/lib/cjs/validators/differentFromEmptyString.js +23 -0
  17. package/lib/cjs/validators/hasContent.js +24 -0
  18. package/lib/cjs/validators/isGuid.js +25 -0
  19. package/lib/cjs/validators/recognizedGuid.js +23 -0
  20. package/lib/esm/OData.types.d.js +0 -0
  21. package/lib/esm/getAliasedProperty.js +29 -0
  22. package/lib/esm/getExpandFromParser.js +115 -0
  23. package/lib/esm/getFetchXmlFromParser.js +45 -0
  24. package/lib/esm/getFilterFromParser.js +250 -0
  25. package/lib/esm/getOrderByFromParser.js +71 -0
  26. package/lib/esm/getSelectFromParser.js +20 -0
  27. package/lib/esm/getTopFromParser.js +33 -0
  28. package/lib/esm/getXQueryFromParser.js +19 -0
  29. package/lib/esm/index.js +9 -0
  30. package/lib/esm/parseOData.js +19 -0
  31. package/lib/esm/validators/atMostOnce.js +17 -0
  32. package/lib/esm/validators/differentFromEmptyString.js +16 -0
  33. package/lib/esm/validators/hasContent.js +17 -0
  34. package/lib/esm/validators/isGuid.js +18 -0
  35. package/lib/esm/validators/recognizedGuid.js +16 -0
  36. package/lib/modern/OData.types.d.js +0 -0
  37. package/lib/modern/getAliasedProperty.js +29 -0
  38. package/lib/modern/getExpandFromParser.js +115 -0
  39. package/lib/modern/getFetchXmlFromParser.js +45 -0
  40. package/lib/modern/getFilterFromParser.js +255 -0
  41. package/lib/modern/getOrderByFromParser.js +72 -0
  42. package/lib/modern/getSelectFromParser.js +20 -0
  43. package/lib/modern/getTopFromParser.js +33 -0
  44. package/lib/modern/getXQueryFromParser.js +19 -0
  45. package/lib/modern/index.js +9 -0
  46. package/lib/modern/parseOData.js +19 -0
  47. package/lib/modern/validators/atMostOnce.js +17 -0
  48. package/lib/modern/validators/differentFromEmptyString.js +16 -0
  49. package/lib/modern/validators/hasContent.js +17 -0
  50. package/lib/modern/validators/isGuid.js +18 -0
  51. package/lib/modern/validators/recognizedGuid.js +16 -0
  52. package/lib/ts3.4/OData.types.d.ts +172 -0
  53. package/lib/ts3.4/getAliasedProperty.d.ts +10 -0
  54. package/lib/ts3.4/getExpandFromParser.d.ts +7 -0
  55. package/lib/ts3.4/getFetchXmlFromParser.d.ts +7 -0
  56. package/lib/ts3.4/getFilterFromParser.d.ts +7 -0
  57. package/lib/ts3.4/getOrderByFromParser.d.ts +7 -0
  58. package/lib/ts3.4/getSelectFromParser.d.ts +7 -0
  59. package/lib/ts3.4/getTopFromParser.d.ts +7 -0
  60. package/lib/ts3.4/getXQueryFromParser.d.ts +8 -0
  61. package/lib/ts3.4/index.d.ts +11 -0
  62. package/lib/ts3.4/parseOData.d.ts +8 -0
  63. package/lib/ts3.4/validators/atMostOnce.d.ts +10 -0
  64. package/lib/ts3.4/validators/differentFromEmptyString.d.ts +9 -0
  65. package/lib/ts3.4/validators/hasContent.d.ts +10 -0
  66. package/lib/ts3.4/validators/isGuid.d.ts +9 -0
  67. package/lib/ts3.4/validators/recognizedGuid.d.ts +9 -0
  68. package/lib/ts3.9/OData.types.d.ts +172 -0
  69. package/lib/ts3.9/getAliasedProperty.d.ts +10 -0
  70. package/lib/ts3.9/getExpandFromParser.d.ts +7 -0
  71. package/lib/ts3.9/getFetchXmlFromParser.d.ts +7 -0
  72. package/lib/ts3.9/getFilterFromParser.d.ts +7 -0
  73. package/lib/ts3.9/getOrderByFromParser.d.ts +7 -0
  74. package/lib/ts3.9/getSelectFromParser.d.ts +7 -0
  75. package/lib/ts3.9/getTopFromParser.d.ts +7 -0
  76. package/lib/ts3.9/getXQueryFromParser.d.ts +8 -0
  77. package/lib/ts3.9/index.d.ts +11 -0
  78. package/lib/ts3.9/parseOData.d.ts +8 -0
  79. package/lib/ts3.9/validators/atMostOnce.d.ts +10 -0
  80. package/lib/ts3.9/validators/differentFromEmptyString.d.ts +9 -0
  81. package/lib/ts3.9/validators/hasContent.d.ts +10 -0
  82. package/lib/ts3.9/validators/isGuid.d.ts +9 -0
  83. package/lib/ts3.9/validators/recognizedGuid.d.ts +9 -0
  84. package/lib/ts4.2/OData.types.d.ts +226 -0
  85. package/lib/ts4.2/getAliasedProperty.d.ts +10 -0
  86. package/lib/ts4.2/getAliasedProperty.d.ts.map +1 -0
  87. package/lib/ts4.2/getExpandFromParser.d.ts +7 -0
  88. package/lib/ts4.2/getExpandFromParser.d.ts.map +1 -0
  89. package/lib/ts4.2/getFetchXmlFromParser.d.ts +7 -0
  90. package/lib/ts4.2/getFetchXmlFromParser.d.ts.map +1 -0
  91. package/lib/ts4.2/getFilterFromParser.d.ts +7 -0
  92. package/lib/ts4.2/getFilterFromParser.d.ts.map +1 -0
  93. package/lib/ts4.2/getOrderByFromParser.d.ts +7 -0
  94. package/lib/ts4.2/getOrderByFromParser.d.ts.map +1 -0
  95. package/lib/ts4.2/getSelectFromParser.d.ts +7 -0
  96. package/lib/ts4.2/getSelectFromParser.d.ts.map +1 -0
  97. package/lib/ts4.2/getTopFromParser.d.ts +7 -0
  98. package/lib/ts4.2/getTopFromParser.d.ts.map +1 -0
  99. package/lib/ts4.2/getXQueryFromParser.d.ts +8 -0
  100. package/lib/ts4.2/getXQueryFromParser.d.ts.map +1 -0
  101. package/lib/ts4.2/index.d.ts +11 -0
  102. package/lib/ts4.2/index.d.ts.map +1 -0
  103. package/lib/ts4.2/parseOData.d.ts +8 -0
  104. package/lib/ts4.2/parseOData.d.ts.map +1 -0
  105. package/lib/ts4.2/validators/atMostOnce.d.ts +10 -0
  106. package/lib/ts4.2/validators/atMostOnce.d.ts.map +1 -0
  107. package/lib/ts4.2/validators/differentFromEmptyString.d.ts +9 -0
  108. package/lib/ts4.2/validators/differentFromEmptyString.d.ts.map +1 -0
  109. package/lib/ts4.2/validators/hasContent.d.ts +10 -0
  110. package/lib/ts4.2/validators/hasContent.d.ts.map +1 -0
  111. package/lib/ts4.2/validators/isGuid.d.ts +9 -0
  112. package/lib/ts4.2/validators/isGuid.d.ts.map +1 -0
  113. package/lib/ts4.2/validators/recognizedGuid.d.ts +9 -0
  114. package/lib/ts4.2/validators/recognizedGuid.d.ts.map +1 -0
  115. package/package.json +1 -1
  116. package/src/OData.types.d.ts +48 -5
  117. package/src/getFilterFromParser.ts +206 -5
@@ -0,0 +1,71 @@
1
+ import { getAliasedProperty } from './getAliasedProperty';
2
+ import { atMostOnce } from './validators/atMostOnce';
3
+ import { hasContent } from './validators/hasContent';
4
+ const option = '$orderby';
5
+
6
+ /**
7
+ * Parses the {@link ODataOrderBy.$orderby $orderby} query
8
+ * @returns Returns `false` when the parse has an error
9
+ */
10
+ export const getOrderByFromParser = (parser, result) => {
11
+ let value = parser.getAll(option);
12
+ if (value.length === 0) {
13
+ return true;
14
+ }
15
+ if (!atMostOnce(option, value, result) || !hasContent(option, value, result)) {
16
+ return false;
17
+ }
18
+ let $orderby = value[0].trimEnd();
19
+ let $orderbyParts = $orderby.split(',');
20
+ const orderByArray = [];
21
+ let position = 0;
22
+ for (const element of $orderbyParts) {
23
+ const parts = Array.from(element.matchAll(/\s*(\S+)/gi));
24
+ if (parts.length > 2) {
25
+ position = position + parts[0][0].length + parts[1][0].length + parts[2][0].length;
26
+ result.error = {
27
+ code: '0x80060888',
28
+ message: `Syntax error at position ${position} in '${$orderby}'.`
29
+ };
30
+ return false;
31
+ }
32
+ if (!/^[@a-zA-Z]\w+/gi.test(parts[0][1])) {
33
+ position = position + parts[0][0].length;
34
+ result.error = {
35
+ code: '0x80060888',
36
+ message: `Syntax error at position ${position} in '${$orderby}'.`
37
+ };
38
+ return false;
39
+ }
40
+ const orderBy = {
41
+ column: parts[0][1],
42
+ asc: true // default is ascending
43
+ };
44
+ if (parts[0][1].startsWith('@')) {
45
+ orderBy.column = getAliasedProperty(parser, result, parts[0][1]);
46
+ if (!orderBy.column) {
47
+ return false;
48
+ }
49
+ }
50
+ if (parts.length === 1) {
51
+ orderByArray.push(orderBy);
52
+ continue;
53
+ }
54
+ if (parts[1][1].toLowerCase() === 'asc') {
55
+ orderBy.asc = true;
56
+ orderByArray.push(orderBy);
57
+ } else if (parts[1][1].toLowerCase() === 'desc') {
58
+ orderBy.asc = false;
59
+ orderByArray.push(orderBy);
60
+ } else {
61
+ position = position + parts[0][0].length + parts[1][0].length;
62
+ result.error = {
63
+ code: '0x80060888',
64
+ message: `Syntax error at position ${position} in '${$orderby}'.`
65
+ };
66
+ return false;
67
+ }
68
+ }
69
+ result.$orderby = orderByArray;
70
+ return true;
71
+ };
@@ -0,0 +1,20 @@
1
+ import { atMostOnce } from './validators/atMostOnce';
2
+ const option = '$select';
3
+
4
+ /**
5
+ * Parses the {@link ODataSelect.$select $select} query
6
+ * @returns {boolean} Returns `false` when the parse has an error
7
+ */
8
+ export const getSelectFromParser = (parser, result) => {
9
+ const value = parser.getAll(option);
10
+ if (value.length === 0) {
11
+ return true;
12
+ }
13
+ if (!atMostOnce(option, value, result)) {
14
+ return false;
15
+ }
16
+ if (value.length > 0) {
17
+ result.$select = value[0].split(',');
18
+ }
19
+ return true;
20
+ };
@@ -0,0 +1,33 @@
1
+ import { atMostOnce } from './validators/atMostOnce';
2
+ import { hasContent } from './validators/hasContent';
3
+ const option = '$top';
4
+
5
+ /**
6
+ * Parses the {@link ODataTop.$top $top} query
7
+ * @returns Returns `false` when the parse has an error
8
+ */
9
+ export const getTopFromParser = (parser, result) => {
10
+ const value = parser.getAll(option);
11
+ if (value.length === 0) {
12
+ return true;
13
+ }
14
+ if (!atMostOnce(option, value, result) || !hasContent(option, value, result)) {
15
+ return false;
16
+ }
17
+ let $top;
18
+ if (!value[0].match(/^\d+$/) || ($top = parseInt(value[0])) < 0) {
19
+ result.error = {
20
+ code: '0x0',
21
+ message: `Invalid value '${value}' for $top query option found. The $top query option requires a non-negative integer value.`
22
+ };
23
+ return false;
24
+ } else if ($top === 0) {
25
+ result.error = {
26
+ code: '0x0',
27
+ message: `Invalid value for $top query option.`
28
+ };
29
+ return false;
30
+ }
31
+ result.$top = $top;
32
+ return true;
33
+ };
@@ -0,0 +1,19 @@
1
+ import { isGuid } from './validators/isGuid';
2
+ import { recognizedGuid } from './validators/recognizedGuid';
3
+
4
+ /**
5
+ * Parses the {@link ODataSavedQuery.savedQuery savedQuery} or
6
+ * {@link ODataUserQuery.userQuery userQuery} query
7
+ * @returns Returns `false` when the parse has an error
8
+ */
9
+ export const getXQueryFromParser = (X, parser, result) => {
10
+ const value = parser.getAll(X);
11
+ if (value.length === 0) {
12
+ return true;
13
+ }
14
+ if (!recognizedGuid(value, result) || !isGuid(value, result)) {
15
+ return false;
16
+ }
17
+ result[X] = value[0];
18
+ return true;
19
+ };
@@ -0,0 +1,9 @@
1
+ export { getExpandFromParser } from './getExpandFromParser';
2
+ export { getFetchXmlFromParser } from './getFetchXmlFromParser';
3
+ export { getOrderByFromParser } from './getOrderByFromParser';
4
+ export { getSelectFromParser } from './getSelectFromParser';
5
+ export { getTopFromParser } from './getTopFromParser';
6
+ export { getXQueryFromParser } from './getXQueryFromParser';
7
+ export { parseOData } from './parseOData';
8
+ import { parseOData } from './parseOData';
9
+ export default parseOData;
@@ -0,0 +1,19 @@
1
+ import { getTopFromParser } from './getTopFromParser';
2
+ import { getSelectFromParser } from './getSelectFromParser';
3
+ import { getExpandFromParser } from './getExpandFromParser';
4
+ import { getFetchXmlFromParser } from './getFetchXmlFromParser';
5
+ import { getXQueryFromParser } from './getXQueryFromParser';
6
+ import { getOrderByFromParser } from './getOrderByFromParser';
7
+ import { getFilterFromParser } from './getFilterFromParser';
8
+
9
+ /**
10
+ * parses the OData query and applies some Dataverse validations
11
+ * @param query The OData query
12
+ * @returns The parsed OData query
13
+ */
14
+ export const parseOData = query => {
15
+ const parser = new URLSearchParams(query);
16
+ const result = {};
17
+ getExpandFromParser(parser, result) && getFetchXmlFromParser(parser, result) && getFilterFromParser(parser, result) && getSelectFromParser(parser, result) && getTopFromParser(parser, result) && getXQueryFromParser('savedQuery', parser, result) && getXQueryFromParser('userQuery', parser, result) && getOrderByFromParser(parser, result);
18
+ return result;
19
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Options of this type must be specified at most once.
3
+ * @param option The option being validated (ex. $top)
4
+ * @param value The result of {@link URLSearchParams.prototype.getAll URLSearchParams.getAll}
5
+ * @param result The {@link ODataQuery} to append the error to
6
+ * @returns {boolean} Returns `false` when the parse has an error
7
+ */
8
+ export const atMostOnce = (option, value, result) => {
9
+ if (value.length > 1) {
10
+ result.error = {
11
+ code: '0x0',
12
+ message: `Query option '${option}' was specified more than once, but it must be specified at most once.`
13
+ };
14
+ return false;
15
+ }
16
+ return true;
17
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ *
3
+ * @param value The result of {@link URLSearchParams.getAll getAll}
4
+ * @param result The {@link ODataQuery} to append the error to
5
+ * @returns {boolean} Returns `false` when the parse has an error
6
+ */
7
+ export const differentFromEmptyString = (value, result) => {
8
+ if (value[0] === '') {
9
+ result.error = {
10
+ code: '0x80040203',
11
+ message: 'Expected non-empty string.'
12
+ };
13
+ return false;
14
+ }
15
+ return true;
16
+ };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Options of this type must be specified at most once.
3
+ * @param option The option being validated (ex. $top)
4
+ * @param value The result of {@link URLSearchParams.getAll getAll}
5
+ * @param result The {@link ODataQuery} to append the error to
6
+ * @returns {boolean} Returns `false` when the parse has an error
7
+ */
8
+ export const hasContent = (query, value, result) => {
9
+ if (!value[0].trim()) {
10
+ result.error = {
11
+ code: '0x0',
12
+ message: `The value for OData query '${query}' cannot be empty.`
13
+ };
14
+ return false;
15
+ }
16
+ return true;
17
+ };
@@ -0,0 +1,18 @@
1
+ const guidRegex = /[0-9A-F]{8}\-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/gi;
2
+
3
+ /**
4
+ *
5
+ * @param value The result of {@link URLSearchParams.getAll getAll}
6
+ * @param result The {@link ODataQuery} to append the error to
7
+ * @returns {boolean} Returns `false` when the parse has an error
8
+ */
9
+ export const isGuid = (value, result) => {
10
+ if (!value[0].match(guidRegex)) {
11
+ result.error = {
12
+ code: '0x0',
13
+ message: 'Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).'
14
+ };
15
+ return false;
16
+ }
17
+ return true;
18
+ };
@@ -0,0 +1,16 @@
1
+ /**
2
+ *
3
+ * @param value The result of {@link URLSearchParams.getAll getAll}
4
+ * @param result The {@link ODataQuery} to append the error to
5
+ * @returns {boolean} Returns `false` when the parse has an error
6
+ */
7
+ export const recognizedGuid = (value, result) => {
8
+ if (!value[0].trim()) {
9
+ result.error = {
10
+ code: '0x0',
11
+ message: 'Unrecognized Guid format.'
12
+ };
13
+ return false;
14
+ }
15
+ return true;
16
+ };
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
+ };