@speclynx/apidom-reference 5.2.2 → 5.2.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 (51) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.md +30 -11
  3. package/dist/apidom-reference.browser.js +1121 -516
  4. package/dist/apidom-reference.browser.min.js +1 -1
  5. package/package.json +28 -28
  6. package/src/bundle/strategies/arazzo-1/visitor.cjs +105 -22
  7. package/src/bundle/strategies/arazzo-1/visitor.mjs +107 -24
  8. package/src/bundle/strategies/openapi-3-1/visitor.cjs +197 -47
  9. package/src/bundle/strategies/openapi-3-1/visitor.mjs +200 -50
  10. package/src/bundle/util.cjs +57 -2
  11. package/src/bundle/util.mjs +52 -0
  12. package/src/dereference/strategies/arazzo-1/index.cjs +9 -7
  13. package/src/dereference/strategies/arazzo-1/index.mjs +3 -1
  14. package/src/dereference/strategies/arazzo-1/selectors/$anchor.cjs +6 -0
  15. package/src/dereference/strategies/arazzo-1/selectors/$anchor.mjs +1 -1
  16. package/src/dereference/strategies/arazzo-1/selectors/uri.cjs +6 -0
  17. package/src/dereference/strategies/arazzo-1/selectors/uri.mjs +1 -1
  18. package/src/dereference/strategies/arazzo-1/util.cjs +6 -0
  19. package/src/dereference/strategies/arazzo-1/util.mjs +1 -1
  20. package/src/dereference/strategies/arazzo-1/visitor.cjs +67 -9
  21. package/src/dereference/strategies/arazzo-1/visitor.mjs +71 -13
  22. package/src/dereference/strategies/openapi-3-1/index.cjs +2 -1
  23. package/src/dereference/strategies/openapi-3-1/index.mjs +3 -1
  24. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.cjs +16 -4
  25. package/src/dereference/strategies/openapi-3-1/selectors/$anchor.mjs +14 -3
  26. package/src/dereference/strategies/openapi-3-1/selectors/json-pointer.cjs +29 -0
  27. package/src/dereference/strategies/openapi-3-1/selectors/json-pointer.mjs +22 -0
  28. package/src/dereference/strategies/openapi-3-1/selectors/uri.cjs +54 -27
  29. package/src/dereference/strategies/openapi-3-1/selectors/uri.mjs +51 -27
  30. package/src/dereference/strategies/openapi-3-1/util.cjs +75 -14
  31. package/src/dereference/strategies/openapi-3-1/util.mjs +68 -14
  32. package/src/dereference/strategies/openapi-3-1/visitor.cjs +85 -9
  33. package/src/dereference/strategies/openapi-3-1/visitor.mjs +89 -13
  34. package/src/util/url.cjs +35 -2
  35. package/src/util/url.mjs +32 -0
  36. package/types/apidom-reference.d.ts +12 -0
  37. package/types/bundle/strategies/arazzo-1/index.d.ts +1 -1
  38. package/types/bundle/strategies/arazzo-1/visitor.d.ts +51 -6
  39. package/types/bundle/strategies/openapi-3-1/index.d.ts +1 -1
  40. package/types/bundle/strategies/openapi-3-1/visitor.d.ts +59 -3
  41. package/types/bundle/util.d.ts +39 -0
  42. package/types/dereference/strategies/arazzo-1/selectors/$anchor.d.ts +1 -1
  43. package/types/dereference/strategies/arazzo-1/selectors/uri.d.ts +1 -1
  44. package/types/dereference/strategies/arazzo-1/util.d.ts +1 -1
  45. package/types/dereference/strategies/arazzo-1/visitor.d.ts +17 -1
  46. package/types/dereference/strategies/openapi-3-1/selectors/$anchor.d.ts +8 -0
  47. package/types/dereference/strategies/openapi-3-1/selectors/json-pointer.d.ts +9 -0
  48. package/types/dereference/strategies/openapi-3-1/selectors/uri.d.ts +37 -4
  49. package/types/dereference/strategies/openapi-3-1/util.d.ts +53 -2
  50. package/types/dereference/strategies/openapi-3-1/visitor.d.ts +17 -1
  51. package/types/util/url.d.ts +10 -0
