From 264ba2910d6ef2561ffde3cf56523bcae4f086b1 Mon Sep 17 00:00:00 2001 From: Tony Tam Date: Fri, 12 Sep 2014 16:42:49 -0700 Subject: [PATCH] updated client, version --- dist/lib/swagger.js | 70 ++++++++++++++++++++++++++++++++++++++------- dist/swagger-ui.js | 2 +- lib/swagger.js | 70 ++++++++++++++++++++++++++++++++++++++------- package.json | 2 +- 4 files changed, 122 insertions(+), 22 deletions(-) diff --git a/dist/lib/swagger.js b/dist/lib/swagger.js index 8ea25d7b..9e73a9b3 100644 --- a/dist/lib/swagger.js +++ b/dist/lib/swagger.js @@ -1,5 +1,5 @@ // swagger.js -// version 2.0.37 +// version 2.0.39 var __bind = function(fn, me){ return function(){ @@ -15,6 +15,16 @@ log = function(){ } }; +// if you want to apply conditional formatting of parameter values +parameterMacro = function(value) { + return value; +} + +// if you want to apply conditional formatting of model property values +modelPropertyMacro = function(value) { + return value; +} + if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { @@ -80,6 +90,7 @@ Object.keys = Object.keys || (function () { })(); var SwaggerApi = function(url, options) { + this.isBuilt = false; this.url = null; this.debug = false; this.basePath = null; @@ -87,6 +98,7 @@ var SwaggerApi = function(url, options) { this.authorizationScheme = null; this.info = null; this.useJQuery = false; + this.modelsArray = []; options = (options||{}); if (url) @@ -108,11 +120,15 @@ var SwaggerApi = function(url, options) { this.failure = options.failure != null ? options.failure : function() {}; this.progress = options.progress != null ? options.progress : function() {}; - if (options.success != null) + if (options.success != null) { this.build(); + this.isBuilt = true; + } } SwaggerApi.prototype.build = function() { + if(this.isBuilt) + return this; var _this = this; this.progress('fetching resource list: ' + this.url); var obj = { @@ -120,7 +136,7 @@ SwaggerApi.prototype.build = function() { url: this.url, method: "get", headers: { - accept: "application/json" + accept: "application/json,application/json;charset=\"utf-8\",*/*" }, on: { error: function(response) { @@ -276,7 +292,6 @@ SwaggerApi.prototype.fail = function(message) { SwaggerApi.prototype.setConsolidatedModels = function() { var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results; - this.modelsArray = []; this.models = {}; _ref = this.apis; for (resource_name in _ref) { @@ -353,7 +368,7 @@ var SwaggerResource = function(resourceObj, api) { method: "get", useJQuery: this.useJQuery, headers: { - accept: "application/json" + accept: "application/json,application/json;charset=\"utf-8\",*/*" }, on: { response: function(resp) { @@ -593,6 +608,7 @@ var SwaggerModelProperty = function(name, obj) { this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set'); this.descr = obj.description; this.required = obj.required; + this.defaultValue = modelPropertyMacro(obj.defaultValue); if (obj.items != null) { if (obj.items.type != null) { this.refDataType = obj.items.type; @@ -638,7 +654,9 @@ SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) { SwaggerModelProperty.prototype.toSampleValue = function(value) { var result; - if (value === "integer") { + if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) { + result = this.defaultValue; + } else if (value === "integer") { result = 0; } else if (value === "boolean") { result = false; @@ -768,6 +786,7 @@ var SwaggerOperation = function(nickname, path, method, parameters, summary, not } } } + param.defaultValue = parameterMacro(param.defaultValue); } this.resource[this.nickname] = function(args, callback, error) { return _this["do"](args, callback, error); @@ -1218,7 +1237,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) { // 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; + consumes = this.opts.requestContentType; } else { // if any form params, content type must be set if(definedFormParams.length > 0) { @@ -1227,7 +1246,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) { else consumes = "application/x-www-form-urlencoded"; } - else if (this.type == "DELETE") + else if (this.type === "DELETE") body = "{}"; else if (this.type != "DELETE") accepts = null; @@ -1461,11 +1480,36 @@ ShredHttpClient.prototype.execute = function(obj) { return out; }; + // Transform an error into a usable response-like object + var transformError = function(error) { + var out = { + // Default to a status of 0 - The client will treat this as a generic permissions sort of error + status: 0, + data: error.message || error + }; + + if(error.code) { + out.obj = error; + + if(error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED' ) { + // We can tell the client that this should be treated as a missing resource and not as a permissions thing + out.status = 404; + } + } + + return out; + }; + res = { error: function(response) { if (obj) return cb.error(transform(response)); }, + // Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming) + request_error: function(err) { + if(obj) + return cb.error(transformError(err)); + }, redirect: function(response) { if (obj) return cb.redirect(transform(response)); @@ -1533,10 +1577,11 @@ SwaggerAuthorizations.prototype.apply = function(obj, authorizations) { /** * ApiKeyAuthorization allows a query param or header to be injected */ -var ApiKeyAuthorization = function(name, value, type) { +var ApiKeyAuthorization = function(name, value, type, delimiter) { this.name = name; this.value = value; this.type = type; + this.delimiter = delimiter; }; ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { @@ -1547,7 +1592,12 @@ ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { obj.url = obj.url + "?" + this.name + "=" + this.value; return true; } else if (this.type === "header") { - obj.headers[this.name] = this.value; + if(typeof obj.headers[this.name] !== 'undefined') { + if(typeof this.delimiter !== 'undefined') + obj.headers[this.name] = obj.headers[this.name] + this.delimiter + this.value; + } + else + obj.headers[this.name] = this.value; return true; } }; diff --git a/dist/swagger-ui.js b/dist/swagger-ui.js index c2ffe2fc..f2409ef7 100644 --- a/dist/swagger-ui.js +++ b/dist/swagger-ui.js @@ -1,5 +1,5 @@ // swagger-ui.js -// version 2.0.22 +// version 2.0.23 $(function() { // Helper function for vertically aligning DOM elements diff --git a/lib/swagger.js b/lib/swagger.js index 8ea25d7b..9e73a9b3 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -1,5 +1,5 @@ // swagger.js -// version 2.0.37 +// version 2.0.39 var __bind = function(fn, me){ return function(){ @@ -15,6 +15,16 @@ log = function(){ } }; +// if you want to apply conditional formatting of parameter values +parameterMacro = function(value) { + return value; +} + +// if you want to apply conditional formatting of model property values +modelPropertyMacro = function(value) { + return value; +} + if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(obj, start) { for (var i = (start || 0), j = this.length; i < j; i++) { @@ -80,6 +90,7 @@ Object.keys = Object.keys || (function () { })(); var SwaggerApi = function(url, options) { + this.isBuilt = false; this.url = null; this.debug = false; this.basePath = null; @@ -87,6 +98,7 @@ var SwaggerApi = function(url, options) { this.authorizationScheme = null; this.info = null; this.useJQuery = false; + this.modelsArray = []; options = (options||{}); if (url) @@ -108,11 +120,15 @@ var SwaggerApi = function(url, options) { this.failure = options.failure != null ? options.failure : function() {}; this.progress = options.progress != null ? options.progress : function() {}; - if (options.success != null) + if (options.success != null) { this.build(); + this.isBuilt = true; + } } SwaggerApi.prototype.build = function() { + if(this.isBuilt) + return this; var _this = this; this.progress('fetching resource list: ' + this.url); var obj = { @@ -120,7 +136,7 @@ SwaggerApi.prototype.build = function() { url: this.url, method: "get", headers: { - accept: "application/json" + accept: "application/json,application/json;charset=\"utf-8\",*/*" }, on: { error: function(response) { @@ -276,7 +292,6 @@ SwaggerApi.prototype.fail = function(message) { SwaggerApi.prototype.setConsolidatedModels = function() { var model, modelName, resource, resource_name, _i, _len, _ref, _ref1, _results; - this.modelsArray = []; this.models = {}; _ref = this.apis; for (resource_name in _ref) { @@ -353,7 +368,7 @@ var SwaggerResource = function(resourceObj, api) { method: "get", useJQuery: this.useJQuery, headers: { - accept: "application/json" + accept: "application/json,application/json;charset=\"utf-8\",*/*" }, on: { response: function(resp) { @@ -593,6 +608,7 @@ var SwaggerModelProperty = function(name, obj) { this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set'); this.descr = obj.description; this.required = obj.required; + this.defaultValue = modelPropertyMacro(obj.defaultValue); if (obj.items != null) { if (obj.items.type != null) { this.refDataType = obj.items.type; @@ -638,7 +654,9 @@ SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) { SwaggerModelProperty.prototype.toSampleValue = function(value) { var result; - if (value === "integer") { + if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) { + result = this.defaultValue; + } else if (value === "integer") { result = 0; } else if (value === "boolean") { result = false; @@ -768,6 +786,7 @@ var SwaggerOperation = function(nickname, path, method, parameters, summary, not } } } + param.defaultValue = parameterMacro(param.defaultValue); } this.resource[this.nickname] = function(args, callback, error) { return _this["do"](args, callback, error); @@ -1218,7 +1237,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) { // 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; + consumes = this.opts.requestContentType; } else { // if any form params, content type must be set if(definedFormParams.length > 0) { @@ -1227,7 +1246,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) { else consumes = "application/x-www-form-urlencoded"; } - else if (this.type == "DELETE") + else if (this.type === "DELETE") body = "{}"; else if (this.type != "DELETE") accepts = null; @@ -1461,11 +1480,36 @@ ShredHttpClient.prototype.execute = function(obj) { return out; }; + // Transform an error into a usable response-like object + var transformError = function(error) { + var out = { + // Default to a status of 0 - The client will treat this as a generic permissions sort of error + status: 0, + data: error.message || error + }; + + if(error.code) { + out.obj = error; + + if(error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED' ) { + // We can tell the client that this should be treated as a missing resource and not as a permissions thing + out.status = 404; + } + } + + return out; + }; + res = { error: function(response) { if (obj) return cb.error(transform(response)); }, + // Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming) + request_error: function(err) { + if(obj) + return cb.error(transformError(err)); + }, redirect: function(response) { if (obj) return cb.redirect(transform(response)); @@ -1533,10 +1577,11 @@ SwaggerAuthorizations.prototype.apply = function(obj, authorizations) { /** * ApiKeyAuthorization allows a query param or header to be injected */ -var ApiKeyAuthorization = function(name, value, type) { +var ApiKeyAuthorization = function(name, value, type, delimiter) { this.name = name; this.value = value; this.type = type; + this.delimiter = delimiter; }; ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { @@ -1547,7 +1592,12 @@ ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { obj.url = obj.url + "?" + this.name + "=" + this.value; return true; } else if (this.type === "header") { - obj.headers[this.name] = this.value; + if(typeof obj.headers[this.name] !== 'undefined') { + if(typeof this.delimiter !== 'undefined') + obj.headers[this.name] = obj.headers[this.name] + this.delimiter + this.value; + } + else + obj.headers[this.name] = this.value; return true; } }; diff --git a/package.json b/package.json index 19e25b56..20584431 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-ui", - "version": "2.0.22", + "version": "2.0.24", "description": "Swagger UI is a dependency-free collection of HTML, Javascript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API", "scripts": { "build": "PATH=$PATH:./node_modules/.bin cake dist",