@shko.online/dataverse-odata 0.1.3 → 0.1.5

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 (83) hide show
  1. package/.editorconfig +8 -0
  2. package/.github/workflows/publish.yml +44 -0
  3. package/.prettierrc.json +7 -7
  4. package/.releaserc.yaml +23 -23
  5. package/CHANGELOG.md +14 -0
  6. package/LICENSE +21 -0
  7. package/azure-pipelines.yml +32 -0
  8. package/package.json +20 -11
  9. package/src/OData.types.d.ts +35 -6
  10. package/src/getAliasedProperty.ts +34 -0
  11. package/src/getExpandFromParser.ts +24 -13
  12. package/src/getFetchXmlFromParser.ts +48 -45
  13. package/src/getFilterFromParser.ts +23 -0
  14. package/src/getOrderByFromParser.ts +64 -24
  15. package/src/getSelectFromParser.ts +13 -4
  16. package/src/getTopFromParser.ts +25 -21
  17. package/src/getXQueryFromParser.ts +10 -19
  18. package/src/parseOData.ts +12 -18
  19. package/src/validators/atMostOnce.ts +19 -0
  20. package/src/validators/differentFromEmptyString.ts +18 -0
  21. package/src/validators/hasContent.ts +19 -0
  22. package/src/validators/isGuid.ts +20 -0
  23. package/src/validators/recognizedGuid.ts +18 -0
  24. package/lib/cjs/OData.types.d.js +0 -1
  25. package/lib/cjs/getExpandFromParser.js +0 -115
  26. package/lib/cjs/getFetchXmlFromParser.js +0 -50
  27. package/lib/cjs/getOrderByFromParser.js +0 -44
  28. package/lib/cjs/getSelectFromParser.js +0 -18
  29. package/lib/cjs/getTopFromParser.js +0 -36
  30. package/lib/cjs/getXQueryFromParser.js +0 -35
  31. package/lib/cjs/index.js +0 -57
  32. package/lib/cjs/parseOData.js +0 -40
  33. package/lib/cjs/validateNotEmpty.js +0 -17
  34. package/lib/esm/OData.types.d.js +0 -0
  35. package/lib/esm/getExpandFromParser.js +0 -109
  36. package/lib/esm/getFetchXmlFromParser.js +0 -43
  37. package/lib/esm/getOrderByFromParser.js +0 -37
  38. package/lib/esm/getSelectFromParser.js +0 -11
  39. package/lib/esm/getTopFromParser.js +0 -30
  40. package/lib/esm/getXQueryFromParser.js +0 -28
  41. package/lib/esm/index.js +0 -9
  42. package/lib/esm/parseOData.js +0 -34
  43. package/lib/esm/validateNotEmpty.js +0 -10
  44. package/lib/modern/OData.types.d.js +0 -0
  45. package/lib/modern/getExpandFromParser.js +0 -109
  46. package/lib/modern/getFetchXmlFromParser.js +0 -43
  47. package/lib/modern/getOrderByFromParser.js +0 -38
  48. package/lib/modern/getSelectFromParser.js +0 -11
  49. package/lib/modern/getTopFromParser.js +0 -30
  50. package/lib/modern/getXQueryFromParser.js +0 -28
  51. package/lib/modern/index.js +0 -9
  52. package/lib/modern/parseOData.js +0 -34
  53. package/lib/modern/validateNotEmpty.js +0 -10
  54. package/lib/ts3.4/OData.types.d.ts +0 -118
  55. package/lib/ts3.4/getExpandFromParser.d.ts +0 -7
  56. package/lib/ts3.4/getFetchXmlFromParser.d.ts +0 -7
  57. package/lib/ts3.4/getOrderByFromParser.d.ts +0 -7
  58. package/lib/ts3.4/getSelectFromParser.d.ts +0 -7
  59. package/lib/ts3.4/getTopFromParser.d.ts +0 -7
  60. package/lib/ts3.4/getXQueryFromParser.d.ts +0 -8
  61. package/lib/ts3.4/index.d.ts +0 -11
  62. package/lib/ts3.4/parseOData.d.ts +0 -8
  63. package/lib/ts3.4/validateNotEmpty.d.ts +0 -3
  64. package/lib/ts3.9/OData.types.d.ts +0 -154
  65. package/lib/ts3.9/getExpandFromParser.d.ts +0 -7
  66. package/lib/ts3.9/getExpandFromParser.d.ts.map +0 -1
  67. package/lib/ts3.9/getFetchXmlFromParser.d.ts +0 -7
  68. package/lib/ts3.9/getFetchXmlFromParser.d.ts.map +0 -1
  69. package/lib/ts3.9/getOrderByFromParser.d.ts +0 -7
  70. package/lib/ts3.9/getOrderByFromParser.d.ts.map +0 -1
  71. package/lib/ts3.9/getSelectFromParser.d.ts +0 -7
  72. package/lib/ts3.9/getSelectFromParser.d.ts.map +0 -1
  73. package/lib/ts3.9/getTopFromParser.d.ts +0 -7
  74. package/lib/ts3.9/getTopFromParser.d.ts.map +0 -1
  75. package/lib/ts3.9/getXQueryFromParser.d.ts +0 -8
  76. package/lib/ts3.9/getXQueryFromParser.d.ts.map +0 -1
  77. package/lib/ts3.9/index.d.ts +0 -11
  78. package/lib/ts3.9/index.d.ts.map +0 -1
  79. package/lib/ts3.9/parseOData.d.ts +0 -8
  80. package/lib/ts3.9/parseOData.d.ts.map +0 -1
  81. package/lib/ts3.9/validateNotEmpty.d.ts +0 -3
  82. package/lib/ts3.9/validateNotEmpty.d.ts.map +0 -1
  83. package/src/validateNotEmpty.ts +0 -12
