Adding response status code views (swagger.js updated).
This commit is contained in:
1
Cakefile
1
Cakefile
@@ -11,6 +11,7 @@ sourceFiles = [
|
|||||||
'view/MainView'
|
'view/MainView'
|
||||||
'view/ResourceView'
|
'view/ResourceView'
|
||||||
'view/OperationView'
|
'view/OperationView'
|
||||||
|
'view/StatusCodeView'
|
||||||
'view/ParameterView'
|
'view/ParameterView'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
206
lib/swagger.js
206
lib/swagger.js
@@ -1,6 +1,6 @@
|
|||||||
// Generated by CoffeeScript 1.3.3
|
// Generated by CoffeeScript 1.3.3
|
||||||
(function() {
|
(function() {
|
||||||
var SwaggerApi, SwaggerOperation, SwaggerRequest, SwaggerResource,
|
var SwaggerApi, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource,
|
||||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||||
|
|
||||||
SwaggerApi = (function() {
|
SwaggerApi = (function() {
|
||||||
@@ -41,6 +41,7 @@
|
|||||||
this.failure = options.failure != null ? options.failure : function() {};
|
this.failure = options.failure != null ? options.failure : function() {};
|
||||||
this.progress = options.progress != null ? options.progress : function() {};
|
this.progress = options.progress != null ? options.progress : function() {};
|
||||||
this.headers = options.headers != null ? options.headers : {};
|
this.headers = options.headers != null ? options.headers : {};
|
||||||
|
this.booleanValues = options.booleanValues != null ? options.booleanValues : new Array('true', 'false');
|
||||||
this.discoveryUrl = this.suffixApiKey(this.discoveryUrl);
|
this.discoveryUrl = this.suffixApiKey(this.discoveryUrl);
|
||||||
if (options.success != null) {
|
if (options.success != null) {
|
||||||
this.build();
|
this.build();
|
||||||
@@ -109,6 +110,7 @@
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.setConsolidatedModels();
|
||||||
this.ready = true;
|
this.ready = true;
|
||||||
if (this.success != null) {
|
if (this.success != null) {
|
||||||
return this.success();
|
return this.success();
|
||||||
@@ -120,6 +122,29 @@
|
|||||||
throw message;
|
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) {
|
SwaggerApi.prototype.suffixApiKey = function(url) {
|
||||||
var sep;
|
var sep;
|
||||||
if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
|
if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
|
||||||
@@ -167,8 +192,11 @@
|
|||||||
this.basePath = this.api.basePath;
|
this.basePath = this.api.basePath;
|
||||||
this.operations = {};
|
this.operations = {};
|
||||||
this.operationsArray = [];
|
this.operationsArray = [];
|
||||||
|
this.modelsArray = [];
|
||||||
|
this.models = {};
|
||||||
if ((resourceObj.operations != null) && (this.api.resourcePath != null)) {
|
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.addOperations(resourceObj.path, resourceObj.operations);
|
||||||
this.api[this.name] = this;
|
this.api[this.name] = this;
|
||||||
} else {
|
} else {
|
||||||
@@ -183,6 +211,7 @@
|
|||||||
_this.basePath = response.basePath;
|
_this.basePath = response.basePath;
|
||||||
_this.basePath = _this.basePath.replace(/\/$/, '');
|
_this.basePath = _this.basePath.replace(/\/$/, '');
|
||||||
}
|
}
|
||||||
|
_this.addModels(response.models);
|
||||||
if (response.apis) {
|
if (response.apis) {
|
||||||
_ref = response.apis;
|
_ref = response.apis;
|
||||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||||
@@ -199,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) {
|
SwaggerResource.prototype.addOperations = function(resource_path, ops) {
|
||||||
var o, op, _i, _len, _results;
|
var o, op, _i, _len, _results;
|
||||||
if (ops) {
|
if (ops) {
|
||||||
_results = [];
|
_results = [];
|
||||||
for (_i = 0, _len = ops.length; _i < _len; _i++) {
|
for (_i = 0, _len = ops.length; _i < _len; _i++) {
|
||||||
o = ops[_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, o.errorResponses, this);
|
||||||
this.operations[op.nickname] = op;
|
this.operations[op.nickname] = op;
|
||||||
_results.push(this.operationsArray.push(op));
|
_results.push(this.operationsArray.push(op));
|
||||||
}
|
}
|
||||||
@@ -232,10 +279,110 @@
|
|||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
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() {
|
SwaggerOperation = (function() {
|
||||||
|
|
||||||
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, resource) {
|
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource) {
|
||||||
var parameter, v, _i, _j, _len, _len1, _ref, _ref1,
|
var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2,
|
||||||
_this = this;
|
_this = this;
|
||||||
this.nickname = nickname;
|
this.nickname = nickname;
|
||||||
this.path = path;
|
this.path = path;
|
||||||
@@ -243,6 +390,8 @@
|
|||||||
this.parameters = parameters != null ? parameters : [];
|
this.parameters = parameters != null ? parameters : [];
|
||||||
this.summary = summary;
|
this.summary = summary;
|
||||||
this.notes = notes;
|
this.notes = notes;
|
||||||
|
this.responseClass = responseClass;
|
||||||
|
this.errorResponses = errorResponses;
|
||||||
this.resource = resource;
|
this.resource = resource;
|
||||||
this["do"] = __bind(this["do"], this);
|
this["do"] = __bind(this["do"], this);
|
||||||
|
|
||||||
@@ -259,10 +408,22 @@
|
|||||||
this.httpMethod = this.httpMethod.toLowerCase();
|
this.httpMethod = this.httpMethod.toLowerCase();
|
||||||
this.isGetMethod = this.httpMethod === "get";
|
this.isGetMethod = this.httpMethod === "get";
|
||||||
this.resourceName = this.resource.name;
|
this.resourceName = this.resource.name;
|
||||||
_ref = this.parameters;
|
if (((_ref = this.responseClass) != null ? _ref.toLowerCase() : void 0) === 'void') {
|
||||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
this.responseClass = void 0;
|
||||||
parameter = _ref[_i];
|
}
|
||||||
|
if (this.responseClass != null) {
|
||||||
|
this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models);
|
||||||
|
}
|
||||||
|
this.errorResponses = this.errorResponses || [];
|
||||||
|
_ref1 = this.parameters;
|
||||||
|
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||||
|
parameter = _ref1[_i];
|
||||||
parameter.name = parameter.name || parameter.dataType;
|
parameter.name = parameter.name || parameter.dataType;
|
||||||
|
if (parameter.dataType.toLowerCase() === 'boolean') {
|
||||||
|
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 != null) {
|
||||||
if (parameter.allowableValues.valueType === "RANGE") {
|
if (parameter.allowableValues.valueType === "RANGE") {
|
||||||
parameter.isRange = true;
|
parameter.isRange = true;
|
||||||
@@ -271,9 +432,9 @@
|
|||||||
}
|
}
|
||||||
if (parameter.allowableValues.values != null) {
|
if (parameter.allowableValues.values != null) {
|
||||||
parameter.allowableValues.descriptiveValues = [];
|
parameter.allowableValues.descriptiveValues = [];
|
||||||
_ref1 = parameter.allowableValues.values;
|
_ref2 = parameter.allowableValues.values;
|
||||||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
|
||||||
v = _ref1[_j];
|
v = _ref2[_j];
|
||||||
if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
|
if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
|
||||||
parameter.allowableValues.descriptiveValues.push({
|
parameter.allowableValues.descriptiveValues.push({
|
||||||
value: v,
|
value: v,
|
||||||
@@ -294,6 +455,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) {
|
SwaggerOperation.prototype["do"] = function(args, callback, error) {
|
||||||
var body, headers;
|
var body, headers;
|
||||||
if (args == null) {
|
if (args == null) {
|
||||||
|
|||||||
@@ -16,6 +16,10 @@ class OperationView extends Backbone.View
|
|||||||
|
|
||||||
# Render each parameter
|
# Render each parameter
|
||||||
@addParameter param for param in @model.parameters
|
@addParameter param for param in @model.parameters
|
||||||
|
|
||||||
|
# Render each response code
|
||||||
|
@addStatusCode statusCode for statusCode in @model.errorResponses
|
||||||
|
|
||||||
@
|
@
|
||||||
|
|
||||||
addParameter: (param) ->
|
addParameter: (param) ->
|
||||||
@@ -23,6 +27,10 @@ class OperationView extends Backbone.View
|
|||||||
paramView = new ParameterView({model: param, tagName: 'tr', readOnly: @model.isReadOnly})
|
paramView = new ParameterView({model: param, tagName: 'tr', readOnly: @model.isReadOnly})
|
||||||
$('.operation-params', $(@el)).append paramView.render().el
|
$('.operation-params', $(@el)).append paramView.render().el
|
||||||
|
|
||||||
|
addStatusCode: (statusCode) ->
|
||||||
|
# Render status codes
|
||||||
|
statusCodeView = new StatusCodeView({model: statusCode, tagName: 'tr'})
|
||||||
|
$('.operation-status', $(@el)).append statusCodeView.render().el
|
||||||
|
|
||||||
submitOperation: (e) ->
|
submitOperation: (e) ->
|
||||||
e?.preventDefault()
|
e?.preventDefault()
|
||||||
|
|||||||
11
src/main/coffeescript/view/StatusCodeView.coffee
Normal file
11
src/main/coffeescript/view/StatusCodeView.coffee
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
class StatusCodeView extends Backbone.View
|
||||||
|
initialize: ->
|
||||||
|
|
||||||
|
render: ->
|
||||||
|
template = @template()
|
||||||
|
$(@el).html(template(@model))
|
||||||
|
@
|
||||||
|
|
||||||
|
template: ->
|
||||||
|
Handlebars.templates.status_code
|
||||||
|
|
||||||
@@ -36,6 +36,21 @@
|
|||||||
|
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
{{#if errorResponses}}
|
||||||
|
<div style='margin:0;padding:0;display:inline'></div>
|
||||||
|
<h4>Status Codes</h4>
|
||||||
|
<table class='fullwidth'>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>HTTP Status Code</th>
|
||||||
|
<th>Reason</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="operation-status">
|
||||||
|
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{{/if}}
|
||||||
{{#if isReadOnly}}
|
{{#if isReadOnly}}
|
||||||
{{else}}
|
{{else}}
|
||||||
<div class='sandbox_header'>
|
<div class='sandbox_header'>
|
||||||
|
|||||||
3
src/main/template/status_code.handlebars
Normal file
3
src/main/template/status_code.handlebars
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
<td width='15%' class='code'>{{code}}</td>
|
||||||
|
<td>{{{reason}}}</td>
|
||||||
|
|
||||||
Reference in New Issue
Block a user