@apidevtools/json-schema-ref-parser 10.0.1 → 11.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.
Files changed (111) hide show
  1. package/README.md +22 -0
  2. package/dist/lib/bundle.d.ts +10 -0
  3. package/dist/lib/bundle.js +266 -0
  4. package/dist/lib/dereference.d.ts +9 -0
  5. package/dist/lib/dereference.js +206 -0
  6. package/dist/lib/index.d.ts +206 -0
  7. package/dist/lib/index.js +206 -0
  8. package/dist/lib/normalize-args.d.ts +10 -0
  9. package/dist/lib/normalize-args.js +42 -0
  10. package/dist/lib/options.d.ts +77 -0
  11. package/dist/lib/options.js +119 -0
  12. package/dist/lib/parse.d.ts +8 -0
  13. package/dist/lib/parse.js +162 -0
  14. package/dist/lib/parsers/binary.d.ts +3 -0
  15. package/dist/lib/parsers/binary.js +35 -0
  16. package/dist/lib/parsers/json.d.ts +3 -0
  17. package/dist/lib/parsers/json.js +46 -0
  18. package/dist/lib/parsers/text.d.ts +3 -0
  19. package/dist/lib/parsers/text.js +42 -0
  20. package/dist/lib/parsers/yaml.d.ts +3 -0
  21. package/dist/lib/parsers/yaml.js +54 -0
  22. package/dist/lib/pointer.d.ts +87 -0
  23. package/dist/lib/pointer.js +256 -0
  24. package/dist/lib/ref.d.ts +180 -0
  25. package/dist/lib/ref.js +238 -0
  26. package/dist/lib/refs.d.ts +127 -0
  27. package/dist/lib/refs.js +222 -0
  28. package/dist/lib/resolve-external.d.ts +14 -0
  29. package/dist/lib/resolve-external.js +133 -0
  30. package/dist/lib/resolvers/file.d.ts +3 -0
  31. package/dist/lib/resolvers/file.js +62 -0
  32. package/dist/lib/resolvers/http.d.ts +3 -0
  33. package/dist/lib/resolvers/http.js +146 -0
  34. package/dist/lib/types/index.d.ts +104 -0
  35. package/dist/lib/types/index.js +2 -0
  36. package/dist/lib/util/convert-path-to-posix.d.ts +1 -0
  37. package/dist/lib/util/convert-path-to-posix.js +14 -0
  38. package/dist/lib/util/errors.d.ts +50 -0
  39. package/dist/lib/util/errors.js +106 -0
  40. package/dist/lib/util/is-windows.d.ts +1 -0
  41. package/dist/lib/util/is-windows.js +6 -0
  42. package/dist/lib/util/maybe.d.ts +3 -0
  43. package/dist/lib/util/maybe.js +24 -0
  44. package/dist/lib/util/next.d.ts +2 -0
  45. package/dist/lib/util/next.js +16 -0
  46. package/dist/lib/util/plugins.d.ts +36 -0
  47. package/dist/lib/util/plugins.js +133 -0
  48. package/dist/lib/util/url.d.ts +94 -0
  49. package/dist/lib/util/url.js +304 -0
  50. package/dist/vite.config.d.ts +2 -0
  51. package/dist/vite.config.js +19 -0
  52. package/lib/{bundle.js → bundle.ts} +106 -101
  53. package/lib/{dereference.js → dereference.ts} +113 -52
  54. package/lib/index.ts +413 -0
  55. package/lib/{normalize-args.js → normalize-args.ts} +7 -14
  56. package/lib/options.ts +202 -0
  57. package/lib/parse.ts +153 -0
  58. package/lib/parsers/binary.ts +39 -0
  59. package/lib/parsers/{json.js → json.ts} +9 -22
  60. package/lib/parsers/text.ts +46 -0
  61. package/lib/parsers/{yaml.js → yaml.ts} +15 -19
  62. package/lib/pointer.ts +296 -0
  63. package/lib/ref.ts +287 -0
  64. package/lib/refs.ts +236 -0
  65. package/lib/{resolve-external.js → resolve-external.ts} +44 -41
  66. package/lib/resolvers/file.ts +40 -0
  67. package/lib/resolvers/http.ts +136 -0
  68. package/lib/tsconfig.json +103 -0
  69. package/lib/types/index.ts +135 -0
  70. package/lib/util/convert-path-to-posix.ts +11 -0
  71. package/lib/util/errors.ts +141 -0
  72. package/lib/util/is-windows.ts +2 -0
  73. package/lib/util/maybe.ts +22 -0
  74. package/lib/util/next.ts +13 -0
  75. package/lib/util/{plugins.js → plugins.ts} +58 -57
  76. package/lib/util/{url.js → url.ts} +92 -91
  77. package/package.json +44 -46
  78. package/cjs/bundle.js +0 -304
  79. package/cjs/dereference.js +0 -258
  80. package/cjs/index.js +0 -603
  81. package/cjs/normalize-args.js +0 -64
  82. package/cjs/options.js +0 -125
  83. package/cjs/package.json +0 -3
  84. package/cjs/parse.js +0 -338
  85. package/cjs/parsers/binary.js +0 -54
  86. package/cjs/parsers/json.js +0 -199
  87. package/cjs/parsers/text.js +0 -61
  88. package/cjs/parsers/yaml.js +0 -239
  89. package/cjs/pointer.js +0 -290
  90. package/cjs/ref.js +0 -333
  91. package/cjs/refs.js +0 -214
  92. package/cjs/resolve-external.js +0 -333
  93. package/cjs/resolvers/file.js +0 -106
  94. package/cjs/resolvers/http.js +0 -184
  95. package/cjs/util/errors.js +0 -401
  96. package/cjs/util/plugins.js +0 -159
  97. package/cjs/util/projectDir.cjs +0 -6
  98. package/cjs/util/url.js +0 -228
  99. package/lib/index.d.ts +0 -496
  100. package/lib/index.js +0 -290
  101. package/lib/options.js +0 -128
  102. package/lib/parse.js +0 -162
  103. package/lib/parsers/binary.js +0 -53
  104. package/lib/parsers/text.js +0 -64
  105. package/lib/pointer.js +0 -293
  106. package/lib/ref.js +0 -292
  107. package/lib/refs.js +0 -196
  108. package/lib/resolvers/file.js +0 -63
  109. package/lib/resolvers/http.js +0 -155
  110. package/lib/util/errors.js +0 -134
  111. package/lib/util/projectDir.cjs +0 -6
