From 26db2ce375422167396c3336e1680e441928e9da Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 1 Aug 2014 17:11:36 -0700 Subject: [PATCH] updated swagger-js to 2.0.34 --- dist/lib/swagger.js | 194 ++++++++++++++++++++++++++------------------ lib/swagger.js | 194 ++++++++++++++++++++++++++------------------ 2 files changed, 230 insertions(+), 158 deletions(-) diff --git a/dist/lib/swagger.js b/dist/lib/swagger.js index df78db98..dcda201e 100644 --- a/dist/lib/swagger.js +++ b/dist/lib/swagger.js @@ -1,5 +1,5 @@ // swagger.js -// version 2.0.31 +// version 2.0.34 var __bind = function(fn, me){ return function(){ @@ -11,7 +11,7 @@ log = function(){ log.history = log.history || []; log.history.push(arguments); 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); var parts = url.split("/"); var rootUrl = parts[0] + "//" + parts[2]; - //if the relative path is '/' return the root url - if (relativeBasePath === '/'){ - return rootUrl + + if(relativeBasePath.indexOf("http") === 0) + 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 if (pos === -1 ) { - if (relativeBasePath.indexOf("/") === 0) { - return url + relativeBasePath; - } else { - return url + "/" + relativeBasePath; - } - //If the relative path is in the base path - } else { - return url.substring(0, pos) + relativeBasePath; + else { + var pos = this.basePath.lastIndexOf("/"); + var base = this.basePath.substring(0, pos); + if(base.substring(base.length - 1) == "/") + return base + relativeBasePath; + else + return base + "/" + relativeBasePath; } }; @@ -1097,64 +1099,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal 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 parent = params["parent"]; - var requestContentType = "application/json"; - var formParams = []; - var fileParams = []; - 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) { + // encode the body for form submits + if (headers["Accept"] && headers["Accept"].indexOf("application/x-www-form-urlencoded") === 0) { var fields = {}; var possibleParams = {}; var values = {}; @@ -1176,19 +1126,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal } 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))) { obj = { url: this.url, method: this.type, - headers: myHeaders, + headers: headers, body: body, useJQuery: this.useJQuery, 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() { var results = []; if(this.headers) { diff --git a/lib/swagger.js b/lib/swagger.js index df78db98..dcda201e 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -1,5 +1,5 @@ // swagger.js -// version 2.0.31 +// version 2.0.34 var __bind = function(fn, me){ return function(){ @@ -11,7 +11,7 @@ log = function(){ log.history = log.history || []; log.history.push(arguments); 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); var parts = url.split("/"); var rootUrl = parts[0] + "//" + parts[2]; - //if the relative path is '/' return the root url - if (relativeBasePath === '/'){ - return rootUrl + + if(relativeBasePath.indexOf("http") === 0) + 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 if (pos === -1 ) { - if (relativeBasePath.indexOf("/") === 0) { - return url + relativeBasePath; - } else { - return url + "/" + relativeBasePath; - } - //If the relative path is in the base path - } else { - return url.substring(0, pos) + relativeBasePath; + else { + var pos = this.basePath.lastIndexOf("/"); + var base = this.basePath.substring(0, pos); + if(base.substring(base.length - 1) == "/") + return base + relativeBasePath; + else + return base + "/" + relativeBasePath; } }; @@ -1097,64 +1099,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal 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 parent = params["parent"]; - var requestContentType = "application/json"; - var formParams = []; - var fileParams = []; - 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) { + // encode the body for form submits + if (headers["Accept"] && headers["Accept"].indexOf("application/x-www-form-urlencoded") === 0) { var fields = {}; var possibleParams = {}; var values = {}; @@ -1176,19 +1126,12 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal } 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))) { obj = { url: this.url, method: this.type, - headers: myHeaders, + headers: headers, body: body, useJQuery: this.useJQuery, 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() { var results = []; if(this.headers) {