@@ -1,5 +1,6 @@
1
1
  import { Element } from '@speclynx/apidom-datamodel';
2
2
  import { type Path } from '@speclynx/apidom-traverse';
3
+ import { type Schema$idIndex } from './selectors/uri.ts';
3
4
  import Reference from '../../../Reference.ts';
4
5
  import { AncestorLineage } from '../../util.ts';
5
6
  import type { ReferenceOptions } from '../../../options/index.ts';
@@ -10,8 +11,10 @@ export interface Arazzo1DereferenceVisitorOptions {
10
11
  readonly reference: Reference;
11
12
  readonly options: ReferenceOptions;
12
13
  readonly indirections?: Element[];
14
+ readonly schema$idIndex?: Schema$idIndex;
13
15
  readonly ancestors?: AncestorLineage<Element>;
14
16
  readonly visited?: WeakSet<Element>;
17
+ readonly ancestorSchema$ids?: string[];
15
18
  }
16
19
  /**
17
20
  * @public
@@ -26,14 +29,27 @@ declare class Arazzo1DereferenceVisitor {
26
29
  */
27
30
  protected readonly baseURI: string;
28
31
  protected readonly options: ReferenceOptions;
32
+ /**
33
+ * Index of `$id`-bearing schemas per document, shared across nested visitors
34
+ * of a single dereference run so it never outlives mutations of the documents.
35
+ */
36
+ protected readonly schema$idIndex: Schema$idIndex;
29
37
  protected readonly visited: WeakSet<Element>;
38
+ /**
39
+ * `$id`s of the JSON Schema elements enclosing the traversal root within its
40
+ * document (outermost first). Empty when the root is the document itself; a
41
+ * nested visitor traversing a fragment is seeded with the `$id`s of the
42
+ * schemas the fragment sits under, so the `$ref`s and `$id`s within resolve
43
+ * against the same base as they would from the document root.
44
+ */
45
+ protected readonly ancestorSchema$ids: string[];
30
46
  /**
31
47
  * Tracks element ancestors across dive-deep traversal boundaries.
32
48
  * Used for cycle detection: if a referenced element is found in
33
49
  * the ancestor lineage, a circular reference is detected.
34
50
  */
35
51
  protected readonly ancestors: AncestorLineage<Element>;
36
- constructor({ reference, options, indirections, ancestors, visited, }: Arazzo1DereferenceVisitorOptions);
52
+ constructor({ reference, options, indirections, schema$idIndex, ancestors, visited, ancestorSchema$ids, }: Arazzo1DereferenceVisitorOptions);
37
53
  protected toAncestorLineage(path: Path<Element>): [AncestorLineage<Element>, Set<Element>];
38
54
  protected toBaseURI(uri: string): string;
39
55
  protected toReference(uri: string): Promise<Reference>;
@@ -1,4 +1,5 @@
1
1
  import { Element } from '@speclynx/apidom-datamodel';
2
+ import { type SchemaLocation } from '../util.ts';
2
3
  import EvaluationJsonSchema$anchorError from '../../../../errors/EvaluationJsonSchema$anchorError.ts';
3
4
  import InvalidJsonSchema$anchorError from '../../../../errors/InvalidJsonSchema$anchorError.ts';
4
5
  /**
@@ -13,10 +14,17 @@ export declare const uriToAnchor: (uri: string) => string;
13
14
  * @public
14
15
  */
15
16
  export declare const parse: (anchor: string) => string;
