@swaggerexpert/jsonpath 1.0.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/cjs/index.cjs ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.translateEvaluator = exports.parse = exports.JSONPathQueryCST = exports.Grammar = void 0;
5
+ var _grammar = _interopRequireDefault(require("./grammar.cjs"));
6
+ exports.Grammar = _grammar.default;
7
+ var _index = _interopRequireDefault(require("./parse/index.cjs"));
8
+ exports.parse = _index.default;
9
+ var _JSONPathQueryCST = _interopRequireDefault(require("./parse/ast/JSONPathQueryCST.cjs"));
10
+ exports.JSONPathQueryCST = _JSONPathQueryCST.default;
11
+ var _translate = _interopRequireDefault(require("./parse/evaluators/translate.cjs"));
12
+ exports.translateEvaluator = _translate.default;
13
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _apgLite = require("../../apg-lite.cjs");
6
+ var _cst = _interopRequireDefault(require("../callbacks/cst.cjs"));
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ class JSONPathQueryCST extends _apgLite.Ast {
9
+ constructor() {
10
+ super();
11
+
12
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.1.1
13
+ this.callbacks['jsonpath-query'] = (0, _cst.default)('jsonpath-query');
14
+ this.callbacks['segments'] = (0, _cst.default)('segments');
15
+ this.callbacks['B'] = (0, _cst.default)('text');
16
+ this.callbacks['S'] = (0, _cst.default)('text');
17
+
18
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.2.1
19
+ this.callbacks['root-identifier'] = (0, _cst.default)('root-identifier');
20
+
21
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.3
22
+ this.callbacks['selector'] = (0, _cst.default)('selector');
23
+
24
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.3.1.1
25
+ this.callbacks['name-selector'] = (0, _cst.default)('name-selector');
26
+ this.callbacks['string-literal'] = (0, _cst.default)('string-literal');
27
+ this.callbacks['double-quoted'] = (0, _cst.default)('double-quoted');
28
+ this.callbacks['single-quoted'] = (0, _cst.default)('single-quoted');
29
+
30
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.3.2.1
31
+ this.callbacks['wildcard-selector'] = (0, _cst.default)('wildcard-selector');
32
+
33
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.3.3.1
34
+ this.callbacks['index-selector'] = (0, _cst.default)('index-selector');
35
+
36
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.3.4.1
37
+ this.callbacks['slice-selector'] = (0, _cst.default)('slice-selector');
38
+ this.callbacks['start'] = (0, _cst.default)('start');
39
+ this.callbacks['end'] = (0, _cst.default)('end');
40
+ this.callbacks['step'] = (0, _cst.default)('step');
41
+
42
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.3.5.1
43
+ this.callbacks['filter-selector'] = (0, _cst.default)('filter-selector');
44
+ this.callbacks['logical-expr'] = (0, _cst.default)('logical-expr');
45
+ this.callbacks['logical-or-expr'] = (0, _cst.default)('logical-or-expr');
46
+ this.callbacks['logical-and-expr'] = (0, _cst.default)('logical-and-expr');
47
+ this.callbacks['basic-expr'] = (0, _cst.default)('basic-expr');
48
+ this.callbacks['paren-expr'] = (0, _cst.default)('paren-expr');
49
+ this.callbacks['logical-not-op'] = (0, _cst.default)('logical-not-op');
50
+ this.callbacks['test-expr'] = (0, _cst.default)('test-expr');
51
+ this.callbacks['filter-query'] = (0, _cst.default)('filter-query');
52
+ this.callbacks['rel-query'] = (0, _cst.default)('rel-query');
53
+ this.callbacks['current-node-identifier'] = (0, _cst.default)('current-node-identifier');
54
+ this.callbacks['comparison-expr'] = (0, _cst.default)('comparison-expr');
55
+ this.callbacks['literal'] = (0, _cst.default)('literal');
56
+ this.callbacks['comparable'] = (0, _cst.default)('comparable');
57
+ this.callbacks['comparison-op'] = (0, _cst.default)('comparison-op');
58
+ this.callbacks['singular-query'] = (0, _cst.default)('singular-query');
59
+ this.callbacks['rel-singular-query'] = (0, _cst.default)('rel-singular-query');
60
+ this.callbacks['abs-singular-query'] = (0, _cst.default)('abs-singular-query');
61
+ this.callbacks['singular-query-segments'] = (0, _cst.default)('singular-query-segments');
62
+ this.callbacks['name-segment'] = (0, _cst.default)('name-segment');
63
+ this.callbacks['index-segment'] = (0, _cst.default)('index-segment');
64
+ this.callbacks['number'] = (0, _cst.default)('number');
65
+ this.callbacks['true'] = (0, _cst.default)('true');
66
+ this.callbacks['false'] = (0, _cst.default)('false');
67
+ this.callbacks['null'] = (0, _cst.default)('null');
68
+
69
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.4
70
+ this.callbacks['function-name'] = (0, _cst.default)('function-name');
71
+ this.callbacks['function-expr'] = (0, _cst.default)('function-expr');
72
+ this.callbacks['function-argument'] = (0, _cst.default)('function-argument');
73
+
74
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.5
75
+ this.callbacks['segment'] = (0, _cst.default)('segment');
76
+
77
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.5.1.1
78
+ this.callbacks['child-segment'] = (0, _cst.default)('child-segment');
79
+ this.callbacks['bracketed-selection'] = (0, _cst.default)('bracketed-selection');
80
+ this.callbacks['member-name-shorthand'] = (0, _cst.default)('member-name-shorthand');
81
+
82
+ // https://www.rfc-editor.org/rfc/rfc9535#section-2.5.2.1
83
+ this.callbacks['descendant-segment'] = (0, _cst.default)('descendant-segment');
84
+
85
+ // Surrogate named rules
86
+ this.callbacks['dot-prefix'] = (0, _cst.default)('text');
87
+ this.callbacks['double-dot-prefix'] = (0, _cst.default)('text');
88
+ this.callbacks['left-bracket'] = (0, _cst.default)('text');
89
+ this.callbacks['right-bracket'] = (0, _cst.default)('text');
90
+ this.callbacks['comma'] = (0, _cst.default)('text');
91
+ this.callbacks['colon'] = (0, _cst.default)('text');
92
+ this.callbacks['dquote'] = (0, _cst.default)('text');
93
+ this.callbacks['squote'] = (0, _cst.default)('text');
94
+ this.callbacks['questionmark'] = (0, _cst.default)('text');
95
+ this.callbacks['disjunction'] = (0, _cst.default)('text');
96
+ this.callbacks['conjunction'] = (0, _cst.default)('text');
97
+ this.callbacks['left-paren'] = (0, _cst.default)('text');
98
+ this.callbacks['right-paren'] = (0, _cst.default)('text');
99
+ }
100
+ }
101
+ var _default = exports.default = JSONPathQueryCST;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _apgLite = require("../../apg-lite.cjs");
6
+ var _JSONPathParseError = _interopRequireDefault(require("../../errors/JSONPathParseError.cjs"));
7
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
8
+ const cst = ruleName => {
9
+ return (state, chars, phraseIndex, phraseLength, data) => {
10
+ if (!(typeof data === 'object' && data !== null && !Array.isArray(data))) {
11
+ throw new _JSONPathParseError.default("parser's user data must be an object");
12
+ }
13
+ if (!data.stack) {
14
+ data.stack = [];
15
+ data.root = null;
16
+ }
17
+
18
+ // drop the empty nodes
19
+ if (phraseLength === 0) return;
20
+ if (state === _apgLite.identifiers.SEM_PRE) {
21
+ const node = {
22
+ type: ruleName,
23
+ text: _apgLite.utilities.charsToString(chars, phraseIndex, phraseLength),
24
+ start: phraseIndex,
25
+ length: phraseLength,
26
+ children: []
27
+ };
28
+ if (data.stack.length > 0) {
29
+ const parent = data.stack[data.stack.length - 1];
30
+ const isTextWithinTextNode = parent.type === 'text' && node.type === 'text';
31
+ if (!isTextWithinTextNode) {
32
+ parent.children.push(node);
33
+ }
34
+ } else {
35
+ data.root = node;
36
+ }
37
+ data.stack.push(node);
38
+ }
39
+ if (state === _apgLite.identifiers.SEM_POST) {
40
+ data.stack.pop();
41
+ }
42
+ };
43
+ };
44
+ var _default = exports.default = cst;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ const translateEvaluator = (ast, {
6
+ result
7
+ }) => {
8
+ if (!result.success) return null;
9
+ const parts = {};
10
+ ast.translate(parts);
11
+ return parts;
12
+ };
13
+ var _default = exports.default = translateEvaluator;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ exports.__esModule = true;
4
+ exports.default = void 0;
5
+ var _apgLite = require("../apg-lite.cjs");
6
+ var _grammar = _interopRequireDefault(require("../grammar.cjs"));
7
+ var _translate = _interopRequireDefault(require("./evaluators/translate.cjs"));
8
+ var _JSONPathQueryCST = _interopRequireDefault(require("./ast/JSONPathQueryCST.cjs"));
9
+ var _JSONPathParseError = _interopRequireDefault(require("../errors/JSONPathParseError.cjs"));
10
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
11
+ const grammar = new _grammar.default();
12
+ const parse = (jsonPath, {
13
+ ast = new _JSONPathQueryCST.default(),
14
+ evaluator = _translate.default
15
+ } = {}) => {
16
+ if (typeof jsonPath !== 'string') {
17
+ throw new TypeError('JSONPath must be a string');
18
+ }
19
+ try {
20
+ const parser = new _apgLite.Parser();
21
+ parser.ast = ast;
22
+ const result = parser.parse(grammar, 'jsonpath-query', jsonPath);
23
+ const computed = evaluator(ast, {
24
+ result
25
+ });
26
+ return {
27
+ result,
28
+ ast,
29
+ computed
30
+ };
31
+ } catch (error) {
32
+ throw new _JSONPathParseError.default('Unexpected error during JSONPath parsing', {
33
+ cause: error,
34
+ jsonPath
35
+ });
36
+ }
37
+ };
38
+ var _default = exports.default = parse;
@@ -0,0 +1,40 @@
1
+ class JSONPathError extends Error {
2
+ constructor(message, options = undefined) {
3
+ super(message, options);
4
+ this.name = this.constructor.name;
5
+ if (typeof message === 'string') {
6
+ this.message = message;
7
+ }
8
+ if (typeof Error.captureStackTrace === 'function') {
9
+ Error.captureStackTrace(this, this.constructor);
10
+ } else {
11
+ this.stack = new Error(message).stack;
12
+ }
13
+
14
+ /**
15
+ * This needs to stay here until our minimum supported version of Node.js is >= 16.9.0.
16
+ * Node.js is >= 16.9.0 supports error causes natively.
17
+ */
18
+ if (options != null && typeof options === 'object' && Object.prototype.hasOwnProperty.call(options, 'cause') && !('cause' in this)) {
19
+ const {
20
+ cause
21
+ } = options;
22
+ this.cause = cause;
23
+ if (cause instanceof Error && 'stack' in cause) {
24
+ this.stack = `${this.stack}\nCAUSE: ${cause.stack}`;
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Allows to assign arbitrary properties to the error object.
30
+ */
31
+ if (options != null && typeof options === 'object') {
32
+ const {
33
+ cause,
34
+ ...causelessOptions
35
+ } = options;
36
+ Object.assign(this, causelessOptions);
37
+ }
38
+ }
39
+ }
40
+ export default JSONPathError;
@@ -0,0 +1,3 @@
1
+ import JSONPathError from "./JSONPathError.mjs";
2
+ class JSONPathParseError extends JSONPathError {}
3
+ export default JSONPathParseError;