Skip to content

Commit 2245d15

Browse files
Added named and default CJS and ESM exports
Also added tests to verify exports
1 parent 2a83bcf commit 2245d15

File tree

2 files changed

+106
-15
lines changed

2 files changed

+106
-15
lines changed

lib/index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
"use strict";
22

3-
const Options = require("./options");
43
const $Refs = require("./refs");
5-
const parse = require("./parse");
4+
const _parse = require("./parse");
65
const normalizeArgs = require("./normalize-args");
76
const resolveExternal = require("./resolve-external");
8-
const bundle = require("./bundle");
9-
const dereference = require("./dereference");
7+
const _bundle = require("./bundle");
8+
const _dereference = require("./dereference");
109
const url = require("./util/url");
1110
const { JSONParserError, InvalidPointerError, MissingPointerError, ResolverError, ParserError, UnmatchedParserError, UnmatchedResolverError, isHandledError, JSONParserErrorGroup } = require("./util/errors");
1211
const maybe = require("call-me-maybe");
1312
const { ono } = require("@jsdevtools/ono");
1413

1514
module.exports = $RefParser;
15+
module.exports.default = $RefParser;
1616
module.exports.JSONParserError = JSONParserError;
1717
module.exports.InvalidPointerError = InvalidPointerError;
1818
module.exports.MissingPointerError = MissingPointerError;
@@ -56,7 +56,7 @@ function $RefParser () {
5656
* @param {function} [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
5757
* @returns {Promise} - The returned promise resolves with the parsed JSON schema object.
5858
*/
59-
$RefParser.parse = function (path, schema, options, callback) {
59+
$RefParser.parse = function parse (path, schema, options, callback) {
6060
let Class = this; // eslint-disable-line consistent-this
6161
let instance = new Class();
6262
return instance.parse.apply(instance, arguments);
@@ -73,7 +73,7 @@ $RefParser.parse = function (path, schema, options, callback) {
7373
* @param {function} [callback] - An error-first callback. The second parameter is the parsed JSON schema object.
7474
* @returns {Promise} - The returned promise resolves with the parsed JSON schema object.
7575
*/
76-
$RefParser.prototype.parse = async function (path, schema, options, callback) {
76+
$RefParser.prototype.parse = async function parse (path, schema, options, callback) {
7777
let args = normalizeArgs(arguments);
7878
let promise;
7979

@@ -111,7 +111,7 @@ $RefParser.prototype.parse = async function (path, schema, options, callback) {
111111
}
112112
else {
113113
// Parse the schema file/url
114-
promise = parse(args.path, this.$refs, args.options);
114+
promise = _parse(args.path, this.$refs, args.options);
115115
}
116116

117117
let me = this;
@@ -156,7 +156,7 @@ $RefParser.prototype.parse = async function (path, schema, options, callback) {
156156
* @returns {Promise}
157157
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
158158
*/
159-
$RefParser.resolve = function (path, schema, options, callback) {
159+
$RefParser.resolve = function resolve (path, schema, options, callback) {
160160
let Class = this; // eslint-disable-line consistent-this
161161
let instance = new Class();
162162
return instance.resolve.apply(instance, arguments);
@@ -175,7 +175,7 @@ $RefParser.resolve = function (path, schema, options, callback) {
175175
* @returns {Promise}
176176
* The returned promise resolves with a {@link $Refs} object containing the resolved JSON references
177177
*/
178-
$RefParser.prototype.resolve = async function (path, schema, options, callback) {
178+
$RefParser.prototype.resolve = async function resolve (path, schema, options, callback) {
179179
let me = this;
180180
let args = normalizeArgs(arguments);
181181

@@ -201,7 +201,7 @@ $RefParser.prototype.resolve = async function (path, schema, options, callback)
201201
* @param {function} [callback] - An error-first callback. The second parameter is the bundled JSON schema object
202202
* @returns {Promise} - The returned promise resolves with the bundled JSON schema object.
203203
*/
204-
$RefParser.bundle = function (path, schema, options, callback) {
204+
$RefParser.bundle = function bundle (path, schema, options, callback) {
205205
let Class = this; // eslint-disable-line consistent-this
206206
let instance = new Class();
207207
return instance.bundle.apply(instance, arguments);
@@ -218,13 +218,13 @@ $RefParser.bundle = function (path, schema, options, callback) {
218218
* @param {function} [callback] - An error-first callback. The second parameter is the bundled JSON schema object
219219
* @returns {Promise} - The returned promise resolves with the bundled JSON schema object.
220220
*/
221-
$RefParser.prototype.bundle = async function (path, schema, options, callback) {
221+
$RefParser.prototype.bundle = async function bundle (path, schema, options, callback) {
222222
let me = this;
223223
let args = normalizeArgs(arguments);
224224

225225
try {
226226
await this.resolve(args.path, args.schema, args.options);
227-
bundle(me, args.options);
227+
_bundle(me, args.options);
228228
finalize(me);
229229
return maybe(args.callback, Promise.resolve(me.schema));
230230
}
@@ -243,7 +243,7 @@ $RefParser.prototype.bundle = async function (path, schema, options, callback) {
243243
* @param {function} [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
244244
* @returns {Promise} - The returned promise resolves with the dereferenced JSON schema object.
245245
*/
246-
$RefParser.dereference = function (path, schema, options, callback) {
246+
$RefParser.dereference = function dereference (path, schema, options, callback) {
247247
let Class = this; // eslint-disable-line consistent-this
248248
let instance = new Class();
249249
return instance.dereference.apply(instance, arguments);
@@ -259,13 +259,13 @@ $RefParser.dereference = function (path, schema, options, callback) {
259259
* @param {function} [callback] - An error-first callback. The second parameter is the dereferenced JSON schema object
260260
* @returns {Promise} - The returned promise resolves with the dereferenced JSON schema object.
261261
*/
262-
$RefParser.prototype.dereference = async function (path, schema, options, callback) {
262+
$RefParser.prototype.dereference = async function dereference (path, schema, options, callback) {
263263
let me = this;
264264
let args = normalizeArgs(arguments);
265265

266266
try {
267267
await this.resolve(args.path, args.schema, args.options);
268-
dereference(me, args.options);
268+
_dereference(me, args.options);
269269
finalize(me);
270270
return maybe(args.callback, Promise.resolve(me.schema));
271271
}

test/specs/exports.spec.js

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"use strict";
2+
3+
const { expect } = require("chai");
4+
const commonJSExport = require("../../");
5+
const { default: defaultExport } = require("../../");
6+
const {
7+
JSONParserError,
8+
InvalidPointerError,
9+
MissingPointerError,
10+
ResolverError,
11+
ParserError,
12+
UnmatchedParserError,
13+
UnmatchedResolverError,
14+
} = require("../../");
15+
16+
describe("json-schema-ref-parser package exports", () => {
17+
18+
function is$RefParser (parser) {
19+
expect(parser).to.be.a("function").with.property("name", "$RefParser");
20+
expect(parser.parse).to.be.a("function").with.property("name", "parse");
21+
expect(parser.prototype.parse).to.be.a("function").with.property("name", "parse");
22+
expect(parser.resolve).to.be.a("function").with.property("name", "resolve");
23+
expect(parser.prototype.resolve).to.be.a("function").with.property("name", "resolve");
24+
expect(parser.dereference).to.be.a("function").with.property("name", "dereference");
25+
expect(parser.prototype.dereference).to.be.a("function").with.property("name", "dereference");
26+
expect(parser.bundle).to.be.a("function").with.property("name", "bundle");
27+
expect(parser.prototype.bundle).to.be.a("function").with.property("name", "bundle");
28+
return true;
29+
}
30+
31+
it("should export the $RefParser class as the default CommonJS export", async () => {
32+
expect(commonJSExport).to.satisfy(is$RefParser);
33+
});
34+
35+
it("should export the $RefParser class as the default ESM export", async () => {
36+
expect(defaultExport).to.satisfy(is$RefParser);
37+
});
38+
39+
it("should export the JSONParserError class as a named ESM export", async () => {
40+
expect(JSONParserError).to.be.a("function");
41+
expect(JSONParserError.name).to.equal("JSONParserError");
42+
});
43+
44+
it("should export the InvalidPointerError class as a named ESM export", async () => {
45+
expect(InvalidPointerError).to.be.a("function");
46+
expect(InvalidPointerError.name).to.equal("InvalidPointerError");
47+
});
48+
49+
it("should export the MissingPointerError class as a named ESM export", async () => {
50+
expect(MissingPointerError).to.be.a("function");
51+
expect(MissingPointerError.name).to.equal("MissingPointerError");
52+
});
53+
54+
it("should export the ResolverError class as a named ESM export", async () => {
55+
expect(ResolverError).to.be.a("function");
56+
expect(ResolverError.name).to.equal("ResolverError");
57+
});
58+
59+
it("should export the ParserError class as a named ESM export", async () => {
60+
expect(ParserError).to.be.a("function");
61+
expect(ParserError.name).to.equal("ParserError");
62+
});
63+
64+
it("should export the UnmatchedParserError class as a named ESM export", async () => {
65+
expect(UnmatchedParserError).to.be.a("function");
66+
expect(UnmatchedParserError.name).to.equal("UnmatchedParserError");
67+
});
68+
69+
it("should export the UnmatchedResolverError class as a named ESM export", async () => {
70+
expect(UnmatchedResolverError).to.be.a("function");
71+
expect(UnmatchedResolverError.name).to.equal("UnmatchedResolverError");
72+
});
73+
74+
it("should not export anything else", async () => {
75+
expect(commonJSExport).to.have.same.keys(
76+
"default",
77+
"parse",
78+
"resolve",
79+
"dereference",
80+
"bundle",
81+
"JSONParserError",
82+
"InvalidPointerError",
83+
"MissingPointerError",
84+
"ResolverError",
85+
"ParserError",
86+
"UnmatchedParserError",
87+
"UnmatchedResolverError",
88+
);
89+
});
90+
91+
});

0 commit comments

Comments
 (0)