@@ -0,0 +1,206 @@
1
+ import $Refs from "./refs.js";
2
+ import { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError } from "./util/errors.js";
3
+ import type { ParserOptions } from "./options.js";
4
+ import type { $RefsCallback, JSONSchema, SchemaCallback } from "./types/index.js";
5
+ export { JSONParserError };
6
+ export { InvalidPointerError };
7
+ export { MissingPointerError };
8
+ export { ResolverError };
9
+ export { ParserError };
10
+ export { UnmatchedParserError };
11
+ export { UnmatchedResolverError };
12
+ type RefParserSchema = string | JSONSchema;
13
+ /**
14
+ * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
15
+ * and provides methods for traversing, manipulating, and dereferencing those references.
16
+ *
17
+ * @class
18
+ */
19
+ export declare class $RefParser {
20
+ /**
21
+ * The parsed (and possibly dereferenced) JSON schema object
22
+ *
23
+ * @type {object}
24
+ * @readonly
25
+ */
26
+ schema: JSONSchema | null;
27
+ /**
28
+ * The resolved JSON references
29
+ *
30
+ * @type {$Refs}
31
+ * @readonly
32
+ */
33
+ $refs: $Refs;
34
+ /**
35
+ * Parses the given JSON schema.
36
+ * This method does not resolve any JSON references.
37
+ * It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
38
+ *
39
+ * @param [path] - The file path or URL of the JSON schema
40
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
41
+ * @param [options] - Options that determine how the schema is parsed
42
+ * @param [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
43
+ * @returns - The returned promise resolves with the parsed JSON schema object.
44
+ */
45
+ parse(schema: RefParserSchema): Promise<JSONSchema>;
46
+ parse(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
47
+ parse(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
48
+ parse(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
49
+ parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
50
+ parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
51
+ static parse(schema: RefParserSchema): Promise<JSONSchema>;
52
+ static parse(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
53
+ static parse(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
54
+ static parse(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
55
+ static parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
56
+ static parse(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
57
+ /**
58
+ * *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
59
+ *
60
+ * Resolves all JSON references (`$ref` pointers) in the given JSON Schema file. If it references any other files/URLs, then they will be downloaded and resolved as well. This method **does not** dereference anything. It simply gives you a `$Refs` object, which is a map of all the resolved references and their values.
61
+ *
62
+ * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#resolveschema-options-callback
63
+ *
64
+ * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
65
+ * @param options (optional)
66
+ * @param callback (optional) A callback that will receive a `$Refs` object
67
+ */
68
+ resolve(schema: RefParserSchema): Promise<$Refs>;
69
+ resolve(schema: RefParserSchema, callback: $RefsCallback): Promise<void>;
70
+ resolve(schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
71
+ resolve(schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
72
+ resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
73
+ resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
74
+ /**
75
+ * *This method is used internally by other methods, such as `bundle` and `dereference`. You probably won't need to call this method yourself.*
76
+ *
77
+ * Resolves all JSON references (`$ref` pointers) in the given JSON Schema file. If it references any other files/URLs, then they will be downloaded and resolved as well. This method **does not** dereference anything. It simply gives you a `$Refs` object, which is a map of all the resolved references and their values.
78
+ *
79
+ * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#resolveschema-options-callback
80
+ *
81
+ * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
82
+ * @param options (optional)
83
+ * @param callback (optional) A callback that will receive a `$Refs` object
84
+ */
85
+ static resolve(schema: RefParserSchema): Promise<$Refs>;
86
+ static resolve(schema: RefParserSchema, callback: $RefsCallback): Promise<void>;
87
+ static resolve(schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
88
+ static resolve(schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
89
+ static resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<$Refs>;
90
+ static resolve(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: $RefsCallback): Promise<void>;
91
+ /**
92
+ * Parses the given JSON schema, resolves any JSON references, and bundles all external references
93
+ * into the main JSON schema. This produces a JSON schema that only has *internal* references,
94
+ * not any *external* references.
95
+ *
96
+ * @param [path] - The file path or URL of the JSON schema
97
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
98
+ * @param [options] - Options that determine how the schema is parsed, resolved, and dereferenced
99
+ * @param [callback] - An error-first callback. The second parameter is the bundled JSON schema object
100
+ * @returns - The returned promise resolves with the bundled JSON schema object.
101
+ */
102
+ /**
103
+ * Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
104
+ *
105
+ * This also eliminates the risk of circular references, so the schema can be safely serialized using `JSON.stringify()`.
106
+ *
107
+ * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundleschema-options-callback
108
+ *
109
+ * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
110
+ * @param options (optional)
111
+ * @param callback (optional) A callback that will receive the bundled schema object
112
+ */
113
+ static bundle(schema: RefParserSchema): Promise<JSONSchema>;
114
+ static bundle(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
115
+ static bundle(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
116
+ static bundle(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
117
+ static bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
118
+ static bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<JSONSchema>;
119
+ /**
120
+ * Parses the given JSON schema, resolves any JSON references, and bundles all external references
121
+ * into the main JSON schema. This produces a JSON schema that only has *internal* references,
122
+ * not any *external* references.
123
+ *
124
+ * @param [path] - The file path or URL of the JSON schema
125
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
126
+ * @param [options] - Options that determine how the schema is parsed, resolved, and dereferenced
127
+ * @param [callback] - An error-first callback. The second parameter is the bundled JSON schema object
128
+ * @returns - The returned promise resolves with the bundled JSON schema object.
129
+ */
130
+ /**
131
+ * Bundles all referenced files/URLs into a single schema that only has internal `$ref` pointers. This lets you split-up your schema however you want while you're building it, but easily combine all those files together when it's time to package or distribute the schema to other people. The resulting schema size will be small, since it will still contain internal JSON references rather than being fully-dereferenced.
132
+ *
133
+ * This also eliminates the risk of circular references, so the schema can be safely serialized using `JSON.stringify()`.
134
+ *
135
+ * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundleschema-options-callback
136
+ *
137
+ * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
138
+ * @param options (optional)
139
+ * @param callback (optional) A callback that will receive the bundled schema object
140
+ */
141
+ bundle(schema: RefParserSchema): Promise<JSONSchema>;
142
+ bundle(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
143
+ bundle(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
144
+ bundle(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
145
+ bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
146
+ bundle(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
147
+ /**
148
+ * Parses the given JSON schema, resolves any JSON references, and dereferences the JSON schema.
149
+ * That is, all JSON references are replaced with their resolved values.
150
+ *
151
+ * @param [path] - The file path or URL of the JSON schema
152
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
153
+ * @param [options] - Options that determine how the schema is parsed, resolved, and dereferenced
154
+ * @param [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
155
+ * @returns - The returned promise resolves with the dereferenced JSON schema object.
156
+ */
157
+ /**
158
+ * Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain any `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references.
159
+ *
160
+ * The dereference method maintains object reference equality, meaning that all `$ref` pointers that point to the same object will be replaced with references to the same object. Again, this is great for programmatic usage, but it does introduce the risk of circular references, so be careful if you intend to serialize the schema using `JSON.stringify()`. Consider using the bundle method instead, which does not create circular references.
161
+ *
162
+ * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferenceschema-options-callback
163
+ *
164
+ * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
165
+ * @param options (optional)
166
+ * @param callback (optional) A callback that will receive the dereferenced schema object
167
+ */
168
+ static dereference(schema: RefParserSchema): Promise<JSONSchema>;
169
+ static dereference(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
170
+ static dereference(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
171
+ static dereference(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
172
+ static dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
173
+ static dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
174
+ /**
175
+ * Parses the given JSON schema, resolves any JSON references, and dereferences the JSON schema.
176
+ * That is, all JSON references are replaced with their resolved values.
177
+ *
178
+ * @param [path] - The file path or URL of the JSON schema
179
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
180
+ * @param [options] - Options that determine how the schema is parsed, resolved, and dereferenced
181
+ * @param [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
182
+ * @returns - The returned promise resolves with the dereferenced JSON schema object.
183
+ */
184
+ /**
185
+ * Dereferences all `$ref` pointers in the JSON Schema, replacing each reference with its resolved value. This results in a schema object that does not contain any `$ref` pointers. Instead, it's a normal JavaScript object tree that can easily be crawled and used just like any other JavaScript object. This is great for programmatic usage, especially when using tools that don't understand JSON references.
186
+ *
187
+ * The dereference method maintains object reference equality, meaning that all `$ref` pointers that point to the same object will be replaced with references to the same object. Again, this is great for programmatic usage, but it does introduce the risk of circular references, so be careful if you intend to serialize the schema using `JSON.stringify()`. Consider using the bundle method instead, which does not create circular references.
188
+ *
189
+ * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferenceschema-options-callback
190
+ *
191
+ * @param schema A JSON Schema object, or the file path or URL of a JSON Schema file. See the `parse` method for more info.
192
+ * @param options (optional)
193
+ * @param callback (optional) A callback that will receive the dereferenced schema object
194
+ */
195
+ dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
196
+ dereference(schema: RefParserSchema, options: ParserOptions, callback: SchemaCallback): Promise<void>;
197
+ dereference(schema: RefParserSchema, callback: SchemaCallback): Promise<void>;
198
+ dereference(baseUrl: string, schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
199
+ dereference(schema: RefParserSchema, options: ParserOptions): Promise<JSONSchema>;
200
+ dereference(schema: RefParserSchema): Promise<JSONSchema>;
201
+ }
202
+ export default $RefParser;
203
+ export declare const parse: typeof $RefParser.parse;
204
+ export declare const resolve: typeof $RefParser.resolve;
205
+ export declare const bundle: typeof $RefParser.bundle;
206
+ export declare const dereference: typeof $RefParser.dereference;
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.dereference = exports.bundle = exports.resolve = exports.parse = exports.$RefParser = exports.UnmatchedResolverError = exports.UnmatchedParserError = exports.ParserError = exports.ResolverError = exports.MissingPointerError = exports.InvalidPointerError = exports.JSONParserError = void 0;
30
+ const refs_js_1 = __importDefault(require("./refs.js"));
31
+ const parse_js_1 = __importDefault(require("./parse.js"));
32
+ const normalize_args_js_1 = __importDefault(require("./normalize-args.js"));
33
+ const resolve_external_js_1 = __importDefault(require("./resolve-external.js"));
34
+ const bundle_js_1 = __importDefault(require("./bundle.js"));
35
+ const dereference_js_1 = __importDefault(require("./dereference.js"));
36
+ const url = __importStar(require("./util/url.js"));
37
+ const errors_js_1 = require("./util/errors.js");
38
+ Object.defineProperty(exports, "JSONParserError", { enumerable: true, get: function () { return errors_js_1.JSONParserError; } });
39
+ Object.defineProperty(exports, "InvalidPointerError", { enumerable: true, get: function () { return errors_js_1.InvalidPointerError; } });
40
+ Object.defineProperty(exports, "MissingPointerError", { enumerable: true, get: function () { return errors_js_1.MissingPointerError; } });
41
+ Object.defineProperty(exports, "ResolverError", { enumerable: true, get: function () { return errors_js_1.ResolverError; } });
42
+ Object.defineProperty(exports, "ParserError", { enumerable: true, get: function () { return errors_js_1.ParserError; } });
43
+ Object.defineProperty(exports, "UnmatchedParserError", { enumerable: true, get: function () { return errors_js_1.UnmatchedParserError; } });
44
+ Object.defineProperty(exports, "UnmatchedResolverError", { enumerable: true, get: function () { return errors_js_1.UnmatchedResolverError; } });
45
+ const ono_1 = require("@jsdevtools/ono");
46
+ const maybe_js_1 = __importDefault(require("./util/maybe.js"));
47
+ /**
48
+ * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
49
+ * and provides methods for traversing, manipulating, and dereferencing those references.
50
+ *
51
+ * @class
52
+ */
53
+ class $RefParser {
54
+ constructor() {
55
+ /**
56
+ * The parsed (and possibly dereferenced) JSON schema object
57
+ *
58
+ * @type {object}
59
+ * @readonly
60
+ */
61
+ this.schema = null;
62
+ /**
63
+ * The resolved JSON references
64
+ *
65
+ * @type {$Refs}
66
+ * @readonly
67
+ */
68
+ this.$refs = new refs_js_1.default();
69
+ }
70
+ async parse() {
71
+ const args = (0, normalize_args_js_1.default)(arguments);
72
+ let promise;
73
+ if (!args.path && !args.schema) {
74
+ const err = (0, ono_1.ono)(`Expected a file path, URL, or object. Got ${args.path || args.schema}`);
75
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
76
+ }
77
+ // Reset everything
78
+ this.schema = null;
79
+ this.$refs = new refs_js_1.default();
80
+ // If the path is a filesystem path, then convert it to a URL.
81
+ // NOTE: According to the JSON Reference spec, these should already be URLs,
82
+ // but, in practice, many people use local filesystem paths instead.
83
+ // So we're being generous here and doing the conversion automatically.
84
+ // This is not intended to be a 100% bulletproof solution.
85
+ // If it doesn't work for your use-case, then use a URL instead.
86
+ let pathType = "http";
87
+ if (url.isFileSystemPath(args.path)) {
88
+ args.path = url.fromFileSystemPath(args.path);
89
+ pathType = "file";
90
+ }
91
+ // Resolve the absolute path of the schema
92
+ args.path = url.resolve(url.cwd(), args.path);
93
+ if (args.schema && typeof args.schema === "object") {
94
+ // A schema object was passed-in.
95
+ // So immediately add a new $Ref with the schema object as its value
96
+ const $ref = this.$refs._add(args.path);
97
+ $ref.value = args.schema;
98
+ $ref.pathType = pathType;
99
+ promise = Promise.resolve(args.schema);
100
+ }
101
+ else {
102
+ // Parse the schema file/url
103
+ promise = (0, parse_js_1.default)(args.path, this.$refs, args.options);
104
+ }
105
+ try {
106
+ const result = await promise;
107
+ if (result !== null && typeof result === "object" && !Buffer.isBuffer(result)) {
108
+ this.schema = result;
109
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
110
+ }
111
+ else if (args.options.continueOnError) {
112
+ this.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
113
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
114
+ }
115
+ else {
116
+ throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
117
+ }
118
+ }
119
+ catch (err) {
120
+ if (!args.options.continueOnError || !(0, errors_js_1.isHandledError)(err)) {
121
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
122
+ }
123
+ if (this.$refs._$refs[url.stripHash(args.path)]) {
124
+ this.$refs._$refs[url.stripHash(args.path)].addError(err);
125
+ }
126
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(null));
127
+ }
128
+ }
129
+ static parse() {
130
+ const parser = new $RefParser();
131
+ return parser.parse.apply(parser, arguments);
132
+ }
133
+ /**
134
+ * Parses the given JSON schema and resolves any JSON references, including references in
135
+ * externally-referenced files.
136
+ *
137
+ * @param [path] - The file path or URL of the JSON schema
138
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
139
+ * @param [options] - Options that determine how the schema is parsed and resolved
140
+ * @param [callback]
141
+ * - An error-first callback. The second parameter is a {@link $Refs} object containing the resolved JSON references
142
+ *
143
+ * @returns
144
+ * The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
145
+ */
146
+ async resolve() {
147
+ const args = (0, normalize_args_js_1.default)(arguments);
148
+ try {
149
+ await this.parse(args.path, args.schema, args.options);
150
+ await (0, resolve_external_js_1.default)(this, args.options);
151
+ finalize(this);
152
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.$refs));
153
+ }
154
+ catch (err) {
155
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
156
+ }
157
+ }
158
+ static resolve() {
159
+ const instance = new $RefParser();
160
+ return instance.resolve.apply(instance, arguments);
161
+ }
162
+ static bundle() {
163
+ const instance = new $RefParser();
164
+ return instance.bundle.apply(instance, arguments);
165
+ }
166
+ async bundle() {
167
+ const args = (0, normalize_args_js_1.default)(arguments);
168
+ try {
169
+ await this.resolve(args.path, args.schema, args.options);
170
+ (0, bundle_js_1.default)(this, args.options);
171
+ finalize(this);
172
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
173
+ }
174
+ catch (err) {
175
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
176
+ }
177
+ }
178
+ static dereference() {
179
+ const instance = new $RefParser();
180
+ return instance.dereference.apply(instance, arguments);
181
+ }
182
+ async dereference() {
183
+ const args = (0, normalize_args_js_1.default)(arguments);
184
+ try {
185
+ await this.resolve(args.path, args.schema, args.options);
186
+ (0, dereference_js_1.default)(this, args.options);
187
+ finalize(this);
188
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
189
+ }
190
+ catch (err) {
191
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
192
+ }
193
+ }
194
+ }
195
+ exports.$RefParser = $RefParser;
196
+ exports.default = $RefParser;
197
+ function finalize(parser) {
198
+ const errors = errors_js_1.JSONParserErrorGroup.getParserErrors(parser);
199
+ if (errors.length > 0) {
200
+ throw new errors_js_1.JSONParserErrorGroup(parser);
201
+ }
202
+ }
203
+ exports.parse = $RefParser.parse;
204
+ exports.resolve = $RefParser.resolve;
205
+ exports.bundle = $RefParser.bundle;
206
+ exports.dereference = $RefParser.dereference;
@@ -0,0 +1,10 @@
1
+ export default normalizeArgs;
2
+ /**
3
+ * Normalizes the given arguments, accounting for optional args.
4
+ */
5
+ declare function normalizeArgs(_args: Partial<IArguments>): {
6
+ path: string;
7
+ schema: any;
8
+ options: import("./options.js").default;
9
+ callback: any;
10
+ };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const options_js_1 = require("./options.js");
4
+ exports.default = normalizeArgs;
5
+ /**
6
+ * Normalizes the given arguments, accounting for optional args.
7
+ */
8
+ function normalizeArgs(_args) {
9
+ let path, schema, options, callback;
10
+ const args = Array.prototype.slice.call(_args);
11
+ if (typeof args[args.length - 1] === "function") {
12
+ // The last parameter is a callback function
13
+ callback = args.pop();
14
+ }
15
+ if (typeof args[0] === "string") {
16
+ // The first parameter is the path
17
+ path = args[0];
18
+ if (typeof args[2] === "object") {
19
+ // The second parameter is the schema, and the third parameter is the options
20
+ schema = args[1];
21
+ options = args[2];
22
+ }
23
+ else {
24
+ // The second parameter is the options
25
+ schema = undefined;
26
+ options = args[1];
27
+ }
28
+ }
29
+ else {
30
+ // The first parameter is the schema
31
+ path = "";
32
+ schema = args[0];
33
+ options = args[1];
34
+ }
35
+ options = (0, options_js_1.getNewOptions)(options);
36
+ return {
37
+ path,
38
+ schema,
39
+ options,
40
+ callback,
41
+ };
42
+ }
@@ -0,0 +1,77 @@
1
+ import type { HTTPResolverOptions, JSONSchemaObject, Plugin, ResolverOptions } from "./types/index.js";
2
+ type DeepPartial<T> = T extends object ? {
3
+ [P in keyof T]?: DeepPartial<T[P]>;
4
+ } : T;
5
+ /**
6
+ * Options that determine how JSON schemas are parsed, resolved, and dereferenced.
7
+ *
8
+ * @param [options] - Overridden options
9
+ * @class
10
+ */
11
+ interface $RefParserOptions {
12
+ /**
13
+ * The `parse` options determine how different types of files will be parsed.
14
+ *
15
+ * JSON Schema `$Ref` Parser comes with built-in JSON, YAML, plain-text, and binary parsers, any of which you can configure or disable. You can also add your own custom parsers if you want.
16
+ */
17
+ parse: {
18
+ json?: Plugin | boolean;
19
+ yaml?: Plugin | boolean;
20
+ binary?: Plugin | boolean;
21
+ text?: (Plugin & {
22
+ encoding?: string;
23
+ }) | boolean;
24
+ [key: string]: Plugin | boolean | undefined;
25
+ };
26
+ /**
27
+ * The `resolve` options control how JSON Schema $Ref Parser will resolve file paths and URLs, and how those files will be read/downloaded.
28
+ *
29
+ * JSON Schema `$Ref` Parser comes with built-in support for HTTP and HTTPS, as well as support for local files (when running in Node.js). You can configure or disable either of these built-in resolvers. You can also add your own custom resolvers if you want.
30
+ */
31
+ resolve: {
32
+ /**
33
+ * Determines whether external $ref pointers will be resolved. If this option is disabled, then external `$ref` pointers will simply be ignored.
34
+ */
35
+ external?: boolean;
36
+ file?: Partial<ResolverOptions> | boolean;
37
+ http?: HTTPResolverOptions | boolean;
38
+ } & {
39
+ [key: string]: Partial<ResolverOptions> | HTTPResolverOptions | boolean | undefined;
40
+ };
41
+ /**
42
+ * By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
43
+ * causes it to keep processing as much as possible and then throw a single error that contains all errors
44
+ * that were encountered.
45
+ */
46
+ continueOnError: boolean;
47
+ /**
48
+ * The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
49
+ */
50
+ dereference: {
51
+ /**
52
+ * Determines whether circular `$ref` pointers are handled.
53
+ *
54
+ * If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
55
+ *
56
+ * If set to `"ignore"`, then circular references will simply be ignored. No error will be thrown, but the `$Refs.circular` property will still be set to `true`.
57
+ */
58
+ circular?: boolean | "ignore";
59
+ /**
60
+ * A function, called for each path, which can return true to stop this path and all
61
+ * subpaths from being dereferenced further. This is useful in schemas where some
62
+ * subpaths contain literal $ref keys that should not be dereferenced.
63
+ */
64
+ excludedPathMatcher?(path: string): boolean;
65
+ /**
66
+ * Callback invoked during dereferencing.
67
+ *
68
+ * @argument {string} path The path being dereferenced (ie. the `$ref` string).
69
+ * @argument {JSONSchemaObject} object The JSON-Schema that the `$ref` resolved to.
70
+ */
71
+ onDereference?(path: string, value: JSONSchemaObject): void;
72
+ };
73
+ }
74
+ export declare const getNewOptions: (options: DeepPartial<$RefParserOptions>) => $RefParserOptions;
75
+ export type Options = $RefParserOptions;
76
+ export type ParserOptions = DeepPartial<$RefParserOptions>;
77
+ export default $RefParserOptions;
@@ -0,0 +1,119 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getNewOptions = void 0;
7
+ const json_js_1 = __importDefault(require("./parsers/json.js"));
8
+ const yaml_js_1 = __importDefault(require("./parsers/yaml.js"));
9
+ const text_js_1 = __importDefault(require("./parsers/text.js"));
10
+ const binary_js_1 = __importDefault(require("./parsers/binary.js"));
11
+ const file_js_1 = __importDefault(require("./resolvers/file.js"));
12
+ const http_js_1 = __importDefault(require("./resolvers/http.js"));
13
+ const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
14
+ const getDefaults = () => {
15
+ const defaults = {
16
+ /**
17
+ * Determines how different types of files will be parsed.
18
+ *
19
+ * You can add additional parsers of your own, replace an existing one with
20
+ * your own implementation, or disable any parser by setting it to false.
21
+ */
22
+ parse: {
23
+ json: json_js_1.default,
24
+ yaml: yaml_js_1.default,
25
+ text: text_js_1.default,
26
+ binary: binary_js_1.default,
27
+ },
28
+ /**
29
+ * Determines how JSON References will be resolved.
30
+ *
31
+ * You can add additional resolvers of your own, replace an existing one with
32
+ * your own implementation, or disable any resolver by setting it to false.
33
+ */
34
+ resolve: {
35
+ file: file_js_1.default,
36
+ http: http_js_1.default,
37
+ /**
38
+ * Determines whether external $ref pointers will be resolved.
39
+ * If this option is disabled, then none of above resolvers will be called.
40
+ * Instead, external $ref pointers will simply be ignored.
41
+ *
42
+ * @type {boolean}
43
+ */
44
+ external: true,
45
+ },
46
+ /**
47
+ * By default, JSON Schema $Ref Parser throws the first error it encounters. Setting `continueOnError` to `true`
48
+ * causes it to keep processing as much as possible and then throw a single error that contains all errors
49
+ * that were encountered.
50
+ */
51
+ continueOnError: false,
52
+ /**
53
+ * Determines the types of JSON references that are allowed.
54
+ */
55
+ dereference: {
56
+ /**
57
+ * Dereference circular (recursive) JSON references?
58
+ * If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
59
+ * If "ignore", then circular references will not be dereferenced.
60
+ *
61
+ * @type {boolean|string}
62
+ */
63
+ circular: true,
64
+ /**
65
+ * A function, called for each path, which can return true to stop this path and all
66
+ * subpaths from being dereferenced further. This is useful in schemas where some
67
+ * subpaths contain literal $ref keys that should not be dereferenced.
68
+ *
69
+ * @type {function}
70
+ */
71
+ excludedPathMatcher: () => false,
72
+ },
73
+ };
74
+ return (0, lodash_clonedeep_1.default)(defaults);
75
+ };
76
+ const getNewOptions = (options) => {
77
+ const newOptions = getDefaults();
78
+ if (options) {
79
+ merge(newOptions, options);
80
+ }
81
+ return newOptions;
82
+ };
83
+ exports.getNewOptions = getNewOptions;
84
+ /**
85
+ * Merges the properties of the source object into the target object.
86
+ *
87
+ * @param target - The object that we're populating
88
+ * @param source - The options that are being merged
89
+ * @returns
90
+ */
91
+ function merge(target, source) {
92
+ if (isMergeable(source)) {
93
+ const keys = Object.keys(source);
94
+ for (let i = 0; i < keys.length; i++) {
95
+ const key = keys[i];
96
+ const sourceSetting = source[key];
97
+ const targetSetting = target[key];
98
+ if (isMergeable(sourceSetting)) {
99
+ // It's a nested object, so merge it recursively
100
+ target[key] = merge(targetSetting || {}, sourceSetting);
101
+ }
102
+ else if (sourceSetting !== undefined) {
103
+ // It's a scalar value, function, or array. No merging necessary. Just overwrite the target value.
104
+ target[key] = sourceSetting;
105
+ }
106
+ }
107
+ }
108
+ return target;
109
+ }
110
+ /**
111
+ * Determines whether the given value can be merged,
112
+ * or if it is a scalar value that should just override the target value.
113
+ *
114
+ * @param val
115
+ * @returns
116
+ */
117
+ function isMergeable(val) {
118
+ return val && typeof val === "object" && !Array.isArray(val) && !(val instanceof RegExp) && !(val instanceof Date);
119
+ }