17
+ /**
18
+ * Locates the schema identified by JSON Schema $anchor within ApiDOM fragment,
19
+ * along with the `$id`s of the schemas enclosing it, the search root included.
20
+ * @public
21
+ */
22
+ export declare const locate: <T extends Element>(anchor: string, element: T) => SchemaLocation;
16
23
  /**
17
24
  * Evaluates JSON Schema $anchor against ApiDOM fragment.
18
25
  * @public
19
26
  */
20
27
  export declare const evaluate: <T extends Element>(anchor: string, element: T) => Element | undefined;
28
+ export type { SchemaLocation } from '../util.ts';
21
29
  export { EvaluationJsonSchema$anchorError, InvalidJsonSchema$anchorError };
22
30
  export { default as JsonSchema$anchorError } from '../../../../errors/JsonSchema$anchorError.ts';
@@ -0,0 +1,9 @@
1
+ import { Element } from '@speclynx/apidom-datamodel';
2
+ import { type JSONPointer } from '@speclynx/apidom-json-pointer';
3
+ import { type SchemaLocation } from '../util.ts';
4
+ /**
5
+ * Locates the element addressed by JSON Pointer within ApiDOM fragment, along
6
+ * with the `$id`s of the schemas enclosing it: the elements the pointer walks
7
+ * through on its way to the target, the search root included.
8
+ */
9
+ export declare const locate: <T extends Element>(element: T, jsonPointer: JSONPointer) => SchemaLocation;
@@ -1,12 +1,45 @@
1
1
  import { Element } from '@speclynx/apidom-datamodel';
2
+ import { JSONSchemaElement } from '@speclynx/apidom-ns-json-schema-2020-12';
2
3
  import EvaluationJsonSchemaUriError from '../../../../errors/EvaluationJsonSchemaUriError.ts';
4
+ import { type SchemaLocation } from '../util.ts';
5
+ /**
6
+ * Index of `$id`-bearing schemas per indexed element (a document root, or the
7
+ * fragment a visitor traverses). Each entry pairs a schema with the `$id`s of its
8
+ * enclosing schema resources (outermost first, including its own). The index is
9
+ * populated once per element and never refreshed, so callers scope it
10
+ * to a single dereference/bundle run instead of sharing it globally. Within a run the
11
+ * `$id` graph of an indexed document is stable: the bundle strategies mutate deep
12
+ * clones and place components only after the entry traversal ends, and the
13
+ * dereference strategies only replace `$ref`-bearing schemas.
14
+ * @public
15
+ */
16
+ export type Schema$idIndex = WeakMap<Element, {
17
+ schema: JSONSchemaElement;
18
+ $ids: string[];
19
+ }[]>;
20
+ /**
21
+ * @public
22
+ */
23
+ export interface EvaluateOptions {
24
+ /**
25
+ * URI the `$id`s of the document resolve against (its retrieval URI or `$self`).
26
+ * Defaults to the evaluated URI itself, which only matches schemas identified by an
27
+ * absolute `$id`; pass the document's base URI to match relative `$id`s correctly.
28
+ */
29
+ readonly baseURI?: string;
30
+ readonly index?: Schema$idIndex;
31
+ }
32
+ /**
33
+ * Locates the schema addressed by JSON Schema $ref containing unknown URI within
34
+ * ApiDOM fragment, along with the `$id`s of the schemas enclosing it.
35
+ * @public
36
+ */
37
+ export declare const locate: <T extends Element>(uri: string, element: T, options?: EvaluateOptions) => SchemaLocation;
3
38
  /**
4
39
  * Evaluates JSON Schema $ref containing unknown URI against ApiDOM fragment.
5
40
  * @public
6
41
  */
7
- export declare const evaluate: {
8
- <T extends Element>(uri: string, element: T): Element | undefined;
9
- cache: WeakMap<WeakKey, any>;
10
- };
42
+ export declare const evaluate: <T extends Element>(uri: string, element: T, options?: EvaluateOptions) => Element | undefined;
43
+ export type { SchemaLocation } from '../util.ts';
11
44
  export { EvaluationJsonSchemaUriError };
