navi-hey 1.9.2 → 1.10.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.
|
@@ -2,7 +2,6 @@ import { FieldMapper } from './json_path/FieldMapper.js';
|
|
|
2
2
|
import { FilterMatcher } from './json_path/FilterMatcher.js';
|
|
3
3
|
import { MatchResolver } from './json_path/MatchResolver.js';
|
|
4
4
|
import { MissingParserFields } from '../exceptions/config/MissingParserFields.js';
|
|
5
|
-
import { MissingParserMatch } from '../exceptions/config/MissingParserMatch.js';
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
7
|
* @typedef {object} ExtractedItem
|
|
@@ -28,8 +27,9 @@ class JsonPathParser {
|
|
|
28
27
|
* and remapping fields via `attributes.fields`.
|
|
29
28
|
* @param {string} rawBody The raw response body to extract data from (parsed as JSON).
|
|
30
29
|
* @param {object} attributes The parser attributes.
|
|
31
|
-
* @param {string} attributes.match The dot-notation path (e.g. `data.items`) to the array
|
|
32
|
-
* to extract items from, resolved against the parsed body.
|
|
30
|
+
* @param {string} [attributes.match] The dot-notation path (e.g. `data.items`) to the array
|
|
31
|
+
* to extract items from, resolved against the parsed body. When absent, an empty string, or
|
|
32
|
+
* exactly `'.'`, the parsed body itself is treated as the array of items (root-array form).
|
|
33
33
|
* @param {Array<object>} [attributes.filter] Optional list of AND'ed conditions an item
|
|
34
34
|
* must satisfy to be included. Each condition is either `{ field, equals }` (literal
|
|
35
35
|
* comparison) or `{ field, equals_field }` (field-to-field comparison, both fields read
|
|
@@ -39,13 +39,11 @@ class JsonPathParser {
|
|
|
39
39
|
* @returns {Array<ExtractedItem>} An array of `{ [outputKey]: value, ... }` items, one per
|
|
40
40
|
* matched item that passes `filter` (or all matched items when `filter` is absent). An empty
|
|
41
41
|
* array when no items match or none pass the filter.
|
|
42
|
-
* @throws {MissingParserMatch} If `attributes.match` is absent.
|
|
43
42
|
* @throws {MissingParserFields} If `attributes.fields` is absent.
|
|
44
43
|
* @throws {InvalidParserMatch} If `attributes.match` does not resolve to an array within
|
|
45
|
-
* the parsed body.
|
|
44
|
+
* the parsed body, or the root-array form is used against a non-array body.
|
|
46
45
|
*/
|
|
47
46
|
extract(rawBody, { match, filter, fields } = {}) {
|
|
48
|
-
if (!match) throw new MissingParserMatch();
|
|
49
47
|
if (!fields) throw new MissingParserFields();
|
|
50
48
|
|
|
51
49
|
const parsedBody = JSON.parse(rawBody);
|
|
@@ -8,31 +8,49 @@ import { InvalidParserMatch } from '../../exceptions/config/InvalidParserMatch.j
|
|
|
8
8
|
*/
|
|
9
9
|
class MatchResolver {
|
|
10
10
|
/**
|
|
11
|
-
* @param {string} match The dot-notation path (e.g. `data.items`) to the array
|
|
11
|
+
* @param {string} [match] The dot-notation path (e.g. `data.items`) to the array
|
|
12
12
|
* to resolve, evaluated against the parsed body passed to {@link MatchResolver#resolve}.
|
|
13
|
+
* When absent, an empty string, or exactly `'.'`, the parsed body itself is treated
|
|
14
|
+
* as the array (root-array form).
|
|
13
15
|
*/
|
|
14
16
|
constructor(match) {
|
|
15
17
|
this.match = match;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
/**
|
|
19
|
-
* Resolves this resolver's `match` path against the given parsed body.
|
|
21
|
+
* Resolves this resolver's `match` path against the given parsed body. When
|
|
22
|
+
* `match` is absent, an empty string, or exactly `'.'`, the parsed body itself
|
|
23
|
+
* is returned as the array.
|
|
20
24
|
* @param {object} parsedBody The parsed JSON body to navigate.
|
|
21
|
-
* @returns {Array} The array resolved by navigating `match` against `parsedBody
|
|
25
|
+
* @returns {Array} The array resolved by navigating `match` against `parsedBody`,
|
|
26
|
+
* or `parsedBody` itself for the root-array form.
|
|
22
27
|
* @throws {InvalidParserMatch} If `match` does not resolve to an array within
|
|
23
|
-
* `parsedBody` (a missing intermediate key, or a resolved value that isn't an array)
|
|
28
|
+
* `parsedBody` (a missing intermediate key, or a resolved value that isn't an array),
|
|
29
|
+
* or the root-array form is used against a non-array body.
|
|
24
30
|
*/
|
|
25
31
|
resolve(parsedBody) {
|
|
26
|
-
const resolved = this
|
|
27
|
-
|
|
32
|
+
const resolved = this.#isRootMatch()
|
|
33
|
+
? parsedBody
|
|
34
|
+
: this.match.split('.').reduce((value, key) => {
|
|
35
|
+
if (value === undefined || value === null) return undefined;
|
|
28
36
|
|
|
29
|
-
|
|
30
|
-
|
|
37
|
+
return value[key];
|
|
38
|
+
}, parsedBody);
|
|
31
39
|
|
|
32
|
-
if (!Array.isArray(resolved))
|
|
40
|
+
if (!Array.isArray(resolved)) {
|
|
41
|
+
throw new InvalidParserMatch(this.#isRootMatch() ? '.' : this.match);
|
|
42
|
+
}
|
|
33
43
|
|
|
34
44
|
return resolved;
|
|
35
45
|
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @returns {boolean} `true` when `match` selects the parsed body itself (absent,
|
|
49
|
+
* empty string, or exactly `'.'`).
|
|
50
|
+
*/
|
|
51
|
+
#isRootMatch() {
|
|
52
|
+
return !this.match || this.match === '.';
|
|
53
|
+
}
|
|
36
54
|
}
|
|
37
55
|
|
|
38
56
|
export { MatchResolver };
|