1
1
// swagger.js
2
- // version 2.0.37
2
+ // version 2.0.39
3
3
4
4
var __bind = function ( fn , me ) {
5
5
return function ( ) {
@@ -15,6 +15,16 @@ log = function(){
15
15
}
16
16
} ;
17
17
18
+ // if you want to apply conditional formatting of parameter values
19
+ parameterMacro = function ( value ) {
20
+ return value ;
21
+ }
22
+
23
+ // if you want to apply conditional formatting of model property values
24
+ modelPropertyMacro = function ( value ) {
25
+ return value ;
26
+ }
27
+
18
28
if ( ! Array . prototype . indexOf ) {
19
29
Array . prototype . indexOf = function ( obj , start ) {
20
30
for ( var i = ( start || 0 ) , j = this . length ; i < j ; i ++ ) {
@@ -80,13 +90,15 @@ Object.keys = Object.keys || (function () {
80
90
} ) ( ) ;
81
91
82
92
var SwaggerApi = function ( url , options ) {
93
+ this . isBuilt = false ;
83
94
this . url = null ;
84
95
this . debug = false ;
85
96
this . basePath = null ;
86
97
this . authorizations = null ;
87
98
this . authorizationScheme = null ;
88
99
this . info = null ;
89
100
this . useJQuery = false ;
101
+ this . modelsArray = [ ] ;
90
102
91
103
options = ( options || { } ) ;
92
104
if ( url )
@@ -108,19 +120,23 @@ var SwaggerApi = function(url, options) {
108
120
109
121
this . failure = options . failure != null ? options . failure : function ( ) { } ;
110
122
this . progress = options . progress != null ? options . progress : function ( ) { } ;
111
- if ( options . success != null )
123
+ if ( options . success != null ) {
112
124
this . build ( ) ;
125
+ this . isBuilt = true ;
126
+ }
113
127
}
114
128
115
129
SwaggerApi . prototype . build = function ( ) {
130
+ if ( this . isBuilt )
131
+ return this ;
116
132
var _this = this ;
117
133
this . progress ( 'fetching resource list: ' + this . url ) ;
118
134
var obj = {
119
135
useJQuery : this . useJQuery ,
120
136
url : this . url ,
121
137
method : "get" ,
122
138
headers : {
123
- accept : "application/json"
139
+ accept : "application/json,application/json;charset=\"utf-8\",*/* "
124
140
} ,
125
141
on : {
126
142
error : function ( response ) {
@@ -276,7 +292,6 @@ SwaggerApi.prototype.fail = function(message) {
276
292
277
293
SwaggerApi . prototype . setConsolidatedModels = function ( ) {
278
294
var model , modelName , resource , resource_name , _i , _len , _ref , _ref1 , _results ;
279
- this . modelsArray = [ ] ;
280
295
this . models = { } ;
281
296
_ref = this . apis ;
282
297
for ( resource_name in _ref ) {
@@ -353,7 +368,7 @@ var SwaggerResource = function(resourceObj, api) {
353
368
method : "get" ,
354
369
useJQuery : this . useJQuery ,
355
370
headers : {
356
- accept : "application/json"
371
+ accept : "application/json,application/json;charset=\"utf-8\",*/* "
357
372
} ,
358
373
on : {
359
374
response : function ( resp ) {
@@ -593,6 +608,7 @@ var SwaggerModelProperty = function(name, obj) {
593
608
this . isCollection = this . dataType && ( this . dataType . toLowerCase ( ) === 'array' || this . dataType . toLowerCase ( ) === 'list' || this . dataType . toLowerCase ( ) === 'set' ) ;
594
609
this . descr = obj . description ;
595
610
this . required = obj . required ;
611
+ this . defaultValue = modelPropertyMacro ( obj . defaultValue ) ;
596
612
if ( obj . items != null ) {
597
613
if ( obj . items . type != null ) {
598
614
this . refDataType = obj . items . type ;
@@ -638,7 +654,9 @@ SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
638
654
639
655
SwaggerModelProperty . prototype . toSampleValue = function ( value ) {
640
656
var result ;
641
- if ( value === "integer" ) {
657
+ if ( ( typeof this . defaultValue !== 'undefined' ) && this . defaultValue !== null ) {
658
+ result = this . defaultValue ;
659
+ } else if ( value === "integer" ) {
642
660
result = 0 ;
643
661
} else if ( value === "boolean" ) {
644
662
result = false ;
@@ -768,6 +786,7 @@ var SwaggerOperation = function(nickname, path, method, parameters, summary, not
768
786
}
769
787
}
770
788
}
789
+ param . defaultValue = parameterMacro ( param . defaultValue ) ;
771
790
}
772
791
this . resource [ this . nickname ] = function ( args , callback , error ) {
773
792
return _this [ "do" ] ( args , callback , error ) ;
@@ -1218,7 +1237,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) {
1218
1237
// if there's a body, need to set the accepts header via requestContentType
1219
1238
if ( body && ( this . type === "POST" || this . type === "PUT" || this . type === "PATCH" || this . type === "DELETE" ) ) {
1220
1239
if ( this . opts . requestContentType )
1221
- accepts = this . opts . requestContentType ;
1240
+ consumes = this . opts . requestContentType ;
1222
1241
} else {
1223
1242
// if any form params, content type must be set
1224
1243
if ( definedFormParams . length > 0 ) {
@@ -1227,7 +1246,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) {
1227
1246
else
1228
1247
consumes = "application/x-www-form-urlencoded" ;
1229
1248
}
1230
- else if ( this . type == "DELETE" )
1249
+ else if ( this . type === "DELETE" )
1231
1250
body = "{}" ;
1232
1251
else if ( this . type != "DELETE" )
1233
1252
accepts = null ;
@@ -1461,11 +1480,36 @@ ShredHttpClient.prototype.execute = function(obj) {
1461
1480
return out ;
1462
1481
} ;
1463
1482
1483
+ // Transform an error into a usable response-like object
1484
+ var transformError = function ( error ) {
1485
+ var out = {
1486
+ // Default to a status of 0 - The client will treat this as a generic permissions sort of error
1487
+ status : 0 ,
1488
+ data : error . message || error
1489
+ } ;
1490
+
1491
+ if ( error . code ) {
1492
+ out . obj = error ;
1493
+
1494
+ if ( error . code === 'ENOTFOUND' || error . code === 'ECONNREFUSED' ) {
1495
+ // We can tell the client that this should be treated as a missing resource and not as a permissions thing
1496
+ out . status = 404 ;
1497
+ }
1498
+ }
1499
+
1500
+ return out ;
1501
+ } ;
1502
+
1464
1503
res = {
1465
1504
error : function ( response ) {
1466
1505
if ( obj )
1467
1506
return cb . error ( transform ( response ) ) ;
1468
1507
} ,
1508
+ // Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming)
1509
+ request_error : function ( err ) {
1510
+ if ( obj )
1511
+ return cb . error ( transformError ( err ) ) ;
1512
+ } ,
1469
1513
redirect : function ( response ) {
1470
1514
if ( obj )
1471
1515
return cb . redirect ( transform ( response ) ) ;
@@ -1533,10 +1577,11 @@ SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
1533
1577
/**
1534
1578
* ApiKeyAuthorization allows a query param or header to be injected
1535
1579
*/
1536
- var ApiKeyAuthorization = function ( name , value , type ) {
1580
+ var ApiKeyAuthorization = function ( name , value , type , delimiter ) {
1537
1581
this . name = name ;
1538
1582
this . value = value ;
1539
1583
this . type = type ;
1584
+ this . delimiter = delimiter ;
1540
1585
} ;
1541
1586
1542
1587
ApiKeyAuthorization . prototype . apply = function ( obj , authorizations ) {
@@ -1547,7 +1592,12 @@ ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
1547
1592
obj . url = obj . url + "?" + this . name + "=" + this . value ;
1548
1593
return true ;
1549
1594
} else if ( this . type === "header" ) {
1550
- obj . headers [ this . name ] = this . value ;
1595
+ if ( typeof obj . headers [ this . name ] !== 'undefined' ) {
1596
+ if ( typeof this . delimiter !== 'undefined' )
1597
+ obj . headers [ this . name ] = obj . headers [ this . name ] + this . delimiter + this . value ;
1598
+ }
1599
+ else
1600
+ obj . headers [ this . name ] = this . value ;
1551
1601
return true ;
1552
1602
}
1553
1603
} ;
0 commit comments