package/.editorconfig ADDED
@@ -0,0 +1,8 @@
1
+ root = true
2
+
3
+ [*]
4
+ end_of_line = lf
5
+
6
+ [*.{js,json,ts,tsx,vue,svelte,html}]
7
+ indent_style = space
8
+ indent_size = 2
@@ -0,0 +1,44 @@
1
+ # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
+ # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
+
4
+ name: Semantic Release
5
+
6
+ on:
7
+ push:
8
+ branches: [ main, beta, alpha ]
9
+
10
+ permissions:
11
+ contents: read # for checkout
12
+
13
+ jobs:
14
+ build:
15
+ runs-on: ubuntu-latest
16
+ permissions:
17
+ contents: write # to be able to publish a GitHub release
18
+ issues: write # to be able to comment on released issues
19
+ pull-requests: write # to be able to comment on released pull requests
20
+ id-token: write # to enable use of OIDC for trusted publishing and npm provenance
21
+ steps:
22
+ - name: Checkout
23
+ uses: actions/checkout@v6
24
+ with:
25
+ fetch-depth: 0 # to be able to get all tags for semantic-release
26
+
27
+ - name: Setup Node.js
28
+ uses: actions/setup-node@v6
29
+ with:
30
+ node-version: "lts/*"
31
+
32
+ - name: Update npm
33
+ run: npm install -g npm@latest
34
+
35
+ - name: Install dependencies
36
+ run: npm ci
37
+
38
+ - name: Run tests
39
+ run: npm test
40
+
41
+ - name: Release
42
+ env:
43
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
+ run: npx semantic-release
package/.prettierrc.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
- "semi": true,
3
- "trailingComma": "all",
4
- "singleQuote": true,
5
- "printWidth": 120,
6
- "tabWidth": 4,
7
- "endOfLine":"auto"
8
- }
2
+ "semi": true,
3
+ "trailingComma": "all",
4
+ "singleQuote": true,
5
+ "printWidth": 120,
6
+ "tabWidth": 4,
7
+ "endOfLine": "auto"
8
+ }
package/.releaserc.yaml CHANGED
@@ -1,23 +1,23 @@
1
- plugins:
2
- - '@semantic-release/commit-analyzer'
3
- - '@semantic-release/release-notes-generator'
4
- - '@semantic-release/changelog'
5
- - '@semantic-release/npm'
6
- - '@semantic-release/exec'
7
- - - '@semantic-release/git'
8
- - assets:
9
- - package.json
10
- - CHANGELOG.md
11
- message: |-
12
- chore(release): ${nextRelease.version} [skip ci]
13
-
14
- ${nextRelease.notes}
15
- branches:
16
- - '+([0-9])?(.{+([0-9]),x}).x'
17
- - main
18
- - next
19
- - next-major
20
- - name: beta
21
- prerelease: true
22
- - name: alpha
23
- prerelease: true
1
+ plugins:
2
+ - '@semantic-release/commit-analyzer'
3
+ - '@semantic-release/release-notes-generator'
4
+ - '@semantic-release/changelog'
5
+ - '@semantic-release/npm'
6
+ - '@semantic-release/exec'
7
+ - - '@semantic-release/git'
8
+ - assets:
9
+ - package.json
10
+ - CHANGELOG.md
11
+ message: |-
12
+ chore(release): ${nextRelease.version} [skip ci]
13
+
14
+ ${nextRelease.notes}
15
+ branches:
16
+ - '+([0-9])?(.{+([0-9]),x}).x'
17
+ - main
18
+ - next
19
+ - next-major
20
+ - name: beta
21
+ prerelease: true
22
+ - name: alpha
23
+ prerelease: true
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [0.1.5](https://github.com/Shko-Online/dataverse-odata/compare/v0.1.4...v0.1.5) (2026-03-15)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * Properly implemented $orderBy ([c693298](https://github.com/Shko-Online/dataverse-odata/commit/c693298bcc4cb2a96ebbc39939c60fb5abdd93d4))
7
+
8
+ ## [0.1.4](https://github.com/shko-online/dataverse-odata/compare/v0.1.3...v0.1.4) (2023-02-12)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * added orderby support ([ea3748a](https://github.com/shko-online/dataverse-odata/commit/ea3748a5569d6ce2a086ff14ceb4a99421ecd069))
14
+
1
15
  ## [0.1.3](https://github.com/shko-online/dataverse-odata/compare/v0.1.2...v0.1.3) (2023-02-11)
2
16
 
3
17
 
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Betim Beja and Shko Online LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,32 @@
1
+ pool:
2
+ vmImage: ubuntu-latest
3
+
4
+ steps:
5
+ - task: NodeTool@0
6
+ inputs:
7
+ versionSpec: '24.x'
8
+ displayName: 'Install Node.js'
9
+
10
+ - task: Npm@1
11
+ inputs:
12
+ command: 'install'
13
+ displayName: "Install NPM Dependencies"
14
+
15
+ - task: Npm@1
16
+ inputs:
17
+ command: 'custom'
18
+ customCommand: 'test'
19
+ displayName: "Test"
20
+
21
+ - task: Npm@1
22
+ inputs:
23
+ command: 'custom'
24
+ customCommand: 'run build'
25
+ displayName: "Build"
26
+
27
+ - task: PublishCodeCoverageResults@2
28
+ displayName: "Publish code coverage results"
29
+ condition: succeededOrFailed()
30
+ inputs:
31
+ codeCoverageTool: Cobertura
32
+ summaryFileLocation: "coverage/cobertura-coverage.xml"
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@shko.online/dataverse-odata",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "This package will help parse OData strings (only the Microsoft Dataverse subset). It can be used as a validator, or you can build some javascript library which consumes the output of this library.",
5
5
  "scripts": {
6
- "build": "npm run lint && node ../scripts/build.js",
6
+ "build": "npm run lint && build-npm-package",
7
7
  "lint": "eslint",
8
8
  "tsc": "tsc --project tsconfig.build.json",
9
- "test": "jest --coverage --maxWorkers=4",
9
+ "test": "jest",
10
+ "test:coverage": "jest --coverage",
10
11
  "semantic-release": "semantic-release"
11
12
  },
12
13
  "exports": {
@@ -26,7 +27,10 @@
26
27
  ]
27
28
  }
