@apidevtools/json-schema-ref-parser 15.0.1 → 15.1.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/parse.js +9 -9
- package/dist/lib/util/plugins.d.ts +1 -1
- package/dist/lib/util/plugins.js +2 -2
- package/lib/parse.ts +9 -9
- package/lib/util/plugins.ts +10 -4
- package/package.json +1 -1
package/dist/lib/parse.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as url from "./util/url.js";
|
|
2
|
-
import
|
|
2
|
+
import { filter, all, sort, run } from "./util/plugins.js";
|
|
3
3
|
import { ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, } from "./util/errors.js";
|
|
4
4
|
/**
|
|
5
5
|
* Reads and parses the specified file path or URL.
|
|
@@ -52,12 +52,12 @@ async function parse(path, $refs, options) {
|
|
|
52
52
|
async function readFile(file, options, $refs) {
|
|
53
53
|
// console.log('Reading %s', file.url);
|
|
54
54
|
// Find the resolvers that can read this file
|
|
55
|
-
let resolvers =
|
|
56
|
-
resolvers =
|
|
55
|
+
let resolvers = all(options.resolve);
|
|
56
|
+
resolvers = filter(resolvers, "canRead", file, undefined, $refs);
|
|
57
57
|
// Run the resolvers, in order, until one of them succeeds
|
|
58
|
-
|
|
58
|
+
sort(resolvers);
|
|
59
59
|
try {
|
|
60
|
-
const data = await
|
|
60
|
+
const data = await run(resolvers, "read", file, $refs);
|
|
61
61
|
return data;
|
|
62
62
|
}
|
|
63
63
|
catch (err) {
|
|
@@ -95,13 +95,13 @@ async function parseFile(file, options, $refs) {
|
|
|
95
95
|
// Find the parsers that can read this file type.
|
|
96
96
|
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
|
|
97
97
|
// This handles situations where the file IS a supported type, just with an unknown extension.
|
|
98
|
-
const allParsers =
|
|
99
|
-
const filteredParsers =
|
|
98
|
+
const allParsers = all(options.parse);
|
|
99
|
+
const filteredParsers = filter(allParsers, "canParse", file);
|
|
100
100
|
const parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
|
|
101
101
|
// Run the parsers, in order, until one of them succeeds
|
|
102
|
-
|
|
102
|
+
sort(parsers);
|
|
103
103
|
try {
|
|
104
|
-
const parser = await
|
|
104
|
+
const parser = await run(parsers, "parse", file, $refs);
|
|
105
105
|
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
|
|
106
106
|
throw new SyntaxError(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
|
|
107
107
|
}
|
|
@@ -13,7 +13,7 @@ export declare function all<S extends object = JSONSchema, O extends ParserOptio
|
|
|
13
13
|
/**
|
|
14
14
|
* Filters the given plugins, returning only the ones return `true` for the given method.
|
|
15
15
|
*/
|
|
16
|
-
export declare function filter(plugins: Plugin[], method:
|
|
16
|
+
export declare function filter<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(plugins: Plugin[], method: keyof Plugin | keyof ResolverOptions<S>, file: FileInfo, callback?: (err?: Error, result?: any) => void, $refs?: $Refs<S, O>): Plugin[];
|
|
17
17
|
/**
|
|
18
18
|
* Sorts the given plugins, in place, by their `order` property.
|
|
19
19
|
*/
|
package/dist/lib/util/plugins.js
CHANGED
|
@@ -17,9 +17,9 @@ export function all(plugins) {
|
|
|
17
17
|
/**
|
|
18
18
|
* Filters the given plugins, returning only the ones return `true` for the given method.
|
|
19
19
|
*/
|
|
20
|
-
export function filter(plugins, method, file) {
|
|
20
|
+
export function filter(plugins, method, file, callback, $refs) {
|
|
21
21
|
return plugins.filter((plugin) => {
|
|
22
|
-
return !!getResult(plugin, method, file);
|
|
22
|
+
return !!getResult(plugin, method, file, callback, $refs);
|
|
23
23
|
});
|
|
24
24
|
}
|
|
25
25
|
/**
|
package/lib/parse.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as url from "./util/url.js";
|
|
2
|
-
import
|
|
2
|
+
import { filter, all, sort, run } from "./util/plugins.js";
|
|
3
3
|
import {
|
|
4
4
|
ResolverError,
|
|
5
5
|
ParserError,
|
|
@@ -77,13 +77,13 @@ async function readFile<S extends object = JSONSchema, O extends ParserOptions<S
|
|
|
77
77
|
// console.log('Reading %s', file.url);
|
|
78
78
|
|
|
79
79
|
// Find the resolvers that can read this file
|
|
80
|
-
let resolvers =
|
|
81
|
-
resolvers =
|
|
80
|
+
let resolvers = all(options.resolve);
|
|
81
|
+
resolvers = filter(resolvers, "canRead", file, undefined, $refs);
|
|
82
82
|
|
|
83
83
|
// Run the resolvers, in order, until one of them succeeds
|
|
84
|
-
|
|
84
|
+
sort(resolvers);
|
|
85
85
|
try {
|
|
86
|
-
const data = await
|
|
86
|
+
const data = await run(resolvers, "read", file, $refs);
|
|
87
87
|
return data;
|
|
88
88
|
} catch (err: any) {
|
|
89
89
|
if (!err && options.continueOnError) {
|
|
@@ -123,14 +123,14 @@ async function parseFile<S extends object = JSONSchema, O extends ParserOptions<
|
|
|
123
123
|
// Find the parsers that can read this file type.
|
|
124
124
|
// If none of the parsers are an exact match for this file, then we'll try ALL of them.
|
|
125
125
|
// This handles situations where the file IS a supported type, just with an unknown extension.
|
|
126
|
-
const allParsers =
|
|
127
|
-
const filteredParsers =
|
|
126
|
+
const allParsers = all(options.parse);
|
|
127
|
+
const filteredParsers = filter(allParsers, "canParse", file);
|
|
128
128
|
const parsers = filteredParsers.length > 0 ? filteredParsers : allParsers;
|
|
129
129
|
|
|
130
130
|
// Run the parsers, in order, until one of them succeeds
|
|
131
|
-
|
|
131
|
+
sort(parsers);
|
|
132
132
|
try {
|
|
133
|
-
const parser = await
|
|
133
|
+
const parser = await run<S, O>(parsers, "parse", file, $refs);
|
|
134
134
|
if (!parser.plugin.allowEmpty && isEmpty(parser.result)) {
|
|
135
135
|
throw new SyntaxError(`Error parsing "${file.url}" as ${parser.plugin.name}. \nParsed value is empty`);
|
|
136
136
|
} else {
|
package/lib/util/plugins.ts
CHANGED
|
@@ -26,9 +26,15 @@ export function all<S extends object = JSONSchema, O extends ParserOptions<S> =
|
|
|
26
26
|
/**
|
|
27
27
|
* Filters the given plugins, returning only the ones return `true` for the given method.
|
|
28
28
|
*/
|
|
29
|
-
export function filter
|
|
29
|
+
export function filter<S extends object = JSONSchema, O extends ParserOptions<S> = ParserOptions<S>>(
|
|
30
|
+
plugins: Plugin[],
|
|
31
|
+
method: keyof Plugin | keyof ResolverOptions<S>,
|
|
32
|
+
file: FileInfo,
|
|
33
|
+
callback?: (err?: Error, result?: any) => void,
|
|
34
|
+
$refs?: $Refs<S, O>,
|
|
35
|
+
) {
|
|
30
36
|
return plugins.filter((plugin: Plugin) => {
|
|
31
|
-
return !!getResult(plugin, method, file);
|
|
37
|
+
return !!getResult(plugin, method, file, callback, $refs);
|
|
32
38
|
});
|
|
33
39
|
}
|
|
34
40
|
|
|
@@ -40,8 +46,8 @@ export function sort(plugins: Plugin[]) {
|
|
|
40
46
|
plugin.order = plugin.order || Number.MAX_SAFE_INTEGER;
|
|
41
47
|
}
|
|
42
48
|
|
|
43
|
-
return plugins.sort((a:
|
|
44
|
-
return a.order - b.order
|
|
49
|
+
return plugins.sort((a: Plugin, b: Plugin) => {
|
|
50
|
+
return a.order! - b.order!;
|
|
45
51
|
});
|
|
46
52
|
}
|
|
47
53
|
|