@@ -49,7 +49,8 @@ function $RefParser() {
49
49
*/
50
50
$RefParser . parse = function ( schema , options , callback ) {
51
51
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 ) ;
53
54
} ;
54
55
55
56
/**
@@ -66,40 +67,37 @@ $RefParser.prototype.parse = function(schema, options, callback) {
66
67
var args = normalizeArgs ( arguments ) ;
67
68
var promise ;
68
69
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 ) ;
71
72
return maybe ( args . callback , Promise . reject ( err ) ) ;
72
73
}
73
74
74
75
// Reset everything
75
76
this . schema = null ;
76
77
this . $refs = new $Refs ( ) ;
77
78
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 .
80
81
// 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 ) ;
82
83
promise = Promise . resolve ( args . schema ) ;
83
84
}
84
85
else {
85
- // The schema is a path/url
86
- var path = args . schema ;
87
-
88
86
// If it's a filesystem path, then convert it to a URL.
89
87
// NOTE: According to the JSON Reference spec, these should already be URLs,
90
88
// but, in practice, many people use local filesystem paths instead.
91
89
// So we're being generous here and doing the conversion automatically.
92
90
// This is not intended to be a 100% bulletproof solution.
93
91
// 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 ) ;
96
94
}
97
95
98
96
// Resolve the absolute path of the schema
99
- path = url . resolve ( url . cwd ( ) , path ) ;
97
+ args . path = url . resolve ( url . cwd ( ) , args . path ) ;
100
98
101
99
// Parse the schema file/url
102
- promise = parse ( path , this . $refs , args . options ) ;
100
+ promise = parse ( args . path , this . $refs , args . options ) ;
103
101
}
104
102
105
103
var me = this ;
@@ -132,7 +130,8 @@ $RefParser.prototype.parse = function(schema, options, callback) {
132
130
*/
133
131
$RefParser . resolve = function ( schema , options , callback ) {
134
132
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 ) ;
136
135
} ;
137
136
138
137
/**
@@ -151,7 +150,7 @@ $RefParser.prototype.resolve = function(schema, options, callback) {
151
150
var me = this ;
152
151
var args = normalizeArgs ( arguments ) ;
153
152
154
- return this . parse ( args . schema , args . options )
153
+ return this . parse ( args . path , args . schema , args . options )
155
154
. then ( function ( ) {
156
155
return resolveExternal ( me , args . options ) ;
157
156
} )
@@ -175,7 +174,8 @@ $RefParser.prototype.resolve = function(schema, options, callback) {
175
174
*/
176
175
$RefParser . bundle = function ( schema , options , callback ) {
177
176
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 ) ;
179
179
} ;
180
180
181
181
/**
@@ -192,7 +192,7 @@ $RefParser.prototype.bundle = function(schema, options, callback) {
192
192
var me = this ;
193
193
var args = normalizeArgs ( arguments ) ;
194
194
195
- return this . resolve ( args . schema , args . options )
195
+ return this . resolve ( args . path , args . schema , args . options )
196
196
. then ( function ( ) {
197
197
bundle ( me , args . options ) ;
198
198
return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
@@ -213,7 +213,8 @@ $RefParser.prototype.bundle = function(schema, options, callback) {
213
213
*/
214
214
$RefParser . dereference = function ( schema , options , callback ) {
215
215
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 ) ;
217
218
} ;
218
219
219
220
/**
@@ -229,7 +230,7 @@ $RefParser.prototype.dereference = function(schema, options, callback) {
229
230
var me = this ;
230
231
var args = normalizeArgs ( arguments ) ;
231
232
232
- return this . resolve ( args . schema , args . options )
233
+ return this . resolve ( args . path , args . schema , args . options )
233
234
. then ( function ( ) {
234
235
dereference ( me , args . options ) ;
235
236
return maybe ( args . callback , Promise . resolve ( me . schema ) ) ;
@@ -246,16 +247,42 @@ $RefParser.prototype.dereference = function(schema, options, callback) {
246
247
* @returns {object }
247
248
*/
248
249
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 ] ;
253
277
}
278
+
254
279
if ( ! ( options instanceof Options ) ) {
255
280
options = new Options ( options ) ;
256
281
}
282
+
257
283
return {
258
- schema : args [ 0 ] ,
284
+ path : path ,
285
+ schema : schema ,
259
286
options : options ,
260
287
callback : callback
261
288
} ;
0 commit comments