Skip to content

Commit 02a83f9

Browse files
1 parent 2954873 commit 02a83f9

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

lib/index.js

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ function $RefParser() {
4949
*/
5050
$RefParser.parse = function(schema, options, callback) {
5151
var Class = this; // eslint-disable-line consistent-this
52-
return new Class().parse(schema, options, callback);
52+
var instance = new Class();
53+
return instance.parse.apply(instance, arguments);
5354
};
5455

5556
/**
@@ -66,40 +67,37 @@ $RefParser.prototype.parse = function(schema, options, callback) {
6667
var args = normalizeArgs(arguments);
6768
var promise;
6869

69-
if (!args.schema || (typeof args.schema !== 'string' && typeof args.schema !== 'object')) {
70-
var err = ono('Expected a file path, URL, or object. Got %s', args.schema);
70+
if (!args.path && !args.schema) {
71+
var err = ono('Expected a file path, URL, or object. Got %s', args.path || args.schema);
7172
return maybe(args.callback, Promise.reject(err));
7273
}
7374

7475
// Reset everything
7576
this.schema = null;
7677
this.$refs = new $Refs();
7778

78-
if (typeof args.schema === 'object') {
79-
// The schema is an object, not a path/url.
79+
if (args.schema && typeof args.schema === 'object') {
80+
// A schema object was passed-in.
8081
// So immediately add a new $Ref with the schema object as its value
81-
this.$refs._add('', args.schema);
82+
this.$refs._add(args.path, args.schema);
8283
promise = Promise.resolve(args.schema);
8384
}
8485
else {
85-
// The schema is a path/url
86-
var path = args.schema;
87-
8886
// If it's a filesystem path, then convert it to a URL.
8987
// NOTE: According to the JSON Reference spec, these should already be URLs,
9088
// but, in practice, many people use local filesystem paths instead.
9189
// So we're being generous here and doing the conversion automatically.
9290
// This is not intended to be a 100% bulletproof solution.
9391
// If it doesn't work for your use-case, then use a URL instead.
94-
if (url.isFileSystemPath(path)) {
95-
path = url.fromFileSystemPath(path);
92+
if (url.isFileSystemPath(args.path)) {
93+
args.path = url.fromFileSystemPath(args.path);
9694
}
9795

9896
// Resolve the absolute path of the schema
99-
path = url.resolve(url.cwd(), path);
97+
args.path = url.resolve(url.cwd(), args.path);
10098

10199
// Parse the schema file/url
102-
promise = parse(path, this.$refs, args.options);
100+
promise = parse(args.path, this.$refs, args.options);
103101
}
104102

105103
var me = this;
@@ -132,7 +130,8 @@ $RefParser.prototype.parse = function(schema, options, callback) {
132130
*/
133131
$RefParser.resolve = function(schema, options, callback) {
134132
var Class = this; // eslint-disable-line consistent-this
135-
return new Class().resolve(schema, options, callback);
133+
var instance = new Class();
134+
return instance.resolve.apply(instance, arguments);
136135
};
137136

138137
/**
@@ -151,7 +150,7 @@ $RefParser.prototype.resolve = function(schema, options, callback) {
151150
var me = this;
152151
var args = normalizeArgs(arguments);
153152

154-
return this.parse(args.schema, args.options)
153+
return this.parse(args.path, args.schema, args.options)
155154
.then(function() {
156155
return resolveExternal(me, args.options);
157156
})
@@ -175,7 +174,8 @@ $RefParser.prototype.resolve = function(schema, options, callback) {
175174
*/
176175
$RefParser.bundle = function(schema, options, callback) {
177176
var Class = this; // eslint-disable-line consistent-this
178-
return new Class().bundle(schema, options, callback);
177+
var instance = new Class();
178+
return instance.bundle.apply(instance, arguments);
179179
};
180180

181181
/**
@@ -192,7 +192,7 @@ $RefParser.prototype.bundle = function(schema, options, callback) {
192192
var me = this;
193193
var args = normalizeArgs(arguments);
194194

195-
return this.resolve(args.schema, args.options)
195+
return this.resolve(args.path, args.schema, args.options)
196196
.then(function() {
197197
bundle(me, args.options);
198198
return maybe(args.callback, Promise.resolve(me.schema));
@@ -213,7 +213,8 @@ $RefParser.prototype.bundle = function(schema, options, callback) {
213213
*/
214214
$RefParser.dereference = function(schema, options, callback) {
215215
var Class = this; // eslint-disable-line consistent-this
216-
return new Class().dereference(schema, options, callback);
216+
var instance = new Class();
217+
return instance.dereference.apply(instance, arguments);
217218
};
218219

219220
/**
@@ -229,7 +230,7 @@ $RefParser.prototype.dereference = function(schema, options, callback) {
229230
var me = this;
230231
var args = normalizeArgs(arguments);
231232

232-
return this.resolve(args.schema, args.options)
233+
return this.resolve(args.path, args.schema, args.options)
233234
.then(function() {
234235
dereference(me, args.options);
235236
return maybe(args.callback, Promise.resolve(me.schema));
@@ -246,16 +247,42 @@ $RefParser.prototype.dereference = function(schema, options, callback) {
246247
* @returns {object}
247248
*/
248249
function normalizeArgs(args) {
249-
var options = args[1], callback = args[2];
250-
if (typeof options === 'function') {
251-
callback = options;
252-
options = undefined;
250+
var path, schema, options, callback;
251+
args = Array.prototype.slice.call(args);
252+
253+
if (typeof args[args.length - 1] === 'function') {
254+
// The last parameter is a callback function
255+
callback = args.pop();
256+
}
257+
258+
if (typeof args[0] === 'string') {
259+
// The first parameter is the path
260+
path = args[0];
261+
if (typeof args[2] === 'object') {
262+
// The second parameter is the schema, and the third parameter is the options
263+
schema = args[1];
264+
options = args[2];
265+
}
266+
else {
267+
// The second parameter is the options
268+
schema = undefined;
269+
options = args[1];
270+
}
271+
}
272+
else {
273+
// The first parameter is the schema
274+
path = '';
275+
schema = args[0];
276+
options = args[1];
253277
}
278+
254279
if (!(options instanceof Options)) {
255280
options = new Options(options);
256281
}
282+
257283
return {
258-
schema: args[0],
284+
path: path,
285+
schema: schema,
259286
options: options,
260287
callback: callback
261288
};

0 commit comments

Comments
 (0)