@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.
Files changed (92) hide show
  1. package/README.md +9 -84
  2. package/dist/index.d.mts +629 -0
  3. package/dist/index.d.mts.map +1 -0
  4. package/dist/index.mjs +1887 -0
  5. package/dist/index.mjs.map +1 -0
  6. package/package.json +42 -78
  7. package/src/__tests__/bundle.test.ts +59 -0
  8. package/src/__tests__/index.test.ts +43 -0
  9. package/src/__tests__/pointer.test.ts +34 -0
  10. package/src/__tests__/utils.ts +3 -0
  11. package/{lib → src}/bundle.ts +143 -229
  12. package/{lib → src}/dereference.ts +20 -43
  13. package/{lib → src}/index.ts +103 -125
  14. package/{lib → src}/options.ts +13 -9
  15. package/{lib → src}/parse.ts +19 -15
  16. package/src/parsers/binary.ts +13 -0
  17. package/{lib → src}/parsers/json.ts +5 -6
  18. package/src/parsers/text.ts +21 -0
  19. package/{lib → src}/parsers/yaml.ts +9 -9
  20. package/{lib → src}/pointer.ts +42 -23
  21. package/{lib → src}/ref.ts +25 -21
  22. package/{lib → src}/refs.ts +23 -26
  23. package/{lib → src}/resolve-external.ts +91 -60
  24. package/{lib → src}/resolvers/file.ts +7 -10
  25. package/{lib → src}/resolvers/url.ts +12 -8
  26. package/{lib → src}/types/index.ts +9 -2
  27. package/src/util/convert-path-to-posix.ts +8 -0
  28. package/{lib → src}/util/errors.ts +38 -36
  29. package/{lib → src}/util/is-windows.ts +1 -1
  30. package/{lib → src}/util/plugins.ts +7 -8
  31. package/{lib → src}/util/url.ts +41 -42
  32. package/dist/lib/__tests__/bundle.test.d.ts +0 -1
  33. package/dist/lib/__tests__/bundle.test.js +0 -50
  34. package/dist/lib/__tests__/index.test.d.ts +0 -1
  35. package/dist/lib/__tests__/index.test.js +0 -43
  36. package/dist/lib/__tests__/pointer.test.d.ts +0 -1
  37. package/dist/lib/__tests__/pointer.test.js +0 -27
  38. package/dist/lib/bundle.d.ts +0 -26
  39. package/dist/lib/bundle.js +0 -600
  40. package/dist/lib/dereference.d.ts +0 -11
  41. package/dist/lib/dereference.js +0 -226
  42. package/dist/lib/index.d.ts +0 -92
  43. package/dist/lib/index.js +0 -525
  44. package/dist/lib/options.d.ts +0 -61
  45. package/dist/lib/options.js +0 -45
  46. package/dist/lib/parse.d.ts +0 -13
  47. package/dist/lib/parse.js +0 -87
  48. package/dist/lib/parsers/binary.d.ts +0 -2
  49. package/dist/lib/parsers/binary.js +0 -12
  50. package/dist/lib/parsers/json.d.ts +0 -2
  51. package/dist/lib/parsers/json.js +0 -38
  52. package/dist/lib/parsers/text.d.ts +0 -2
  53. package/dist/lib/parsers/text.js +0 -18
  54. package/dist/lib/parsers/yaml.d.ts +0 -2
  55. package/dist/lib/parsers/yaml.js +0 -28
  56. package/dist/lib/pointer.d.ts +0 -88
  57. package/dist/lib/pointer.js +0 -297
  58. package/dist/lib/ref.d.ts +0 -180
  59. package/dist/lib/ref.js +0 -226
  60. package/dist/lib/refs.d.ts +0 -127
  61. package/dist/lib/refs.js +0 -232
  62. package/dist/lib/resolve-external.d.ts +0 -13
  63. package/dist/lib/resolve-external.js +0 -151
  64. package/dist/lib/resolvers/file.d.ts +0 -6
  65. package/dist/lib/resolvers/file.js +0 -61
  66. package/dist/lib/resolvers/url.d.ts +0 -17
  67. package/dist/lib/resolvers/url.js +0 -62
  68. package/dist/lib/types/index.d.ts +0 -43
  69. package/dist/lib/types/index.js +0 -2
  70. package/dist/lib/util/convert-path-to-posix.d.ts +0 -1
  71. package/dist/lib/util/convert-path-to-posix.js +0 -14
  72. package/dist/lib/util/errors.d.ts +0 -56
  73. package/dist/lib/util/errors.js +0 -112
  74. package/dist/lib/util/is-windows.d.ts +0 -1
  75. package/dist/lib/util/is-windows.js +0 -6
  76. package/dist/lib/util/plugins.d.ts +0 -16
  77. package/dist/lib/util/plugins.js +0 -45
  78. package/dist/lib/util/url.d.ts +0 -79
  79. package/dist/lib/util/url.js +0 -285
  80. package/dist/vite.config.d.ts +0 -2
  81. package/dist/vite.config.js +0 -19
  82. package/lib/__tests__/bundle.test.ts +0 -52
  83. package/lib/__tests__/index.test.ts +0 -45
  84. package/lib/__tests__/pointer.test.ts +0 -26
  85. package/lib/__tests__/spec/circular-ref-with-description.json +0 -11
  86. package/lib/__tests__/spec/multiple-refs.json +0 -34
  87. package/lib/__tests__/spec/openapi-paths-ref.json +0 -46
  88. package/lib/__tests__/spec/path-parameter.json +0 -16
  89. package/lib/parsers/binary.ts +0 -13
  90. package/lib/parsers/text.ts +0 -21
  91. package/lib/util/convert-path-to-posix.ts +0 -11
  92. /package/{LICENSE → LICENSE.md} +0 -0