12
45
  export { default as JsonSchemaUriError } from '../../../../errors/JsonSchemaUriError.ts';
@@ -1,13 +1,64 @@
1
1
  import { Element } from '@speclynx/apidom-datamodel';
2
+ import type { Path } from '@speclynx/apidom-traverse';
2
3
  import { JSONSchemaElement } from '@speclynx/apidom-ns-json-schema-2020-12';
3
4
  /**
5
+ * A schema located within a document, together with the `$id`s of the JSON Schema
6
+ * elements enclosing it (outermost first; the located schema's own `$id` excluded).
4
7
  * @public
5
8
  */
6
- export declare const resolveSchema$refField: (retrievalURI: string, schemaElement: JSONSchemaElement) => string | undefined;
9
+ export interface SchemaLocation {
10
+ readonly element: Element;
11
+ readonly ancestorSchema$ids: string[];
12
+ }
7
13
  /**
14
+ * Whether the element is a JSON Schema element declaring a `$id`.
15
+ */
16
+ export declare const isJSONSchemaElementWith$id: (element: unknown) => element is JSONSchemaElement;
17
+ /**
18
+ * `$id`s declared by the JSON Schema elements among `nodes`, in order.
19
+ */
20
+ export declare const schema$idsOf: (nodes: readonly unknown[]) => string[];
21
+ /**
22
+ * `$id`s of the JSON Schema elements on `path` (outermost first, the node itself
23
+ * included). The `$id`s are read from the elements themselves rather than from
24
+ * refraction-time metadata, so a `$id` assigned after refraction (e.g. by the
25
+ * bundler) is honored.
26
+ */
27
+ export declare const collectSchema$ids: (path: Path<Element>) => string[];
28
+ /**
29
+ * `$id`s of the schemas enclosing a traversal root that is a fragment detached
30
+ * from its document, read from the `ancestorsSchemaIdentifiers` meta the JSON
31
+ * Schema refractors record. The meta also carries the root's own `$id`, which
32
+ * the ancestor walk contributes itself, so it is dropped. A detached fragment
33
+ * has no live ancestors, so the meta is the only carrier of its lexical
34
+ * context; this is the one reader of it the refractors must keep serving.
35
+ */
36
+ export declare const detachedRootSchema$ids: (root: Element | undefined) => string[];
37
+ /**
38
+ * Resolves a chain of `$id`s (outermost first) against `baseURI`, each `$id`
39
+ * refining the base the next one resolves against.
40
+ */
41
+ export declare const resolveSchema$ids: (baseURI: string, $ids: readonly string[]) => string;
42
+ /**
43
+ * Resolves the base URI of the schema at `path`: `baseURI` (the base URI in
44
+ * effect at the traversal root) refined by each `$id` on the schema's ancestor
45
+ * chain in turn, the schema's own `$id` included.
46
+ */
47
+ export declare const resolveSchemaBaseURI: (baseURI: string, path: Path<Element>) => string;
48
+ /**
49
+ * Resolves the `$ref` of the schema against its base URI (see
50
+ * `resolveSchemaBaseURI`), or returns `undefined` when the schema has no string `$ref`.
51
+ *
52
+ * @public
53
+ */
54
+ export declare const resolveSchema$refField: (schemaBaseURI: string, schemaElement: JSONSchemaElement) => string | undefined;
55
+ /**
56
+ * Resolves the `$id` of the schema against `baseURI`, the base URI in effect
57
+ * where the schema sits, or returns `undefined` when the schema declares no string `$id`.
58
+ *
8
59
  * @public
9
60
  */
10
- export declare const resolveSchema$idField: (retrievalURI: string, schemaElement: JSONSchemaElement) => string | undefined;
61
+ export declare const resolveSchema$idField: (baseURI: string, schemaElement: JSONSchemaElement) => string | undefined;
11
62
  /**
12
63
  * Cached version of SchemaElement.refract.
13
64
  */
