closes #35
This commit is contained in:
185
lib/swagger.js
185
lib/swagger.js
@@ -1,6 +1,6 @@
|
||||
// Generated by CoffeeScript 1.3.3
|
||||
(function() {
|
||||
var SwaggerApi, SwaggerOperation, SwaggerRequest, SwaggerResource,
|
||||
var SwaggerApi, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource,
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
SwaggerApi = (function() {
|
||||
@@ -110,6 +110,7 @@
|
||||
return false;
|
||||
}
|
||||
}
|
||||
this.setConsolidatedModels();
|
||||
this.ready = true;
|
||||
if (this.success != null) {
|
||||
return this.success();
|
||||
@@ -121,6 +122,29 @@
|
||||
throw message;
|
||||
};
|
||||
|
||||
SwaggerApi.prototype.setConsolidatedModels = function() {
|
||||
var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results;
|
||||
this.modelsArray = [];
|
||||
this.models = {};
|
||||
_ref = this.resources;
|
||||
for (resource_name in _ref) {
|
||||
resource = _ref[resource_name];
|
||||
for (modelName in resource.models) {
|
||||
if (!(this.models[modelName] != null)) {
|
||||
this.models[modelName] = resource.models[modelName];
|
||||
this.modelsArray.push(resource.models[modelName]);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ref1 = this.modelsArray;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||
model = _ref1[_i];
|
||||
_results.push(model.setReferencedModels(this.models));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
SwaggerApi.prototype.suffixApiKey = function(url) {
|
||||
var sep;
|
||||
if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
|
||||
@@ -168,8 +192,11 @@
|
||||
this.basePath = this.api.basePath;
|
||||
this.operations = {};
|
||||
this.operationsArray = [];
|
||||
this.modelsArray = [];
|
||||
this.models = {};
|
||||
if ((resourceObj.operations != null) && (this.api.resourcePath != null)) {
|
||||
this.api.progress('reading resource ' + this.name + ' operations');
|
||||
this.api.progress('reading resource ' + this.name + ' models and operations');
|
||||
this.addModels(resourceObj.models);
|
||||
this.addOperations(resourceObj.path, resourceObj.operations);
|
||||
this.api[this.name] = this;
|
||||
} else {
|
||||
@@ -184,6 +211,7 @@
|
||||
_this.basePath = response.basePath;
|
||||
_this.basePath = _this.basePath.replace(/\/$/, '');
|
||||
}
|
||||
_this.addModels(response.models);
|
||||
if (response.apis) {
|
||||
_ref = response.apis;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
@@ -200,13 +228,31 @@
|
||||
}
|
||||
}
|
||||
|
||||
SwaggerResource.prototype.addModels = function(models) {
|
||||
var model, modelName, swaggerModel, _i, _len, _ref, _results;
|
||||
if (models != null) {
|
||||
for (modelName in models) {
|
||||
swaggerModel = new SwaggerModel(modelName, models[modelName]);
|
||||
this.modelsArray.push(swaggerModel);
|
||||
this.models[modelName] = swaggerModel;
|
||||
}
|
||||
_ref = this.modelsArray;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
model = _ref[_i];
|
||||
_results.push(model.setReferencedModels(this.models));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
SwaggerResource.prototype.addOperations = function(resource_path, ops) {
|
||||
var o, op, _i, _len, _results;
|
||||
if (ops) {
|
||||
_results = [];
|
||||
for (_i = 0, _len = ops.length; _i < _len; _i++) {
|
||||
o = ops[_i];
|
||||
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, this);
|
||||
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, this);
|
||||
this.operations[op.nickname] = op;
|
||||
_results.push(this.operationsArray.push(op));
|
||||
}
|
||||
@@ -233,9 +279,109 @@
|
||||
|
||||
})();
|
||||
|
||||
SwaggerModel = (function() {
|
||||
|
||||
function SwaggerModel(modelName, obj) {
|
||||
var propertyName;
|
||||
this.name = obj.id != null ? obj.id : modelName;
|
||||
this.properties = [];
|
||||
for (propertyName in obj.properties) {
|
||||
this.properties.push(new SwaggerModelProperty(propertyName, obj.properties[propertyName]));
|
||||
}
|
||||
}
|
||||
|
||||
SwaggerModel.prototype.setReferencedModels = function(allModels) {
|
||||
var prop, _i, _len, _ref, _results;
|
||||
_ref = this.properties;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
prop = _ref[_i];
|
||||
if (allModels[prop.dataType] != null) {
|
||||
_results.push(prop.refModel = allModels[prop.dataType]);
|
||||
} else if ((prop.refDataType != null) && (allModels[prop.refDataType] != null)) {
|
||||
_results.push(prop.refModel = allModels[prop.refDataType]);
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
SwaggerModel.prototype.getMockSignature = function(prefix, modelToIgnore) {
|
||||
var classClose, classOpen, prop, propertiesStr, returnVal, strong, strongClose, stronger, _i, _j, _len, _len1, _ref, _ref1;
|
||||
propertiesStr = [];
|
||||
_ref = this.properties;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
prop = _ref[_i];
|
||||
propertiesStr.push(prop.toString());
|
||||
}
|
||||
strong = '<span style="font-weight: bold; color: #000; font-size: 1.0em">';
|
||||
stronger = '<span style="font-weight: bold; color: #000; font-size: 1.1em">';
|
||||
strongClose = '</span>';
|
||||
classOpen = strong + 'class ' + this.name + '(' + strongClose;
|
||||
classClose = strong + ')' + strongClose;
|
||||
returnVal = classOpen + '<span>' + propertiesStr.join('</span>, <span>') + '</span>' + classClose;
|
||||
if (prefix != null) {
|
||||
returnVal = stronger + prefix + strongClose + '<br/>' + returnVal;
|
||||
}
|
||||
_ref1 = this.properties;
|
||||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
||||
prop = _ref1[_j];
|
||||
if ((prop.refModel != null) && (!(prop.refModel === modelToIgnore))) {
|
||||
returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(void 0, this));
|
||||
}
|
||||
}
|
||||
return returnVal;
|
||||
};
|
||||
|
||||
return SwaggerModel;
|
||||
|
||||
})();
|
||||
|
||||
SwaggerModelProperty = (function() {
|
||||
|
||||
function SwaggerModelProperty(name, obj) {
|
||||
this.name = name;
|
||||
this.dataType = obj.type;
|
||||
this.isArray = this.dataType.toLowerCase() === 'array';
|
||||
this.descr = obj.description;
|
||||
if (obj.items != null) {
|
||||
if (obj.items.type != null) {
|
||||
this.refDataType = obj.items.type;
|
||||
}
|
||||
if (obj.items.$ref != null) {
|
||||
this.refDataType = obj.items.$ref;
|
||||
}
|
||||
}
|
||||
this.dataTypeWithRef = this.refDataType != null ? this.dataType + '[' + this.refDataType + ']' : this.dataType;
|
||||
if (obj.allowableValues != null) {
|
||||
this.valueType = obj.allowableValues.valueType;
|
||||
this.values = obj.allowableValues.values;
|
||||
if (this.values != null) {
|
||||
this.valuesString = "'" + this.values.join("' or '") + "'";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SwaggerModelProperty.prototype.toString = function() {
|
||||
var str;
|
||||
str = this.name + ': ' + this.dataTypeWithRef;
|
||||
if (this.values != null) {
|
||||
str += " = ['" + this.values.join("' or '") + "']";
|
||||
}
|
||||
if (this.descr != null) {
|
||||
str += ' {' + this.descr + '}';
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
return SwaggerModelProperty;
|
||||
|
||||
})();
|
||||
|
||||
SwaggerOperation = (function() {
|
||||
|
||||
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, resource) {
|
||||
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, resource) {
|
||||
var parameter, v, _i, _j, _len, _len1, _ref, _ref1,
|
||||
_this = this;
|
||||
this.nickname = nickname;
|
||||
@@ -244,6 +390,7 @@
|
||||
this.parameters = parameters != null ? parameters : [];
|
||||
this.summary = summary;
|
||||
this.notes = notes;
|
||||
this.responseClass = responseClass;
|
||||
this.resource = resource;
|
||||
this["do"] = __bind(this["do"], this);
|
||||
|
||||
@@ -260,6 +407,12 @@
|
||||
this.httpMethod = this.httpMethod.toLowerCase();
|
||||
this.isGetMethod = this.httpMethod === "get";
|
||||
this.resourceName = this.resource.name;
|
||||
if (this.responseClass.toLowerCase() === 'void') {
|
||||
this.responseClass = void 0;
|
||||
}
|
||||
if (this.responseClass != null) {
|
||||
this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models);
|
||||
}
|
||||
_ref = this.parameters;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
parameter = _ref[_i];
|
||||
@@ -268,6 +421,7 @@
|
||||
parameter.allowableValues = {};
|
||||
parameter.allowableValues.values = this.resource.api.booleanValues;
|
||||
}
|
||||
parameter.signature = this.getSignature(parameter.dataType, this.resource.models);
|
||||
if (parameter.allowableValues != null) {
|
||||
if (parameter.allowableValues.valueType === "RANGE") {
|
||||
parameter.isRange = true;
|
||||
@@ -299,6 +453,29 @@
|
||||
};
|
||||
}
|
||||
|
||||
SwaggerOperation.prototype.isListType = function(dataType) {
|
||||
if (dataType.indexOf('[') >= 0) {
|
||||
return dataType.substring(dataType.indexOf('[') + 1, dataType.indexOf(']'));
|
||||
} else {
|
||||
return void 0;
|
||||
}
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.getSignature = function(dataType, models) {
|
||||
var isPrimitive, listType;
|
||||
listType = this.isListType(dataType);
|
||||
isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true;
|
||||
if (isPrimitive) {
|
||||
return dataType;
|
||||
} else {
|
||||
if (listType != null) {
|
||||
return models[listType].getMockSignature(dataType);
|
||||
} else {
|
||||
return models[dataType].getMockSignature(dataType);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype["do"] = function(args, callback, error) {
|
||||
var body, headers;
|
||||
if (args == null) {
|
||||
|
||||
@@ -54,7 +54,7 @@ class SwaggerUi extends Backbone.Router
|
||||
switch @options.docExpansion
|
||||
when "full" then Docs.expandOperationsForResource('')
|
||||
when "list" then Docs.collapseOperationsForResource('')
|
||||
@options.onComplete() if @options.onComplete
|
||||
@options.onComplete(@api, @) if @options.onComplete
|
||||
setTimeout(
|
||||
=>
|
||||
Docs.shebang()
|
||||
|
||||
@@ -6,6 +6,7 @@ class ParameterView extends Backbone.View
|
||||
|
||||
template = @template()
|
||||
$(@el).html(template(@model))
|
||||
|
||||
@
|
||||
|
||||
# Return an appropriate template based on if the parameter is a list, readonly, required
|
||||
|
||||
@@ -1502,3 +1502,16 @@ body ul#resources li.resource ul.endpoints li.endpoint ul.operations li.operatio
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
|
||||
.model-signature {
|
||||
font-family: "Droid Sans", sans-serif;
|
||||
font-size: 1em;
|
||||
line-height: 1.5em;
|
||||
}
|
||||
.model-signature span {
|
||||
font-size: 0.9em;
|
||||
line-height: 1.5em;
|
||||
}
|
||||
.model-signature span:nth-child(odd) { color:#333; }
|
||||
.model-signature span:nth-child(even) { color:#C5862B; }
|
||||
|
||||
@@ -47,8 +47,12 @@
|
||||
dom_id:"swagger-ui-container",
|
||||
supportHeaderParams: false,
|
||||
supportedSubmitMethods: ['get', 'post', 'put'],
|
||||
onComplete: function(){
|
||||
if(console) console.log("Loaded SwaggerUI")
|
||||
onComplete: function(swaggerApi, swaggerUi){
|
||||
if(console) {
|
||||
console.log("Loaded SwaggerUI")
|
||||
console.log(swaggerApi);
|
||||
console.log(swaggerUi);
|
||||
}
|
||||
},
|
||||
onFailure: function(data) {
|
||||
if(console) {
|
||||
@@ -56,7 +60,7 @@
|
||||
console.log(data);
|
||||
}
|
||||
},
|
||||
docExpansion: "list"
|
||||
docExpansion: "none"
|
||||
});
|
||||
|
||||
window.swaggerUi.load();
|
||||
|
||||
@@ -21,15 +21,24 @@
|
||||
<h4>Implementation Notes</h4>
|
||||
<p>{{{notes}}}</p>
|
||||
{{/if}}
|
||||
{{#if responseClass}}
|
||||
<h4>Response Class</h4>
|
||||
{{#if responseClassSignature}}
|
||||
<p><span class="model-signature">{{{responseClassSignature}}}</span></p>
|
||||
{{else}}
|
||||
<p>{{{responseClass}}}</p>
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
<form accept-charset='UTF-8' class='sandbox'>
|
||||
<div style='margin:0;padding:0;display:inline'></div>
|
||||
<h4>Parameters</h4>
|
||||
<table class='fullwidth'>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Parameter</th>
|
||||
<th>Value</th>
|
||||
<th>Description</th>
|
||||
<th style="width: 100px; max-width: 100px" >Parameter</th>
|
||||
<th style="width: 310px; max-width: 310px">Value</th>
|
||||
<th style="width: 200px; max-width: 200px">Description</th>
|
||||
<th style="width: 320px; max-width: 330px">Data Type</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody class="operation-params">
|
||||
|
||||
@@ -17,5 +17,8 @@
|
||||
{{/if}}
|
||||
|
||||
</td>
|
||||
<td width='500'>{{{description}}}</td>
|
||||
<td>{{{description}}}</td>
|
||||
<td>
|
||||
<span class="model-signature">{{{signature}}}</span>
|
||||
</td>
|
||||
|
||||
|
||||
@@ -17,5 +17,5 @@
|
||||
{{/each}}
|
||||
</select>
|
||||
</td>
|
||||
<td width='500'>{{{description}}}</td>
|
||||
|
||||
<td>{{{description}}}</td>
|
||||
<td><span class="model-signature">{{{signature}}}</span></td>
|
||||
@@ -6,5 +6,5 @@
|
||||
{{defaultValue}}
|
||||
{{/if}}
|
||||
</td>
|
||||
<td width='500'>{{{description}}}</td>
|
||||
|
||||
<td>{{{description}}}</td>
|
||||
<td><span class="model-signature">{{{signature}}}</span></td>
|
||||
|
||||
@@ -6,4 +6,5 @@
|
||||
{{defaultValue}}
|
||||
{{/if}}
|
||||
</td>
|
||||
<td width='500'>{{{description}}}</td>
|
||||
<td>{{{description}}}</td>
|
||||
<td><span class="model-signature">{{{signature}}}</span></td>
|
||||
@@ -15,6 +15,7 @@
|
||||
{{/if}}
|
||||
{{/if}}
|
||||
</td>
|
||||
<td width='500'>
|
||||
<td>
|
||||
<strong>{{{description}}}</strong>
|
||||
</td>
|
||||
<td><span class="model-signature">{{{signature}}}</span></td>
|
||||
Reference in New Issue
Block a user