@@ -1,226 +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
- const ref_js_1 = __importDefault(require("./ref.js"));
40
- const cloneDeep_1 = __importDefault(require("lodash/cloneDeep"));
41
- const pointer_js_1 = __importDefault(require("./pointer.js"));
42
- const ono_1 = require("@jsdevtools/ono");
43
- const url = __importStar(require("./util/url.js"));
44
- const errors_1 = require("./util/errors");
45
- exports.default = dereference;
46
- /**
47
- * Crawls the JSON schema, finds all JSON references, and dereferences them.
48
- * This method mutates the JSON schema object, replacing JSON references with their resolved value.
49
- *
50
- * @param parser
51
- * @param options
52
- */
53
- function dereference(parser, options) {
54
- const start = Date.now();
55
- // console.log('Dereferencing $ref pointers in %s', parser.$refs._root$Ref.path);
56
- const dereferenced = crawl(parser.schema, parser.$refs._root$Ref.path, "#", new Set(), new Set(), new Map(), parser.$refs, options, start);
57
- parser.$refs.circular = dereferenced.circular;
58
- parser.schema = dereferenced.value;
59
- }
60
- /**
61
- * Recursively crawls the given value, and dereferences any JSON references.
62
- *
63
- * @param obj - The value to crawl. If it's not an object or array, it will be ignored.
64
- * @param path - The full path of `obj`, possibly with a JSON Pointer in the hash
65
- * @param pathFromRoot - The path of `obj` from the schema root
66
- * @param parents - An array of the parent objects that have already been dereferenced
67
- * @param processedObjects - An array of all the objects that have already been processed
68
- * @param dereferencedCache - An map of all the dereferenced objects
69
- * @param $refs
70
- * @param options
71
- * @param startTime - The time when the dereferencing started
72
- * @returns
73
- */
74
- function crawl(obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime) {
75
- let dereferenced;
76
- const result = {
77
- value: obj,
78
- circular: false,
79
- };
80
- if (options && options.timeoutMs) {
81
- if (Date.now() - startTime > options.timeoutMs) {
82
- throw new errors_1.TimeoutError(options.timeoutMs);
83
- }
84
- }
85
- const derefOptions = (options.dereference || {});
86
- const isExcludedPath = derefOptions.excludedPathMatcher || (() => false);
87
- if (derefOptions?.circular === "ignore" || !processedObjects.has(obj)) {
88
- if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !isExcludedPath(pathFromRoot)) {
89
- parents.add(obj);
90
- processedObjects.add(obj);
91
- if (ref_js_1.default.isAllowed$Ref(obj)) {
92
- dereferenced = dereference$Ref(obj, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime);
93
- result.circular = dereferenced.circular;
94
- result.value = dereferenced.value;
95
- }
96
- else {
97
- for (const key of Object.keys(obj)) {
98
- const keyPath = pointer_js_1.default.join(path, key);
99
- const keyPathFromRoot = pointer_js_1.default.join(pathFromRoot, key);
100
- if (isExcludedPath(keyPathFromRoot)) {
101
- continue;
102
- }
103
- const value = obj[key];
104
- let circular = false;
105
- if (ref_js_1.default.isAllowed$Ref(value)) {
106
- dereferenced = dereference$Ref(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime);
107
- circular = dereferenced.circular;
108
- // Avoid pointless mutations; breaks frozen objects to no profit
109
- if (obj[key] !== dereferenced.value) {
110
- obj[key] = dereferenced.value;
111
- derefOptions?.onDereference?.(value.$ref, obj[key], obj, key);
112
- }
113
- }
114
- else {
115
- if (!parents.has(value)) {
116
- dereferenced = crawl(value, keyPath, keyPathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime);
117
- circular = dereferenced.circular;
118
- // Avoid pointless mutations; breaks frozen objects to no profit
119
- if (obj[key] !== dereferenced.value) {
120
- obj[key] = dereferenced.value;
121
- }
122
- }
123
- else {
124
- circular = foundCircularReference(keyPath, $refs, options);
125
- }
126
- }
127
- // Set the "isCircular" flag if this or any other property is circular
128
- result.circular = result.circular || circular;
129
- }
130
- }
131
- parents.delete(obj);
132
- }
133
- }
134
- return result;
135
- }
136
- /**
137
- * Dereferences the given JSON Reference, and then crawls the resulting value.
138
- *
139
- * @param $ref - The JSON Reference to resolve
140
- * @param path - The full path of `$ref`, possibly with a JSON Pointer in the hash
141
- * @param pathFromRoot - The path of `$ref` from the schema root
142
- * @param parents - An array of the parent objects that have already been dereferenced
143
- * @param processedObjects - An array of all the objects that have already been dereferenced
144
- * @param dereferencedCache - An map of all the dereferenced objects
145
- * @param $refs
146
- * @param options
147
- * @returns
148
- */
149
- function dereference$Ref($ref, path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime) {
150
- const $refPath = url.resolve(path, $ref.$ref);
151
- const cache = dereferencedCache.get($refPath);
152
- if (cache && !cache.circular) {
153
- const refKeys = Object.keys($ref);
154
- if (refKeys.length > 1) {
155
- const extraKeys = {};
156
- for (const key of refKeys) {
157
- if (key !== "$ref" && !(key in cache.value)) {
158
- // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
159
- extraKeys[key] = $ref[key];
160
- }
161
- }
162
- return {
163
- circular: cache.circular,
164
- value: Object.assign({}, (0, cloneDeep_1.default)(cache.value), extraKeys),
165
- };
166
- }
167
- // Return a deep-cloned value so each occurrence is an independent copy
168
- return { circular: cache.circular, value: (0, cloneDeep_1.default)(cache.value) };
169
- }
170
- const pointer = $refs._resolve($refPath, path, options);
171
- if (pointer === null) {
172
- return {
173
- circular: false,
174
- value: null,
175
- };
176
- }
177
- // Check for circular references
178
- const directCircular = pointer.circular;
179
- let circular = directCircular || parents.has(pointer.value);
180
- if (circular) {
181
- foundCircularReference(path, $refs, options);
182
- }
183
- // Dereference the JSON reference
184
- let dereferencedValue = ref_js_1.default.dereference($ref, pointer.value);
185
- // Crawl the dereferenced value (unless it's circular)
186
- if (!circular) {
187
- // Determine if the dereferenced value is circular
188
- const dereferenced = crawl(dereferencedValue, pointer.path, pathFromRoot, parents, processedObjects, dereferencedCache, $refs, options, startTime);
189
- circular = dereferenced.circular;
190
- dereferencedValue = dereferenced.value;
191
- }
192
- if (circular && !directCircular && options.dereference?.circular === "ignore") {
193
- // The user has chosen to "ignore" circular references, so don't change the value
194
- dereferencedValue = $ref;
195
- }
196
- if (directCircular) {
197
- // The pointer is a DIRECT circular reference (i.e. it references itself).
198
- // So replace the $ref path with the absolute path from the JSON Schema root
199
- dereferencedValue.$ref = pathFromRoot;
200
- }
201
- const dereferencedObject = {
202
- circular,
203
- value: dereferencedValue,
204
- };
205
- // only cache if no extra properties than $ref
206
- if (Object.keys($ref).length === 1) {
207
- dereferencedCache.set($refPath, dereferencedObject);
208
- }
209
- return dereferencedObject;
210
- }
211
- /**
212
- * Called when a circular reference is found.
213
- * It sets the {@link $Refs#circular} flag, and throws an error if options.dereference.circular is false.
214
- *
215
- * @param keyPath - The JSON Reference path of the circular reference
216
- * @param $refs
217
- * @param options
218
- * @returns - always returns true, to indicate that a circular reference was found
219
- */
220
- function foundCircularReference(keyPath, $refs, options) {
221
- $refs.circular = true;
222
- if (!options.dereference.circular) {
223
- throw ono_1.ono.reference(`Circular $ref pointer found at ${keyPath}`);
224
- }
225
- return true;
226
- }
@@ -1,92 +0,0 @@
1
- import $Refs from "./refs.js";
2
- import type { JSONSchema } from "./types/index.js";
3
- interface ResolvedInput {
4
- path: string;
5
- schema: string | JSONSchema | Buffer | Awaited<JSONSchema> | undefined;
6
- type: "file" | "json" | "url";
7
- }
8
- export declare const getResolvedInput: ({ pathOrUrlOrSchema, }: {
9
- pathOrUrlOrSchema: JSONSchema | string | unknown;
10
- }) => ResolvedInput;
11
- /**
12
- * This class parses a JSON schema, builds a map of its JSON references and their resolved values,
13
- * and provides methods for traversing, manipulating, and dereferencing those references.
14
- */
15
- export declare class $RefParser {
16
- /**
17
- * The resolved JSON references
18
- *
19
- * @type {$Refs}
20
- * @readonly
21
- */
22
- $refs: $Refs<JSONSchema>;
23
- options: import("./options.js").$RefParserOptions;
24
- /**
25
- * The parsed (and possibly dereferenced) JSON schema object
26
- *
27
- * @type {object}
28
- * @readonly
29
- */
30
- schema: JSONSchema | null;
31
- schemaMany: JSONSchema[];
32
- schemaManySources: string[];
33
- sourcePathToPrefix: Map<string, string>;
34
- /**
35
- * 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.
36
- *
37
- * This also eliminates the risk of circular references, so the schema can be safely serialized using `JSON.stringify()`.
38
- *
39
- * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#bundleschema-options-callback
40
- *
41
- * @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
42
- */
43
- bundle({ arrayBuffer, fetch, pathOrUrlOrSchema, resolvedInput, }: {
44
- arrayBuffer?: ArrayBuffer;
45
- fetch?: RequestInit;
46
- pathOrUrlOrSchema: JSONSchema | string | unknown;
47
- resolvedInput?: ResolvedInput;
48
- }): Promise<JSONSchema>;
49
- /**
50
- * Bundles multiple roots (files/URLs/objects) into a single schema by creating a synthetic root
51
- * that references each input, resolving all externals, and then hoisting via the existing bundler.
52
- */
53
- bundleMany({ arrayBuffer, fetch, pathOrUrlOrSchemas, resolvedInputs, }: {
54
- arrayBuffer?: ArrayBuffer[];
55
- fetch?: RequestInit;
56
- pathOrUrlOrSchemas: Array<JSONSchema | string | unknown>;
57
- resolvedInputs?: ResolvedInput[];
58
- }): Promise<JSONSchema>;
59
- /**
60
- * 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.
61
- *
62
- * 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.
63
- *
64
- * See https://apitools.dev/json-schema-ref-parser/docs/ref-parser.html#dereferenceschema-options-callback
65
- *
66
- * @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
67
- */
68
- dereference({ fetch, pathOrUrlOrSchema, }: {
69
- fetch?: RequestInit;
70
- pathOrUrlOrSchema: JSONSchema | string | unknown;
71
- }): Promise<JSONSchema>;
72
- /**
73
- * Parses the given JSON schema.
74
- * This method does not resolve any JSON references.
75
- * It just reads a single file in JSON or YAML format, and parse it as a JavaScript object.
76
- *
77
- * @param pathOrUrlOrSchema A JSON Schema object, or the file path or URL of a JSON Schema file.
78
- * @returns - The returned promise resolves with the parsed JSON schema object.
79
- */
80
- parse({ arrayBuffer, fetch, pathOrUrlOrSchema, resolvedInput: _resolvedInput, }: {
81
- arrayBuffer?: ArrayBuffer;
82
- fetch?: RequestInit;
83
- pathOrUrlOrSchema: JSONSchema | string | unknown;
84
- resolvedInput?: ResolvedInput;
85
- }): Promise<{
86
- schema: JSONSchema;
87
- }>;
88
- private parseMany;
89
- mergeMany(): JSONSchema;
90
- }
91
- export { sendRequest } from "./resolvers/url.js";
92
- export type { JSONSchema } from "./types/index.js";