@apidevtools/json-schema-ref-parser 11.4.2 → 11.5.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/dist/lib/bundle.d.ts +5 -2
- package/dist/lib/bundle.js +4 -4
- package/dist/lib/dereference.d.ts +4 -1
- package/dist/lib/dereference.js +6 -7
- package/dist/lib/index.d.ts +54 -53
- package/dist/lib/normalize-args.d.ts +6 -6
- package/dist/lib/normalize-args.js +4 -1
- package/dist/lib/options.d.ts +47 -46
- package/dist/lib/parse.d.ts +3 -2
- package/dist/lib/parse.js +3 -2
- package/dist/lib/pointer.d.ts +6 -5
- package/dist/lib/ref.d.ts +9 -8
- package/dist/lib/ref.js +1 -1
- package/dist/lib/refs.d.ts +14 -14
- package/dist/lib/refs.js +1 -1
- package/dist/lib/resolve-external.d.ts +4 -3
- package/dist/lib/resolve-external.js +4 -4
- package/dist/lib/resolvers/file.d.ts +2 -2
- package/dist/lib/resolvers/http.d.ts +2 -2
- package/dist/lib/types/index.d.ts +5 -5
- package/dist/lib/util/errors.d.ts +6 -4
- package/dist/lib/util/plugins.d.ts +4 -5
- package/lib/bundle.ts +23 -19
- package/lib/dereference.ts +21 -17
- package/lib/index.ts +165 -102
- package/lib/normalize-args.ts +13 -8
- package/lib/options.ts +52 -50
- package/lib/parse.ts +22 -10
- package/lib/pointer.ts +6 -5
- package/lib/ref.ts +12 -11
- package/lib/refs.ts +13 -13
- package/lib/resolve-external.ts +21 -14
- package/lib/resolvers/file.ts +2 -2
- package/lib/resolvers/http.ts +8 -4
- package/lib/types/index.ts +5 -5
- package/lib/util/errors.ts +14 -6
- package/lib/util/plugins.ts +14 -15
- package/package.json +1 -1
package/lib/pointer.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type $RefParserOptions from "./options.js";
|
|
|
3
3
|
import $Ref from "./ref.js";
|
|
4
4
|
import * as url from "./util/url.js";
|
|
5
5
|
import { JSONParserError, InvalidPointerError, MissingPointerError, isHandledError } from "./util/errors.js";
|
|
6
|
+
import type { JSONSchema } from "./types";
|
|
6
7
|
|
|
7
8
|
const slashes = /\//g;
|
|
8
9
|
const tildes = /~/g;
|
|
@@ -25,11 +26,11 @@ const safeDecodeURIComponent = (encodedURIComponent: string): string => {
|
|
|
25
26
|
* @param [friendlyPath] - The original user-specified path (used for error messages)
|
|
26
27
|
* @class
|
|
27
28
|
*/
|
|
28
|
-
class Pointer {
|
|
29
|
+
class Pointer<S = JSONSchema> {
|
|
29
30
|
/**
|
|
30
31
|
* The {@link $Ref} object that contains this {@link Pointer} object.
|
|
31
32
|
*/
|
|
32
|
-
$ref: $Ref
|
|
33
|
+
$ref: $Ref<S>;
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* The file path or URL, containing the JSON pointer in the hash.
|
|
@@ -58,7 +59,7 @@ class Pointer {
|
|
|
58
59
|
*/
|
|
59
60
|
indirections: number;
|
|
60
61
|
|
|
61
|
-
constructor($ref: $Ref
|
|
62
|
+
constructor($ref: $Ref<S>, path: string, friendlyPath?: string) {
|
|
62
63
|
this.$ref = $ref;
|
|
63
64
|
|
|
64
65
|
this.path = path;
|
|
@@ -85,7 +86,7 @@ class Pointer {
|
|
|
85
86
|
* the {@link Pointer#$ref} and {@link Pointer#path} will reflect the resolution path
|
|
86
87
|
* of the resolved value.
|
|
87
88
|
*/
|
|
88
|
-
resolve(obj: any, options?: $RefParserOptions
|
|
89
|
+
resolve(obj: any, options?: $RefParserOptions<S>, pathFromRoot?: string) {
|
|
89
90
|
const tokens = Pointer.parse(this.path, this.originalPath);
|
|
90
91
|
|
|
91
92
|
// Crawl the object, one token at a time
|
|
@@ -143,7 +144,7 @@ class Pointer {
|
|
|
143
144
|
* @returns
|
|
144
145
|
* Returns the modified object, or an entirely new object if the entire object is overwritten.
|
|
145
146
|
*/
|
|
146
|
-
set(obj: any, value: any, options?: $RefParserOptions) {
|
|
147
|
+
set(obj: any, value: any, options?: $RefParserOptions<S>) {
|
|
147
148
|
const tokens = Pointer.parse(this.path);
|
|
148
149
|
let token;
|
|
149
150
|
|
package/lib/ref.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { InvalidPointerError, isHandledError, normalizeError } from "./util/erro
|
|
|
4
4
|
import { safePointerToPath, stripHash, getHash } from "./util/url.js";
|
|
5
5
|
import type $Refs from "./refs.js";
|
|
6
6
|
import type $RefParserOptions from "./options.js";
|
|
7
|
+
import type { ParserOptions } from "./options.js";
|
|
7
8
|
import type { JSONSchema } from "./types";
|
|
8
9
|
|
|
9
10
|
export type $RefError = JSONParserError | ResolverError | ParserError | MissingPointerError;
|
|
@@ -13,7 +14,7 @@ export type $RefError = JSONParserError | ResolverError | ParserError | MissingP
|
|
|
13
14
|
*
|
|
14
15
|
* @class
|
|
15
16
|
*/
|
|
16
|
-
class $Ref {
|
|
17
|
+
class $Ref<S = JSONSchema> {
|
|
17
18
|
/**
|
|
18
19
|
* The file path or URL of the referenced file.
|
|
19
20
|
* This path is relative to the path of the main JSON schema file.
|
|
@@ -39,7 +40,7 @@ class $Ref {
|
|
|
39
40
|
*
|
|
40
41
|
* @type {$Refs}
|
|
41
42
|
*/
|
|
42
|
-
$refs: $Refs
|
|
43
|
+
$refs: $Refs<S>;
|
|
43
44
|
|
|
44
45
|
/**
|
|
45
46
|
* Indicates the type of {@link $Ref#path} (e.g. "file", "http", etc.)
|
|
@@ -51,7 +52,7 @@ class $Ref {
|
|
|
51
52
|
*/
|
|
52
53
|
errors: Array<$RefError> = [];
|
|
53
54
|
|
|
54
|
-
constructor($refs: $Refs) {
|
|
55
|
+
constructor($refs: $Refs<S>) {
|
|
55
56
|
this.$refs = $refs;
|
|
56
57
|
}
|
|
57
58
|
|
|
@@ -87,7 +88,7 @@ class $Ref {
|
|
|
87
88
|
* @param options
|
|
88
89
|
* @returns
|
|
89
90
|
*/
|
|
90
|
-
exists(path: string, options?: $RefParserOptions) {
|
|
91
|
+
exists(path: string, options?: $RefParserOptions<S>) {
|
|
91
92
|
try {
|
|
92
93
|
this.resolve(path, options);
|
|
93
94
|
return true;
|
|
@@ -103,7 +104,7 @@ class $Ref {
|
|
|
103
104
|
* @param options
|
|
104
105
|
* @returns - Returns the resolved value
|
|
105
106
|
*/
|
|
106
|
-
get(path:
|
|
107
|
+
get(path: string, options?: $RefParserOptions<S>) {
|
|
107
108
|
return this.resolve(path, options)?.value;
|
|
108
109
|
}
|
|
109
110
|
|
|
@@ -116,8 +117,8 @@ class $Ref {
|
|
|
116
117
|
* @param pathFromRoot - The path of `obj` from the schema root
|
|
117
118
|
* @returns
|
|
118
119
|
*/
|
|
119
|
-
resolve(path:
|
|
120
|
-
const pointer = new Pointer(this, path, friendlyPath);
|
|
120
|
+
resolve(path: string, options?: $RefParserOptions<S>, friendlyPath?: string, pathFromRoot?: string) {
|
|
121
|
+
const pointer = new Pointer<S>(this, path, friendlyPath);
|
|
121
122
|
try {
|
|
122
123
|
return pointer.resolve(this.value, options, pathFromRoot);
|
|
123
124
|
} catch (err: any) {
|
|
@@ -185,12 +186,12 @@ class $Ref {
|
|
|
185
186
|
* @param options
|
|
186
187
|
* @returns
|
|
187
188
|
*/
|
|
188
|
-
static isAllowed$Ref(value: unknown, options?:
|
|
189
|
+
static isAllowed$Ref(value: unknown, options?: ParserOptions) {
|
|
189
190
|
if (this.is$Ref(value)) {
|
|
190
191
|
if (value.$ref.substring(0, 2) === "#/" || value.$ref === "#") {
|
|
191
192
|
// It's a JSON Pointer reference, which is always allowed
|
|
192
193
|
return true;
|
|
193
|
-
} else if (value.$ref[0] !== "#" && (!options || options.resolve
|
|
194
|
+
} else if (value.$ref[0] !== "#" && (!options || options.resolve?.external)) {
|
|
194
195
|
// It's an external reference, which is allowed by the options
|
|
195
196
|
return true;
|
|
196
197
|
}
|
|
@@ -266,7 +267,7 @@ class $Ref {
|
|
|
266
267
|
* @param resolvedValue - The resolved value, which can be any type
|
|
267
268
|
* @returns - Returns the dereferenced value
|
|
268
269
|
*/
|
|
269
|
-
static dereference($ref: $Ref
|
|
270
|
+
static dereference<S>($ref: $Ref<S>, resolvedValue: S): S {
|
|
270
271
|
if (resolvedValue && typeof resolvedValue === "object" && $Ref.isExtended$Ref($ref)) {
|
|
271
272
|
const merged = {};
|
|
272
273
|
for (const key of Object.keys($ref)) {
|
|
@@ -283,7 +284,7 @@ class $Ref {
|
|
|
283
284
|
}
|
|
284
285
|
}
|
|
285
286
|
|
|
286
|
-
return merged;
|
|
287
|
+
return merged as S;
|
|
287
288
|
} else {
|
|
288
289
|
// Completely replace the original reference with the resolved value
|
|
289
290
|
return resolvedValue;
|
package/lib/refs.ts
CHANGED
|
@@ -2,12 +2,12 @@ import { ono } from "@jsdevtools/ono";
|
|
|
2
2
|
import $Ref from "./ref.js";
|
|
3
3
|
import * as url from "./util/url.js";
|
|
4
4
|
import type { JSONSchema4Type, JSONSchema6Type, JSONSchema7Type } from "json-schema";
|
|
5
|
-
import type { JSONSchema } from "./types/index.js";
|
|
6
5
|
import type $RefParserOptions from "./options.js";
|
|
7
6
|
import convertPathToPosix from "./util/convert-path-to-posix";
|
|
7
|
+
import type { JSONSchema } from "./types";
|
|
8
8
|
|
|
9
|
-
interface $RefsMap {
|
|
10
|
-
[url: string]: $Ref
|
|
9
|
+
interface $RefsMap<S> {
|
|
10
|
+
[url: string]: $Ref<S>;
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* 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.
|
|
@@ -16,7 +16,7 @@ interface $RefsMap {
|
|
|
16
16
|
*
|
|
17
17
|
* See https://apitools.dev/json-schema-ref-parser/docs/refs.html
|
|
18
18
|
*/
|
|
19
|
-
export default class $Refs {
|
|
19
|
+
export default class $Refs<S = JSONSchema> {
|
|
20
20
|
/**
|
|
21
21
|
* 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.
|
|
22
22
|
*
|
|
@@ -45,13 +45,13 @@ export default class $Refs {
|
|
|
45
45
|
*
|
|
46
46
|
* @param types (optional) Optionally only return values from certain locations ("file", "http", etc.)
|
|
47
47
|
*/
|
|
48
|
-
values(...types: string[]):
|
|
48
|
+
values(...types: string[]): S {
|
|
49
49
|
const $refs = this._$refs;
|
|
50
50
|
const paths = getPaths($refs, types);
|
|
51
51
|
return paths.reduce<Record<string, any>>((obj, path) => {
|
|
52
52
|
obj[convertPathToPosix(path.decoded)] = $refs[path.encoded].value;
|
|
53
53
|
return obj;
|
|
54
|
-
}, {});
|
|
54
|
+
}, {}) as S;
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
/**
|
|
@@ -84,17 +84,17 @@ export default class $Refs {
|
|
|
84
84
|
* @param [options]
|
|
85
85
|
* @returns - Returns the resolved value
|
|
86
86
|
*/
|
|
87
|
-
get(path: string, options?: $RefParserOptions): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type {
|
|
87
|
+
get(path: string, options?: $RefParserOptions<S>): JSONSchema4Type | JSONSchema6Type | JSONSchema7Type {
|
|
88
88
|
return this._resolve(path, "", options)!.value;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
91
|
/**
|
|
92
92
|
* 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.
|
|
93
93
|
*
|
|
94
|
-
* @param
|
|
94
|
+
* @param path The JSON Reference path, optionally with a JSON Pointer in the hash
|
|
95
95
|
* @param value The value to assign. Can be anything (object, string, number, etc.)
|
|
96
96
|
*/
|
|
97
|
-
set(path:
|
|
97
|
+
set(path: string, value: JSONSchema4Type | JSONSchema6Type | JSONSchema7Type) {
|
|
98
98
|
const absPath = url.resolve(this._root$Ref.path!, path);
|
|
99
99
|
const withoutHash = url.stripHash(absPath);
|
|
100
100
|
const $ref = this._$refs[withoutHash];
|
|
@@ -126,7 +126,7 @@ export default class $Refs {
|
|
|
126
126
|
_add(path: string) {
|
|
127
127
|
const withoutHash = url.stripHash(path);
|
|
128
128
|
|
|
129
|
-
const $ref = new $Ref(this);
|
|
129
|
+
const $ref = new $Ref<S>(this);
|
|
130
130
|
$ref.path = withoutHash;
|
|
131
131
|
|
|
132
132
|
this._$refs[withoutHash] = $ref;
|
|
@@ -162,7 +162,7 @@ export default class $Refs {
|
|
|
162
162
|
* @type {object}
|
|
163
163
|
* @protected
|
|
164
164
|
*/
|
|
165
|
-
_$refs: $RefsMap = {};
|
|
165
|
+
_$refs: $RefsMap<S> = {};
|
|
166
166
|
|
|
167
167
|
/**
|
|
168
168
|
* The {@link $Ref} object that is the root of the JSON schema.
|
|
@@ -170,7 +170,7 @@ export default class $Refs {
|
|
|
170
170
|
* @type {$Ref}
|
|
171
171
|
* @protected
|
|
172
172
|
*/
|
|
173
|
-
_root$Ref: $Ref
|
|
173
|
+
_root$Ref: $Ref<S>;
|
|
174
174
|
|
|
175
175
|
constructor() {
|
|
176
176
|
/**
|
|
@@ -215,7 +215,7 @@ export default class $Refs {
|
|
|
215
215
|
* @param [types] - Only return paths of the given types ("file", "http", etc.)
|
|
216
216
|
* @returns
|
|
217
217
|
*/
|
|
218
|
-
function getPaths($refs: $RefsMap
|
|
218
|
+
function getPaths<S>($refs: $RefsMap<S>, types: string[]) {
|
|
219
219
|
let paths = Object.keys($refs);
|
|
220
220
|
|
|
221
221
|
// Filter the paths by type
|
package/lib/resolve-external.ts
CHANGED
|
@@ -4,12 +4,10 @@ import parse from "./parse.js";
|
|
|
4
4
|
import * as url from "./util/url.js";
|
|
5
5
|
import { isHandledError } from "./util/errors.js";
|
|
6
6
|
import type $Refs from "./refs.js";
|
|
7
|
-
import type { Options } from "./options.js";
|
|
7
|
+
import type { Options, ParserOptions } from "./options.js";
|
|
8
8
|
import type { JSONSchema } from "./types/index.js";
|
|
9
9
|
import type $RefParser from "./index.js";
|
|
10
10
|
|
|
11
|
-
export default resolveExternal;
|
|
12
|
-
|
|
13
11
|
/**
|
|
14
12
|
* Crawls the JSON schema, finds all external JSON references, and resolves their values.
|
|
15
13
|
* This method does not mutate the JSON schema. The resolved values are added to {@link $RefParser#$refs}.
|
|
@@ -20,7 +18,10 @@ export default resolveExternal;
|
|
|
20
18
|
* The promise resolves once all JSON references in the schema have been resolved,
|
|
21
19
|
* including nested references that are contained in externally-referenced files.
|
|
22
20
|
*/
|
|
23
|
-
function resolveExternal
|
|
21
|
+
function resolveExternal<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
|
|
22
|
+
parser: $RefParser<S, O>,
|
|
23
|
+
options: Options,
|
|
24
|
+
) {
|
|
24
25
|
if (!options.resolve.external) {
|
|
25
26
|
// Nothing to resolve, so exit early
|
|
26
27
|
return Promise.resolve();
|
|
@@ -51,10 +52,10 @@ function resolveExternal(parser: $RefParser, options: Options) {
|
|
|
51
52
|
* If any of the JSON references point to files that contain additional JSON references,
|
|
52
53
|
* then the corresponding promise will internally reference an array of promises.
|
|
53
54
|
*/
|
|
54
|
-
function crawl(
|
|
55
|
-
obj: string | Buffer |
|
|
55
|
+
function crawl<S extends JSONSchema = JSONSchema>(
|
|
56
|
+
obj: string | Buffer | S | undefined | null,
|
|
56
57
|
path: string,
|
|
57
|
-
$refs: $Refs
|
|
58
|
+
$refs: $Refs<S>,
|
|
58
59
|
options: Options,
|
|
59
60
|
seen?: Set<any>,
|
|
60
61
|
external?: boolean,
|
|
@@ -65,13 +66,13 @@ function crawl(
|
|
|
65
66
|
if (obj && typeof obj === "object" && !ArrayBuffer.isView(obj) && !seen.has(obj)) {
|
|
66
67
|
seen.add(obj); // Track previously seen objects to avoid infinite recursion
|
|
67
68
|
if ($Ref.isExternal$Ref(obj)) {
|
|
68
|
-
promises.push(resolve$Ref(obj, path, $refs, options));
|
|
69
|
+
promises.push(resolve$Ref<S>(obj, path, $refs, options));
|
|
69
70
|
}
|
|
70
71
|
|
|
71
|
-
const keys = Object.keys(obj) as
|
|
72
|
+
const keys = Object.keys(obj) as string[];
|
|
72
73
|
for (const key of keys) {
|
|
73
74
|
const keyPath = Pointer.join(path, key);
|
|
74
|
-
const value = obj[key] as string | JSONSchema | Buffer | undefined;
|
|
75
|
+
const value = obj[key as keyof typeof obj] as string | JSONSchema | Buffer | undefined;
|
|
75
76
|
promises = promises.concat(crawl(value, keyPath, $refs, options, seen, external));
|
|
76
77
|
}
|
|
77
78
|
}
|
|
@@ -91,7 +92,12 @@ function crawl(
|
|
|
91
92
|
* The promise resolves once all JSON references in the object have been resolved,
|
|
92
93
|
* including nested references that are contained in externally-referenced files.
|
|
93
94
|
*/
|
|
94
|
-
async function resolve$Ref
|
|
95
|
+
async function resolve$Ref<S extends JSONSchema = JSONSchema>(
|
|
96
|
+
$ref: S,
|
|
97
|
+
path: string,
|
|
98
|
+
$refs: $Refs<S>,
|
|
99
|
+
options: Options,
|
|
100
|
+
) {
|
|
95
101
|
const shouldResolveOnCwd = options.dereference.externalReferenceResolution === "root";
|
|
96
102
|
const resolvedPath = url.resolve(shouldResolveOnCwd ? url.cwd() : path, $ref.$ref!);
|
|
97
103
|
const withoutHash = url.stripHash(resolvedPath);
|
|
@@ -99,10 +105,10 @@ async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options
|
|
|
99
105
|
// $ref.$ref = url.relative($refs._root$Ref.path, resolvedPath);
|
|
100
106
|
|
|
101
107
|
// Do we already have this $ref?
|
|
102
|
-
|
|
103
|
-
if (
|
|
108
|
+
const ref = $refs._$refs[withoutHash];
|
|
109
|
+
if (ref) {
|
|
104
110
|
// We've already parsed this $ref, so use the existing value
|
|
105
|
-
return Promise.resolve(
|
|
111
|
+
return Promise.resolve(ref.value);
|
|
106
112
|
}
|
|
107
113
|
|
|
108
114
|
// Parse the $referenced file/url
|
|
@@ -127,3 +133,4 @@ async function resolve$Ref($ref: JSONSchema, path: string, $refs: $Refs, options
|
|
|
127
133
|
return [];
|
|
128
134
|
}
|
|
129
135
|
}
|
|
136
|
+
export default resolveExternal;
|
package/lib/resolvers/file.ts
CHANGED
|
@@ -2,7 +2,7 @@ import fs from "fs";
|
|
|
2
2
|
import { ono } from "@jsdevtools/ono";
|
|
3
3
|
import * as url from "../util/url.js";
|
|
4
4
|
import { ResolverError } from "../util/errors.js";
|
|
5
|
-
import type { ResolverOptions } from "../types/index.js";
|
|
5
|
+
import type { JSONSchema, ResolverOptions } from "../types/index.js";
|
|
6
6
|
import type { FileInfo } from "../types/index.js";
|
|
7
7
|
|
|
8
8
|
export default {
|
|
@@ -36,4 +36,4 @@ export default {
|
|
|
36
36
|
throw new ResolverError(ono(err, `Error opening file "${path}"`), path);
|
|
37
37
|
}
|
|
38
38
|
},
|
|
39
|
-
} as ResolverOptions
|
|
39
|
+
} as ResolverOptions<JSONSchema>;
|
package/lib/resolvers/http.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ono } from "@jsdevtools/ono";
|
|
2
2
|
import * as url from "../util/url.js";
|
|
3
3
|
import { ResolverError } from "../util/errors.js";
|
|
4
|
-
import type { FileInfo, HTTPResolverOptions } from "../types/index.js";
|
|
4
|
+
import type { FileInfo, HTTPResolverOptions, JSONSchema } from "../types/index.js";
|
|
5
5
|
|
|
6
6
|
export default {
|
|
7
7
|
/**
|
|
@@ -59,14 +59,18 @@ export default {
|
|
|
59
59
|
|
|
60
60
|
return download(u, this);
|
|
61
61
|
},
|
|
62
|
-
} as HTTPResolverOptions
|
|
62
|
+
} as HTTPResolverOptions<JSONSchema>;
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
65
|
* Downloads the given file.
|
|
66
66
|
* @returns
|
|
67
67
|
* The promise resolves with the raw downloaded data, or rejects if there is an HTTP error.
|
|
68
68
|
*/
|
|
69
|
-
async function download
|
|
69
|
+
async function download<S>(
|
|
70
|
+
u: URL | string,
|
|
71
|
+
httpOptions: HTTPResolverOptions<S>,
|
|
72
|
+
_redirects?: string[],
|
|
73
|
+
): Promise<Buffer> {
|
|
70
74
|
u = url.parse(u);
|
|
71
75
|
const redirects = _redirects || [];
|
|
72
76
|
redirects.push(u.href);
|
|
@@ -105,7 +109,7 @@ async function download(u: URL | string, httpOptions: HTTPResolverOptions, _redi
|
|
|
105
109
|
* Sends an HTTP GET request.
|
|
106
110
|
* The promise resolves with the HTTP Response object.
|
|
107
111
|
*/
|
|
108
|
-
async function get(u: RequestInfo | URL, httpOptions: HTTPResolverOptions) {
|
|
112
|
+
async function get<S>(u: RequestInfo | URL, httpOptions: HTTPResolverOptions<S>) {
|
|
109
113
|
let controller: any;
|
|
110
114
|
let timeoutId: any;
|
|
111
115
|
if (httpOptions.timeout) {
|
package/lib/types/index.ts
CHANGED
|
@@ -10,14 +10,14 @@ import type $Refs from "../refs.js";
|
|
|
10
10
|
|
|
11
11
|
export type JSONSchema = JSONSchema4 | JSONSchema6 | JSONSchema7;
|
|
12
12
|
export type JSONSchemaObject = JSONSchema4Object | JSONSchema6Object | JSONSchema7Object;
|
|
13
|
-
export type SchemaCallback = (err: Error | null, schema?:
|
|
14
|
-
export type $RefsCallback = (err: Error | null, $refs?: $Refs) => any;
|
|
13
|
+
export type SchemaCallback<S = JSONSchema> = (err: Error | null, schema?: S | object | null) => any;
|
|
14
|
+
export type $RefsCallback<S = JSONSchema> = (err: Error | null, $refs?: $Refs<S>) => any;
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* See https://apitools.dev/json-schema-ref-parser/docs/options.html
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
export interface HTTPResolverOptions extends Partial<ResolverOptions
|
|
20
|
+
export interface HTTPResolverOptions<S = JSONSchema> extends Partial<ResolverOptions<S>> {
|
|
21
21
|
/**
|
|
22
22
|
* You can specify any HTTP headers that should be sent when downloading files. For example, some servers may require you to set the `Accept` or `Referrer` header.
|
|
23
23
|
*/
|
|
@@ -44,7 +44,7 @@ export interface HTTPResolverOptions extends Partial<ResolverOptions> {
|
|
|
44
44
|
*
|
|
45
45
|
* See https://apitools.dev/json-schema-ref-parser/docs/plugins/resolvers.html
|
|
46
46
|
*/
|
|
47
|
-
export interface ResolverOptions {
|
|
47
|
+
export interface ResolverOptions<S = JSONSchema> {
|
|
48
48
|
name?: string;
|
|
49
49
|
/**
|
|
50
50
|
* All resolvers have an order property, even the built-in resolvers. If you don't specify an order property, then your resolver will run last. Specifying `order: 1`, like we did in this example, will make your resolver run first. Or you can squeeze your resolver in-between some of the built-in resolvers. For example, `order: 101` would make it run after the file resolver, but before the HTTP resolver. You can see the order of all the built-in resolvers by looking at their source code.
|
|
@@ -69,7 +69,7 @@ export interface ResolverOptions {
|
|
|
69
69
|
| ((
|
|
70
70
|
file: FileInfo,
|
|
71
71
|
callback?: (error: Error | null, data: string | null) => any,
|
|
72
|
-
) => string | Buffer |
|
|
72
|
+
) => string | Buffer | S | Promise<string | Buffer | S>);
|
|
73
73
|
}
|
|
74
74
|
|
|
75
75
|
export interface Plugin {
|
package/lib/util/errors.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Ono } from "@jsdevtools/ono";
|
|
2
2
|
import { stripHash, toFileSystemPath } from "./url.js";
|
|
3
3
|
import type $RefParser from "../index.js";
|
|
4
|
+
import type { ParserOptions } from "../index.js";
|
|
5
|
+
import type { JSONSchema } from "../index.js";
|
|
4
6
|
import type $Ref from "../ref";
|
|
5
7
|
|
|
6
8
|
export type JSONParserErrorType =
|
|
@@ -11,6 +13,7 @@ export type JSONParserErrorType =
|
|
|
11
13
|
| "EUNMATCHEDRESOLVER"
|
|
12
14
|
| "EMISSINGPOINTER"
|
|
13
15
|
| "EINVALIDPOINTER";
|
|
16
|
+
|
|
14
17
|
export class JSONParserError extends Error {
|
|
15
18
|
public readonly name: string;
|
|
16
19
|
public readonly message: string;
|
|
@@ -34,10 +37,13 @@ export class JSONParserError extends Error {
|
|
|
34
37
|
}
|
|
35
38
|
}
|
|
36
39
|
|
|
37
|
-
export class JSONParserErrorGroup
|
|
38
|
-
|
|
40
|
+
export class JSONParserErrorGroup<
|
|
41
|
+
S extends JSONSchema = JSONSchema,
|
|
42
|
+
O extends ParserOptions = ParserOptions,
|
|
43
|
+
> extends Error {
|
|
44
|
+
files: $RefParser<S, O>;
|
|
39
45
|
|
|
40
|
-
constructor(parser: $RefParser) {
|
|
46
|
+
constructor(parser: $RefParser<S, O>) {
|
|
41
47
|
super();
|
|
42
48
|
|
|
43
49
|
this.files = parser;
|
|
@@ -49,10 +55,12 @@ export class JSONParserErrorGroup extends Error {
|
|
|
49
55
|
Ono.extend(this);
|
|
50
56
|
}
|
|
51
57
|
|
|
52
|
-
static getParserErrors
|
|
58
|
+
static getParserErrors<S extends JSONSchema = JSONSchema, O extends ParserOptions = ParserOptions>(
|
|
59
|
+
parser: $RefParser<S, O>,
|
|
60
|
+
) {
|
|
53
61
|
const errors = [];
|
|
54
62
|
|
|
55
|
-
for (const $ref of Object.values(parser.$refs._$refs) as $Ref[]) {
|
|
63
|
+
for (const $ref of Object.values(parser.$refs._$refs) as $Ref<unknown>[]) {
|
|
56
64
|
if ($ref.errors) {
|
|
57
65
|
errors.push(...$ref.errors);
|
|
58
66
|
}
|
|
@@ -70,7 +78,7 @@ export class JSONParserErrorGroup extends Error {
|
|
|
70
78
|
| UnmatchedParserError
|
|
71
79
|
| UnmatchedResolverError
|
|
72
80
|
> {
|
|
73
|
-
return JSONParserErrorGroup.getParserErrors(this.files);
|
|
81
|
+
return JSONParserErrorGroup.getParserErrors<S>(this.files);
|
|
74
82
|
}
|
|
75
83
|
}
|
|
76
84
|
|
package/lib/util/plugins.ts
CHANGED
|
@@ -3,7 +3,6 @@ import type $RefParserOptions from "../options.js";
|
|
|
3
3
|
import type { ResolverOptions } from "../types/index.js";
|
|
4
4
|
import type $Refs from "../refs.js";
|
|
5
5
|
import type { Plugin } from "../types/index.js";
|
|
6
|
-
import type { JSONSchema } from "../types/index.js";
|
|
7
6
|
|
|
8
7
|
/**
|
|
9
8
|
* Returns the given plugins as an array, rather than an object map.
|
|
@@ -11,13 +10,13 @@ import type { JSONSchema } from "../types/index.js";
|
|
|
11
10
|
*
|
|
12
11
|
* @returns
|
|
13
12
|
*/
|
|
14
|
-
export function all(plugins: $RefParserOptions["resolve"]): Plugin[] {
|
|
13
|
+
export function all<S>(plugins: $RefParserOptions<S>["resolve"]): Plugin[] {
|
|
15
14
|
return Object.keys(plugins)
|
|
16
15
|
.filter((key) => {
|
|
17
16
|
return typeof plugins[key] === "object";
|
|
18
17
|
})
|
|
19
18
|
.map((key) => {
|
|
20
|
-
(plugins[key] as ResolverOptions)!.name = key;
|
|
19
|
+
(plugins[key] as ResolverOptions<S>)!.name = key;
|
|
21
20
|
return plugins[key] as Plugin;
|
|
22
21
|
});
|
|
23
22
|
}
|
|
@@ -44,9 +43,9 @@ export function sort(plugins: Plugin[]) {
|
|
|
44
43
|
});
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
export interface PluginResult {
|
|
46
|
+
export interface PluginResult<S> {
|
|
48
47
|
plugin: Plugin;
|
|
49
|
-
result?: string | Buffer |
|
|
48
|
+
result?: string | Buffer | S;
|
|
50
49
|
error?: any;
|
|
51
50
|
}
|
|
52
51
|
|
|
@@ -58,17 +57,17 @@ export interface PluginResult {
|
|
|
58
57
|
* If the promise rejects, or the callback is called with an error, then the next plugin is called.
|
|
59
58
|
* If ALL plugins fail, then the last error is thrown.
|
|
60
59
|
*/
|
|
61
|
-
export async function run(
|
|
60
|
+
export async function run<S>(
|
|
62
61
|
plugins: Plugin[],
|
|
63
|
-
method: keyof Plugin | keyof ResolverOptions
|
|
62
|
+
method: keyof Plugin | keyof ResolverOptions<S>,
|
|
64
63
|
file: FileInfo,
|
|
65
|
-
$refs: $Refs
|
|
64
|
+
$refs: $Refs<S>,
|
|
66
65
|
) {
|
|
67
66
|
let plugin: Plugin;
|
|
68
|
-
let lastError: PluginResult
|
|
67
|
+
let lastError: PluginResult<S>;
|
|
69
68
|
let index = 0;
|
|
70
69
|
|
|
71
|
-
return new Promise<PluginResult
|
|
70
|
+
return new Promise<PluginResult<S>>((resolve, reject) => {
|
|
72
71
|
runNextPlugin();
|
|
73
72
|
|
|
74
73
|
function runNextPlugin() {
|
|
@@ -95,7 +94,7 @@ export async function run(
|
|
|
95
94
|
}
|
|
96
95
|
}
|
|
97
96
|
|
|
98
|
-
function callback(err: PluginResult["error"], result: PluginResult["result"]) {
|
|
97
|
+
function callback(err: PluginResult<S>["error"], result: PluginResult<S>["result"]) {
|
|
99
98
|
if (err) {
|
|
100
99
|
onError(err);
|
|
101
100
|
} else {
|
|
@@ -103,7 +102,7 @@ export async function run(
|
|
|
103
102
|
}
|
|
104
103
|
}
|
|
105
104
|
|
|
106
|
-
function onSuccess(result: PluginResult["result"]) {
|
|
105
|
+
function onSuccess(result: PluginResult<S>["result"]) {
|
|
107
106
|
// console.log(' success');
|
|
108
107
|
resolve({
|
|
109
108
|
plugin,
|
|
@@ -111,7 +110,7 @@ export async function run(
|
|
|
111
110
|
});
|
|
112
111
|
}
|
|
113
112
|
|
|
114
|
-
function onError(error: PluginResult["error"]) {
|
|
113
|
+
function onError(error: PluginResult<S>["error"]) {
|
|
115
114
|
// console.log(' %s', err.message || err);
|
|
116
115
|
lastError = {
|
|
117
116
|
plugin,
|
|
@@ -128,9 +127,9 @@ export async function run(
|
|
|
128
127
|
* If the value is a RegExp, then it will be tested against the file URL.
|
|
129
128
|
* If the value is an array, then it will be compared against the file extension.
|
|
130
129
|
*/
|
|
131
|
-
function getResult(
|
|
130
|
+
function getResult<S>(
|
|
132
131
|
obj: Plugin,
|
|
133
|
-
prop: keyof Plugin | keyof ResolverOptions
|
|
132
|
+
prop: keyof Plugin | keyof ResolverOptions<S>,
|
|
134
133
|
file: FileInfo,
|
|
135
134
|
callback?: (err?: Error, result?: any) => void,
|
|
136
135
|
$refs?: any,
|