@@ -1,5 +1,6 @@
1
1
  import { Element } from '@speclynx/apidom-datamodel';
2
2
  import { type Path } from '@speclynx/apidom-traverse';
3
+ import { type Schema$idIndex } from './selectors/uri.ts';
3
4
  import Reference from '../../../Reference.ts';
4
5
  import { AncestorLineage } from '../../util.ts';
5
6
  import type { ReferenceOptions } from '../../../options/index.ts';
@@ -11,8 +12,10 @@ export interface OpenAPI3_1DereferenceVisitorOptions {
11
12
  readonly options: ReferenceOptions;
12
13
  readonly indirections?: Element[];
13
14
  readonly refractCache?: WeakMap<Element, Element>;
15
+ readonly schema$idIndex?: Schema$idIndex;
14
16
  readonly ancestors?: AncestorLineage<Element>;
15
17
  readonly visited?: WeakSet<Element>;
18
+ readonly ancestorSchema$ids?: string[];
16
19
  }
17
20
  /**
18
21
  * @public
@@ -22,14 +25,27 @@ declare class OpenAPI3_1DereferenceVisitor {
22
25
  protected readonly reference: Reference;
23
26
  protected readonly options: ReferenceOptions;
24
27
  protected readonly refractCache: WeakMap<Element, Element>;
28
+ /**
29
+ * Index of `$id`-bearing schemas per document, shared across nested visitors
30
+ * of a single dereference run so it never outlives mutations of the documents.
31
+ */
32
+ protected readonly schema$idIndex: Schema$idIndex;
25
33
  protected readonly visited: WeakSet<Element>;
34
+ /**
35
+ * `$id`s of the JSON Schema elements enclosing the traversal root within its
36
+ * document (outermost first). Empty when the root is the document itself; a
37
+ * nested visitor traversing a fragment is seeded with the `$id`s of the
38
+ * schemas the fragment sits under, so the `$ref`s and `$id`s within resolve
39
+ * against the same base as they would from the document root.
40
+ */
41
+ protected readonly ancestorSchema$ids: string[];
26
42
  /**
27
43
  * Tracks element ancestors across dive-deep traversal boundaries.
28
44
  * Used for cycle detection: if a referenced element is found in
29
45
  * the ancestor lineage, a circular reference is detected.
30
46
  */
31
47
  protected readonly ancestors: AncestorLineage<Element>;
32
- constructor({ reference, options, indirections, refractCache, ancestors, visited, }: OpenAPI3_1DereferenceVisitorOptions);
48
+ constructor({ reference, options, indirections, refractCache, schema$idIndex, ancestors, visited, ancestorSchema$ids, }: OpenAPI3_1DereferenceVisitorOptions);
33
49
  protected toAncestorLineage(path: Path<Element>): [AncestorLineage<Element>, Set<Element>];
34
50
  protected toBaseURI(uri: string): string;
35
51
  protected toReference(uri: string): Promise<Reference>;
@@ -96,6 +96,16 @@ export declare const cwd: () => string;
96
96
  * @public
97
97
  */
98
98
  export declare const resolve: (from: string, to: string) => string;
99
+ /**
100
+ * Computes the URI reference that resolves to `to` against `from` — the inverse
101
+ * of `resolve`: `resolve(from, relative(from, to)) === to`. The reference is
102
+ * relative to the directory of `from` (its last path segment is the document,
103
+ * not a directory). When the two URIs differ in scheme or authority, or either
104
+ * has no hierarchical path (e.g. a URN), no relative form exists and `to` is
105
+ * returned unchanged.
106
+ * @public
107
+ */
108
+ export declare const relative: (from: string, to: string) => string;
99
109
  /**
100
110
  * Sanitizes/Encodes URI to it's url encoded form.
101
111
  *