@hey-api/json-schema-ref-parser 1.2.3 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -84
- package/dist/index.d.mts +629 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1887 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +42 -78
- package/src/__tests__/bundle.test.ts +59 -0
- package/src/__tests__/index.test.ts +43 -0
- package/src/__tests__/pointer.test.ts +34 -0
- package/src/__tests__/utils.ts +3 -0
- package/{lib → src}/bundle.ts +143 -229
- package/{lib → src}/dereference.ts +20 -43
- package/{lib → src}/index.ts +103 -125
- package/{lib → src}/options.ts +13 -9
- package/{lib → src}/parse.ts +19 -15
- package/src/parsers/binary.ts +13 -0
- package/{lib → src}/parsers/json.ts +5 -6
- package/src/parsers/text.ts +21 -0
- package/{lib → src}/parsers/yaml.ts +9 -9
- package/{lib → src}/pointer.ts +42 -23
- package/{lib → src}/ref.ts +25 -21
- package/{lib → src}/refs.ts +23 -26
- package/{lib → src}/resolve-external.ts +91 -60
- package/{lib → src}/resolvers/file.ts +7 -10
- package/{lib → src}/resolvers/url.ts +12 -8
- package/{lib → src}/types/index.ts +9 -2
- package/src/util/convert-path-to-posix.ts +8 -0
- package/{lib → src}/util/errors.ts +38 -36
- package/{lib → src}/util/is-windows.ts +1 -1
- package/{lib → src}/util/plugins.ts +7 -8
- package/{lib → src}/util/url.ts +41 -42
- package/dist/lib/__tests__/bundle.test.d.ts +0 -1
- package/dist/lib/__tests__/bundle.test.js +0 -50
- package/dist/lib/__tests__/index.test.d.ts +0 -1
- package/dist/lib/__tests__/index.test.js +0 -43
- package/dist/lib/__tests__/pointer.test.d.ts +0 -1
- package/dist/lib/__tests__/pointer.test.js +0 -27
- package/dist/lib/bundle.d.ts +0 -26
- package/dist/lib/bundle.js +0 -600
- package/dist/lib/dereference.d.ts +0 -11
- package/dist/lib/dereference.js +0 -226
- package/dist/lib/index.d.ts +0 -92
- package/dist/lib/index.js +0 -525
- package/dist/lib/options.d.ts +0 -61
- package/dist/lib/options.js +0 -45
- package/dist/lib/parse.d.ts +0 -13
- package/dist/lib/parse.js +0 -87
- package/dist/lib/parsers/binary.d.ts +0 -2
- package/dist/lib/parsers/binary.js +0 -12
- package/dist/lib/parsers/json.d.ts +0 -2
- package/dist/lib/parsers/json.js +0 -38
- package/dist/lib/parsers/text.d.ts +0 -2
- package/dist/lib/parsers/text.js +0 -18
- package/dist/lib/parsers/yaml.d.ts +0 -2
- package/dist/lib/parsers/yaml.js +0 -28
- package/dist/lib/pointer.d.ts +0 -88
- package/dist/lib/pointer.js +0 -297
- package/dist/lib/ref.d.ts +0 -180
- package/dist/lib/ref.js +0 -226
- package/dist/lib/refs.d.ts +0 -127
- package/dist/lib/refs.js +0 -232
- package/dist/lib/resolve-external.d.ts +0 -13
- package/dist/lib/resolve-external.js +0 -151
- package/dist/lib/resolvers/file.d.ts +0 -6
- package/dist/lib/resolvers/file.js +0 -61
- package/dist/lib/resolvers/url.d.ts +0 -17
- package/dist/lib/resolvers/url.js +0 -62
- package/dist/lib/types/index.d.ts +0 -43
- package/dist/lib/types/index.js +0 -2
- package/dist/lib/util/convert-path-to-posix.d.ts +0 -1
- package/dist/lib/util/convert-path-to-posix.js +0 -14
- package/dist/lib/util/errors.d.ts +0 -56
- package/dist/lib/util/errors.js +0 -112
- package/dist/lib/util/is-windows.d.ts +0 -1
- package/dist/lib/util/is-windows.js +0 -6
- package/dist/lib/util/plugins.d.ts +0 -16
- package/dist/lib/util/plugins.js +0 -45
- package/dist/lib/util/url.d.ts +0 -79
- package/dist/lib/util/url.js +0 -285
- package/dist/vite.config.d.ts +0 -2
- package/dist/vite.config.js +0 -19
- package/lib/__tests__/bundle.test.ts +0 -52
- package/lib/__tests__/index.test.ts +0 -45
- package/lib/__tests__/pointer.test.ts +0 -26
- package/lib/__tests__/spec/circular-ref-with-description.json +0 -11
- package/lib/__tests__/spec/multiple-refs.json +0 -34
- package/lib/__tests__/spec/openapi-paths-ref.json +0 -46
- package/lib/__tests__/spec/path-parameter.json +0 -16
- package/lib/parsers/binary.ts +0 -13
- package/lib/parsers/text.ts +0 -21
- package/lib/util/convert-path-to-posix.ts +0 -11
- /package/{LICENSE → LICENSE.md} +0 -0
package/dist/lib/index.js
DELETED
|
@@ -1,525 +0,0 @@
|
|
|
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 () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.sendRequest = exports.$RefParser = exports.getResolvedInput = void 0;
|
|
40
|
-
const refs_js_1 = __importDefault(require("./refs.js"));
|
|
41
|
-
const parse_js_1 = require("./parse.js");
|
|
42
|
-
const resolve_external_js_1 = require("./resolve-external.js");
|
|
43
|
-
const bundle_js_1 = 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
|
-
const ono_1 = require("@jsdevtools/ono");
|
|
48
|
-
const options_js_1 = require("./options.js");
|
|
49
|
-
const url_js_1 = require("./resolvers/url.js");
|
|
50
|
-
const file_js_1 = require("./resolvers/file.js");
|
|
51
|
-
const getResolvedInput = ({ pathOrUrlOrSchema, }) => {
|
|
52
|
-
if (!pathOrUrlOrSchema) {
|
|
53
|
-
throw (0, ono_1.ono)(`Expected a file path, URL, or object. Got ${pathOrUrlOrSchema}`);
|
|
54
|
-
}
|
|
55
|
-
const resolvedInput = {
|
|
56
|
-
path: typeof pathOrUrlOrSchema === "string" ? pathOrUrlOrSchema : "",
|
|
57
|
-
schema: undefined,
|
|
58
|
-
type: "url",
|
|
59
|
-
};
|
|
60
|
-
// If the path is a filesystem path, then convert it to a URL.
|
|
61
|
-
// NOTE: According to the JSON Reference spec, these should already be URLs,
|
|
62
|
-
// but, in practice, many people use local filesystem paths instead.
|
|
63
|
-
// So we're being generous here and doing the conversion automatically.
|
|
64
|
-
// This is not intended to be a 100% bulletproof solution.
|
|
65
|
-
// If it doesn't work for your use-case, then use a URL instead.
|
|
66
|
-
if (resolvedInput.path && url.isFileSystemPath(resolvedInput.path)) {
|
|
67
|
-
resolvedInput.path = url.fromFileSystemPath(resolvedInput.path);
|
|
68
|
-
resolvedInput.type = "file";
|
|
69
|
-
}
|
|
70
|
-
else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema === "object") {
|
|
71
|
-
if ("$id" in pathOrUrlOrSchema && pathOrUrlOrSchema.$id) {
|
|
72
|
-
// when schema id has defined an URL should use that hostname to request the references,
|
|
73
|
-
// instead of using the current page URL
|
|
74
|
-
const { hostname, protocol } = new URL(pathOrUrlOrSchema.$id);
|
|
75
|
-
resolvedInput.path = `${protocol}//${hostname}:${protocol === "https:" ? 443 : 80}`;
|
|
76
|
-
resolvedInput.type = "url";
|
|
77
|
-
}
|
|
78
|
-
else {
|
|
79
|
-
resolvedInput.schema = pathOrUrlOrSchema;
|
|
80
|
-
resolvedInput.type = "json";
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (resolvedInput.type !== "json") {
|
|
84
|
-
// resolve the absolute path of the schema
|
|
85
|
-
resolvedInput.path = url.resolve(url.cwd(), resolvedInput.path);
|
|
86
|
-
}
|
|
87
|
-
return resolvedInput;
|
|
88
|
-
};
|
|
89
|
-
exports.getResolvedInput = getResolvedInput;
|
|
90
|
-
const _ensureResolvedInputPath = (input, fallbackPath) => {
|
|
91
|
-
if (input.type === "json" && (!input.path || input.path.length === 0)) {
|
|
92
|
-
return { ...input, path: fallbackPath };
|
|
93
|
-
}
|
|
94
|
-
return input;
|
|
95
|
-
};
|
|
96
|
-
// NOTE: previously used helper removed as unused
|
|
97
|
-
/**
|
|
98
|
-
* This class parses a JSON schema, builds a map of its JSON references and their resolved values,
|
|
99
|
-
* and provides methods for traversing, manipulating, and dereferencing those references.
|
|
100
|
-
*/
|
|
101
|
-
class $RefParser {
|
|
102
|
-
constructor() {
|
|
103
|
-
/**
|
|
104
|
-
* The resolved JSON references
|
|
105
|
-
*
|
|
106
|
-
* @type {$Refs}
|
|
107
|
-
* @readonly
|
|
108
|
-
*/
|
|
109
|
-
this.$refs = new refs_js_1.default();
|
|
110
|
-
this.options = (0, options_js_1.getJsonSchemaRefParserDefaultOptions)();
|
|
111
|
-
/**
|
|
112
|
-
* The parsed (and possibly dereferenced) JSON schema object
|
|
113
|
-
*
|
|
114
|
-
* @type {object}
|
|
115
|
-
* @readonly
|
|
116
|
-
*/
|
|
117
|
-
this.schema = null;
|
|
118
|
-
this.schemaMany = [];
|
|
119
|
-
this.schemaManySources = [];
|
|
120
|
-
this.sourcePathToPrefix = new Map();
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* 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.
|
|
124
|
-
*
|
|
125
|
-
* This also eliminates the risk of circular references, so the schema can be safely serialized using `JSON.stringify()`.
|
|
126
|
-
*
|
|
127
|
-
* See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundleschema-options-callback
|
|
128
|
-
*
|
|
129
|
-
* @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
|
|
130
|
-
*/
|
|
131
|
-
async bundle({ arrayBuffer, fetch, pathOrUrlOrSchema, resolvedInput, }) {
|
|
132
|
-
await this.parse({
|
|
133
|
-
arrayBuffer,
|
|
134
|
-
fetch,
|
|
135
|
-
pathOrUrlOrSchema,
|
|
136
|
-
resolvedInput,
|
|
137
|
-
});
|
|
138
|
-
await (0, resolve_external_js_1.resolveExternal)(this, this.options);
|
|
139
|
-
const errors = errors_js_1.JSONParserErrorGroup.getParserErrors(this);
|
|
140
|
-
if (errors.length > 0) {
|
|
141
|
-
throw new errors_js_1.JSONParserErrorGroup(this);
|
|
142
|
-
}
|
|
143
|
-
(0, bundle_js_1.bundle)(this, this.options);
|
|
144
|
-
const errors2 = errors_js_1.JSONParserErrorGroup.getParserErrors(this);
|
|
145
|
-
if (errors2.length > 0) {
|
|
146
|
-
throw new errors_js_1.JSONParserErrorGroup(this);
|
|
147
|
-
}
|
|
148
|
-
return this.schema;
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Bundles multiple roots (files/URLs/objects) into a single schema by creating a synthetic root
|
|
152
|
-
* that references each input, resolving all externals, and then hoisting via the existing bundler.
|
|
153
|
-
*/
|
|
154
|
-
async bundleMany({ arrayBuffer, fetch, pathOrUrlOrSchemas, resolvedInputs, }) {
|
|
155
|
-
await this.parseMany({ arrayBuffer, fetch, pathOrUrlOrSchemas, resolvedInputs });
|
|
156
|
-
this.mergeMany();
|
|
157
|
-
await (0, resolve_external_js_1.resolveExternal)(this, this.options);
|
|
158
|
-
const errors = errors_js_1.JSONParserErrorGroup.getParserErrors(this);
|
|
159
|
-
if (errors.length > 0) {
|
|
160
|
-
throw new errors_js_1.JSONParserErrorGroup(this);
|
|
161
|
-
}
|
|
162
|
-
(0, bundle_js_1.bundle)(this, this.options);
|
|
163
|
-
// Merged root is ready for bundling
|
|
164
|
-
const errors2 = errors_js_1.JSONParserErrorGroup.getParserErrors(this);
|
|
165
|
-
if (errors2.length > 0) {
|
|
166
|
-
throw new errors_js_1.JSONParserErrorGroup(this);
|
|
167
|
-
}
|
|
168
|
-
return this.schema;
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* 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.
|
|
172
|
-
*
|
|
173
|
-
* 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.
|
|
174
|
-
*
|
|
175
|
-
* See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferenceschema-options-callback
|
|
176
|
-
*
|
|
177
|
-
* @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
|
|
178
|
-
*/
|
|
179
|
-
async dereference({ fetch, pathOrUrlOrSchema, }) {
|
|
180
|
-
await this.parse({
|
|
181
|
-
fetch,
|
|
182
|
-
pathOrUrlOrSchema,
|
|
183
|
-
});
|
|
184
|
-
await (0, resolve_external_js_1.resolveExternal)(this, this.options);
|
|
185
|
-
const errors = errors_js_1.JSONParserErrorGroup.getParserErrors(this);
|
|
186
|
-
if (errors.length > 0) {
|
|
187
|
-
throw new errors_js_1.JSONParserErrorGroup(this);
|
|
188
|
-
}
|
|
189
|
-
(0, dereference_js_1.default)(this, this.options);
|
|
190
|
-
const errors2 = errors_js_1.JSONParserErrorGroup.getParserErrors(this);
|
|
191
|
-
if (errors2.length > 0) {
|
|
192
|
-
throw new errors_js_1.JSONParserErrorGroup(this);
|
|
193
|
-
}
|
|
194
|
-
return this.schema;
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* Parses the given JSON schema.
|
|
198
|
-
* This method does not resolve any JSON references.
|
|
199
|
-
* It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
|
|
200
|
-
*
|
|
201
|
-
* @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
|
|
202
|
-
* @returns - The returned promise resolves with the parsed JSON schema object.
|
|
203
|
-
*/
|
|
204
|
-
async parse({ arrayBuffer, fetch, pathOrUrlOrSchema, resolvedInput: _resolvedInput, }) {
|
|
205
|
-
const resolvedInput = _resolvedInput || (0, exports.getResolvedInput)({ pathOrUrlOrSchema });
|
|
206
|
-
const { path, type } = resolvedInput;
|
|
207
|
-
let { schema } = resolvedInput;
|
|
208
|
-
// reset everything
|
|
209
|
-
this.schema = null;
|
|
210
|
-
this.$refs = new refs_js_1.default();
|
|
211
|
-
if (schema) {
|
|
212
|
-
// immediately add a new $Ref with the schema object as value
|
|
213
|
-
const $ref = this.$refs._add(path);
|
|
214
|
-
$ref.pathType = url.isFileSystemPath(path) ? "file" : "http";
|
|
215
|
-
$ref.value = schema;
|
|
216
|
-
}
|
|
217
|
-
else if (type !== "json") {
|
|
218
|
-
const file = (0, parse_js_1.newFile)(path);
|
|
219
|
-
// Add a new $Ref for this file, even though we don't have the value yet.
|
|
220
|
-
// This ensures that we don't simultaneously read & parse the same file multiple times
|
|
221
|
-
const $refAdded = this.$refs._add(file.url);
|
|
222
|
-
$refAdded.pathType = type;
|
|
223
|
-
try {
|
|
224
|
-
const resolver = type === "file" ? file_js_1.fileResolver : url_js_1.urlResolver;
|
|
225
|
-
await resolver.handler({
|
|
226
|
-
arrayBuffer,
|
|
227
|
-
fetch,
|
|
228
|
-
file,
|
|
229
|
-
});
|
|
230
|
-
const parseResult = await (0, parse_js_1.parseFile)(file, this.options);
|
|
231
|
-
$refAdded.value = parseResult.result;
|
|
232
|
-
schema = parseResult.result;
|
|
233
|
-
}
|
|
234
|
-
catch (err) {
|
|
235
|
-
if ((0, errors_js_1.isHandledError)(err)) {
|
|
236
|
-
$refAdded.value = err;
|
|
237
|
-
}
|
|
238
|
-
throw err;
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
if (schema === null || typeof schema !== "object" || Buffer.isBuffer(schema)) {
|
|
242
|
-
throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || schema}" is not a valid JSON Schema`);
|
|
243
|
-
}
|
|
244
|
-
this.schema = schema;
|
|
245
|
-
return {
|
|
246
|
-
schema,
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
async parseMany({ arrayBuffer, fetch, pathOrUrlOrSchemas, resolvedInputs: _resolvedInputs, }) {
|
|
250
|
-
const resolvedInputs = [...(_resolvedInputs || [])];
|
|
251
|
-
resolvedInputs.push(...(pathOrUrlOrSchemas.map((schema) => (0, exports.getResolvedInput)({ pathOrUrlOrSchema: schema })) || []));
|
|
252
|
-
this.schemaMany = [];
|
|
253
|
-
this.schemaManySources = [];
|
|
254
|
-
this.sourcePathToPrefix = new Map();
|
|
255
|
-
for (let i = 0; i < resolvedInputs.length; i++) {
|
|
256
|
-
const resolvedInput = resolvedInputs[i];
|
|
257
|
-
const { path, type } = resolvedInput;
|
|
258
|
-
let { schema } = resolvedInput;
|
|
259
|
-
if (schema) {
|
|
260
|
-
// keep schema as-is
|
|
261
|
-
}
|
|
262
|
-
else if (type !== "json") {
|
|
263
|
-
const file = (0, parse_js_1.newFile)(path);
|
|
264
|
-
// Add a new $Ref for this file, even though we don't have the value yet.
|
|
265
|
-
// This ensures that we don't simultaneously read & parse the same file multiple times
|
|
266
|
-
const $refAdded = this.$refs._add(file.url);
|
|
267
|
-
$refAdded.pathType = type;
|
|
268
|
-
try {
|
|
269
|
-
const resolver = type === "file" ? file_js_1.fileResolver : url_js_1.urlResolver;
|
|
270
|
-
await resolver.handler({
|
|
271
|
-
arrayBuffer: arrayBuffer?.[i],
|
|
272
|
-
fetch,
|
|
273
|
-
file,
|
|
274
|
-
});
|
|
275
|
-
const parseResult = await (0, parse_js_1.parseFile)(file, this.options);
|
|
276
|
-
$refAdded.value = parseResult.result;
|
|
277
|
-
schema = parseResult.result;
|
|
278
|
-
}
|
|
279
|
-
catch (err) {
|
|
280
|
-
if ((0, errors_js_1.isHandledError)(err)) {
|
|
281
|
-
$refAdded.value = err;
|
|
282
|
-
}
|
|
283
|
-
throw err;
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
if (schema === null || typeof schema !== "object" || Buffer.isBuffer(schema)) {
|
|
287
|
-
throw ono_1.ono.syntax(`"${this.$refs._root$Ref.path || schema}" is not a valid JSON Schema`);
|
|
288
|
-
}
|
|
289
|
-
this.schemaMany.push(schema);
|
|
290
|
-
this.schemaManySources.push(path && path.length ? path : url.cwd());
|
|
291
|
-
}
|
|
292
|
-
return {
|
|
293
|
-
schemaMany: this.schemaMany,
|
|
294
|
-
};
|
|
295
|
-
}
|
|
296
|
-
mergeMany() {
|
|
297
|
-
const schemas = this.schemaMany || [];
|
|
298
|
-
if (schemas.length === 0) {
|
|
299
|
-
throw (0, ono_1.ono)("mergeMany called with no schemas. Did you run parseMany?");
|
|
300
|
-
}
|
|
301
|
-
const merged = {};
|
|
302
|
-
// Determine spec version: prefer first occurrence of openapi, else swagger
|
|
303
|
-
let chosenOpenapi;
|
|
304
|
-
let chosenSwagger;
|
|
305
|
-
for (const s of schemas) {
|
|
306
|
-
if (!chosenOpenapi && s && typeof s.openapi === "string") {
|
|
307
|
-
chosenOpenapi = s.openapi;
|
|
308
|
-
}
|
|
309
|
-
if (!chosenSwagger && s && typeof s.swagger === "string") {
|
|
310
|
-
chosenSwagger = s.swagger;
|
|
311
|
-
}
|
|
312
|
-
if (chosenOpenapi && chosenSwagger) {
|
|
313
|
-
break;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
if (typeof chosenOpenapi === "string") {
|
|
317
|
-
merged.openapi = chosenOpenapi;
|
|
318
|
-
}
|
|
319
|
-
else if (typeof chosenSwagger === "string") {
|
|
320
|
-
merged.swagger = chosenSwagger;
|
|
321
|
-
}
|
|
322
|
-
// Merge info: take first non-empty per-field across inputs
|
|
323
|
-
const infoAccumulator = {};
|
|
324
|
-
for (const s of schemas) {
|
|
325
|
-
const info = s?.info;
|
|
326
|
-
if (info && typeof info === "object") {
|
|
327
|
-
for (const [k, v] of Object.entries(info)) {
|
|
328
|
-
if (infoAccumulator[k] === undefined && v !== undefined) {
|
|
329
|
-
infoAccumulator[k] = JSON.parse(JSON.stringify(v));
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
if (Object.keys(infoAccumulator).length > 0) {
|
|
335
|
-
merged.info = infoAccumulator;
|
|
336
|
-
}
|
|
337
|
-
// Merge servers: union by url+description
|
|
338
|
-
const servers = [];
|
|
339
|
-
const seenServers = new Set();
|
|
340
|
-
for (const s of schemas) {
|
|
341
|
-
const arr = s?.servers;
|
|
342
|
-
if (Array.isArray(arr)) {
|
|
343
|
-
for (const srv of arr) {
|
|
344
|
-
if (srv && typeof srv === "object") {
|
|
345
|
-
const key = `${srv.url || ""}|${srv.description || ""}`;
|
|
346
|
-
if (!seenServers.has(key)) {
|
|
347
|
-
seenServers.add(key);
|
|
348
|
-
servers.push(JSON.parse(JSON.stringify(srv)));
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
if (servers.length > 0) {
|
|
355
|
-
merged.servers = servers;
|
|
356
|
-
}
|
|
357
|
-
merged.paths = {};
|
|
358
|
-
merged.components = {};
|
|
359
|
-
const componentSections = [
|
|
360
|
-
"schemas",
|
|
361
|
-
"parameters",
|
|
362
|
-
"requestBodies",
|
|
363
|
-
"responses",
|
|
364
|
-
"headers",
|
|
365
|
-
"securitySchemes",
|
|
366
|
-
"examples",
|
|
367
|
-
"links",
|
|
368
|
-
"callbacks",
|
|
369
|
-
];
|
|
370
|
-
for (const sec of componentSections) {
|
|
371
|
-
merged.components[sec] = {};
|
|
372
|
-
}
|
|
373
|
-
const tagNameSet = new Set();
|
|
374
|
-
const tags = [];
|
|
375
|
-
const usedOpIds = new Set();
|
|
376
|
-
const baseName = (p) => {
|
|
377
|
-
try {
|
|
378
|
-
const withoutHash = p.split("#")[0];
|
|
379
|
-
const parts = withoutHash.split("/");
|
|
380
|
-
const filename = parts[parts.length - 1] || "schema";
|
|
381
|
-
const dot = filename.lastIndexOf(".");
|
|
382
|
-
const raw = dot > 0 ? filename.substring(0, dot) : filename;
|
|
383
|
-
return raw.replace(/[^A-Za-z0-9_-]/g, "_");
|
|
384
|
-
}
|
|
385
|
-
catch {
|
|
386
|
-
return "schema";
|
|
387
|
-
}
|
|
388
|
-
};
|
|
389
|
-
const unique = (set, proposed) => {
|
|
390
|
-
let name = proposed;
|
|
391
|
-
let i = 2;
|
|
392
|
-
while (set.has(name)) {
|
|
393
|
-
name = `${proposed}_${i++}`;
|
|
394
|
-
}
|
|
395
|
-
set.add(name);
|
|
396
|
-
return name;
|
|
397
|
-
};
|
|
398
|
-
const rewriteRef = (ref, refMap) => {
|
|
399
|
-
// OAS3: #/components/{section}/{name}...
|
|
400
|
-
let m = ref.match(/^#\/components\/([^/]+)\/([^/]+)(.*)$/);
|
|
401
|
-
if (m) {
|
|
402
|
-
const base = `#/components/${m[1]}/${m[2]}`;
|
|
403
|
-
const mapped = refMap.get(base);
|
|
404
|
-
if (mapped) {
|
|
405
|
-
return mapped + (m[3] || "");
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
// OAS2: #/definitions/{name}...
|
|
409
|
-
m = ref.match(/^#\/definitions\/([^/]+)(.*)$/);
|
|
410
|
-
if (m) {
|
|
411
|
-
const base = `#/components/schemas/${m[1]}`;
|
|
412
|
-
const mapped = refMap.get(base);
|
|
413
|
-
if (mapped) {
|
|
414
|
-
// map definitions -> components/schemas
|
|
415
|
-
return mapped + (m[2] || "");
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
return ref;
|
|
419
|
-
};
|
|
420
|
-
const cloneAndRewrite = (obj, refMap, tagMap, opIdPrefix, basePath) => {
|
|
421
|
-
if (obj === null || obj === undefined) {
|
|
422
|
-
return obj;
|
|
423
|
-
}
|
|
424
|
-
if (Array.isArray(obj)) {
|
|
425
|
-
return obj.map((v) => cloneAndRewrite(v, refMap, tagMap, opIdPrefix, basePath));
|
|
426
|
-
}
|
|
427
|
-
if (typeof obj !== "object") {
|
|
428
|
-
return obj;
|
|
429
|
-
}
|
|
430
|
-
const out = {};
|
|
431
|
-
for (const [k, v] of Object.entries(obj)) {
|
|
432
|
-
if (k === "$ref" && typeof v === "string") {
|
|
433
|
-
const s = v;
|
|
434
|
-
if (s.startsWith("#")) {
|
|
435
|
-
out[k] = rewriteRef(s, refMap);
|
|
436
|
-
}
|
|
437
|
-
else {
|
|
438
|
-
const proto = url.getProtocol(s);
|
|
439
|
-
if (proto === undefined) {
|
|
440
|
-
// relative external ref -> absolutize against source base path
|
|
441
|
-
out[k] = url.resolve(basePath + "#", s);
|
|
442
|
-
}
|
|
443
|
-
else {
|
|
444
|
-
out[k] = s;
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
}
|
|
448
|
-
else if (k === "tags" && Array.isArray(v) && v.every((x) => typeof x === "string")) {
|
|
449
|
-
out[k] = v.map((t) => tagMap.get(t) || t);
|
|
450
|
-
}
|
|
451
|
-
else if (k === "operationId" && typeof v === "string") {
|
|
452
|
-
out[k] = unique(usedOpIds, `${opIdPrefix}_${v}`);
|
|
453
|
-
}
|
|
454
|
-
else {
|
|
455
|
-
out[k] = cloneAndRewrite(v, refMap, tagMap, opIdPrefix, basePath);
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
return out;
|
|
459
|
-
};
|
|
460
|
-
for (let i = 0; i < schemas.length; i++) {
|
|
461
|
-
const schema = schemas[i] || {};
|
|
462
|
-
const sourcePath = this.schemaManySources[i] || `multi://input/${i + 1}`;
|
|
463
|
-
const prefix = baseName(sourcePath);
|
|
464
|
-
// Track prefix for this source path (strip hash). Only map real file/http paths
|
|
465
|
-
const withoutHash = url.stripHash(sourcePath);
|
|
466
|
-
const protocol = url.getProtocol(withoutHash);
|
|
467
|
-
if (protocol === undefined || protocol === "file" || protocol === "http" || protocol === "https") {
|
|
468
|
-
this.sourcePathToPrefix.set(withoutHash, prefix);
|
|
469
|
-
}
|
|
470
|
-
const refMap = new Map();
|
|
471
|
-
const tagMap = new Map();
|
|
472
|
-
const srcComponents = (schema.components || {});
|
|
473
|
-
for (const sec of componentSections) {
|
|
474
|
-
const group = srcComponents[sec] || {};
|
|
475
|
-
for (const [name] of Object.entries(group)) {
|
|
476
|
-
const newName = `${prefix}_${name}`;
|
|
477
|
-
refMap.set(`#/components/${sec}/${name}`, `#/components/${sec}/${newName}`);
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
const srcTags = Array.isArray(schema.tags) ? schema.tags : [];
|
|
481
|
-
for (const t of srcTags) {
|
|
482
|
-
if (!t || typeof t !== "object" || typeof t.name !== "string") {
|
|
483
|
-
continue;
|
|
484
|
-
}
|
|
485
|
-
const desired = t.name;
|
|
486
|
-
const finalName = tagNameSet.has(desired) ? `${prefix}_${desired}` : desired;
|
|
487
|
-
tagNameSet.add(finalName);
|
|
488
|
-
tagMap.set(desired, finalName);
|
|
489
|
-
if (!tags.find((x) => x && x.name === finalName)) {
|
|
490
|
-
tags.push({ ...t, name: finalName });
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
for (const sec of componentSections) {
|
|
494
|
-
const group = (schema.components && schema.components[sec]) || {};
|
|
495
|
-
for (const [name, val] of Object.entries(group)) {
|
|
496
|
-
const newName = `${prefix}_${name}`;
|
|
497
|
-
merged.components[sec][newName] = cloneAndRewrite(val, refMap, tagMap, prefix, url.stripHash(sourcePath));
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
const srcPaths = (schema.paths || {});
|
|
501
|
-
for (const [p, item] of Object.entries(srcPaths)) {
|
|
502
|
-
let targetPath = p;
|
|
503
|
-
if (merged.paths[p]) {
|
|
504
|
-
const trimmed = p.startsWith("/") ? p.substring(1) : p;
|
|
505
|
-
targetPath = `/${prefix}/${trimmed}`;
|
|
506
|
-
}
|
|
507
|
-
merged.paths[targetPath] = cloneAndRewrite(item, refMap, tagMap, prefix, url.stripHash(sourcePath));
|
|
508
|
-
}
|
|
509
|
-
}
|
|
510
|
-
if (tags.length > 0) {
|
|
511
|
-
merged.tags = tags;
|
|
512
|
-
}
|
|
513
|
-
// Rebuild $refs root using the first input's path to preserve external resolution semantics
|
|
514
|
-
const rootPath = this.schemaManySources[0] || url.cwd();
|
|
515
|
-
this.$refs = new refs_js_1.default();
|
|
516
|
-
const rootRef = this.$refs._add(rootPath);
|
|
517
|
-
rootRef.pathType = url.isFileSystemPath(rootPath) ? "file" : "http";
|
|
518
|
-
rootRef.value = merged;
|
|
519
|
-
this.schema = merged;
|
|
520
|
-
return merged;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
exports.$RefParser = $RefParser;
|
|
524
|
-
var url_js_2 = require("./resolvers/url.js");
|
|
525
|
-
Object.defineProperty(exports, "sendRequest", { enumerable: true, get: function () { return url_js_2.sendRequest; } });
|
package/dist/lib/options.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { JSONSchemaObject, Plugin } from "./types/index.js";
|
|
2
|
-
export interface DereferenceOptions {
|
|
3
|
-
/**
|
|
4
|
-
* Determines whether circular `$ref` pointers are handled.
|
|
5
|
-
*
|
|
6
|
-
* If set to `false`, then a `ReferenceError` will be thrown if the schema contains any circular references.
|
|
7
|
-
*
|
|
8
|
-
* 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`.
|
|
9
|
-
*/
|
|
10
|
-
circular?: boolean | "ignore";
|
|
11
|
-
/**
|
|
12
|
-
* A function, called for each path, which can return true to stop this path and all
|
|
13
|
-
* subpaths from being dereferenced further. This is useful in schemas where some
|
|
14
|
-
* subpaths contain literal $ref keys that should not be dereferenced.
|
|
15
|
-
*/
|
|
16
|
-
excludedPathMatcher?(path: string): boolean;
|
|
17
|
-
/**
|
|
18
|
-
* Callback invoked during dereferencing.
|
|
19
|
-
*
|
|
20
|
-
* @argument {string} path - The path being dereferenced (ie. the `$ref` string)
|
|
21
|
-
* @argument {JSONSchemaObject} value - The JSON-Schema that the `$ref` resolved to
|
|
22
|
-
* @argument {JSONSchemaObject} parent - The parent of the dereferenced object
|
|
23
|
-
* @argument {string} parentPropName - The prop name of the parent object whose value was dereferenced
|
|
24
|
-
*/
|
|
25
|
-
onDereference?(path: string, value: JSONSchemaObject, parent?: JSONSchemaObject, parentPropName?: string): void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Options that determine how JSON schemas are parsed, resolved, and dereferenced.
|
|
29
|
-
*
|
|
30
|
-
* @param [options] - Overridden options
|
|
31
|
-
* @class
|
|
32
|
-
*/
|
|
33
|
-
export interface $RefParserOptions {
|
|
34
|
-
/**
|
|
35
|
-
* The `dereference` options control how JSON Schema `$Ref` Parser will dereference `$ref` pointers within the JSON schema.
|
|
36
|
-
*/
|
|
37
|
-
dereference: DereferenceOptions;
|
|
38
|
-
/**
|
|
39
|
-
* The `parse` options determine how different types of files will be parsed.
|
|
40
|
-
*
|
|
41
|
-
* 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.
|
|
42
|
-
*/
|
|
43
|
-
parse: {
|
|
44
|
-
binary: Plugin;
|
|
45
|
-
json: Plugin;
|
|
46
|
-
text: Plugin;
|
|
47
|
-
yaml: Plugin;
|
|
48
|
-
};
|
|
49
|
-
/**
|
|
50
|
-
* The maximum amount of time (in milliseconds) that JSON Schema $Ref Parser will spend dereferencing a single schema.
|
|
51
|
-
* It will throw a timeout error if the operation takes longer than this.
|
|
52
|
-
*/
|
|
53
|
-
timeoutMs?: number;
|
|
54
|
-
}
|
|
55
|
-
export declare const getJsonSchemaRefParserDefaultOptions: () => $RefParserOptions;
|
|
56
|
-
export type Options = $RefParserOptions;
|
|
57
|
-
type DeepPartial<T> = T extends object ? {
|
|
58
|
-
[P in keyof T]?: DeepPartial<T[P]>;
|
|
59
|
-
} : T;
|
|
60
|
-
export type ParserOptions = DeepPartial<$RefParserOptions>;
|
|
61
|
-
export {};
|
package/dist/lib/options.js
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getJsonSchemaRefParserDefaultOptions = void 0;
|
|
4
|
-
const json_js_1 = require("./parsers/json.js");
|
|
5
|
-
const yaml_js_1 = require("./parsers/yaml.js");
|
|
6
|
-
const text_js_1 = require("./parsers/text.js");
|
|
7
|
-
const binary_js_1 = require("./parsers/binary.js");
|
|
8
|
-
const getJsonSchemaRefParserDefaultOptions = () => ({
|
|
9
|
-
/**
|
|
10
|
-
* Determines the types of JSON references that are allowed.
|
|
11
|
-
*/
|
|
12
|
-
dereference: {
|
|
13
|
-
/**
|
|
14
|
-
* Dereference circular (recursive) JSON references?
|
|
15
|
-
* If false, then a {@link ReferenceError} will be thrown if a circular reference is found.
|
|
16
|
-
* If "ignore", then circular references will not be dereferenced.
|
|
17
|
-
*
|
|
18
|
-
* @type {boolean|string}
|
|
19
|
-
*/
|
|
20
|
-
circular: true,
|
|
21
|
-
/**
|
|
22
|
-
* A function, called for each path, which can return true to stop this path and all
|
|
23
|
-
* subpaths from being dereferenced further. This is useful in schemas where some
|
|
24
|
-
* subpaths contain literal $ref keys that should not be dereferenced.
|
|
25
|
-
*
|
|
26
|
-
* @type {function}
|
|
27
|
-
*/
|
|
28
|
-
excludedPathMatcher: () => false,
|
|
29
|
-
// @ts-expect-error
|
|
30
|
-
referenceResolution: "relative",
|
|
31
|
-
},
|
|
32
|
-
/**
|
|
33
|
-
* Determines how different types of files will be parsed.
|
|
34
|
-
*
|
|
35
|
-
* You can add additional parsers of your own, replace an existing one with
|
|
36
|
-
* your own implementation, or disable any parser by setting it to false.
|
|
37
|
-
*/
|
|
38
|
-
parse: {
|
|
39
|
-
binary: { ...binary_js_1.binaryParser },
|
|
40
|
-
json: { ...json_js_1.jsonParser },
|
|
41
|
-
text: { ...text_js_1.textParser },
|
|
42
|
-
yaml: { ...yaml_js_1.yamlParser },
|
|
43
|
-
},
|
|
44
|
-
});
|
|
45
|
-
exports.getJsonSchemaRefParserDefaultOptions = getJsonSchemaRefParserDefaultOptions;
|
package/dist/lib/parse.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { PluginResult } from "./util/plugins.js";
|
|
2
|
-
import type { $RefParserOptions } from "./options.js";
|
|
3
|
-
import type { FileInfo } from "./types/index.js";
|
|
4
|
-
/**
|
|
5
|
-
* Prepares the file object so we can populate it with data and other values
|
|
6
|
-
* when it's read and parsed. This "file object" will be passed to all
|
|
7
|
-
* resolvers and parsers.
|
|
8
|
-
*/
|
|
9
|
-
export declare function newFile(path: string): FileInfo;
|
|
10
|
-
/**
|
|
11
|
-
* Parses the given file's contents, using the configured parser plugins.
|
|
12
|
-
*/
|
|
13
|
-
export declare const parseFile: (file: FileInfo, options: $RefParserOptions) => Promise<PluginResult>;
|