@swagger-api/apidom-json-pointer 0.75.0 → 0.76.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/es/index.js CHANGED
@@ -1,86 +1,9 @@
1
- import { split, replace, tail, startsWith, map, pipe } from 'ramda';
2
- import { isEmptyString, isInteger, trimCharsStart } from 'ramda-adjunct';
3
- import { isObjectElement, isArrayElement } from '@swagger-api/apidom-core';
4
- import { InvalidJsonPointerError, EvaluationJsonPointerError } from "./errors/index.js";
5
- export { InvalidJsonPointerError, EvaluationJsonPointerError };
6
-
7
- /**
8
- * decodeURIComponent can throw URIError in certain cases like 'c%d'.
9
- * safeDecodeURIComponent is a safe variant of decodeURIComponent that never trows.
10
- *
11
- * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_URI|More info about URIError}
12
- */
13
- const safeDecodeURIComponent = encodedURIComponent => {
14
- try {
15
- return decodeURIComponent(encodedURIComponent);
16
- } catch {
17
- return encodedURIComponent;
18
- }
19
- };
20
-
21
- // escape :: String -> String
22
- export const escape = pipe(replace(/~/g, '~0'), replace(/\//g, '~1'), encodeURIComponent);
23
-
24
- // unescape :: String -> String
25
- export const unescape = pipe(replace(/~1/g, '/'), replace(/~0/g, '~'), safeDecodeURIComponent);
26
-
27
- // parse :: String -> String[]
28
- export const parse = pointer => {
29
- if (isEmptyString(pointer)) {
30
- return [];
31
- }
32
- if (!startsWith('/', pointer)) {
33
- throw new InvalidJsonPointerError(pointer);
34
- }
35
- const tokens = pipe(split('/'), map(unescape))(pointer);
36
- return tail(tokens);
37
- };
38
-
39
- // compile :: String[] -> String
40
- export const compile = tokens => {
41
- if (tokens.length === 0) {
42
- return '';
43
- }
44
- return `/${tokens.map(escape).join('/')}`;
45
- };
46
-
47
- // evaluates JSON Pointer against ApiDOM fragment
48
- export const evaluate = (pointer, element) => {
49
- const tokens = parse(pointer);
50
- return tokens.reduce((acc, token) => {
51
- if (isObjectElement(acc)) {
52
- // @ts-ignore
53
- if (!acc.hasKey(token)) {
54
- throw new EvaluationJsonPointerError(`Evaluation failed on token: "${token}"`);
55
- }
56
- // @ts-ignore
57
- return acc.get(token);
58
- }
59
- if (isArrayElement(acc)) {
60
- if (!(token in acc.content) || !isInteger(Number(token))) {
61
- throw new EvaluationJsonPointerError(`Evaluation failed on token: "${token}"`);
62
- }
63
- // @ts-ignore
64
- return acc.get(Number(token));
65
- }
66
- throw new EvaluationJsonPointerError(`Evaluation failed on token: "${token}"`);
67
- }, element);
68
- };
69
-
70
- /**
71
- * Returns the hash (URL fragment), of the given path.
72
- * If there is no hash, then the root hash ("#") is returned.
73
- */
74
- const getHash = uri => {
75
- const hashIndex = uri.indexOf('#');
76
- if (hashIndex !== -1) {
77
- return uri.substring(hashIndex);
78
- }
79
- return '#';
80
- };
81
-
82
- // uriToPointer :: String -> String
83
- export const uriToPointer = uri => {
84
- const hash = getHash(uri);
85
- return trimCharsStart('#', hash);
86
- };
1
+ export { default as JsonPointerError } from "./errors/JsonPointerError.js";
2
+ export { default as InvalidJsonPointerError } from "./errors/InvalidJsonPointerError.js";
3
+ export { default as CompilationJsonPointerError } from "./errors/CompilationJsonPointerError.js";
4
+ export { default as EvaluationJsonPointerError } from "./errors/EvaluationJsonPointerError.js";
5
+ export { default as escape } from "./escape.js";
6
+ export { default as unescape } from "./unescape.js";
7
+ export { default as parse, uriToPointer } from "./parse.js";
8
+ export { default as compile } from "./compile.js";
9
+ export { default as evaluate } from "./evaluate.js";
package/es/parse.js ADDED
@@ -0,0 +1,42 @@
1
+ import { map, pipe, split, startsWith, tail } from 'ramda';
2
+ import { isEmptyString, trimCharsStart } from 'ramda-adjunct';
3
+ import unescape from "./unescape.js";
4
+ import InvalidJsonPointerError from "./errors/InvalidJsonPointerError.js"; // parse :: String -> String[]
5
+ const parse = pointer => {
6
+ if (isEmptyString(pointer)) {
7
+ return [];
8
+ }
9
+ if (!startsWith('/', pointer)) {
10
+ throw new InvalidJsonPointerError(`Invalid JSON Pointer "${pointer}". JSON Pointers must begin with "/"`, {
11
+ pointer
12
+ });
13
+ }
14
+ try {
15
+ const tokens = pipe(split('/'), map(unescape))(pointer);
16
+ return tail(tokens);
17
+ } catch (error) {
18
+ throw new InvalidJsonPointerError(`JSON Pointer parsing of "${pointer}" encountered an error.`, {
19
+ pointer,
20
+ cause: error
21
+ });
22
+ }
23
+ };
24
+
25
+ /**
26
+ * Returns the hash (URL fragment), of the given path.
27
+ * If there is no hash, then the root hash ("#") is returned.
28
+ */
29
+ const getHash = uri => {
30
+ const hashIndex = uri.indexOf('#');
31
+ if (hashIndex !== -1) {
32
+ return uri.substring(hashIndex);
33
+ }
34
+ return '#';
35
+ };
36
+
37
+ // uriToPointer :: String -> String
38
+ export const uriToPointer = uri => {
39
+ const hash = getHash(uri);
40
+ return trimCharsStart('#', hash);
41
+ };
42
+ export default parse;
package/es/unescape.js ADDED
@@ -0,0 +1,19 @@
1
+ import { pipe, replace } from 'ramda';
2
+
3
+ /**
4
+ * decodeURIComponent can throw URIError in certain cases like 'c%d'.
5
+ * safeDecodeURIComponent is a safe variant of decodeURIComponent that never trows.
6
+ *
7
+ * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Malformed_URI|More info about URIError}
8
+ */
9
+ const safeDecodeURIComponent = encodedURIComponent => {
10
+ try {
11
+ return decodeURIComponent(encodedURIComponent);
12
+ } catch {
13
+ return encodedURIComponent;
14
+ }
15
+ };
16
+
17
+ // unescape :: String -> String
18
+ const unescape = pipe(replace(/~1/g, '/'), replace(/~0/g, '~'), safeDecodeURIComponent);
19
+ export default unescape;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@swagger-api/apidom-json-pointer",
3
- "version": "0.75.0",
3
+ "version": "0.76.0",
4
4
  "description": "Evaluate JSON Pointer expressions against ApiDOM.",
5
5
  "publishConfig": {
6
6
  "access": "public",
@@ -38,7 +38,8 @@
38
38
  "license": "Apache-2.0",
39
39
  "dependencies": {
40
40
  "@babel/runtime-corejs3": "^7.20.7",
41
- "@swagger-api/apidom-core": "^0.75.0",
41
+ "@swagger-api/apidom-core": "^0.76.0",
42
+ "@swagger-api/apidom-error": "^0.76.0",
42
43
  "@types/ramda": "~0.29.3",
43
44
  "ramda": "~0.29.0",
44
45
  "ramda-adjunct": "^4.0.0"
@@ -53,5 +54,5 @@
53
54
  "README.md",
54
55
  "CHANGELOG.md"
55
56
  ],
56
- "gitHead": "1be099d8de39f3fb703cbb5e3f59076e1110fd9b"
57
+ "gitHead": "ce497d1083df34162b9cf2047d6cbd8843af3314"
57
58
  }
package/types/dist.d.ts CHANGED
@@ -1,17 +1,51 @@
1
+ import { ApiDOMStructuredError, ApiDOMErrorOptions } from '@swagger-api/apidom-error';
1
2
  import { Element } from '@swagger-api/apidom-core';
2
3
 
3
- declare class EvaluationJsonPointerError extends Error {
4
+ declare class JsonPointerError extends ApiDOMStructuredError {
4
5
  }
5
6
 
6
- declare class InvalidJsonPointerError extends Error {
7
- constructor(pointer: string);
7
+ interface InvalidJsonPointerErrorOptions extends ApiDOMErrorOptions {
8
+ readonly pointer: string;
9
+ }
10
+ declare class InvalidJsonPointerError extends JsonPointerError {
11
+ readonly pointer: string;
12
+ constructor(message?: string, structuredOptions?: InvalidJsonPointerErrorOptions);
13
+ }
14
+
15
+ interface CompilationJsonPointerErrorOptions extends ApiDOMErrorOptions {
16
+ readonly tokens: string[];
17
+ }
18
+ declare class CompilationJsonPointerError extends JsonPointerError {
19
+ readonly tokens: string[];
20
+ constructor(message?: string, structuredOptions?: CompilationJsonPointerErrorOptions);
21
+ }
22
+
23
+ interface EvaluationJsonPointerErrorOptions<T extends Element> extends ApiDOMErrorOptions {
24
+ readonly pointer: string;
25
+ readonly tokens?: string[];
26
+ readonly failedToken?: string;
27
+ readonly failedTokenPosition?: number;
28
+ readonly element: T;
29
+ }
30
+ declare class EvaluationJsonPointerError<T extends Element> extends JsonPointerError {
31
+ readonly pointer: string;
32
+ readonly tokens?: string[];
33
+ readonly failedToken?: string;
34
+ readonly failedTokenPosition?: number;
35
+ readonly element: string;
36
+ readonly elementSourceMap?: [[number, number, number], [number, number, number]];
37
+ constructor(message?: string, structuredOptions?: EvaluationJsonPointerErrorOptions<T>);
8
38
  }
9
39
 
10
40
  declare const escape: (str: string) => string;
41
+
11
42
  declare const unescape: (str: string) => string;
43
+
12
44
  declare const parse: (pointer: string) => string[];
45
+ declare const uriToPointer: (uri: string) => string;
46
+
13
47
  declare const compile: (tokens: string[]) => string;
48
+
14
49
  declare const evaluate: <T extends Element>(pointer: string, element: T) => Element;
15
- declare const uriToPointer: (uri: string) => string;
16
50
 
17
- export { EvaluationJsonPointerError, InvalidJsonPointerError, compile, escape, evaluate, parse, unescape, uriToPointer };
51
+ export { CompilationJsonPointerError, CompilationJsonPointerErrorOptions, EvaluationJsonPointerError, EvaluationJsonPointerErrorOptions, InvalidJsonPointerError, InvalidJsonPointerErrorOptions, JsonPointerError, compile, escape, evaluate, parse, unescape, uriToPointer };
@@ -1,9 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
4
- exports.__esModule = true;
5
- exports.InvalidJsonPointerError = exports.EvaluationJsonPointerError = void 0;
6
- var _EvaluationJsonPointerError = _interopRequireDefault(require("./EvaluationJsonPointerError.cjs"));
7
- exports.EvaluationJsonPointerError = _EvaluationJsonPointerError.default;
8
- var _InvalidJsonPointerError = _interopRequireDefault(require("./InvalidJsonPointerError.cjs"));
9
- exports.InvalidJsonPointerError = _InvalidJsonPointerError.default;
@@ -1,2 +0,0 @@
1
- export { default as EvaluationJsonPointerError } from "./EvaluationJsonPointerError.js";
2
- export { default as InvalidJsonPointerError } from "./InvalidJsonPointerError.js";