|
15 | 15 | Install using [npm](https://docs.npmjs.com/about-npm/):
|
16 | 16 |
|
17 | 17 | ```bash
|
18 |
| -npm install @apidevtools/json-schema-ref-parser |
19 |
| -yarn add @apidevtools/json-schema-ref-parser |
20 |
| -bun add @apidevtools/json-schema-ref-parser |
| 18 | +npm install @hey-api/json-schema-ref-parser |
| 19 | +yarn add @hey-api/json-schema-ref-parser |
| 20 | +bun add @hey-api/json-schema-ref-parser |
21 | 21 | ```
|
22 | 22 |
|
23 | 23 | ## The Problem:
|
@@ -69,21 +69,61 @@ JavaScript objects.
|
69 | 69 | ## Example
|
70 | 70 |
|
71 | 71 | ```javascript
|
72 |
| -import $RefParser from "@apidevtools/json-schema-ref-parser"; |
| 72 | +import { $RefParser } from "@hey-api/json-schema-ref-parser"; |
73 | 73 |
|
74 | 74 | try {
|
75 |
| - await $RefParser.dereference(mySchema); |
76 |
| - // note - by default, mySchema is modified in place, and the returned value is a reference to the same object |
77 |
| - console.log(mySchema.definitions.person.properties.firstName); |
78 |
| - |
79 |
| - // if you want to avoid modifying the original schema, you can disable the `mutateInputSchema` option |
80 |
| - let clonedSchema = await $RefParser.dereference(mySchema, { mutateInputSchema: false }); |
81 |
| - console.log(clonedSchema.definitions.person.properties.firstName); |
| 75 | + const parser = new $RefParser(); |
| 76 | + await parser.dereference({ pathOrUrlOrSchema: mySchema }); |
| 77 | + console.log(parser.schema.definitions.person.properties.firstName); |
82 | 78 | } catch (err) {
|
83 | 79 | console.error(err);
|
84 | 80 | }
|
85 | 81 | ```
|
86 | 82 |
|
| 83 | +### New in this fork (@hey-api) |
| 84 | + |
| 85 | +- **Multiple inputs with `bundleMany`**: Merge and bundle several OpenAPI/JSON Schema inputs (files, URLs, or raw objects) into a single schema. Components are prefixed to avoid name collisions, paths are namespaced on conflict, and `$ref`s are rewritten accordingly. |
| 86 | + |
| 87 | +```javascript |
| 88 | +import { $RefParser } from "@hey-api/json-schema-ref-parser"; |
| 89 | + |
| 90 | +const parser = new $RefParser(); |
| 91 | +const merged = await parser.bundleMany({ |
| 92 | + pathOrUrlOrSchemas: [ |
| 93 | + "./specs/a.yaml", |
| 94 | + "https://example.com/b.yaml", |
| 95 | + { openapi: "3.1.0", info: { title: "Inline" }, paths: {} }, |
| 96 | + ], |
| 97 | +}); |
| 98 | + |
| 99 | +// merged.components.* will contain prefixed names like a_<name>, b_<name>, etc. |
| 100 | +``` |
| 101 | + |
| 102 | +- **Dereference hooks**: Fine-tune dereferencing with `excludedPathMatcher(path) => boolean` to skip subpaths and `onDereference(path, value, parent, parentPropName)` to observe replacements. |
| 103 | + |
| 104 | +```javascript |
| 105 | +const parser = new $RefParser(); |
| 106 | +parser.options.dereference.excludedPathMatcher = (p) => p.includes("/example/"); |
| 107 | +parser.options.dereference.onDereference = (p, v) => { |
| 108 | + // inspect p / v as needed |
| 109 | +}; |
| 110 | +await parser.dereference({ pathOrUrlOrSchema: "./openapi.yaml" }); |
| 111 | +``` |
| 112 | + |
| 113 | +- **Smart input resolution**: You can pass a file path, URL, or raw schema object. If a raw schema includes `$id`, it is used as the base URL for resolving relative `$ref`s. |
| 114 | + |
| 115 | +```javascript |
| 116 | +await new $RefParser().bundle({ |
| 117 | + pathOrUrlOrSchema: { |
| 118 | + $id: "https://api.example.com/openapi.json", |
| 119 | + openapi: "3.1.0", |
| 120 | + paths: { |
| 121 | + "/ping": { get: { responses: { 200: { description: "ok" } } } }, |
| 122 | + }, |
| 123 | + }, |
| 124 | +}); |
| 125 | +``` |
| 126 | + |
87 | 127 | ## Polyfills
|
88 | 128 |
|
89 | 129 | If you are using Node.js < 18, you'll need a polyfill for `fetch`,
|
|
0 commit comments