@apidevtools/json-schema-ref-parser 10.0.0 → 10.1.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 (103) hide show
  1. package/dist/lib/bundle.d.ts +10 -0
  2. package/dist/lib/bundle.js +265 -0
  3. package/dist/lib/dereference.d.ts +9 -0
  4. package/dist/lib/dereference.js +206 -0
  5. package/dist/lib/index.d.ts +206 -0
  6. package/dist/lib/index.js +223 -0
  7. package/dist/lib/normalize-args.d.ts +10 -0
  8. package/dist/lib/normalize-args.js +42 -0
  9. package/dist/lib/options.d.ts +77 -0
  10. package/dist/lib/options.js +119 -0
  11. package/dist/lib/parse.d.ts +8 -0
  12. package/dist/lib/parse.js +177 -0
  13. package/dist/lib/parsers/binary.d.ts +3 -0
  14. package/dist/lib/parsers/binary.js +35 -0
  15. package/dist/lib/parsers/json.d.ts +3 -0
  16. package/dist/lib/parsers/json.js +57 -0
  17. package/dist/lib/parsers/text.d.ts +3 -0
  18. package/dist/lib/parsers/text.js +42 -0
  19. package/dist/lib/parsers/yaml.d.ts +3 -0
  20. package/dist/lib/parsers/yaml.js +65 -0
  21. package/dist/lib/pointer.d.ts +87 -0
  22. package/dist/lib/pointer.js +256 -0
  23. package/dist/lib/ref.d.ts +181 -0
  24. package/dist/lib/ref.js +239 -0
  25. package/dist/lib/refs.d.ts +127 -0
  26. package/dist/lib/refs.js +223 -0
  27. package/dist/lib/resolve-external.d.ts +14 -0
  28. package/dist/lib/resolve-external.js +148 -0
  29. package/dist/lib/resolvers/file.d.ts +3 -0
  30. package/dist/lib/resolvers/file.js +76 -0
  31. package/dist/lib/resolvers/http.d.ts +3 -0
  32. package/dist/lib/resolvers/http.js +159 -0
  33. package/dist/lib/types/index.d.ts +104 -0
  34. package/dist/lib/types/index.js +2 -0
  35. package/dist/lib/util/errors.d.ts +50 -0
  36. package/dist/lib/util/errors.js +106 -0
  37. package/dist/lib/util/maybe.d.ts +3 -0
  38. package/dist/lib/util/maybe.js +24 -0
  39. package/dist/lib/util/next.d.ts +2 -0
  40. package/dist/lib/util/next.js +16 -0
  41. package/dist/lib/util/plugins.d.ts +36 -0
  42. package/dist/lib/util/plugins.js +144 -0
  43. package/dist/lib/util/url.d.ts +93 -0
  44. package/{cjs → dist/lib}/util/url.js +134 -102
  45. package/dist/vite.config.d.ts +2 -0
  46. package/dist/vite.config.js +23 -0
  47. package/lib/{bundle.js → bundle.ts} +105 -101
  48. package/lib/{dereference.js → dereference.ts} +113 -52
  49. package/lib/index.ts +413 -0
  50. package/lib/{normalize-args.js → normalize-args.ts} +7 -14
  51. package/lib/options.ts +202 -0
  52. package/lib/parse.ts +153 -0
  53. package/lib/parsers/binary.ts +39 -0
  54. package/lib/parsers/{json.js → json.ts} +9 -22
  55. package/lib/parsers/text.ts +46 -0
  56. package/lib/parsers/{yaml.js → yaml.ts} +15 -19
  57. package/lib/pointer.ts +296 -0
  58. package/lib/ref.ts +288 -0
  59. package/lib/refs.ts +238 -0
  60. package/lib/{resolve-external.js → resolve-external.ts} +39 -36
  61. package/lib/resolvers/file.ts +40 -0
  62. package/lib/resolvers/http.ts +136 -0
  63. package/lib/tsconfig.json +103 -0
  64. package/lib/types/index.ts +135 -0
  65. package/lib/util/errors.ts +141 -0
  66. package/lib/util/maybe.ts +22 -0
  67. package/lib/util/next.ts +13 -0
  68. package/lib/util/{plugins.js → plugins.ts} +58 -57
  69. package/lib/util/{url.js → url.ts} +64 -83
  70. package/package.json +44 -45
  71. package/cjs/bundle.js +0 -304
  72. package/cjs/dereference.js +0 -258
  73. package/cjs/index.js +0 -603
  74. package/cjs/normalize-args.js +0 -64
  75. package/cjs/options.js +0 -125
  76. package/cjs/package.json +0 -3
  77. package/cjs/parse.js +0 -338
  78. package/cjs/parsers/binary.js +0 -54
  79. package/cjs/parsers/json.js +0 -199
  80. package/cjs/parsers/text.js +0 -61
  81. package/cjs/parsers/yaml.js +0 -239
  82. package/cjs/pointer.js +0 -290
  83. package/cjs/ref.js +0 -333
  84. package/cjs/refs.js +0 -214
  85. package/cjs/resolve-external.js +0 -333
  86. package/cjs/resolvers/file.js +0 -106
  87. package/cjs/resolvers/http.js +0 -184
  88. package/cjs/util/errors.js +0 -401
  89. package/cjs/util/plugins.js +0 -159
  90. package/cjs/util/projectDir.cjs +0 -6
  91. package/lib/index.d.ts +0 -496
  92. package/lib/index.js +0 -290
  93. package/lib/options.js +0 -128
  94. package/lib/parse.js +0 -162
  95. package/lib/parsers/binary.js +0 -53
  96. package/lib/parsers/text.js +0 -64
  97. package/lib/pointer.js +0 -293
  98. package/lib/ref.js +0 -292
  99. package/lib/refs.js +0 -196
  100. package/lib/resolvers/file.js +0 -63
  101. package/lib/resolvers/http.js +0 -155
  102. package/lib/util/errors.js +0 -134
  103. 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,223 @@
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ 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;
39
+ const refs_js_1 = __importDefault(require("./refs.js"));
40
+ const parse_js_1 = __importDefault(require("./parse.js"));
41
+ const normalize_args_js_1 = __importDefault(require("./normalize-args.js"));
42
+ const resolve_external_js_1 = __importDefault(require("./resolve-external.js"));
43
+ const bundle_js_1 = __importDefault(require("./bundle.js"));
44
+ const dereference_js_1 = __importDefault(require("./dereference.js"));
45
+ const url = __importStar(require("./util/url.js"));
46
+ const errors_js_1 = require("./util/errors.js");
47
+ Object.defineProperty(exports, "JSONParserError", { enumerable: true, get: function () { return errors_js_1.JSONParserError; } });
48
+ Object.defineProperty(exports, "InvalidPointerError", { enumerable: true, get: function () { return errors_js_1.InvalidPointerError; } });
49
+ Object.defineProperty(exports, "MissingPointerError", { enumerable: true, get: function () { return errors_js_1.MissingPointerError; } });
50
+ Object.defineProperty(exports, "ResolverError", { enumerable: true, get: function () { return errors_js_1.ResolverError; } });
51
+ Object.defineProperty(exports, "ParserError", { enumerable: true, get: function () { return errors_js_1.ParserError; } });
52
+ Object.defineProperty(exports, "UnmatchedParserError", { enumerable: true, get: function () { return errors_js_1.UnmatchedParserError; } });
53
+ Object.defineProperty(exports, "UnmatchedResolverError", { enumerable: true, get: function () { return errors_js_1.UnmatchedResolverError; } });
54
+ const ono_1 = require("@jsdevtools/ono");
55
+ const maybe_js_1 = __importDefault(require("./util/maybe.js"));
56
+ /**
57
+ * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
58
+ * and provides methods for traversing, manipulating, and dereferencing those references.
59
+ *
60
+ * @class
61
+ */
62
+ class $RefParser {
63
+ constructor() {
64
+ /**
65
+ * The parsed (and possibly dereferenced) JSON schema object
66
+ *
67
+ * @type {object}
68
+ * @readonly
69
+ */
70
+ this.schema = null;
71
+ /**
72
+ * The resolved JSON references
73
+ *
74
+ * @type {$Refs}
75
+ * @readonly
76
+ */
77
+ this.$refs = new refs_js_1.default();
78
+ }
79
+ parse() {
80
+ return __awaiter(this, arguments, void 0, function* () {
81
+ const args = (0, normalize_args_js_1.default)(arguments);
82
+ let promise;
83
+ if (!args.path && !args.schema) {
84
+ const err = (0, ono_1.ono)(`Expected a file path, URL, or object. Got ${args.path || args.schema}`);
85
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
86
+ }
87
+ // Reset everything
88
+ this.schema = null;
89
+ this.$refs = new refs_js_1.default();
90
+ // If the path is a filesystem path, then convert it to a URL.
91
+ // NOTE: According to the JSON Reference spec, these should already be URLs,
92
+ // but, in practice, many people use local filesystem paths instead.
93
+ // So we're being generous here and doing the conversion automatically.
94
+ // This is not intended to be a 100% bulletproof solution.
95
+ // If it doesn't work for your use-case, then use a URL instead.
96
+ let pathType = "http";
97
+ if (url.isFileSystemPath(args.path)) {
98
+ args.path = url.fromFileSystemPath(args.path);
99
+ pathType = "file";
100
+ }
101
+ // Resolve the absolute path of the schema
102
+ args.path = url.resolve(url.cwd(), args.path);
103
+ if (args.schema && typeof args.schema === "object") {
104
+ // A schema object was passed-in.
105
+ // So immediately add a new $Ref with the schema object as its value
106
+ const $ref = this.$refs._add(args.path);
107
+ $ref.value = args.schema;
108
+ $ref.pathType = pathType;
109
+ promise = Promise.resolve(args.schema);
110
+ }
111
+ else {
112
+ // Parse the schema file/url
113
+ promise = (0, parse_js_1.default)(args.path, this.$refs, args.options);
114
+ }
115
+ try {
116
+ const result = yield promise;
117
+ if (result !== null && typeof result === "object" && !Buffer.isBuffer(result)) {
118
+ this.schema = result;
119
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
120
+ }
121
+ else if (args.options.continueOnError) {
122
+ this.schema = null; // it's already set to null at line 79, but let's set it again for the sake of readability
123
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
124
+ }
125
+ else {
126
+ throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || result}" is not a valid JSON Schema`);
127
+ }
128
+ }
129
+ catch (err) {
130
+ if (!args.options.continueOnError || !(0, errors_js_1.isHandledError)(err)) {
131
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
132
+ }
133
+ if (this.$refs._$refs[url.stripHash(args.path)]) {
134
+ this.$refs._$refs[url.stripHash(args.path)].addError(err);
135
+ }
136
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(null));
137
+ }
138
+ });
139
+ }
140
+ static parse() {
141
+ const parser = new $RefParser();
142
+ return parser.parse.apply(parser, arguments);
143
+ }
144
+ /**
145
+ * Parses the given JSON schema and resolves any JSON references, including references in
146
+ * externally-referenced files.
147
+ *
148
+ * @param [path] - The file path or URL of the JSON schema
149
+ * @param [schema] - A JSON schema object. This object will be used instead of reading from `path`.
150
+ * @param [options] - Options that determine how the schema is parsed and resolved
151
+ * @param [callback]
152
+ * - An error-first callback. The second parameter is a {@link $Refs} object containing the resolved JSON references
153
+ *
154
+ * @returns
155
+ * The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
156
+ */
157
+ resolve() {
158
+ return __awaiter(this, arguments, void 0, function* () {
159
+ const args = (0, normalize_args_js_1.default)(arguments);
160
+ try {
161
+ yield this.parse(args.path, args.schema, args.options);
162
+ yield (0, resolve_external_js_1.default)(this, args.options);
163
+ finalize(this);
164
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.$refs));
165
+ }
166
+ catch (err) {
167
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
168
+ }
169
+ });
170
+ }
171
+ static resolve() {
172
+ const instance = new $RefParser();
173
+ return instance.resolve.apply(instance, arguments);
174
+ }
175
+ static bundle() {
176
+ const instance = new $RefParser();
177
+ return instance.bundle.apply(instance, arguments);
178
+ }
179
+ bundle() {
180
+ return __awaiter(this, arguments, void 0, function* () {
181
+ const args = (0, normalize_args_js_1.default)(arguments);
182
+ try {
183
+ yield this.resolve(args.path, args.schema, args.options);
184
+ (0, bundle_js_1.default)(this, args.options);
185
+ finalize(this);
186
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
187
+ }
188
+ catch (err) {
189
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
190
+ }
191
+ });
192
+ }
193
+ static dereference() {
194
+ const instance = new $RefParser();
195
+ return instance.dereference.apply(instance, arguments);
196
+ }
197
+ dereference() {
198
+ return __awaiter(this, arguments, void 0, function* () {
199
+ const args = (0, normalize_args_js_1.default)(arguments);
200
+ try {
201
+ yield this.resolve(args.path, args.schema, args.options);
202
+ (0, dereference_js_1.default)(this, args.options);
203
+ finalize(this);
204
+ return (0, maybe_js_1.default)(args.callback, Promise.resolve(this.schema));
205
+ }
206
+ catch (err) {
207
+ return (0, maybe_js_1.default)(args.callback, Promise.reject(err));
208
+ }
209
+ });
210
+ }
211
+ }
212
+ exports.$RefParser = $RefParser;
213
+ exports.default = $RefParser;
214
+ function finalize(parser) {
215
+ const errors = errors_js_1.JSONParserErrorGroup.getParserErrors(parser);
216
+ if (errors.length > 0) {
217
+ throw new errors_js_1.JSONParserErrorGroup(parser);
218
+ }
219
+ }
220
+ exports.parse = $RefParser.parse;
221
+ exports.resolve = $RefParser.resolve;
222
+ exports.bundle = $RefParser.bundle;
223
+ 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;