updated to latest swagger-js

This commit is contained in:
Tony Tam
2013-07-08 18:38:11 -07:00
parent 22e5f5ccc1
commit 8f7e8f9ee0
10 changed files with 159 additions and 129 deletions

1
dist/index.html vendored
View File

@@ -45,6 +45,7 @@
var key = $('#input_apiKey')[0].value;
console.log("key: " + key);
if(key && key.trim() != "") {
console.log("added key " + key);
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query"));
}
})

122
dist/lib/swagger.js vendored
View File

@@ -38,6 +38,7 @@
var obj,
_this = this;
this.progress('fetching resource list: ' + this.discoveryUrl);
console.log('getting ' + this.discoveryUrl);
obj = {
url: this.discoveryUrl,
method: "get",
@@ -79,7 +80,8 @@
}
}
};
return new SwaggerHttp().execute(obj);
new SwaggerHttp().execute(obj);
return this;
};
SwaggerApi.prototype.selfReflect = function() {
@@ -519,6 +521,9 @@
this.resource[this.nickname] = function(args, callback, error) {
return _this["do"](args, callback, error);
};
this.resource[this.nickname].help = function() {
return _this.help();
};
}
SwaggerOperation.prototype.isListType = function(dataType) {
@@ -555,18 +560,25 @@
}
};
SwaggerOperation.prototype["do"] = function(args, callback, error) {
var key, param, params, possibleParams, requestContentType, responseContentType, value;
SwaggerOperation.prototype["do"] = function(args, opts, callback, error) {
var key, param, params, possibleParams, req, requestContentType, responseContentType, value;
if (args == null) {
args = {};
}
if (opts == null) {
opts = {};
}
requestContentType = null;
responseContentType = null;
if ((typeof args) === "function") {
error = callback;
error = opts;
callback = args;
args = {};
}
if ((typeof opts) === "function") {
error = callback;
callback = opts;
}
if (error == null) {
error = function(xhr, textStatus, error) {
return console.log(xhr, textStatus, error);
@@ -574,16 +586,17 @@
}
if (callback == null) {
callback = function(data) {
return console.log("default callback: " + data);
var content;
content = null;
if (data.content != null) {
content = data.content.data;
} else {
content = "no data";
}
return console.log("default callback: " + content);
};
}
params = {};
if (args.requestContentType) {
requestContentType = args.requestContentType;
}
if (args.responseContentType) {
responseContentType = args.responseContentType;
}
if (args.headers != null) {
params.headers = args.headers;
delete args.headers;
@@ -612,11 +625,12 @@
}
}
}
if (args.mock != null) {
params.mock = args["mock"];
req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this);
if (opts.mock != null) {
return req;
} else {
return true;
}
params["parent"] = args["parent"];
return new SwaggerRequest(this.method, this.urlify(args), params, requestContentType, responseContentType, callback, error, this);
};
SwaggerOperation.prototype.pathJson = function() {
@@ -716,14 +730,13 @@
SwaggerRequest = (function() {
function SwaggerRequest(type, url, params, requestContentType, responseContentType, successCallback, errorCallback, operation, execution) {
var body, e, fields, headers, key, myHeaders, obj, param, parent, possibleParams, urlEncoded, value, values,
function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) {
var body, e, fields, headers, key, myHeaders, obj, param, parent, possibleParams, requestContentType, responseContentType, urlEncoded, value, values,
_this = this;
this.type = type;
this.url = url;
this.params = params;
this.requestContentType = requestContentType;
this.responseContentType = responseContentType;
this.opts = opts;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.operation = operation;
@@ -750,8 +763,8 @@
parent = params["parent"];
requestContentType = "application/json";
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
if (this.requestContentType) {
requestContentType = this.requestContentType;
if (this.opts.requestContentType) {
requestContentType = this.opts.requestContentType;
}
} else {
if (((function() {
@@ -777,14 +790,12 @@
if (this.requestContentType === null) {
requestContentType = this.operation.consumes[0];
}
} else {
console.log("it's ok to send " + requestContentType);
}
}
responseContentType = null;
if (this.type === "POST" || this.type === "GET") {
if (this.responseContentType) {
responseContentType = this.responseContentType;
if (this.opts.responseContentType) {
responseContentType = this.opts.responseContentType;
} else {
responseContentType = "application/json";
}
@@ -794,12 +805,9 @@
if (responseContentType && this.operation.produces) {
if (this.operation.produces.indexOf(responseContentType) === -1) {
console.log("server can't produce " + responseContentType);
} else {
console.log("get ready for " + responseContentType);
}
}
if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
console.log("pulling fields");
fields = {};
possibleParams = (function() {
var _i, _len, _ref, _results;
@@ -813,7 +821,6 @@
}
return _results;
}).call(this);
console.log(possibleParams);
values = {};
for (key in possibleParams) {
value = possibleParams[key];
@@ -845,16 +852,16 @@
body: body,
on: {
error: function(response) {
return _this.errorCallback(response, _this.params["parent"]);
return _this.errorCallback(response, _this.opts.parent);
},
redirect: function(response) {
return _this.successCallback(response, _this.params["parent"]);
return _this.successCallback(response, _this.opts.parent);
},
307: function(response) {
return _this.successCallback(response, _this.params["parent"]);
return _this.successCallback(response, _this.opts.parent);
},
response: function(response) {
return _this.successCallback(response, _this.params["parent"]);
return _this.successCallback(response, _this.opts.parent);
}
}
};
@@ -865,9 +872,10 @@
e = exports;
}
e.authorizations.apply(obj);
if (params.mock == null) {
if (opts.mock == null) {
new SwaggerHttp().execute(obj);
} else {
console.log(obj);
return obj;
}
}
@@ -894,33 +902,42 @@
SwaggerHttp = (function() {
SwaggerHttp.prototype.Shred = null;
SwaggerHttp.prototype.shred = null;
function SwaggerHttp() {
var Shred;
Shred = null;
if (typeof window !== 'undefined') {
Shred = require("./shred");
} else {
Shred = require("shred");
}
this.shred = new Shred();
}
SwaggerHttp.prototype.content = null;
SwaggerHttp.prototype.execute = function(obj) {
var Content, identity, toString,
function SwaggerHttp() {
var identity, toString,
_this = this;
Content = require("./shred/content");
if (typeof window !== 'undefined') {
this.Shred = require("./shred");
} else {
this.Shred = require("shred");
}
this.shred = new this.Shred();
identity = function(x) {
return x;
};
toString = function(x) {
return x.toString;
};
Content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
parser: identity,
stringify: toString
});
if (typeof window !== 'undefined') {
this.content = require("./shred/content");
this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
parser: identity,
stringify: toString
});
} else {
this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
parser: identity,
stringify: toString
});
}
}
SwaggerHttp.prototype.execute = function(obj) {
return this.shred.request(obj);
};
@@ -972,6 +989,7 @@
ApiKeyAuthorization.prototype.apply = function(obj) {
if (this.type === "query") {
console.log(this.value);
if (obj.url.indexOf('?') > 0) {
obj.url = obj.url + "&" + this.name + "=" + this.value;
} else {
@@ -997,8 +1015,6 @@
this.SwaggerModelProperty = SwaggerModelProperty;
this.SwaggerAuthorizations = new SwaggerAuthorizations();
this.ApiKeyAuthorization = ApiKeyAuthorization;
this.authorizations = new SwaggerAuthorizations();

22
dist/swagger-ui.js vendored
View File

@@ -1183,7 +1183,7 @@ templates['resource'] = template(function (Handlebars,depth0,helpers,partials,da
stack1 = foundHelper || depth0.name;
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
else if(stack1=== undef) { stack1 = helperMissing.call(depth0, "name", { hash: {} }); }
buffer += escapeExpression(stack1) + "');\">/";
buffer += escapeExpression(stack1) + "');\">";
foundHelper = helpers.name;
stack1 = foundHelper || depth0.name;
if(typeof stack1 === functionType) { stack1 = stack1.call(depth0, { hash: {} }); }
@@ -1385,7 +1385,6 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
SwaggerUi.prototype.updateSwaggerUi = function(data) {
this.options.discoveryUrl = data.discoveryUrl;
this.options.apiKey = data.apiKey;
return this.load();
};
@@ -1394,7 +1393,7 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
if ((_ref = this.mainView) != null) {
_ref.clear();
}
this.headerView.update(this.options.discoveryUrl, this.options.apiKey);
this.headerView.update(this.options.discoveryUrl);
return this.api = new SwaggerApi(this.options);
};
@@ -1470,15 +1469,13 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
HeaderView.prototype.showPetStore = function(e) {
return this.trigger('update-swagger-ui', {
discoveryUrl: "http://petstore.swagger.wordnik.com/api/api-docs.json",
apiKey: "special-key"
discoveryUrl: "http://petstore.swagger.wordnik.com/api/api-docs.json"
});
};
HeaderView.prototype.showWordnikDev = function(e) {
return this.trigger('update-swagger-ui', {
discoveryUrl: "http://api.wordnik.com/v4/resources.json",
apiKey: ""
discoveryUrl: "http://api.wordnik.com/v4/resources.json"
});
};
@@ -1675,7 +1672,7 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
};
OperationView.prototype.submitOperation = function(e) {
var error_free, form, map, o, _i, _len, _ref;
var error_free, form, map, o, opts, _i, _len, _ref;
if (e != null) {
e.preventDefault();
}
@@ -1695,7 +1692,8 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
}
});
if (error_free) {
map = {
map = {};
opts = {
parent: this
};
_ref = form.serializeArray();
@@ -1706,9 +1704,9 @@ templates['status_code'] = template(function (Handlebars,depth0,helpers,partials
}
}
console.log(map);
map["responseContentType"] = $("div select[name=responseContentType]", $(this.el)).val();
map["requestContentType"] = $("div select[name=parameterContentType]", $(this.el)).val();
return this.model["do"](map, this.showCompleteStatus, this.showErrorStatus, this);
opts.responseContentType = $("div select[name=responseContentType]", $(this.el)).val();
opts.requestContentType = $("div select[name=parameterContentType]", $(this.el)).val();
return this.model["do"](map, opts, this.showCompleteStatus, this.showErrorStatus, this);
}
};

File diff suppressed because one or more lines are too long

View File

@@ -38,6 +38,7 @@
var obj,
_this = this;
this.progress('fetching resource list: ' + this.discoveryUrl);
console.log('getting ' + this.discoveryUrl);
obj = {
url: this.discoveryUrl,
method: "get",
@@ -79,7 +80,8 @@
}
}
};
return new SwaggerHttp().execute(obj);
new SwaggerHttp().execute(obj);
return this;
};
SwaggerApi.prototype.selfReflect = function() {
@@ -519,6 +521,9 @@
this.resource[this.nickname] = function(args, callback, error) {
return _this["do"](args, callback, error);
};
this.resource[this.nickname].help = function() {
return _this.help();
};
}
SwaggerOperation.prototype.isListType = function(dataType) {
@@ -555,18 +560,25 @@
}
};
SwaggerOperation.prototype["do"] = function(args, callback, error) {
var key, param, params, possibleParams, requestContentType, responseContentType, value;
SwaggerOperation.prototype["do"] = function(args, opts, callback, error) {
var key, param, params, possibleParams, req, requestContentType, responseContentType, value;
if (args == null) {
args = {};
}
if (opts == null) {
opts = {};
}
requestContentType = null;
responseContentType = null;
if ((typeof args) === "function") {
error = callback;
error = opts;
callback = args;
args = {};
}
if ((typeof opts) === "function") {
error = callback;
callback = opts;
}
if (error == null) {
error = function(xhr, textStatus, error) {
return console.log(xhr, textStatus, error);
@@ -574,16 +586,17 @@
}
if (callback == null) {
callback = function(data) {
return console.log("default callback: " + data);
var content;
content = null;
if (data.content != null) {
content = data.content.data;
} else {
content = "no data";
}
return console.log("default callback: " + content);
};
}
params = {};
if (args.requestContentType) {
requestContentType = args.requestContentType;
}
if (args.responseContentType) {
responseContentType = args.responseContentType;
}
if (args.headers != null) {
params.headers = args.headers;
delete args.headers;
@@ -612,11 +625,12 @@
}
}
}
if (args.mock != null) {
params.mock = args["mock"];
req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this);
if (opts.mock != null) {
return req;
} else {
return true;
}
params["parent"] = args["parent"];
return new SwaggerRequest(this.method, this.urlify(args), params, requestContentType, responseContentType, callback, error, this);
};
SwaggerOperation.prototype.pathJson = function() {
@@ -716,14 +730,13 @@
SwaggerRequest = (function() {
function SwaggerRequest(type, url, params, requestContentType, responseContentType, successCallback, errorCallback, operation, execution) {
var body, e, fields, headers, key, myHeaders, obj, param, parent, possibleParams, urlEncoded, value, values,
function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) {
var body, e, fields, headers, key, myHeaders, obj, param, parent, possibleParams, requestContentType, responseContentType, urlEncoded, value, values,
_this = this;
this.type = type;
this.url = url;
this.params = params;
this.requestContentType = requestContentType;
this.responseContentType = responseContentType;
this.opts = opts;
this.successCallback = successCallback;
this.errorCallback = errorCallback;
this.operation = operation;
@@ -750,8 +763,8 @@
parent = params["parent"];
requestContentType = "application/json";
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
if (this.requestContentType) {
requestContentType = this.requestContentType;
if (this.opts.requestContentType) {
requestContentType = this.opts.requestContentType;
}
} else {
if (((function() {
@@ -777,14 +790,12 @@
if (this.requestContentType === null) {
requestContentType = this.operation.consumes[0];
}
} else {
console.log("it's ok to send " + requestContentType);
}
}
responseContentType = null;
if (this.type === "POST" || this.type === "GET") {
if (this.responseContentType) {
responseContentType = this.responseContentType;
if (this.opts.responseContentType) {
responseContentType = this.opts.responseContentType;
} else {
responseContentType = "application/json";
}
@@ -794,12 +805,9 @@
if (responseContentType && this.operation.produces) {
if (this.operation.produces.indexOf(responseContentType) === -1) {
console.log("server can't produce " + responseContentType);
} else {
console.log("get ready for " + responseContentType);
}
}
if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
console.log("pulling fields");
fields = {};
possibleParams = (function() {
var _i, _len, _ref, _results;
@@ -813,7 +821,6 @@
}
return _results;
}).call(this);
console.log(possibleParams);
values = {};
for (key in possibleParams) {
value = possibleParams[key];
@@ -845,16 +852,16 @@
body: body,
on: {
error: function(response) {
return _this.errorCallback(response, _this.params["parent"]);
return _this.errorCallback(response, _this.opts.parent);
},
redirect: function(response) {
return _this.successCallback(response, _this.params["parent"]);
return _this.successCallback(response, _this.opts.parent);
},
307: function(response) {
return _this.successCallback(response, _this.params["parent"]);
return _this.successCallback(response, _this.opts.parent);
},
response: function(response) {
return _this.successCallback(response, _this.params["parent"]);
return _this.successCallback(response, _this.opts.parent);
}
}
};
@@ -865,9 +872,10 @@
e = exports;
}
e.authorizations.apply(obj);
if (params.mock == null) {
if (opts.mock == null) {
new SwaggerHttp().execute(obj);
} else {
console.log(obj);
return obj;
}
}
@@ -894,33 +902,42 @@
SwaggerHttp = (function() {
SwaggerHttp.prototype.Shred = null;
SwaggerHttp.prototype.shred = null;
function SwaggerHttp() {
var Shred;
Shred = null;
if (typeof window !== 'undefined') {
Shred = require("./shred");
} else {
Shred = require("shred");
}
this.shred = new Shred();
}
SwaggerHttp.prototype.content = null;
SwaggerHttp.prototype.execute = function(obj) {
var Content, identity, toString,
function SwaggerHttp() {
var identity, toString,
_this = this;
Content = require("./shred/content");
if (typeof window !== 'undefined') {
this.Shred = require("./shred");
} else {
this.Shred = require("shred");
}
this.shred = new this.Shred();
identity = function(x) {
return x;
};
toString = function(x) {
return x.toString;
};
Content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
parser: identity,
stringify: toString
});
if (typeof window !== 'undefined') {
this.content = require("./shred/content");
this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
parser: identity,
stringify: toString
});
} else {
this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
parser: identity,
stringify: toString
});
}
}
SwaggerHttp.prototype.execute = function(obj) {
return this.shred.request(obj);
};
@@ -997,8 +1014,6 @@
this.SwaggerModelProperty = SwaggerModelProperty;
this.SwaggerAuthorizations = new SwaggerAuthorizations();
this.ApiKeyAuthorization = ApiKeyAuthorization;
this.authorizations = new SwaggerAuthorizations();

View File

@@ -35,14 +35,13 @@ class SwaggerUi extends Backbone.Router
# Event handler for when url/key is received from user
updateSwaggerUi: (data) ->
@options.discoveryUrl = data.discoveryUrl
@options.apiKey = data.apiKey
@load()
# Create an api and render
load: ->
# Initialize the API object
@mainView?.clear()
@headerView.update(@options.discoveryUrl, @options.apiKey)
@headerView.update(@options.discoveryUrl)
@api = new SwaggerApi(@options)
# This is bound to success handler for SwaggerApi

View File

@@ -9,17 +9,16 @@ class HeaderView extends Backbone.View
initialize: ->
showPetStore: (e) ->
@trigger(
'update-swagger-ui'
{discoveryUrl:"http://petstore.swagger.wordnik.com/api/api-docs.json", apiKey:"special-key"}
{discoveryUrl:"http://petstore.swagger.wordnik.com/api/api-docs.json"}
)
showWordnikDev: (e) ->
@trigger(
'update-swagger-ui'
{discoveryUrl:"http://api.wordnik.com/v4/resources.json", apiKey:""}
{discoveryUrl:"http://api.wordnik.com/v4/resources.json"}
)
showCustomOnKeyup: (e) ->

View File

@@ -68,17 +68,18 @@ class OperationView extends Backbone.View
# if error free submit it
if error_free
map = {parent: @}
map = {}
opts = {parent: @}
for o in form.serializeArray()
if(o.value? && jQuery.trim(o.value).length > 0)
map[o.name] = o.value
console.log map
map["responseContentType"] = $("div select[name=responseContentType]", $(@el)).val()
map["requestContentType"] = $("div select[name=parameterContentType]", $(@el)).val()
opts.responseContentType = $("div select[name=responseContentType]", $(@el)).val()
opts.requestContentType = $("div select[name=parameterContentType]", $(@el)).val()
@model.do(map, @showCompleteStatus, @showErrorStatus, @)
@model.do(map, opts, @showCompleteStatus, @showErrorStatus, @)
success: (response, parent) ->
parent.showCompleteStatus response

View File

@@ -45,6 +45,7 @@
var key = $('#input_apiKey')[0].value;
console.log("key: " + key);
if(key && key.trim() != "") {
console.log("added key " + key);
window.authorizations.add("key", new ApiKeyAuthorization("api_key", key, "query"));
}
})

View File

@@ -1,6 +1,6 @@
<div class='heading'>
<h2>
<a href='#!/{{name}}' onclick="Docs.toggleEndpointListForResource('{{name}}');">/{{name}}</a>
<a href='#!/{{name}}' onclick="Docs.toggleEndpointListForResource('{{name}}');">{{name}}</a>
</h2>
<ul class='options'>
<li>