@apidevtools/json-schema-ref-parser 11.4.2 → 11.5.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.
@@ -1,9 +1,9 @@
1
1
  import $Ref from "./ref.js";
2
2
  import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
3
- import type { JSONSchema } from "./types/index.js";
4
3
  import type $RefParserOptions from "./options.js";
5
- interface $RefsMap {
6
- [url: string]: $Ref;
4
+ import type { JSONSchema } from "./types";
5
+ interface $RefsMap<S> {
6
+ [url: string]: $Ref<S>;
7
7
  }
8
8
  /**
9
9
  * When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
@@ -12,7 +12,7 @@ interface $RefsMap {
12
12
  *
13
13
  * See https://apitools.dev/json-schema-ref-parser/docs/refs.html
14
14
  */
15
- export default class $Refs {
15
+ export default class $Refs<S = JSONSchema> {
16
16
  /**
17
17
  * This property is true if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify() does not support circular references by default.
18
18
  *
@@ -34,7 +34,7 @@ export default class $Refs {
34
34
  *
35
35
  * @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
36
36
  */
37
- values(...types: string[]): JSONSchema;
37
+ values(...types: string[]): S;
38
38
  /**
39
39
  * Returns `true` if the given path exists in the schema; otherwise, returns `false`
40
40
  *
@@ -57,14 +57,14 @@ export default class $Refs {
57
57
  * @param [options]
58
58
  * @returns - Returns the resolved value
59
59
  */
60
- get(path: string, options?: $RefParserOptions): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type;
60
+ get(path: string, options?: $RefParserOptions<S>): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type;
61
61
  /**
62
62
  * Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
63
63
  *
64
- * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
64
+ * @param path The JSON Reference path, optionally with a JSON Pointer in the hash
65
65
  * @param value The value to assign. Can be anything (object, string, number, etc.)
66
66
  */
67
- set(path: any, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type): void;
67
+ set(path: string, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type): void;
68
68
  /**
69
69
  * Returns the specified {@link $Ref} object, or undefined.
70
70
  *
@@ -72,13 +72,13 @@ export default class $Refs {
72
72
  * @returns
73
73
  * @protected
74
74
  */
75
- _get$Ref(path: any): $Ref;
75
+ _get$Ref(path: any): $Ref<S>;
76
76
  /**
77
77
  * Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
78
78
  *
79
79
  * @param path - The file path or URL of the referenced file
80
80
  */
81
- _add(path: string): $Ref;
81
+ _add(path: string): $Ref<S>;
82
82
  /**
83
83
  * Resolves the given JSON reference.
84
84
  *
@@ -88,21 +88,21 @@ export default class $Refs {
88
88
  * @returns
89
89
  * @protected
90
90
  */
91
- _resolve(path: string, pathFromRoot: string, options?: any): import("./pointer.js").default | null;
91
+ _resolve(path: string, pathFromRoot: string, options?: any): import("./pointer.js").default<S> | null;
92
92
  /**
93
93
  * A map of paths/urls to {@link $Ref} objects
94
94
  *
95
95
  * @type {object}
96
96
  * @protected
97
97
  */
98
- _$refs: $RefsMap;
98
+ _$refs: $RefsMap<S>;
99
99
  /**
100
100
  * The {@link $Ref} object that is the root of the JSON schema.
101
101
  *
102
102
  * @type {$Ref}
103
103
  * @protected
104
104
  */
105
- _root$Ref: $Ref;
105
+ _root$Ref: $Ref<S>;
106
106
  constructor();
107
107
  /**
108
108
  * Returns the paths of all the files/URLs that are referenced by the JSON schema,
@@ -122,6 +122,6 @@ export default class $Refs {
122
122
  *
123
123
  * @returns {object}
124
124
  */
125
- toJSON: (...types: string[]) => JSONSchema;
125
+ toJSON: (...types: string[]) => S;
126
126
  }
127
127
  export {};
package/dist/lib/refs.js CHANGED
@@ -102,7 +102,7 @@ class $Refs {
102
102
  /**
103
103
  * Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
104
104
  *
105
- * @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
105
+ * @param path The JSON Reference path, optionally with a JSON Pointer in the hash
106
106
  * @param value The value to assign. Can be anything (object, string, number, etc.)
107
107
  */
108
108
  set(path, value) {
@@ -1,6 +1,6 @@
1
- import type { Options } from "./options.js";
1
+ import type { Options, ParserOptions } from "./options.js";
2
+ import type { JSONSchema } from "./types/index.js";
2
3
  import type $RefParser from "./index.js";
3
- export default resolveExternal;
4
4
  /**
5
5
  * Crawls the JSON schema, finds all external JSON references, and resolves their values.
6
6
  * This method does not mutate the JSON schema. The resolved values are added to {@link $RefParser#$refs}.
@@ -11,4 +11,5 @@ export default resolveExternal;
11
11
  * The promise resolves once all JSON references in the schema have been resolved,
12
12
  * including nested references that are contained in externally-referenced files.
13
13
  */
14
- declare function resolveExternal(parser: $RefParser, options: Options): Promise<void> | Promise<any[]>;
14
+ declare function resolveExternal<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(parser: $RefParser<S, O>, options: Options): Promise<void> | Promise<any[]>;
15
+ export default resolveExternal;
@@ -31,7 +31,6 @@ const pointer_js_1 = __importDefault(require("./pointer.js"));
31
31
  const parse_js_1 = __importDefault(require("./parse.js"));
32
32
  const url = __importStar(require("./util/url.js"));
33
33
  const errors_js_1 = require("./util/errors.js");
34
- exports.default = resolveExternal;
35
34
  /**
36
35
  * Crawls the JSON schema, finds all external JSON references, and resolves their values.
37
36
  * This method does not mutate the JSON schema. The resolved values are added to {@link $RefParser#$refs}.
@@ -107,10 +106,10 @@ async function resolve$Ref($ref, path, $refs, options) {
107
106
  const withoutHash = url.stripHash(resolvedPath);
108
107
  // $ref.$ref = url.relative($refs._root$Ref.path, resolvedPath);
109
108
  // Do we already have this $ref?
110
- $ref = $refs._$refs[withoutHash];
111
- if ($ref) {
109
+ const ref = $refs._$refs[withoutHash];
110
+ if (ref) {
112
111
  // We've already parsed this $ref, so use the existing value
113
- return Promise.resolve($ref.value);
112
+ return Promise.resolve(ref.value);
114
113
  }
115
114
  // Parse the $referenced file/url
116
115
  try {
@@ -131,3 +130,4 @@ async function resolve$Ref($ref, path, $refs, options) {
131
130
  return [];
132
131
  }
133
132
  }
133
+ exports.default = resolveExternal;
@@ -1,3 +1,3 @@
1
- import type { ResolverOptions } from "../types/index.js";
2
- declare const _default: ResolverOptions;
1
+ import type { JSONSchema, ResolverOptions } from "../types/index.js";
2
+ declare const _default: ResolverOptions<JSONSchema>;
3
3
  export default _default;
@@ -1,3 +1,3 @@
1
- import type { HTTPResolverOptions } from "../types/index.js";
2
- declare const _default: HTTPResolverOptions;
1
+ import type { HTTPResolverOptions, JSONSchema } from "../types/index.js";
2
+ declare const _default: HTTPResolverOptions<JSONSchema>;
3
3
  export default _default;
@@ -3,12 +3,12 @@ import type { JSONSchema4, JSONSchema4Object, JSONSchema6, JSONSchema6Object, JS
3
3
  import type $Refs from "../refs.js";
4
4
  export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
5
5
  export type JSONSchemaObject = JSONSchema4Object | JSONSchema6Object | JSONSchema7Object;
6
- export type SchemaCallback = (err: Error | null, schema?: JSONSchema | object | null) => any;
7
- export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any;
6
+ export type SchemaCallback<S = JSONSchema> = (err: Error | null, schema?: S | object | null) => any;
7
+ export type $RefsCallback<S = JSONSchema> = (err: Error | null, $refs?: $Refs<S>) => any;
8
8
  /**
9
9
  * See https://apitools.dev/json-schema-ref-parser/docs/options.html
10
10
  */
11
- export interface HTTPResolverOptions extends Partial<ResolverOptions> {
11
+ export interface HTTPResolverOptions<S = JSONSchema> extends Partial<ResolverOptions<S>> {
12
12
  /**
13
13
  * You can specify any HTTP headers that should be sent when downloading files. For example, some servers may require you to set the `Accept` or `Referrer` header.
14
14
  */
@@ -31,7 +31,7 @@ export interface HTTPResolverOptions extends Partial<ResolverOptions> {
31
31
  *
32
32
  * See https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html
33
33
  */
34
- export interface ResolverOptions {
34
+ export interface ResolverOptions<S = JSONSchema> {
35
35
  name?: string;
36
36
  /**
37
37
  * All resolvers have an order property, even the built-in resolvers. If you don't specify an order property, then your resolver will run last. Specifying `order: 1`, like we did in this example, will make your resolver run first. Or you can squeeze your resolver in-between some of the built-in resolvers. For example, `order: 101` would make it run after the file resolver, but before the HTTP resolver. You can see the order of all the built-in resolvers by looking at their source code.
@@ -48,7 +48,7 @@ export interface ResolverOptions {
48
48
  *
49
49
  * Unlike the `canRead` function, the `read` method can also be asynchronous. This might be important if your resolver needs to read data from a database or some other external source. You can return your asynchronous value using either an ES6 Promise or a Node.js-style error-first callback. Of course, if your resolver has the ability to return its data synchronously, then that's fine too. Here are examples of all three approaches:
50
50
  */
51
- read: string | object | ((file: FileInfo, callback?: (error: Error | null, data: string | null) => any) => string | Buffer | JSONSchema | Promise<string | Buffer | JSONSchema>);
51
+ read: string | object | ((file: FileInfo, callback?: (error: Error | null, data: string | null) => any) => string | Buffer | S | Promise<string | Buffer | S>);
52
52
  }
53
53
  export interface Plugin {
54
54
  name?: string;
@@ -1,4 +1,6 @@
1
1
  import type $RefParser from "../index.js";
2
+ import type { ParserOptions } from "../index.js";
3
+ import type { JSONSchema } from "../index.js";
2
4
  export type JSONParserErrorType = "EUNKNOWN" | "EPARSER" | "EUNMATCHEDPARSER" | "ERESOLVER" | "EUNMATCHEDRESOLVER" | "EMISSINGPOINTER" | "EINVALIDPOINTER";
3
5
  export declare class JSONParserError extends Error {
4
6
  readonly name: string;
@@ -9,10 +11,10 @@ export declare class JSONParserError extends Error {
9
11
  constructor(message: string, source?: string);
10
12
  get footprint(): string;
11
13
  }
12
- export declare class JSONParserErrorGroup extends Error {
13
- files: $RefParser;
14
- constructor(parser: $RefParser);
15
- static getParserErrors(parser: any): JSONParserError[];
14
+ export declare class JSONParserErrorGroup<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions> extends Error {
15
+ files: $RefParser<S, O>;
16
+ constructor(parser: $RefParser<S, O>);
17
+ static getParserErrors<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(parser: $RefParser<S, O>): JSONParserError[];
16
18
  get errors(): Array<JSONParserError | InvalidPointerError | ResolverError | ParserError | MissingPointerError | UnmatchedParserError | UnmatchedResolverError>;
17
19
  }
18
20
  export declare class ParserError extends JSONParserError {
@@ -4,14 +4,13 @@ import type $RefParserOptions from "../options.js";
4
4
  import type { ResolverOptions } from "../types/index.js";
5
5
  import type $Refs from "../refs.js";
6
6
  import type { Plugin } from "../types/index.js";
7
- import type { JSONSchema } from "../types/index.js";
8
7
  /**
9
8
  * Returns the given plugins as an array, rather than an object map.
10
9
  * All other methods in this module expect an array of plugins rather than an object map.
11
10
  *
12
11
  * @returns
13
12
  */
14
- export declare function all(plugins: $RefParserOptions["resolve"]): Plugin[];
13
+ export declare function all<S>(plugins: $RefParserOptions<S>["resolve"]): Plugin[];
15
14
  /**
16
15
  * Filters the given plugins, returning only the ones return `true` for the given method.
17
16
  */
@@ -20,9 +19,9 @@ export declare function filter(plugins: Plugin[], method: any, file: any): Plugi
20
19
  * Sorts the given plugins, in place, by their `order` property.
21
20
  */
22
21
  export declare function sort(plugins: Plugin[]): Plugin[];
23
- export interface PluginResult {
22
+ export interface PluginResult<S> {
24
23
  plugin: Plugin;
25
- result?: string | Buffer | JSONSchema;
24
+ result?: string | Buffer | S;
26
25
  error?: any;
27
26
  }
28
27
  /**
@@ -33,4 +32,4 @@ export interface PluginResult {
33
32
  * If the promise rejects, or the callback is called with an error, then the next plugin is called.
34
33
  * If ALL plugins fail, then the last error is thrown.
35
34
  */
36
- export declare function run(plugins: Plugin[], method: keyof Plugin | keyof ResolverOptions, file: FileInfo, $refs: $Refs): Promise<PluginResult>;
35
+ export declare function run<S>(plugins: Plugin[], method: keyof Plugin | keyof ResolverOptions<S>, file: FileInfo, $refs: $Refs<S>): Promise<PluginResult<S>>;
package/lib/bundle.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  import $Ref from "./ref.js";
2
2
  import Pointer from "./pointer.js";
3
3
  import * as url from "./util/url.js";
4
- import type $RefParserOptions from "./options.js";
5
4
  import type $Refs from "./refs.js";
6
-
7
- export default bundle;
5
+ import type $RefParser from "./index";
6
+ import type { ParserOptions } from "./index";
7
+ import type { JSONSchema } from "./index";
8
8
 
9
9
  /**
10
10
  * Bundles all external JSON references into the main JSON schema, thus resulting in a schema that
@@ -14,12 +14,15 @@ export default bundle;
14
14
  * @param parser
15
15
  * @param options
16
16
  */
17
- function bundle(parser: any, options: any) {
17
+ function bundle<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
18
+ parser: $RefParser<S, O>,
19
+ options: O,
20
+ ) {
18
21
  // console.log('Bundling $ref pointers in %s', parser.$refs._root$Ref.path);
19
22
 
20
23
  // Build an inventory of all $ref pointers in the JSON Schema
21
24
  const inventory: any = [];
22
- crawl(parser, "schema", parser.$refs._root$Ref.path + "#", "#", 0, inventory, parser.$refs, options);
25
+ crawl<S, O>(parser, "schema", parser.$refs._root$Ref.path + "#", "#", 0, inventory, parser.$refs, options);
23
26
 
24
27
  // Remap all $ref pointers
25
28
  remap(inventory);
@@ -32,19 +35,20 @@ function bundle(parser: any, options: any) {
32
35
  * @param key - The property key of `parent` to be crawled
33
36
  * @param path - The full path of the property being crawled, possibly with a JSON Pointer in the hash
34
37
  * @param pathFromRoot - The path of the property being crawled, from the schema root
38
+ * @param indirections
35
39
  * @param inventory - An array of already-inventoried $ref pointers
36
40
  * @param $refs
37
41
  * @param options
38
42
  */
39
- function crawl(
43
+ function crawl<S, O>(
40
44
  parent: any,
41
- key: any,
42
- path: any,
43
- pathFromRoot: any,
44
- indirections: any,
45
- inventory: any,
46
- $refs: any,
47
- options: any,
45
+ key: string | null,
46
+ path: string,
47
+ pathFromRoot: string,
48
+ indirections: number,
49
+ inventory: unknown[],
50
+ $refs: $Refs<S>,
51
+ options: O,
48
52
  ) {
49
53
  const obj = key === null ? parent : parent[key];
50
54
 
@@ -98,15 +102,15 @@ function crawl(
98
102
  * @param $refs
99
103
  * @param options
100
104
  */
101
- function inventory$Ref(
105
+ function inventory$Ref<S, O>(
102
106
  $refParent: any,
103
107
  $refKey: any,
104
108
  path: string,
105
109
  pathFromRoot: any,
106
110
  indirections: any,
107
111
  inventory: any,
108
- $refs: $Refs,
109
- options: $RefParserOptions,
112
+ $refs: $Refs<S>,
113
+ options: O,
110
114
  ) {
111
115
  const $ref = $refKey === null ? $refParent : $refParent[$refKey];
112
116
  const $refPath = url.resolve(path, $ref.$ref);
@@ -248,9 +252,8 @@ function remap(inventory: any) {
248
252
  * TODO
249
253
  */
250
254
  function findInInventory(inventory: any, $refParent: any, $refKey: any) {
251
- for (let i = 0; i < inventory.length; i++) {
252
- const existingEntry = inventory[i];
253
- if (existingEntry.parent === $refParent && existingEntry.key === $refKey) {
255
+ for (const existingEntry of inventory) {
256
+ if (existingEntry && existingEntry.parent === $refParent && existingEntry.key === $refKey) {
254
257
  return existingEntry;
255
258
  }
256
259
  }
@@ -260,3 +263,4 @@ function removeFromInventory(inventory: any, entry: any) {
260
263
  const index = inventory.indexOf(entry);
261
264
  inventory.splice(index, 1);
262
265
  }
266
+ export default bundle;
@@ -3,7 +3,9 @@ import Pointer from "./pointer.js";
3
3
  import { ono } from "@jsdevtools/ono";
4
4
  import * as url from "./util/url.js";
5
5
  import type $Refs from "./refs.js";
6
- import type $RefParserOptions from "./options.js";
6
+ import type { DereferenceOptions, ParserOptions } from "./options.js";
7
+ import type { JSONSchema } from "./types";
8
+ import type $RefParser from "./index";
7
9
 
8
10
  export default dereference;
9
11
 
@@ -14,11 +16,14 @@ export default dereference;
14
16
  * @param parser
15
17
  * @param options
16
18
  */
17
- function dereference(parser: any, options: any) {
19
+ function dereference<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
20
+ parser: $RefParser<S, O>,
21
+ options: O,
22
+ ) {
18
23
  // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
19
- const dereferenced = crawl(
24
+ const dereferenced = crawl<S, O>(
20
25
  parser.schema,
21
- parser.$refs._root$Ref.path,
26
+ parser.$refs._root$Ref.path!,
22
27
  "#",
23
28
  new Set(),
24
29
  new Set(),
@@ -43,15 +48,15 @@ function dereference(parser: any, options: any) {
43
48
  * @param options
44
49
  * @returns
45
50
  */
46
- function crawl(
51
+ function crawl<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
47
52
  obj: any,
48
53
  path: string,
49
54
  pathFromRoot: string,
50
55
  parents: Set<any>,
51
56
  processedObjects: Set<any>,
52
57
  dereferencedCache: any,
53
- $refs: $Refs,
54
- options: $RefParserOptions,
58
+ $refs: $Refs<S>,
59
+ options: O,
55
60
  ) {
56
61
  let dereferenced;
57
62
  const result = {
@@ -59,9 +64,10 @@ function crawl(
59
64
  circular: false,
60
65
  };
61
66
 
62
- const isExcludedPath = options.dereference.excludedPathMatcher || (() => false);
67
+ const derefOptions = (options.dereference || {}) as DereferenceOptions;
68
+ const isExcludedPath = derefOptions.excludedPathMatcher || (() => false);
63
69
 
64
- if (options.dereference.circular === "ignore" || !processedObjects.has(obj)) {
70
+ if (derefOptions?.circular === "ignore" || !processedObjects.has(obj)) {
65
71
  if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
66
72
  parents.add(obj);
67
73
  processedObjects.add(obj);
@@ -106,9 +112,7 @@ function crawl(
106
112
  // Avoid pointless mutations; breaks frozen objects to no profit
107
113
  if (obj[key] !== dereferenced.value) {
108
114
  obj[key] = dereferenced.value;
109
- if (options.dereference.onDereference) {
110
- options.dereference.onDereference(value.$ref, obj[key], obj, key);
111
- }
115
+ derefOptions?.onDereference?.(value.$ref, obj[key], obj, key);
112
116
  }
113
117
  } else {
114
118
  if (!parents.has(value)) {
@@ -157,18 +161,18 @@ function crawl(
157
161
  * @param options
158
162
  * @returns
159
163
  */
160
- function dereference$Ref(
164
+ function dereference$Ref<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
161
165
  $ref: any,
162
166
  path: string,
163
167
  pathFromRoot: string,
164
168
  parents: Set<any>,
165
169
  processedObjects: any,
166
170
  dereferencedCache: any,
167
- $refs: $Refs,
168
- options: $RefParserOptions,
171
+ $refs: $Refs<S>,
172
+ options: O,
169
173
  ) {
170
174
  const isExternalRef = $Ref.isExternal$Ref($ref);
171
- const shouldResolveOnCwd = isExternalRef && options?.dereference.externalReferenceResolution === "root";
175
+ const shouldResolveOnCwd = isExternalRef && options?.dereference?.externalReferenceResolution === "root";
172
176
  const $refPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref);
173
177
 
174
178
  const cache = dereferencedCache.get($refPath);
@@ -225,7 +229,7 @@ function dereference$Ref(
225
229
  dereferencedValue = dereferenced.value;
226
230
  }
227
231
 
228
- if (circular && !directCircular && options.dereference.circular === "ignore") {
232
+ if (circular && !directCircular && options.dereference?.circular === "ignore") {
229
233
  // The user has chosen to "ignore" circular references, so don't change the value
230
234
  dereferencedValue = $ref;
231
235
  }