updated swagger-js to 2.0.34

This commit is contained in:
Tony Tam
2014-08-01 17:11:36 -07:00
parent c948b40d34
commit 26db2ce375
2 changed files with 230 additions and 158 deletions

194
dist/lib/swagger.js vendored
View File

@@ -1,5 +1,5 @@
// swagger.js // swagger.js
// version 2.0.31 // version 2.0.34
var __bind = function(fn, me){ var __bind = function(fn, me){
return function(){ return function(){
@@ -11,7 +11,7 @@ log = function(){
log.history = log.history || []; log.history = log.history || [];
log.history.push(arguments); log.history.push(arguments);
if(this.console){ if(this.console){
console.log( Array.prototype.slice.call(arguments) ); console.log( Array.prototype.slice.call(arguments)[0] );
} }
}; };
@@ -378,20 +378,22 @@ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
pos = url.lastIndexOf(relativeBasePath); pos = url.lastIndexOf(relativeBasePath);
var parts = url.split("/"); var parts = url.split("/");
var rootUrl = parts[0] + "//" + parts[2]; var rootUrl = parts[0] + "//" + parts[2];
//if the relative path is '/' return the root url
if (relativeBasePath === '/'){ if(relativeBasePath.indexOf("http") === 0)
return rootUrl return relativeBasePath;
if(relativeBasePath === "/")
return rootUrl;
if(relativeBasePath.substring(0, 1) == "/") {
// use root + relative
return rootUrl + relativeBasePath;
} }
//if the relative path is not in the base path else {
else if (pos === -1 ) { var pos = this.basePath.lastIndexOf("/");
if (relativeBasePath.indexOf("/") === 0) { var base = this.basePath.substring(0, pos);
return url + relativeBasePath; if(base.substring(base.length - 1) == "/")
} else { return base + relativeBasePath;
return url + "/" + relativeBasePath; else
} return base + "/" + relativeBasePath;
//If the relative path is in the base path
} else {
return url.substring(0, pos) + relativeBasePath;
} }
}; };
@@ -1097,64 +1099,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal
this.type = this.type.toUpperCase(); this.type = this.type.toUpperCase();
var myHeaders = {}; // set request, response content type headers
var headers = this.setHeaders(params, this.operation);
var body = params.body; var body = params.body;
var parent = params["parent"];
var requestContentType = "application/json";
var formParams = []; // encode the body for form submits
var fileParams = []; if (headers["Accept"] && headers["Accept"].indexOf("application/x-www-form-urlencoded") === 0) {
var params = this.operation.parameters;
for(var i = 0; i < params.length; i++) {
var param = params[i];
if(param.paramType === "form")
formParams.push(param);
else if(param.paramType === "file")
fileParams.push(param);
}
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
if (this.opts.requestContentType) {
requestContentType = this.opts.requestContentType;
}
} else {
// if any form params, content type must be set
if(formParams.length > 0) {
if(fileParams.length > 0)
requestContentType = "multipart/form-data";
else
requestContentType = "application/x-www-form-urlencoded";
}
else if (this.type == "DELETE")
body = "{}";
else if (this.type != "DELETE")
requestContentType = null;
}
if (requestContentType && this.operation.consumes) {
if (this.operation.consumes[requestContentType] === 'undefined') {
log("server doesn't consume " + requestContentType + ", try " + JSON.stringify(this.operation.consumes));
if (this.requestContentType === null) {
requestContentType = this.operation.consumes[0];
}
}
}
var responseContentType = null;
if (this.opts.responseContentType) {
responseContentType = this.opts.responseContentType;
} else {
responseContentType = "application/json";
}
if (responseContentType && this.operation.produces) {
if (this.operation.produces[responseContentType] === 'undefined') {
log("server can't produce " + responseContentType);
}
}
if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
var fields = {}; var fields = {};
var possibleParams = {}; var possibleParams = {};
var values = {}; var values = {};
@@ -1176,19 +1126,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal
} }
body = encoded; body = encoded;
} }
var name;
for (name in this.headers)
myHeaders[name] = this.headers[name];
if ((requestContentType && body !== "") || (requestContentType === "application/x-www-form-urlencoded"))
myHeaders["Content-Type"] = requestContentType;
if (responseContentType)
myHeaders["Accept"] = responseContentType;
if (!((this.headers != null) && (this.headers.mock != null))) { if (!((this.headers != null) && (this.headers.mock != null))) {
obj = { obj = {
url: this.url, url: this.url,
method: this.type, method: this.type,
headers: myHeaders, headers: headers,
body: body, body: body,
useJQuery: this.useJQuery, useJQuery: this.useJQuery,
on: { on: {
@@ -1225,6 +1168,99 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal
} }
}; };
SwaggerRequest.prototype.setHeaders = function(params, operation) {
// default type
var accepts = "application/json";
var contentType = null;
var allDefinedParams = this.operation.parameters;
var definedFormParams = [];
var definedFileParams = [];
var body = params.body;
var headers = {};
// get params from the operation and set them in definedFileParams, definedFormParams, headers
var i;
for(i = 0; i < allDefinedParams.length; i++) {
var param = allDefinedParams[i];
if(param.paramType === "form")
definedFormParams.push(param);
else if(param.paramType === "file")
definedFileParams.push(param);
else if(param.paramType === "header" && this.params.headers) {
var key = param.name;
var headerValue = this.params.headers[param.name];
if(typeof this.params.headers[param.name] !== 'undefined')
headers[key] = headerValue;
}
}
// if there's a body, need to set the accepts header via requestContentType
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH" || this.type === "DELETE")) {
if (this.opts.requestContentType)
accepts = this.opts.requestContentType;
} else {
// if any form params, content type must be set
if(definedFormParams.length > 0) {
if(definedFileParams.length > 0)
accepts = "multipart/form-data";
else
accepts = "application/x-www-form-urlencoded";
}
else if (this.type == "DELETE")
body = "{}";
else if (this.type != "DELETE")
accepts = null;
}
if (contentType && this.operation.consumes) {
if (this.operation.consumes.indexOf(contentType) === -1) {
log("server doesn't consume " + contentType + ", try " + JSON.stringify(this.operation.consumes));
contentType = this.operation.consumes[0];
}
}
if (this.opts.responseContentType) {
accepts = this.opts.responseContentType;
} else {
accepts = "application/json";
}
if (accepts && this.operation.produces) {
if (this.operation.produces.indexOf(accepts) === -1) {
log("server can't produce " + accepts);
accepts = this.operation.produces[0];
}
}
if (contentType && contentType.indexOf("application/x-www-form-urlencoded") === 0) {
var fields = {};
var possibleParams = {};
var values = {};
var key;
for(key in formParams){
var param = formParams[key];
values[param.name] = param;
}
var encoded = "";
var key;
for(key in values) {
value = this.params[key];
if(typeof value !== 'undefined'){
if(encoded !== "")
encoded += "&";
encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
}
body = encoded;
}
if ((contentType && body !== "") || (contentType === "application/x-www-form-urlencoded"))
headers["Content-Type"] = contentType;
if (accepts)
headers["Accept"] = accepts;
return headers;
}
SwaggerRequest.prototype.asCurl = function() { SwaggerRequest.prototype.asCurl = function() {
var results = []; var results = [];
if(this.headers) { if(this.headers) {

View File

@@ -1,5 +1,5 @@
// swagger.js // swagger.js
// version 2.0.31 // version 2.0.34
var __bind = function(fn, me){ var __bind = function(fn, me){
return function(){ return function(){
@@ -11,7 +11,7 @@ log = function(){
log.history = log.history || []; log.history = log.history || [];
log.history.push(arguments); log.history.push(arguments);
if(this.console){ if(this.console){
console.log( Array.prototype.slice.call(arguments) ); console.log( Array.prototype.slice.call(arguments)[0] );
} }
}; };
@@ -378,20 +378,22 @@ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) {
pos = url.lastIndexOf(relativeBasePath); pos = url.lastIndexOf(relativeBasePath);
var parts = url.split("/"); var parts = url.split("/");
var rootUrl = parts[0] + "//" + parts[2]; var rootUrl = parts[0] + "//" + parts[2];
//if the relative path is '/' return the root url
if (relativeBasePath === '/'){ if(relativeBasePath.indexOf("http") === 0)
return rootUrl return relativeBasePath;
if(relativeBasePath === "/")
return rootUrl;
if(relativeBasePath.substring(0, 1) == "/") {
// use root + relative
return rootUrl + relativeBasePath;
} }
//if the relative path is not in the base path else {
else if (pos === -1 ) { var pos = this.basePath.lastIndexOf("/");
if (relativeBasePath.indexOf("/") === 0) { var base = this.basePath.substring(0, pos);
return url + relativeBasePath; if(base.substring(base.length - 1) == "/")
} else { return base + relativeBasePath;
return url + "/" + relativeBasePath; else
} return base + "/" + relativeBasePath;
//If the relative path is in the base path
} else {
return url.substring(0, pos) + relativeBasePath;
} }
}; };
@@ -1097,64 +1099,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal
this.type = this.type.toUpperCase(); this.type = this.type.toUpperCase();
var myHeaders = {}; // set request, response content type headers
var headers = this.setHeaders(params, this.operation);
var body = params.body; var body = params.body;
var parent = params["parent"];
var requestContentType = "application/json";
var formParams = []; // encode the body for form submits
var fileParams = []; if (headers["Accept"] && headers["Accept"].indexOf("application/x-www-form-urlencoded") === 0) {
var params = this.operation.parameters;
for(var i = 0; i < params.length; i++) {
var param = params[i];
if(param.paramType === "form")
formParams.push(param);
else if(param.paramType === "file")
fileParams.push(param);
}
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
if (this.opts.requestContentType) {
requestContentType = this.opts.requestContentType;
}
} else {
// if any form params, content type must be set
if(formParams.length > 0) {
if(fileParams.length > 0)
requestContentType = "multipart/form-data";
else
requestContentType = "application/x-www-form-urlencoded";
}
else if (this.type == "DELETE")
body = "{}";
else if (this.type != "DELETE")
requestContentType = null;
}
if (requestContentType && this.operation.consumes) {
if (this.operation.consumes[requestContentType] === 'undefined') {
log("server doesn't consume " + requestContentType + ", try " + JSON.stringify(this.operation.consumes));
if (this.requestContentType === null) {
requestContentType = this.operation.consumes[0];
}
}
}
var responseContentType = null;
if (this.opts.responseContentType) {
responseContentType = this.opts.responseContentType;
} else {
responseContentType = "application/json";
}
if (responseContentType && this.operation.produces) {
if (this.operation.produces[responseContentType] === 'undefined') {
log("server can't produce " + responseContentType);
}
}
if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
var fields = {}; var fields = {};
var possibleParams = {}; var possibleParams = {};
var values = {}; var values = {};
@@ -1176,19 +1126,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal
} }
body = encoded; body = encoded;
} }
var name;
for (name in this.headers)
myHeaders[name] = this.headers[name];
if ((requestContentType && body !== "") || (requestContentType === "application/x-www-form-urlencoded"))
myHeaders["Content-Type"] = requestContentType;
if (responseContentType)
myHeaders["Accept"] = responseContentType;
if (!((this.headers != null) && (this.headers.mock != null))) { if (!((this.headers != null) && (this.headers.mock != null))) {
obj = { obj = {
url: this.url, url: this.url,
method: this.type, method: this.type,
headers: myHeaders, headers: headers,
body: body, body: body,
useJQuery: this.useJQuery, useJQuery: this.useJQuery,
on: { on: {
@@ -1225,6 +1168,99 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal
} }
}; };
SwaggerRequest.prototype.setHeaders = function(params, operation) {
// default type
var accepts = "application/json";
var contentType = null;
var allDefinedParams = this.operation.parameters;
var definedFormParams = [];
var definedFileParams = [];
var body = params.body;
var headers = {};
// get params from the operation and set them in definedFileParams, definedFormParams, headers
var i;
for(i = 0; i < allDefinedParams.length; i++) {
var param = allDefinedParams[i];
if(param.paramType === "form")
definedFormParams.push(param);
else if(param.paramType === "file")
definedFileParams.push(param);
else if(param.paramType === "header" && this.params.headers) {
var key = param.name;
var headerValue = this.params.headers[param.name];
if(typeof this.params.headers[param.name] !== 'undefined')
headers[key] = headerValue;
}
}
// if there's a body, need to set the accepts header via requestContentType
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH" || this.type === "DELETE")) {
if (this.opts.requestContentType)
accepts = this.opts.requestContentType;
} else {
// if any form params, content type must be set
if(definedFormParams.length > 0) {
if(definedFileParams.length > 0)
accepts = "multipart/form-data";
else
accepts = "application/x-www-form-urlencoded";
}
else if (this.type == "DELETE")
body = "{}";
else if (this.type != "DELETE")
accepts = null;
}
if (contentType && this.operation.consumes) {
if (this.operation.consumes.indexOf(contentType) === -1) {
log("server doesn't consume " + contentType + ", try " + JSON.stringify(this.operation.consumes));
contentType = this.operation.consumes[0];
}
}
if (this.opts.responseContentType) {
accepts = this.opts.responseContentType;
} else {
accepts = "application/json";
}
if (accepts && this.operation.produces) {
if (this.operation.produces.indexOf(accepts) === -1) {
log("server can't produce " + accepts);
accepts = this.operation.produces[0];
}
}
if (contentType && contentType.indexOf("application/x-www-form-urlencoded") === 0) {
var fields = {};
var possibleParams = {};
var values = {};
var key;
for(key in formParams){
var param = formParams[key];
values[param.name] = param;
}
var encoded = "";
var key;
for(key in values) {
value = this.params[key];
if(typeof value !== 'undefined'){
if(encoded !== "")
encoded += "&";
encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
}
}
body = encoded;
}
if ((contentType && body !== "") || (contentType === "application/x-www-form-urlencoded"))
headers["Content-Type"] = contentType;
if (accepts)
headers["Accept"] = accepts;
return headers;
}
SwaggerRequest.prototype.asCurl = function() { SwaggerRequest.prototype.asCurl = function() {
var results = []; var results = [];
if(this.headers) { if(this.headers) {