-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
Copy pathrefs.ts
236 lines (211 loc) · 7.1 KB
/
refs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { ono } from "@jsdevtools/ono";
import $Ref from "./ref.js";
import * as url from "./util/url.js";
import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
import type { JSONSchema } from "./types/index.js";
import type $RefParserOptions from "./options.js";
import convertPathToPosix from "./util/convert-path-to-posix";
interface $RefsMap {
[url: string]: $Ref;
}
/**
* When you call the resolve method, the value that gets passed to the callback function (or Promise) is a $Refs object. This same object is accessible via the parser.$refs property of $RefParser objects.
*
* This object is a map of JSON References and their resolved values. It also has several convenient helper methods that make it easy for you to navigate and manipulate the JSON References.
*
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html
*/
export default class $Refs {
/**
* This property is true if the schema contains any circular references. You may want to check this property before serializing the dereferenced schema as JSON, since JSON.stringify() does not support circular references by default.
*
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#circular
*/
public circular: boolean;
/**
* Returns the paths/URLs of all the files in your schema (including the main schema file).
*
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#pathstypes
*
* @param types (optional) Optionally only return certain types of paths ("file", "http", etc.)
*/
paths(...types: string[]): string[] {
const paths = getPaths(this._$refs, types);
return paths.map((path) => {
return convertPathToPosix(path.decoded);
});
}
/**
* Returns a map of paths/URLs and their correspond values.
*
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#valuestypes
*
* @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
*/
values(...types: string[]): JSONSchema {
const $refs = this._$refs;
const paths = getPaths($refs, types);
return paths.reduce<Record<string, any>>((obj, path) => {
obj[convertPathToPosix(path.decoded)] = $refs[path.encoded].value;
return obj;
}, {});
}
/**
* Returns `true` if the given path exists in the schema; otherwise, returns `false`
*
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html#existsref
*
* @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
*/
/**
* Determines whether the given JSON reference exists.
*
* @param path - The path being resolved, optionally with a JSON pointer in the hash
* @param [options]
* @returns
*/
exists(path: string, options: any) {
try {
this._resolve(path, "", options);
return true;
} catch (e) {
return false;
}
}
/**
* Resolves the given JSON reference and returns the resolved value.
*
* @param path - The path being resolved, with a JSON pointer in the hash
* @param [options]
* @returns - Returns the resolved value
*/
get(path: string, options?: $RefParserOptions): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type {
return this._resolve(path, "", options)!.value;
}
/**
* Sets the value at the given path in the schema. If the property, or any of its parents, don't exist, they will be created.
*
* @param $ref The JSON Reference path, optionally with a JSON Pointer in the hash
* @param value The value to assign. Can be anything (object, string, number, etc.)
*/
set(path: any, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type) {
const absPath = url.resolve(this._root$Ref.path!, path);
const withoutHash = url.stripHash(absPath);
const $ref = this._$refs[withoutHash];
if (!$ref) {
throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
}
$ref.set(absPath, value);
}
/**
* Returns the specified {@link $Ref} object, or undefined.
*
* @param path - The path being resolved, optionally with a JSON pointer in the hash
* @returns
* @protected
*/
_get$Ref(path: any) {
path = url.resolve(this._root$Ref.path!, path);
const withoutHash = url.stripHash(path);
return this._$refs[withoutHash];
}
/**
* Creates a new {@link $Ref} object and adds it to this {@link $Refs} object.
*
* @param path - The file path or URL of the referenced file
*/
_add(path: string) {
const withoutHash = url.stripHash(path);
const $ref = new $Ref(this);
$ref.path = withoutHash;
this._$refs[withoutHash] = $ref;
this._root$Ref = this._root$Ref || $ref;
return $ref;
}
/**
* Resolves the given JSON reference.
*
* @param path - The path being resolved, optionally with a JSON pointer in the hash
* @param pathFromRoot - The path of `obj` from the schema root
* @param [options]
* @returns
* @protected
*/
_resolve(path: string, pathFromRoot: string, options?: any) {
const absPath = url.resolve(this._root$Ref.path!, path);
const withoutHash = url.stripHash(absPath);
const $ref = this._$refs[withoutHash];
if (!$ref) {
throw ono(`Error resolving $ref pointer "${path}". \n"${withoutHash}" not found.`);
}
return $ref.resolve(absPath, options, path, pathFromRoot);
}
/**
* A map of paths/urls to {@link $Ref} objects
*
* @type {object}
* @protected
*/
_$refs: $RefsMap = {};
/**
* The {@link $Ref} object that is the root of the JSON schema.
*
* @type {$Ref}
* @protected
*/
_root$Ref: $Ref;
constructor() {
/**
* Indicates whether the schema contains any circular references.
*
* @type {boolean}
*/
this.circular = false;
this._$refs = {};
// @ts-ignore
this._root$Ref = null;
}
/**
* Returns the paths of all the files/URLs that are referenced by the JSON schema,
* including the schema itself.
*
* @param [types] - Only return paths of the given types ("file", "http", etc.)
* @returns
*/
/**
* Returns the map of JSON references and their resolved values.
*
* @param [types] - Only return references of the given types ("file", "http", etc.)
* @returns
*/
/**
* Returns a POJO (plain old JavaScript object) for serialization as JSON.
*
* @returns {object}
*/
toJSON = this.values;
}
/**
* Returns the encoded and decoded paths keys of the given object.
*
* @param $refs - The object whose keys are URL-encoded paths
* @param [types] - Only return paths of the given types ("file", "http", etc.)
* @returns
*/
function getPaths($refs: $RefsMap, types: string[]) {
let paths = Object.keys($refs);
// Filter the paths by type
types = Array.isArray(types[0]) ? types[0] : Array.prototype.slice.call(types);
if (types.length > 0 && types[0]) {
paths = paths.filter((key) => {
return types.includes($refs[key].pathType as string);
});
}
// Decode local filesystem paths
return paths.map((path) => {
return {
encoded: path,
decoded: $refs[path].pathType === "file" ? url.toFileSystemPath(path, true) : path,
};
});
}