forked from swagger-api/swagger-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswagger-client.js
3294 lines (3017 loc) · 95.3 KB
/
swagger-client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* swagger-client - swagger.js is a javascript client for use with swaggering APIs.
* @version v2.1.9-M1
* @link http://swagger.io
* @license apache 2.0
*/
(function(){
var ArrayModel = function(definition) {
this.name = "arrayModel";
this.definition = definition || {};
this.properties = [];
var requiredFields = definition.enum || [];
var innerType = definition.items;
if(innerType) {
if(innerType.type) {
this.type = typeFromJsonSchema(innerType.type, innerType.format);
}
else {
this.ref = innerType.$ref;
}
}
return this;
};
ArrayModel.prototype.createJSONSample = function(modelsToIgnore) {
var result;
modelsToIgnore = (modelsToIgnore||{});
if(this.type) {
result = this.type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
if(typeof modelsToIgnore[name] === 'undefined') {
modelsToIgnore[name] = this;
result = models[name].createJSONSample(modelsToIgnore);
}
else {
return name;
}
}
return [ result ];
};
ArrayModel.prototype.getSampleValue = function(modelsToIgnore) {
var result;
modelsToIgnore = (modelsToIgnore || {});
if(this.type) {
result = type;
}
else if (this.ref) {
var name = simpleRef(this.ref);
result = models[name].getSampleValue(modelsToIgnore);
}
return [ result ];
};
ArrayModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
var i, prop;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
propertiesStr.push(prop.toString());
}
var strong = '<span class="strong">';
var stronger = '<span class="stronger">';
var strongClose = '</span>';
var classOpen = strong + 'array' + ' {' + strongClose;
var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore)
modelsToIgnore = {};
modelsToIgnore[this.name] = this;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
var ref = prop.$ref;
var model = models[ref];
if (model && typeof modelsToIgnore[ref] === 'undefined') {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
}
}
return returnVal;
};
/**
* SwaggerAuthorizations applys the correct authorization to an operation being executed
*/
var SwaggerAuthorizations = function() {
this.authz = {};
};
SwaggerAuthorizations.prototype.add = function(name, auth) {
this.authz[name] = auth;
return auth;
};
SwaggerAuthorizations.prototype.remove = function(name) {
return delete this.authz[name];
};
SwaggerAuthorizations.prototype.apply = function (obj, authorizations) {
var status = null;
var key, name, value, result;
// if the "authorizations" key is undefined, or has an empty array, add all keys
if (typeof authorizations === 'undefined' || Object.keys(authorizations).length === 0) {
for (key in this.authz) {
value = this.authz[key];
result = value.apply(obj, authorizations);
if (result === true)
status = true;
}
}
else {
// 2.0 support
if (Array.isArray(authorizations)) {
for (var i = 0; i < authorizations.length; i++) {
var auth = authorizations[i];
for (name in auth) {
for (key in this.authz) {
if (key == name) {
value = this.authz[key];
result = value.apply(obj, authorizations);
if (result === true)
status = true;
}
}
}
}
}
else {
// 1.2 support
for (name in authorizations) {
for (key in this.authz) {
if (key == name) {
value = this.authz[key];
result = value.apply(obj, authorizations);
if (result === true)
status = true;
}
}
}
}
}
return status;
};
/**
* ApiKeyAuthorization allows a query param or header to be injected
*/
var ApiKeyAuthorization = function(name, value, type) {
this.name = name;
this.value = value;
this.type = type;
};
ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
if (this.type === "query") {
if (obj.url.indexOf('?') > 0)
obj.url = obj.url + "&" + this.name + "=" + this.value;
else
obj.url = obj.url + "?" + this.name + "=" + this.value;
return true;
} else if (this.type === "header") {
obj.headers[this.name] = this.value;
return true;
}
};
var CookieAuthorization = function(cookie) {
this.cookie = cookie;
};
CookieAuthorization.prototype.apply = function(obj, authorizations) {
obj.cookieJar = obj.cookieJar || CookieJar();
obj.cookieJar.setCookie(this.cookie);
return true;
};
/**
* Password Authorization is a basic auth implementation
*/
var PasswordAuthorization = function(name, username, password) {
this.name = name;
this.username = username;
this.password = password;
this._btoa = null;
if (typeof window !== 'undefined')
this._btoa = btoa;
else
this._btoa = require("btoa");
};
PasswordAuthorization.prototype.apply = function(obj, authorizations) {
var base64encoder = this._btoa;
obj.headers.Authorization = "Basic " + base64encoder(this.username + ":" + this.password);
return true;
};
var __bind = function(fn, me){
return function(){
return fn.apply(me, arguments);
};
};
fail = function(message) {
log(message);
};
log = function(){
log.history = log.history || [];
log.history.push(arguments);
if(this.console){
console.log( Array.prototype.slice.call(arguments)[0] );
}
};
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function(obj, start) {
for (var i = (start || 0), j = this.length; i < j; i++) {
if (this[i] === obj) { return i; }
}
return -1;
};
}
/**
* allows override of the default value based on the parameter being
* supplied
**/
var applyParameterMacro = function (operation, parameter) {
var e = (typeof window !== 'undefined' ? window : exports);
if(e.parameterMacro)
return e.parameterMacro(operation, parameter);
else
return parameter.defaultValue;
};
/**
* allows overriding the default value of an model property
**/
var applyModelPropertyMacro = function (model, property) {
var e = (typeof window !== 'undefined' ? window : exports);
if(e.modelPropertyMacro)
return e.modelPropertyMacro(model, property);
else
return property.defaultValue;
};
/**
* PrimitiveModel
**/
var PrimitiveModel = function(definition) {
this.name = "name";
this.definition = definition || {};
this.properties = [];
var requiredFields = definition.enum || [];
this.type = typeFromJsonSchema(definition.type, definition.format);
};
PrimitiveModel.prototype.createJSONSample = function(modelsToIgnore) {
var result = this.type;
return result;
};
PrimitiveModel.prototype.getSampleValue = function() {
var result = this.type;
return null;
};
PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) {
var propertiesStr = [];
var i, prop;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
propertiesStr.push(prop.toString());
}
var strong = '<span class="strong">';
var stronger = '<span class="stronger">';
var strongClose = '</span>';
var classOpen = strong + this.name + ' {' + strongClose;
var classClose = strong + '}' + strongClose;
var returnVal = classOpen + '<div>' + propertiesStr.join(',</div><div>') + '</div>' + classClose;
if (!modelsToIgnore)
modelsToIgnore = {};
modelsToIgnore[this.name] = this;
for (i = 0; i < this.properties.length; i++) {
prop = this.properties[i];
var ref = prop.$ref;
var model = models[ref];
if (model && typeof modelsToIgnore[ref] === 'undefined') {
returnVal = returnVal + ('<br>' + model.getMockSignature(modelsToIgnore));
}
}
return returnVal;
};
/**
* Resolves a spec's remote references
*/
var Resolver = function (){};
Resolver.prototype.resolve = function(spec, callback, scope) {
this.scope = (scope || this);
var host, name, path, property, propertyName, type;
var processedCalls = 0, resolvedRefs = {}, unresolvedRefs = {};
// store objects for dereferencing
var resolutionTable = {};
// models
for(name in spec.definitions) {
var model = spec.definitions[name];
for(propertyName in model.properties) {
property = model.properties[propertyName];
this.resolveTo(property, resolutionTable);
}
}
// operations
for(name in spec.paths) {
var method, operation, responseCode;
path = spec.paths[name];
for(method in path) {
operation = path[method];
var i, parameters = operation.parameters;
for(i in parameters) {
var parameter = parameters[i];
if(parameter.in === 'body' && parameter.schema) {
this.resolveTo(parameter.schema, resolutionTable);
}
if(parameter.$ref) {
this.resolveInline(spec, parameter, resolutionTable, unresolvedRefs);
}
}
for(responseCode in operation.responses) {
var response = operation.responses[responseCode];
if(response.schema) {
this.resolveTo(response.schema, resolutionTable);
}
}
}
}
// get hosts
var opts = {}, expectedCalls = 0;
for(name in resolutionTable) {
var parts = name.split('#');
if(parts.length == 2) {
host = parts[0]; path = parts[1];
if(!Array.isArray(opts[host])) {
opts[host] = [];
expectedCalls += 1;
}
opts[host].push(path);
}
}
for(name in opts) {
var self = this, opt = opts[name];
host = name;
var obj = {
useJQuery: false, // TODO
url: host,
method: "get",
headers: {
accept: this.scope.swaggerRequestHeaders || 'application/json'
},
on: {
error: function(response) {
processedCalls += 1;
var i;
for(i = 0; i < opt.length; i++) {
// fail all of these
var resolved = host + '#' + opt[i];
unresolvedRefs[resolved] = null;
}
if(processedCalls === expectedCalls)
self.finish(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback);
},
response: function(response) {
var i, j, swagger = response.obj;
processedCalls += 1;
for(i = 0; i < opt.length; i++) {
var location = swagger, path = opt[i], parts = path.split('/');
for(j = 0; j < parts.length; j++) {
var segment = parts[j];
if(typeof location === 'undefined')
break;
if(segment.length > 0)
location = location[segment];
}
var resolved = host + '#' + path, resolvedName = parts[j-1];
if(typeof location !== 'undefined') {
resolvedRefs[resolved] = {
name: resolvedName,
obj: location
};
}
else unresolvedRefs[resolved] = null;
}
if(processedCalls === expectedCalls)
self.finish(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback);
}
}
};
authorizations.apply(obj);
new SwaggerHttp().execute(obj);
}
if(Object.keys(opts).length === 0)
callback.call(this.scope, spec, unresolvedRefs);
};
Resolver.prototype.finish = function(spec, resolutionTable, resolvedRefs, unresolvedRefs, callback) {
// walk resolution table and replace with resolved refs
var ref;
for(ref in resolutionTable) {
var i, locations = resolutionTable[ref];
for(i = 0; i < locations.length; i++) {
var resolvedTo = resolvedRefs[locations[i].obj.$ref];
if(resolvedTo) {
if(!spec.definitions)
spec.definitions = {};
if(locations[i].resolveAs === '$ref') {
spec.definitions[resolvedTo.name] = resolvedTo.obj;
locations[i].obj.$ref = '#/definitions/' + resolvedTo.name;
}
else if (locations[i].resolveAs === 'inline') {
var key;
var targetObj = locations[i].obj;
delete targetObj.$ref;
for(key in resolvedTo.obj) {
targetObj[key] = resolvedTo.obj[key];
}
}
}
}
}
callback.call(this.scope, spec, unresolvedRefs);
};
/**
* immediately in-lines local refs, queues remote refs
* for inline resolution
*/
Resolver.prototype.resolveInline = function (spec, property, objs, unresolvedRefs) {
var ref = property.$ref;
if(ref) {
if(ref.indexOf('http') === 0) {
if(Array.isArray(objs[ref])) {
objs[ref].push({obj: property, resolveAs: 'inline'});
}
else {
objs[ref] = [{obj: property, resolveAs: 'inline'}];
}
}
else if (ref.indexOf('#') === 0) {
// local resolve
var shortenedRef = ref.substring(1);
var i, parts = shortenedRef.split('/'), location = spec;
for(i = 0; i < parts.length; i++) {
var part = parts[i];
if(part.length > 0) {
location = location[part];
}
}
if(location) {
delete property.$ref;
var key;
for(key in location) {
property[key] = location[key];
}
}
else unresolvedRefs[ref] = null;
}
}
else if(property.type === 'array') {
this.resolveTo(property.items, objs);
}
};
Resolver.prototype.resolveTo = function (property, objs) {
var ref = property.$ref;
if(ref) {
if(ref.indexOf('http') === 0) {
if(Array.isArray(objs[ref])) {
objs[ref].push({obj: property, resolveAs: '$ref'});
}
else {
objs[ref] = [{obj: property, resolveAs: '$ref'}];
}
}
}
else if(property.type === 'array') {
var items = property.items;
this.resolveTo(items, objs);
}
};
var addModel = function(name, model) {
models[name] = model;
};
var SwaggerClient = function(url, options) {
this.isBuilt = false;
this.url = null;
this.debug = false;
this.basePath = null;
this.modelsArray = [];
this.authorizations = null;
this.authorizationScheme = null;
this.isValid = false;
this.info = null;
this.useJQuery = false;
this.resourceCount = 0;
if(typeof url !== 'undefined')
return this.initialize(url, options);
};
SwaggerClient.prototype.initialize = function (url, options) {
this.models = models = {};
options = (options||{});
if(typeof url === 'string')
this.url = url;
else if(typeof url === 'object') {
options = url;
this.url = options.url;
}
this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json;charset=utf-8,*/*';
this.defaultSuccessCallback = options.defaultSuccessCallback || null;
this.defaultErrorCallback = options.defaultErrorCallback || null;
if (typeof options.success === 'function')
this.success = options.success;
if (options.useJQuery)
this.useJQuery = options.useJQuery;
if (options.authorizations) {
this.clientAuthorizations = options.authorizations;
} else {
this.clientAuthorizations = authorizations;
}
this.supportedSubmitMethods = options.supportedSubmitMethods || [];
this.failure = options.failure || function() {};
this.progress = options.progress || function() {};
this.spec = options.spec;
this.options = options;
if (typeof options.success === 'function') {
this.ready = true;
this.build();
}
};
SwaggerClient.prototype.build = function(mock) {
if (this.isBuilt) return this;
var self = this;
this.progress('fetching resource list: ' + this.url);
var obj = {
useJQuery: this.useJQuery,
url: this.url,
method: "get",
headers: {
accept: this.swaggerRequstHeaders
},
on: {
error: function(response) {
if (self.url.substring(0, 4) !== 'http')
return self.fail('Please specify the protocol for ' + self.url);
else if (response.status === 0)
return self.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
else if (response.status === 404)
return self.fail('Can\'t read swagger JSON from ' + self.url);
else
return self.fail(response.status + ' : ' + response.statusText + ' ' + self.url);
},
response: function(resp) {
var responseObj = resp.obj || JSON.parse(resp.data);
self.swaggerVersion = responseObj.swaggerVersion;
if(responseObj.swagger && parseInt(responseObj.swagger) === 2) {
self.swaggerVersion = responseObj.swagger;
new Resolver().resolve(responseObj, self.buildFromSpec, self);
self.isValid = true;
}
else {
if (self.swaggerVersion === '1.2') {
return self.buildFrom1_2Spec(responseObj);
} else {
return self.buildFrom1_1Spec(responseObj);
}
}
}
}
};
if(this.spec) {
setTimeout(function() {
new Resolver().resolve(self.spec, self.buildFromSpec, self);
}, 10);
}
else {
authorizations.apply(obj);
if(mock)
return obj;
new SwaggerHttp().execute(obj);
}
return this;
};
SwaggerClient.prototype.buildFromSpec = function(response) {
if(this.isBuilt) return this;
this.info = response.info || {};
this.title = response.title || '';
this.host = response.host || '';
this.schemes = response.schemes || [];
this.basePath = response.basePath || '';
this.apis = {};
this.apisArray = [];
this.consumes = response.consumes;
this.produces = response.produces;
this.securityDefinitions = response.securityDefinitions;
// legacy support
this.authSchemes = response.securityDefinitions;
var definedTags = {};
if(Array.isArray(response.tags)) {
definedTags = {};
for(k = 0; k < response.tags.length; k++) {
var t = response.tags[k];
definedTags[t.name] = t;
}
}
var location;
if(typeof this.url === 'string') {
location = this.parseUri(this.url);
}
if(typeof this.schemes === 'undefined' || this.schemes.length === 0) {
this.scheme = location.scheme || 'http';
}
else {
this.scheme = this.schemes[0];
}
if(typeof this.host === 'undefined' || this.host === '') {
this.host = location.host;
if (location.port) {
this.host = this.host + ':' + location.port;
}
}
this.definitions = response.definitions;
var key;
for(key in this.definitions) {
var model = new Model(key, this.definitions[key]);
if(model) {
models[key] = model;
}
}
// get paths, create functions for each operationId
var path;
var operations = [];
for(path in response.paths) {
if(typeof response.paths[path] === 'object') {
var httpMethod;
for(httpMethod in response.paths[path]) {
if(['delete', 'get', 'head', 'options', 'patch', 'post', 'put'].indexOf(httpMethod) === -1) {
continue;
}
var operation = response.paths[path][httpMethod];
var tags = operation.tags;
if(typeof tags === 'undefined') {
operation.tags = [ 'default' ];
tags = operation.tags;
}
var operationId = this.idFromOp(path, httpMethod, operation);
var operationObject = new Operation (
this,
operation.scheme,
operationId,
httpMethod,
path,
operation,
this.definitions
);
// bind this operation's execute command to the api
if(tags.length > 0) {
var i;
for(i = 0; i < tags.length; i++) {
var tag = this.tagFromLabel(tags[i]);
var operationGroup = this[tag];
if(typeof this.apis[tag] === 'undefined')
this.apis[tag] = {};
if(typeof operationGroup === 'undefined') {
this[tag] = [];
operationGroup = this[tag];
operationGroup.operations = {};
operationGroup.label = tag;
operationGroup.apis = [];
var tagObject = definedTags[tag];
if(typeof tagObject === 'object') {
operationGroup.description = tagObject.description;
operationGroup.externalDocs = tagObject.externalDocs;
}
this[tag].help = this.help.bind(operationGroup);
this.apisArray.push(new OperationGroup(tag, operationGroup.description, operationGroup.externalDocs, operationObject));
}
if(typeof this.apis[tag].help !== 'function')
this.apis[tag].help = this.help.bind(operationGroup);
// bind to the apis object
this.apis[tag][operationId] = operationObject.execute.bind(operationObject);
this.apis[tag][operationId].help = operationObject.help.bind(operationObject);
this.apis[tag][operationId].asCurl = operationObject.asCurl.bind(operationObject);
operationGroup[operationId] = operationObject.execute.bind(operationObject);
operationGroup[operationId].help = operationObject.help.bind(operationObject);
operationGroup[operationId].asCurl = operationObject.asCurl.bind(operationObject);
operationGroup.apis.push(operationObject);
operationGroup.operations[operationId] = operationObject;
// legacy UI feature
var j;
var api;
for(j = 0; j < this.apisArray.length; j++) {
if(this.apisArray[j].tag === tag) {
api = this.apisArray[j];
}
}
if(api) {
api.operationsArray.push(operationObject);
}
}
}
else {
log('no group to bind to');
}
}
}
}
this.isBuilt = true;
if (this.success) {
this.isValid = true;
this.isBuilt = true;
this.success();
}
return this;
};
SwaggerClient.prototype.parseUri = function(uri) {
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
var parts = urlParseRE.exec(uri);
return {
scheme: parts[4].replace(':',''),
host: parts[11],
port: parts[12],
path: parts[15]
};
};
SwaggerClient.prototype.help = function(dontPrint) {
var i;
var output = 'operations for the "' + this.label + '" tag';
for(i = 0; i < this.apis.length; i++) {
var api = this.apis[i];
output += '\n * ' + api.nickname + ': ' + api.operation.summary;
}
if(dontPrint)
return output;
else {
log(output);
return output;
}
};
SwaggerClient.prototype.tagFromLabel = function(label) {
return label;
};
SwaggerClient.prototype.idFromOp = function(path, httpMethod, op) {
var opId = op.operationId || (path.substring(1) + '_' + httpMethod);
return opId.replace(/[\.,-\/#!$%\^&\*;:{}=\-_`~()\+\s]/g,'_');
};
SwaggerClient.prototype.fail = function(message) {
this.failure(message);
throw message;
};
var OperationGroup = function(tag, description, externalDocs, operation) {
this.tag = tag;
this.path = tag;
this.description = description;
this.externalDocs = externalDocs;
this.name = tag;
this.operation = operation;
this.operationsArray = [];
};
var Operation = function(parent, scheme, operationId, httpMethod, path, args, definitions) {
var errors = [];
parent = parent||{};
args = args||{};
this.operations = {};
this.operation = args;
this.deprecated = args.deprecated;
this.consumes = args.consumes;
this.produces = args.produces;
this.parent = parent;
this.host = parent.host || 'localhost';
this.schemes = parent.schemes;
this.scheme = scheme || parent.scheme || 'http';
this.basePath = parent.basePath || '/';
this.nickname = (operationId||errors.push('Operations must have a nickname.'));
this.method = (httpMethod||errors.push('Operation ' + operationId + ' is missing method.'));
this.path = (path||errors.push('Operation ' + this.nickname + ' is missing path.'));
this.parameters = args !== null ? (args.parameters||[]) : {};
this.summary = args.summary || '';
this.responses = (args.responses||{});
this.type = null;
this.security = args.security;
this.authorizations = args.security;
this.description = args.description;
this.useJQuery = parent.useJQuery;
if(typeof this.deprecated === 'string') {
switch(this.deprecated.toLowerCase()) {
case 'true': case 'yes': case '1': {
this.deprecated = true;
break;
}
case 'false': case 'no': case '0': case null: {
this.deprecated = false;
break;
}
default: this.deprecated = Boolean(this.deprecated);
}
}
var i, model;
if(definitions) {
// add to global models
var key;
for(key in this.definitions) {
model = new Model(key, definitions[key]);
if(model) {
models[key] = model;
}
}
}
for(i = 0; i < this.parameters.length; i++) {
var param = this.parameters[i];
if(param.type === 'array') {
param.isList = true;
param.allowMultiple = true;
}
var innerType = this.getType(param);
if(innerType && innerType.toString().toLowerCase() === 'boolean') {
param.allowableValues = {};
param.isList = true;
param['enum'] = ["true", "false"];
}
if(typeof param['enum'] !== 'undefined') {
var id;
param.allowableValues = {};
param.allowableValues.values = [];
param.allowableValues.descriptiveValues = [];
for(id = 0; id < param['enum'].length; id++) {
var value = param['enum'][id];
var isDefault = (value === param.default) ? true : false;
param.allowableValues.values.push(value);
param.allowableValues.descriptiveValues.push({value : value, isDefault: isDefault});
}
}
if(param.type === 'array') {
innerType = [innerType];
if(typeof param.allowableValues === 'undefined') {
// can't show as a list if no values to select from
delete param.isList;
delete param.allowMultiple;
}
}
param.signature = this.getModelSignature(innerType, models).toString();
param.sampleJSON = this.getModelSampleJSON(innerType, models);
param.responseClassSignature = param.signature;
}
var defaultResponseCode, response, responses = this.responses;
if(responses['200']) {
response = responses['200'];
defaultResponseCode = '200';
}
else if(responses['201']) {
response = responses['201'];
defaultResponseCode = '201';
}
else if(responses['202']) {
response = responses['202'];
defaultResponseCode = '202';
}
else if(responses['203']) {
response = responses['203'];
defaultResponseCode = '203';
}
else if(responses['204']) {
response = responses['204'];
defaultResponseCode = '204';
}
else if(responses['205']) {
response = responses['205'];
defaultResponseCode = '205';
}
else if(responses['206']) {
response = responses['206'];
defaultResponseCode = '206';
}
else if(responses['default']) {
response = responses['default'];
defaultResponseCode = 'default';
}
if(response && response.schema) {
var resolvedModel = this.resolveModel(response.schema, definitions);
delete responses[defaultResponseCode];
if(resolvedModel) {
this.successResponse = {};
this.successResponse[defaultResponseCode] = resolvedModel;
}
else {
this.successResponse = {};
this.successResponse[defaultResponseCode] = response.schema.type;
}
this.type = response;
}
if (errors.length > 0) {
if(this.resource && this.resource.api && this.resource.api.fail)
this.resource.api.fail(errors);
}
return this;
};
OperationGroup.prototype.sort = function(sorter) {
};
Operation.prototype.getType = function (param) {
var type = param.type;
var format = param.format;
var isArray = false;
var str;
if(type === 'integer' && format === 'int32')
str = 'integer';
else if(type === 'integer' && format === 'int64')
str = 'long';
else if(type === 'integer')
str = 'integer';
else if(type === 'string') {
if(format === 'date-time')
str = 'date-time';
else if(format === 'date')
str = 'date';
else
str = 'string';
}
else if(type === 'number' && format === 'float')
str = 'float';
else if(type === 'number' && format === 'double')
str = 'double';
else if(type === 'number')
str = 'double';
else if(type === 'boolean')
str = 'boolean';
else if(type === 'array') {
isArray = true;
if(param.items)
str = this.getType(param.items);
}
if(param.$ref)
str = param.$ref;
var schema = param.schema;
if(schema) {
var ref = schema.$ref;