28
29
  },
29
- "repository": "https://github.com/shko-online/dataverse-odata",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/Shko-Online/dataverse-odata"
33
+ },
30
34
  "keywords": [
31
35
  "odata",
32
36
  "dataverse",
@@ -55,24 +59,29 @@
55
59
  "@babel/preset-typescript": "^7.18.6",
56
60
  "@emotion/babel-plugin": "^11.10.5",
57
61
  "@jest/globals": "^29.4.2",
58
- "@semantic-release/changelog": "^6.0.2",
59
- "@semantic-release/commit-analyzer": "^9.0.2",
60
- "@semantic-release/exec": "^6.0.3",
62
+ "@semantic-release/changelog": "^6.0.3",
63
+ "@semantic-release/commit-analyzer": "^13.0.1",
64
+ "@semantic-release/exec": "^7.1.0",
61
65
  "@semantic-release/git": "^10.0.1",
62
- "@semantic-release/npm": "^9.0.2",
63
- "@semantic-release/release-notes-generator": "^10.0.3",
66
+ "@semantic-release/release-notes-generator": "^14.1.0",
67
+ "@shko.online/build-npm-package": "^0.1.0",
64
68
  "@storybook/babel-plugin-require-context-hook": "^1.0.1",
69
+ "@types/node": "^24.12.0",
65
70
  "@typescript-eslint/eslint-plugin": "^5.40.1",
66
71
  "@typescript-eslint/parser": "^5.40.1",
67
72
  "babel-plugin-dynamic-import-node": "^2.3.3",
68
73
  "babel-plugin-macros": "^3.1.0",
69
74
  "copyfiles": "^2.4.1",
70
75
  "eslint": "^8.25.0",
76
+ "eslint-plugin-prettier": "^5.5.5",
77
+ "eslint-plugin-react": "^7.37.5",
71
78
  "jest": "^29.2.1",
72
79
  "jest-environment-jsdom": "^29.2.1",
73
- "semantic-release": "^20.1.0",
80
+ "prettier": "^3.8.1",
81
+ "prettier-eslint": "^16.4.2",
82
+ "semantic-release": "^25.0.3",
74
83
  "ts-jest": "^29.0.3",
75
84
  "ts-node": "^10.9.1",
76
- "typescript": "^4.8.4"
85
+ "typescript": "^5.9.3"
77
86
  }
