@hey-api/json-schema-ref-parser 0.0.0-next-20260212230650
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/LICENSE.md +21 -0
- package/README.md +117 -0
- package/dist/index.d.mts +645 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +2124 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -0
- 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/src/bundle.ts +743 -0
- package/src/dereference.ts +290 -0
- package/src/index.ts +599 -0
- package/src/options.ts +112 -0
- package/src/parse.ts +65 -0
- package/src/parsers/binary.ts +13 -0
- package/src/parsers/json.ts +38 -0
- package/src/parsers/text.ts +21 -0
- package/src/parsers/yaml.ts +26 -0
- package/src/pointer.ts +352 -0
- package/src/ref.ts +283 -0
- package/src/refs.ts +231 -0
- package/src/resolve-external.ts +142 -0
- package/src/resolvers/file.ts +25 -0
- package/src/resolvers/url.ts +99 -0
- package/src/types/index.ts +58 -0
- package/src/util/convert-path-to-posix.ts +8 -0
- package/src/util/errors.ts +155 -0
- package/src/util/is-windows.ts +2 -0
- package/src/util/plugins.ts +55 -0
- package/src/util/url.ts +265 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) Hey API
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
# JSON Schema $Ref Parser
|
|
2
|
+
|
|
3
|
+
#### Parse, Resolve, and Dereference JSON Schema $ref pointers
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install using [npm](https://docs.npmjs.com/about-npm/):
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @hey-api/json-schema-ref-parser
|
|
11
|
+
yarn add @hey-api/json-schema-ref-parser
|
|
12
|
+
bun add @hey-api/json-schema-ref-parser
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## The Problem:
|
|
16
|
+
|
|
17
|
+
You've got a JSON Schema with `$ref` pointers to other files and/or URLs. Maybe you know all the referenced files ahead
|
|
18
|
+
of time. Maybe you don't. Maybe some are local files, and others are remote URLs. Maybe they are a mix of JSON and YAML
|
|
19
|
+
format. Maybe some of the files contain cross-references to each other.
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"definitions": {
|
|
24
|
+
"person": {
|
|
25
|
+
// references an external file
|
|
26
|
+
"$ref": "schemas/people/Bruce-Wayne.json"
|
|
27
|
+
},
|
|
28
|
+
"place": {
|
|
29
|
+
// references a sub-schema in an external file
|
|
30
|
+
"$ref": "schemas/places.yaml#/definitions/Gotham-City"
|
|
31
|
+
},
|
|
32
|
+
"thing": {
|
|
33
|
+
// references a URL
|
|
34
|
+
"$ref": "http://wayne-enterprises.com/things/batmobile"
|
|
35
|
+
},
|
|
36
|
+
"color": {
|
|
37
|
+
// references a value in an external file via an internal reference
|
|
38
|
+
"$ref": "#/definitions/thing/properties/colors/black-as-the-night"
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## The Solution:
|
|
45
|
+
|
|
46
|
+
JSON Schema $Ref Parser is a full [JSON Reference](https://tools.ietf.org/html/draft-pbryan-zyp-json-ref-03)
|
|
47
|
+
and [JSON Pointer](https://tools.ietf.org/html/rfc6901) implementation that crawls even the most
|
|
48
|
+
complex [JSON Schemas](http://json-schema.org/latest/json-schema-core.html) and gives you simple, straightforward
|
|
49
|
+
JavaScript objects.
|
|
50
|
+
|
|
51
|
+
- Use **JSON** or **YAML** schemas — or even a mix of both!
|
|
52
|
+
- Supports `$ref` pointers to external files and URLs, as well as custom sources such as databases
|
|
53
|
+
- Can bundle multiple files into a single schema that only has _internal_ `$ref` pointers
|
|
54
|
+
- Can dereference your schema, producing a plain-old JavaScript object that's easy to work with
|
|
55
|
+
- Supports circular references, nested references,
|
|
56
|
+
back-references, and cross-references between files
|
|
57
|
+
- Maintains object reference equality — `$ref` pointers to the same value always resolve to the same object
|
|
58
|
+
instance
|
|
59
|
+
- Compatible with Node LTS and beyond, and all major web browsers on Windows, Mac, and Linux
|
|
60
|
+
|
|
61
|
+
## Example
|
|
62
|
+
|
|
63
|
+
```javascript
|
|
64
|
+
import { $RefParser } from '@hey-api/json-schema-ref-parser';
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const parser = new $RefParser();
|
|
68
|
+
await parser.dereference({ pathOrUrlOrSchema: mySchema });
|
|
69
|
+
console.log(parser.schema.definitions.person.properties.firstName);
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error(err);
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### New in this fork (@hey-api)
|
|
76
|
+
|
|
77
|
+
- **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.
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
import { $RefParser } from '@hey-api/json-schema-ref-parser';
|
|
81
|
+
|
|
82
|
+
const parser = new $RefParser();
|
|
83
|
+
const merged = await parser.bundleMany({
|
|
84
|
+
pathOrUrlOrSchemas: [
|
|
85
|
+
'./specs/a.yaml',
|
|
86
|
+
'https://example.com/b.yaml',
|
|
87
|
+
{ openapi: '3.1.0', info: { title: 'Inline' }, paths: {} },
|
|
88
|
+
],
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// merged.components.* will contain prefixed names like a_<name>, b_<name>, etc.
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
- **Dereference hooks**: Fine-tune dereferencing with `excludedPathMatcher(path) => boolean` to skip subpaths and `onDereference(path, value, parent, parentPropName)` to observe replacements.
|
|
95
|
+
|
|
96
|
+
```javascript
|
|
97
|
+
const parser = new $RefParser();
|
|
98
|
+
parser.options.dereference.excludedPathMatcher = (p) => p.includes('/example/');
|
|
99
|
+
parser.options.dereference.onDereference = (p, v) => {
|
|
100
|
+
// inspect p / v as needed
|
|
101
|
+
};
|
|
102
|
+
await parser.dereference({ pathOrUrlOrSchema: './openapi.yaml' });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
- **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.
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
await new $RefParser().bundle({
|
|
109
|
+
pathOrUrlOrSchema: {
|
|
110
|
+
$id: 'https://api.example.com/openapi.json',
|
|
111
|
+
openapi: '3.1.0',
|
|
112
|
+
paths: {
|
|
113
|
+
'/ping': { get: { responses: { 200: { description: 'ok' } } } },
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
});
|
|
117
|
+
```
|