78
87
  }
@@ -30,13 +30,15 @@ type ODataExpandQuery = ODataSelect & ODataExpand;
30
30
 
31
31
  interface ODataFilter {
32
32
  /**
33
- * Use the {@link ODataFilter.$filter $filter} system query option to set criteria for which rows will be returned.
33
+ * Use the {@link $filter $filter} system query option to set criteria for which rows will be returned.
34
34
  *
35
35
  * * Microsoft Docs: {@link https://learn.microsoft.com/power-apps/developer/data-platform/webapi/query-data-web-api?WT.mc_id=DX-MVP-5004767#filter-results Filter results }
36
36
  */
37
- $filter?: StandardOperator;
37
+ $filter?: FilterOperator;
38
38
  }
39
39
 
40
+ type FilterOperator = StandardOperator | ColumnOperator | UnaryOperator | BinaryOperator | QueryFunctionOperator;
41
+
40
42
  interface ODataFetch {
41
43
  /**
42
44
  * You can compose a FetchXML query for a specific table.
@@ -100,7 +102,20 @@ interface ODataUserQuery {
100
102
  userQuery?: string;
101
103
  }
102
104
 
103
- type StandardOperators = 'eq' | 'ne' | 'gt' | 'ge' | 'lt' | 'le';
105
+ type StandardEqualityOperators = 'eq' | 'ne';
106
+
107
+ type StandardOperators = StandardEqualityOperators | 'gt' | 'ge' | 'lt' | 'le';
108
+
109
+ /**
110
+ *
111
+ * * Microsoft Docs: {@link https://learn.microsoft.com/en-us/power-apps/developer/data-platform/webapi/query/filter-rows#column-comparison Column comparison }
112
+ */
113
+ interface ColumnOperator {
114
+ column: string;
115
+ operator: StandardOperators;
116
+ otherColumn: string;
117
+ }
118
+
104
119
 
105
120
  interface StandardOperator {
106
121
  operator: StandardOperators;
@@ -114,15 +129,29 @@ interface StandardOperator {
114
129
  right: string | number;
115
130
  }
116
131
 
132
+ type QueryFunctionOperators = 'contains' | 'endswith' | 'startswith';
133
+
134
+ interface QueryFunctionOperator {
135
+ operator: QueryFunctionOperators;
136
+ /**
137
+ * The left side of the 'X' operator must be a property of the entity.
138
+ */
139
+ left: string;
140
+ /**
141
+ * The right side of the 'X' operator must be a constant value.
142
+ */
143
+ right: string;
144
+ }
145
+
117
146
  interface UnaryOperator {
118
147
  operator: 'not';
119
- right: StandardOperator;
148
+ right: FilterOperator;
120
149
  }
121
150
 
122
151
  interface BinaryOperator {
123
152
  operator: 'and' | 'or';
124
- left: StandardOperator;
125
- right: StandardOperator;
153
+ left: FilterOperator;
154
+ right: FilterOperator;
126
155
  }
127
156
 
128
157
  type ODataQuery = ODataError &
@@ -0,0 +1,34 @@
1
+ import { ODataQuery } from "./OData.types";
2
+
3
+ /**
4
+ * Recursively gets the value of an aliased property. For example, if the query is `$orderby=@p1` and `@p1=name`, this function will return `name`
5
+ * @param parser The URLSearchParams object containing the query parameters
6
+ * @param result Will contain the error details in case there is any
7
+ * @param property The property to expand
8
+ * @returns The expanded property or null when there is an error
9
+ */
10
+ export const getAliasedProperty = (parser: URLSearchParams, result: ODataQuery, property: string): string => {
11
+ let propertyName = parser.get(property);
12
+ if (!propertyName) {
13
+ result.error = {
14
+ code: '0x80060888',
15
+ message: 'Order By Property must be of type EdmProperty',
16
+ };
17
+
18
+ return null;
19
+ }
20
+
21
+ if (!/^[@a-zA-Z]\w+/gi.test(propertyName)) {
22
+ const position = propertyName.length;
23
+ result.error = {
24
+ code: '0x80060888',
25
+ message: `Syntax error at position ${position} in '${propertyName}'.`,
26
+ };
27
+ return null;
28
+ }
29
+
30
+ if (propertyName.startsWith('@')) {
31
+ return getAliasedProperty(parser, result, propertyName);
32
+ }
33
+ return propertyName;
34
+ };
@@ -1,25 +1,33 @@
1
1
  import type { ODataError, ODataExpand, ODataExpandQuery, ODataQuery } from './OData.types';
2
2
 
3
3
  import { getSelectFromParser } from './getSelectFromParser';
4
+ import { atMostOnce } from './validators/atMostOnce';
5
+
6
+ const option = '$expand';
4
7
 
5
8
  /**
6
9
  * Parses the {@link ODataExpand.$expand $expand} query
7
10
  * @returns Returns `false` when the parse has an error
8
11
  */
9
12
  export const getExpandFromParser = (parser: URLSearchParams, result: ODataQuery): boolean => {
10
- const $expand = parser.get('$expand');
11
- if ($expand !== null) {
12
- result.$expand = {};
13
+ const value = parser.getAll(option);
14
+ if (value.length === 0) {
15
+ return true;
16
+ }
17
+ if (!atMostOnce(option, value, result)) {
18
+ return false;
19
+ }
13
20
 
14
- if (!extractExpand($expand, result)) {
15
- return false;
16
- }
21
+ result.$expand = {};
22
+ if (!extractExpand(value[0], result)) {
23
+ return false;
17
24
  }
25
+
18
26
  return true;
19
27
  };
20
28
 
21
29
  const extractExpand = (value: string, $expand: ODataExpand & ODataError) => {
22
- const match = value.match(/^\s*(\w(\w|\d|_)*)\s*(,|\()?\s*/);
30
+ const match = value.match(/^\s*(\w(\w|\d|_)*)\s*(,|\(|\))?\s*/);
23
31
  if (
24
32
  match === null ||
25
33
  (match[0].length < value.length && match[3] === null) ||
@@ -27,7 +35,7 @@ const extractExpand = (value: string, $expand: ODataExpand & ODataError) => {
27
35
  ) {
28
36
  $expand.error = {
29
37
  code: '0x0',
30
- message: 'invalid expand expression',
38
+ message: `Term '${value}' is not valid in a $select or $expand expression.`,
31
39
  };
32
40
  return false;
33
41
  }
@@ -59,14 +67,17 @@ const extractExpand = (value: string, $expand: ODataExpand & ODataError) => {
59
67
  return false;
60
68
  }
61
69
  if (innerExpand.$expand === undefined && innerExpand.$select === undefined) {
62
- $expand.error = { code: '0x0', message: 'Empty expand' };
70
+ $expand.error = {
71
+ code: '0x0',
72
+ 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.`,
73
+ };
63
74
  return false;
64
75
  }
65
76
  $expand.$expand[match[1]] = innerExpand;
66
77
  }
67
78
 
68
79
  matchLength = matchLength + index;
69
- const secondMatch = value.substring(matchLength + 1).match(/\s*(,?)\s*d/);
80
+ const secondMatch = new RegExp(/\s*(,?)\s*d/).exec(value.substring(matchLength + 1));
70
81
  if (secondMatch !== null) {
71
82
  matchLength = matchLength + secondMatch[0].length;
72
83
  if (secondMatch[1] !== null) {
@@ -90,7 +101,7 @@ const getClosingBracket = (value: string): { index: number; error?: string } =>
90
101
  while (depth > 0) {
91
102
  const match = value.substring(startAt).match(/\(|\)/);
92
103
  if (match === null) {
93
- return { error: 'no closing bracket found', index: -1 };
104
+ return { error: 'Found an unbalanced bracket expression.', index: -1 };
94
105
  }
95
106
  if (match[0] === ')') {
96
107
  depth -= 1;
@@ -100,7 +111,7 @@ const getClosingBracket = (value: string): { index: number; error?: string } =>
100
111
  } else {
101
112
  depth += 1;
102
113
  }
103
- startAt = (match.index || 0) + 1;
114
+ startAt += (match.index || 0) + 1;
104
115
  }
105
- return { error: 'no closing bracket found', index: -1 };
116
+ return { error: 'Found an unbalanced bracket expression.', index: -1 };
106
117
  };
@@ -1,56 +1,59 @@
1
1
  import type { ODataQuery, ODataFetch } from './OData.types';
2
+ import { atMostOnce } from './validators/atMostOnce';
3
+ import { differentFromEmptyString } from './validators/differentFromEmptyString';
4
+
5
+ const option = 'fetchXml';
2
6
 
3
7
  /**
4
8
  * Parses the {@link ODataFetch.fetchXml fetchXml} query
5
9
  * @returns Returns `false` when the parse has an error
6
10
  */
7
11
  export const getFetchXmlFromParser = (parser: URLSearchParams, result: ODataQuery): boolean => {
8
- const fetchXml = parser.get('fetchXml');
9
- if (fetchXml !== null) {
10
- if (fetchXml === '') {
11
- result.error = {
12
- code: '0x80040203',
13
- message: 'Expected non-empty string.',
14
- };
15
- return false;
16
- }
12
+ const value = parser.getAll(option);
13
+ if (value.length === 0) {
14
+ return true;
15
+ }
16
+ if (!atMostOnce(option, value, result) || !differentFromEmptyString(value, result)) {
17
+ return false;
18
+ }
17
19
 
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
28
- .evaluate('fetch/entity', fetchXmlDocument, null, XPathResult.ANY_TYPE, null)
29
- .iterateNext() as Element;
30
- if (fetchXmlDocument.documentElement.children.length != 1 || !entity || !entity.getAttribute('name')) {
31
- result.error = {
32
- code: '0x80041102',
33
- message: 'Entity Name was not specified in FetchXml String.',
34
- };
35
- return false;
36
- }
37
- const invalidAttribute = fetchXmlDocument
38
- .evaluate(
39
- 'fetch/entity/*[not(self::filter or self::order or self::link-entity or self::attribute or self::all-attributes or self::no-attrs)]',
40
- fetchXmlDocument,
41
- null,
42
- XPathResult.ANY_TYPE,
43
- null,
44
- )
45
- .iterateNext() as Element;
46
- if (invalidAttribute) {
47
- result.error = {
48
- code: '0x8004111c',
49
- message: `Invalid Child Node, valid nodes are filter, order, link-entity, attribute, all-attributes, no-attrs. NodeName = ${invalidAttribute.tagName} NodeXml = ${invalidAttribute.outerHTML}`,
50
- };
51
- return false;
52
- }
53
- result.fetchXml = fetchXmlDocument;
20
+ const fetchXml = value[0];
21
+ const serializer = new DOMParser();
22
+ const fetchXmlDocument = serializer.parseFromString(fetchXml, 'text/xml');
23
+ if (fetchXmlDocument.documentElement.tagName === 'parsererror') {
24
+ result.error = {
25
+ code: '0x80040201',
26
+ message: 'Invalid XML.',
27
+ };
28
+ return false;
54
29
  }
30
+ const entity = fetchXmlDocument
31
+ .evaluate('fetch/entity', fetchXmlDocument, null, XPathResult.ANY_TYPE, null)
32
+ .iterateNext() as Element;
33
+ if (fetchXmlDocument.documentElement.children.length != 1 || !entity || !entity.getAttribute('name')) {
34
+ result.error = {
35
+ code: '0x80041102',
36
+ message: 'Entity Name was not specified in FetchXml String.',
37
+ };
38
+ return false;
39
+ }
40
+ const invalidAttribute = fetchXmlDocument
41
+ .evaluate(
42
+ 'fetch/entity/*[not(self::filter or self::order or self::link-entity or self::attribute or self::all-attributes or self::no-attrs)]',
43
+ fetchXmlDocument,
44
+ null,
45
+ XPathResult.ANY_TYPE,
46
+ null,
47
+ )
48
+ .iterateNext() as Element;
49
+ if (invalidAttribute) {
50
+ result.error = {
51
+ code: '0x8004111c',
52
+ message: `Invalid Child Node, valid nodes are filter, order, link-entity, attribute, all-attributes, no-attrs. NodeName = ${invalidAttribute.tagName} NodeXml = ${invalidAttribute.outerHTML}`,
53
+ };
54
+ return false;
55
+ }
56
+ result.fetchXml = fetchXmlDocument;
57
+
55
58
  return true;
56
59
  };
@@ -0,0 +1,23 @@
1
+ import type { ODataQuery, ODataFilter } from './OData.types';
2
+ import { atMostOnce } from './validators/atMostOnce';
3
+
4
+ const option = '$filter';
5
+
6
+ /**
7
+ * Parses the {@link ODataFilter.$filter $filter} query
8
+ * @returns {boolean} Returns `false` when the parse has an error
9
+ */
10
+ export const getFilterFromParser = (parser: URLSearchParams, result: ODataQuery): boolean => {
11
+ const value = parser.getAll(option);
12
+ if (value.length === 0) {
13
+ return true;
14
+ }
15
+ if (!atMostOnce(option, value, result)) {
16
+ return false;
17
+ }
18
+ if (value.length > 0) {
19
+
20
+ result.$filter = {operator: 'eq', left: '', right: ''};
21
+ }
22
+ return true;
23
+ };