app/assets/javascripts/lib/swagger.js in swagger-ui_rails-0.0.3 vs app/assets/javascripts/lib/swagger.js in swagger-ui_rails-0.1.0

- old
+ new

@@ -1,118 +1,198 @@ -// Generated by CoffeeScript 1.4.0 +// Generated by CoffeeScript 1.6.3 (function() { - var SwaggerApi, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource, - __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + var ApiKeyAuthorization, PasswordAuthorization, SwaggerApi, SwaggerAuthorizations, SwaggerHttp, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; SwaggerApi = (function() { + SwaggerApi.prototype.url = "http://api.wordnik.com/v4/resources.json"; - SwaggerApi.prototype.discoveryUrl = "http://api.wordnik.com/v4/resources.json"; - SwaggerApi.prototype.debug = false; - SwaggerApi.prototype.api_key = null; - SwaggerApi.prototype.basePath = null; - function SwaggerApi(options) { + SwaggerApi.prototype.authorizations = null; + + SwaggerApi.prototype.authorizationScheme = null; + + SwaggerApi.prototype.info = null; + + SwaggerApi.prototype.useJQuery = null; + + function SwaggerApi(url, options) { if (options == null) { options = {}; } - if (options.discoveryUrl != null) { - this.discoveryUrl = options.discoveryUrl; + if (url) { + if (url.url) { + options = url; + } else { + this.url = url; + } + } else { + options = url; } - if (options.debug != null) { - this.debug = options.debug; + if (options.url != null) { + this.url = options.url; } - this.apiKeyName = options.apiKeyName != null ? options.apiKeyName : 'api_key'; - if (options.apiKey != null) { - this.api_key = options.apiKey; - } - if (options.api_key != null) { - this.api_key = options.api_key; - } - if (options.verbose != null) { - this.verbose = options.verbose; - } - this.supportHeaderParams = options.supportHeaderParams != null ? options.supportHeaderParams : false; - this.supportedSubmitMethods = options.supportedSubmitMethods != null ? options.supportedSubmitMethods : ['get']; if (options.success != null) { this.success = options.success; } this.failure = options.failure != null ? options.failure : function() {}; this.progress = options.progress != null ? options.progress : function() {}; - this.headers = options.headers != null ? options.headers : {}; - this.booleanValues = options.booleanValues != null ? options.booleanValues : new Array('true', 'false'); - this.discoveryUrl = this.suffixApiKey(this.discoveryUrl); if (options.success != null) { this.build(); } } SwaggerApi.prototype.build = function() { - var _this = this; - this.progress('fetching resource list: ' + this.discoveryUrl); - return jQuery.getJSON(this.discoveryUrl, function(response) { - var res, resource, _i, _j, _len, _len1, _ref, _ref1; - if (response.apiVersion != null) { - _this.apiVersion = response.apiVersion; - } - if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) { - _this.basePath = response.basePath; - if (_this.basePath.match(/^HTTP/i) == null) { - _this.fail("discoveryUrl basePath must be a URL."); - } - _this.basePath = _this.basePath.replace(/\/$/, ''); - } else { - if (_this.discoveryUrl.indexOf('?') > 0) { - _this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('?')); - } else { - _this.basePath = _this.discoveryUrl; - } - log('derived basepath from discoveryUrl as ' + _this.basePath); - } - _this.apis = {}; - _this.apisArray = []; - if (response.resourcePath != null) { - _this.resourcePath = response.resourcePath; - res = null; - _ref = response.apis; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - resource = _ref[_i]; - if (res === null) { - res = new SwaggerResource(resource, _this); + var e, obj, + _this = this; + this.progress('fetching resource list: ' + this.url); + obj = { + useJQuery: this.useJQuery, + url: this.url, + method: "get", + headers: {}, + on: { + error: function(response) { + if (_this.url.substring(0, 4) !== 'http') { + return _this.fail('Please specify the protocol for ' + _this.url); + } else if (response.status === 0) { + return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.'); + } else if (response.status === 404) { + return _this.fail('Can\'t read swagger JSON from ' + _this.url); } else { - res.addOperations(resource.path, resource.operations); + return _this.fail(response.status + ' : ' + response.statusText + ' ' + _this.url); } + }, + response: function(response) { + var responseObj; + responseObj = JSON.parse(response.data); + _this.swaggerVersion = responseObj.swaggerVersion; + if (_this.swaggerVersion === "1.2") { + return _this.buildFromSpec(responseObj); + } else { + return _this.buildFrom1_1Spec(responseObj); + } } - if (res != null) { - _this.apis[res.name] = res; - _this.apisArray.push(res); - res.ready = true; - _this.selfReflect(); + } + }; + e = {}; + if (typeof window !== 'undefined') { + e = window; + } else { + e = exports; + } + e.authorizations.apply(obj); + new SwaggerHttp().execute(obj); + return this; + }; + + SwaggerApi.prototype.buildFromSpec = function(response) { + var api, isApi, newName, operation, res, resource, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; + if (response.apiVersion != null) { + this.apiVersion = response.apiVersion; + } + this.apis = {}; + this.apisArray = []; + this.produces = response.produces; + this.authSchemes = response.authorizations; + if (response.info != null) { + this.info = response.info; + } + isApi = false; + _ref = response.apis; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + api = _ref[_i]; + if (api.operations) { + _ref1 = api.operations; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + operation = _ref1[_j]; + isApi = true; } - } else { - _ref1 = response.apis; + } + } + if (response.basePath) { + this.basePath = response.basePath; + } else if (this.url.indexOf('?') > 0) { + this.basePath = this.url.substring(0, this.url.lastIndexOf('?')); + } else { + this.basePath = this.url; + } + if (isApi) { + newName = response.resourcePath.replace(/\//g, ''); + this.resourcePath = response.resourcePath; + res = new SwaggerResource(response, this); + this.apis[newName] = res; + this.apisArray.push(res); + } else { + _ref2 = response.apis; + for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { + resource = _ref2[_k]; + res = new SwaggerResource(resource, this); + this.apis[res.name] = res; + this.apisArray.push(res); + } + } + if (this.success) { + this.success(); + } + return this; + }; + + SwaggerApi.prototype.buildFrom1_1Spec = function(response) { + var api, isApi, newName, operation, res, resource, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2; + log("This API is using a deprecated version of Swagger! Please see http://github.com/wordnik/swagger-core/wiki for more info"); + if (response.apiVersion != null) { + this.apiVersion = response.apiVersion; + } + this.apis = {}; + this.apisArray = []; + this.produces = response.produces; + if (response.info != null) { + this.info = response.info; + } + isApi = false; + _ref = response.apis; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + api = _ref[_i]; + if (api.operations) { + _ref1 = api.operations; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { - resource = _ref1[_j]; - res = new SwaggerResource(resource, _this); - _this.apis[res.name] = res; - _this.apisArray.push(res); + operation = _ref1[_j]; + isApi = true; } } - return _this; - }).error(function(error) { - if (_this.discoveryUrl.substring(0, 4) !== 'http') { - return _this.fail('Please specify the protocol for ' + _this.discoveryUrl); - } else if (error.status === 0) { - return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.'); - } else if (error.status === 404) { - return _this.fail('Can\'t read swagger JSON from ' + _this.discoveryUrl); - } else { - return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl); + } + if (response.basePath) { + this.basePath = response.basePath; + } else if (this.url.indexOf('?') > 0) { + this.basePath = this.url.substring(0, this.url.lastIndexOf('?')); + } else { + this.basePath = this.url; + } + if (isApi) { + newName = response.resourcePath.replace(/\//g, ''); + this.resourcePath = response.resourcePath; + res = new SwaggerResource(response, this); + this.apis[newName] = res; + this.apisArray.push(res); + } else { + _ref2 = response.apis; + for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) { + resource = _ref2[_k]; + res = new SwaggerResource(resource, this); + this.apis[res.name] = res; + this.apisArray.push(res); } - }); + } + if (this.success) { + this.success(); + } + return this; }; SwaggerApi.prototype.selfReflect = function() { var resource, resource_name, _ref; if (this.apis == null) { @@ -143,11 +223,11 @@ this.models = {}; _ref = this.apis; for (resource_name in _ref) { resource = _ref[resource_name]; for (modelName in resource.models) { - if (!(this.models[modelName] != null)) { + if (this.models[modelName] == null) { this.models[modelName] = resource.models[modelName]; this.modelsArray.push(resource.models[modelName]); } } } @@ -158,34 +238,24 @@ _results.push(model.setReferencedModels(this.models)); } return _results; }; - SwaggerApi.prototype.suffixApiKey = function(url) { - var sep; - if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) { - sep = url.indexOf('?') > 0 ? '&' : '?'; - return url + sep + this.apiKeyName + '=' + this.api_key; - } else { - return url; - } - }; - SwaggerApi.prototype.help = function() { var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2; _ref = this.apis; for (resource_name in _ref) { resource = _ref[resource_name]; - console.log(resource_name); + log(resource_name); _ref1 = resource.operations; for (operation_name in _ref1) { operation = _ref1[operation_name]; - console.log(" " + operation.nickname); + log(" " + operation.nickname); _ref2 = operation.parameters; for (_i = 0, _len = _ref2.length; _i < _len; _i++) { parameter = _ref2[_i]; - console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); + log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); } } } return this; }; @@ -193,69 +263,124 @@ return SwaggerApi; })(); SwaggerResource = (function() { + SwaggerResource.prototype.api = null; + SwaggerResource.prototype.produces = null; + + SwaggerResource.prototype.consumes = null; + function SwaggerResource(resourceObj, api) { - var parts, + var consumes, e, obj, parts, produces, _this = this; this.api = api; + this.api = this.api; + produces = []; + consumes = []; this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path; this.description = resourceObj.description; parts = this.path.split("/"); this.name = parts[parts.length - 1].replace('.{format}', ''); this.basePath = this.api.basePath; - console.log('bp: ' + this.basePath); this.operations = {}; this.operationsArray = []; this.modelsArray = []; this.models = {}; - if ((resourceObj.operations != null) && (this.api.resourcePath != null)) { - this.api.progress('reading resource ' + this.name + ' models and operations'); - this.addModels(resourceObj.models); - this.addOperations(resourceObj.path, resourceObj.operations); - this.api[this.name] = this; + this.rawModels = {}; + if ((resourceObj.apis != null) && (this.api.resourcePath != null)) { + this.addApiDeclaration(resourceObj); } else { if (this.path == null) { this.api.fail("SwaggerResources must have a path."); } - this.url = this.api.suffixApiKey(this.api.basePath + this.path.replace('{format}', 'json')); - console.log('basePath: ' + this.api.basePath); - console.log('url: ' + this.url); + if (this.path.substring(0, 4) === 'http') { + this.url = this.path.replace('{format}', 'json'); + } else { + this.url = this.api.basePath + this.path.replace('{format}', 'json'); + } this.api.progress('fetching resource ' + this.name + ': ' + this.url); - jQuery.getJSON(this.url, function(response) { - var endpoint, _i, _len, _ref; - if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) { - _this.basePath = response.basePath; - _this.basePath = _this.basePath.replace(/\/$/, ''); - } - _this.addModels(response.models); - if (response.apis) { - _ref = response.apis; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - endpoint = _ref[_i]; - _this.addOperations(endpoint.path, endpoint.operations); + obj = { + url: this.url, + method: "get", + useJQuery: this.useJQuery, + headers: {}, + on: { + error: function(response) { + return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + response.statusText + ")"); + }, + response: function(response) { + var responseObj; + responseObj = JSON.parse(response.data); + return _this.addApiDeclaration(responseObj); } } - _this.api[_this.name] = _this; - _this.ready = true; - return _this.api.selfReflect(); - }).error(function(error) { - return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + error.statusText + ")"); - }); + }; + e = {}; + if (typeof window !== 'undefined') { + e = window; + } else { + e = exports; + } + e.authorizations.apply(obj); + new SwaggerHttp().execute(obj); } } + SwaggerResource.prototype.getAbsoluteBasePath = function(relativeBasePath) { + var parts, pos, url; + url = this.api.basePath; + pos = url.lastIndexOf(relativeBasePath); + if (pos === -1) { + parts = url.split("/"); + url = parts[0] + "//" + parts[2]; + if (relativeBasePath.indexOf("/") === 0) { + return url + relativeBasePath; + } else { + return url + "/" + relativeBasePath; + } + } else if (relativeBasePath === "/") { + return url.substring(0, pos); + } else { + return url.substring(0, pos) + relativeBasePath; + } + }; + + SwaggerResource.prototype.addApiDeclaration = function(response) { + var endpoint, _i, _len, _ref; + if (response.produces != null) { + this.produces = response.produces; + } + if (response.consumes != null) { + this.consumes = response.consumes; + } + if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) { + this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath; + } + this.addModels(response.models); + if (response.apis) { + _ref = response.apis; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + endpoint = _ref[_i]; + this.addOperations(endpoint.path, endpoint.operations, response.consumes, response.produces); + } + } + this.api[this.name] = this; + this.ready = true; + return this.api.selfReflect(); + }; + SwaggerResource.prototype.addModels = function(models) { var model, modelName, swaggerModel, _i, _len, _ref, _results; if (models != null) { for (modelName in models) { - if (!(this.models[modelName] != null)) { + if (this.models[modelName] == null) { swaggerModel = new SwaggerModel(modelName, models[modelName]); this.modelsArray.push(swaggerModel); this.models[modelName] = swaggerModel; + this.rawModels[modelName] = models[modelName]; } } _ref = this.modelsArray; _results = []; for (_i = 0, _len = _ref.length; _i < _len; _i++) { @@ -264,79 +389,118 @@ } return _results; } }; - SwaggerResource.prototype.addOperations = function(resource_path, ops) { - var consumes, err, errorResponses, o, op, _i, _j, _len, _len1, _results; + SwaggerResource.prototype.addOperations = function(resource_path, ops, consumes, produces) { + var method, o, op, r, ref, responseMessages, type, _i, _j, _len, _len1, _results; if (ops) { _results = []; for (_i = 0, _len = ops.length; _i < _len; _i++) { o = ops[_i]; - consumes = o.consumes; - if (o.supportedContentTypes) { - consumes = o.supportedContentTypes; + consumes = this.consumes; + produces = this.produces; + if (o.consumes != null) { + consumes = o.consumes; + } else { + consumes = this.consumes; } - errorResponses = o.responseMessages; - if (errorResponses) { - for (_j = 0, _len1 = errorResponses.length; _j < _len1; _j++) { - err = errorResponses[_j]; - err.reason = err.message; + if (o.produces != null) { + produces = o.produces; + } else { + produces = this.produces; + } + type = o.type || o.responseClass; + if (type === "array") { + ref = null; + if (o.items) { + ref = o.items["type"] || o.items["$ref"]; } + type = "array[" + ref + "]"; } - if (o.errorResponses) { - errorResponses = o.errorResponses; + responseMessages = o.responseMessages; + method = o.method; + if (o.httpMethod) { + method = o.httpMethod; } - if (o.method) { - o.httpMethod = o.method; + if (o.supportedContentTypes) { + consumes = o.supportedContentTypes; } - op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, errorResponses, this, o.consumes, o.produces); + if (o.errorResponses) { + responseMessages = o.errorResponses; + for (_j = 0, _len1 = responseMessages.length; _j < _len1; _j++) { + r = responseMessages[_j]; + r.message = r.reason; + r.reason = null; + } + } + o.nickname = this.sanitize(o.nickname); + op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, type, responseMessages, this, consumes, produces, o.authorizations); this.operations[op.nickname] = op; _results.push(this.operationsArray.push(op)); } return _results; } }; + SwaggerResource.prototype.sanitize = function(nickname) { + var op; + op = nickname.replace(/[\s!@#$%^&*()_+=\[{\]};:<>|./?,\\'""-]/g, '_'); + op = op.replace(/((_){2,})/g, '_'); + op = op.replace(/^(_)*/g, ''); + op = op.replace(/([_])*$/g, ''); + return op; + }; + SwaggerResource.prototype.help = function() { - var operation, operation_name, parameter, _i, _len, _ref, _ref1; + var msg, operation, operation_name, parameter, _i, _len, _ref, _ref1, _results; _ref = this.operations; + _results = []; for (operation_name in _ref) { operation = _ref[operation_name]; - console.log(" " + operation.nickname); + msg = " " + operation.nickname; _ref1 = operation.parameters; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { parameter = _ref1[_i]; - console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); + msg.concat(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); } + _results.push(msg); } - return this; + return _results; }; return SwaggerResource; })(); SwaggerModel = (function() { - function SwaggerModel(modelName, obj) { - var propertyName; + var prop, propertyName, value; this.name = obj.id != null ? obj.id : modelName; this.properties = []; for (propertyName in obj.properties) { - this.properties.push(new SwaggerModelProperty(propertyName, obj.properties[propertyName])); + if (obj.required != null) { + for (value in obj.required) { + if (propertyName === obj.required[value]) { + obj.properties[propertyName].required = true; + } + } + } + prop = new SwaggerModelProperty(propertyName, obj.properties[propertyName]); + this.properties.push(prop); } } SwaggerModel.prototype.setReferencedModels = function(allModels) { - var prop, _i, _len, _ref, _results; + var prop, type, _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]); + type = prop.type || prop.dataType; + if (allModels[type] != null) { + _results.push(prop.refModel = allModels[type]); } else if ((prop.refDataType != null) && (allModels[prop.refDataType] != null)) { _results.push(prop.refModel = allModels[prop.refDataType]); } else { _results.push(void 0); } @@ -363,11 +527,11 @@ } modelsToIgnore.push(this); _ref1 = this.properties; for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { prop = _ref1[_j]; - if ((prop.refModel != null) && (modelsToIgnore.indexOf(prop.refModel)) === -1) { + if ((prop.refModel != null) && (modelsToIgnore[prop.refModel] !== 'undefined') === -1) { returnVal = returnVal + ('<br>' + prop.refModel.getMockSignature(modelsToIgnore)); } } return returnVal; }; @@ -380,22 +544,22 @@ _ref = this.properties; for (_i = 0, _len = _ref.length; _i < _len; _i++) { prop = _ref[_i]; result[prop.name] = prop.getSampleValue(modelsToIgnore); } + modelsToIgnore.pop(this.name); return result; }; return SwaggerModel; })(); SwaggerModelProperty = (function() { - function SwaggerModelProperty(name, obj) { this.name = name; - this.dataType = obj.type; + this.dataType = obj.type || obj.dataType || obj["$ref"]; this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set'); this.descr = obj.description; this.required = obj.required; if (obj.items != null) { if (obj.items.type != null) { @@ -411,15 +575,22 @@ this.values = obj.allowableValues.values; if (this.values != null) { this.valuesString = "'" + this.values.join("' or '") + "'"; } } + if (obj["enum"] != null) { + this.valueType = "string"; + this.values = obj["enum"]; + if (this.values != null) { + this.valueString = "'" + this.values.join("' or '") + "'"; + } + } } SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) { var result; - if ((this.refModel != null) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) { + if ((this.refModel != null) && (modelsToIgnore[this.refModel.name] === 'undefined')) { result = this.refModel.createJSONSample(modelsToIgnore); } else { if (this.isCollection) { result = this.refDataType; } else { @@ -453,69 +624,89 @@ return SwaggerModelProperty; })(); SwaggerOperation = (function() { - - function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, consumes, produces) { - var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2, + function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations) { + var parameter, v, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _this = this; this.nickname = nickname; this.path = path; - this.httpMethod = httpMethod; + this.method = method; this.parameters = parameters != null ? parameters : []; this.summary = summary; this.notes = notes; - this.responseClass = responseClass; - this.errorResponses = errorResponses; + this.type = type; + this.responseMessages = responseMessages; this.resource = resource; this.consumes = consumes; this.produces = produces; + this.authorizations = authorizations; this["do"] = __bind(this["do"], this); - if (this.nickname == null) { this.resource.api.fail("SwaggerOperations must have a nickname."); } if (this.path == null) { this.resource.api.fail("SwaggerOperation " + nickname + " is missing path."); } - if (this.httpMethod == null) { - this.resource.api.fail("SwaggerOperation " + nickname + " is missing httpMethod."); + if (this.method == null) { + this.resource.api.fail("SwaggerOperation " + nickname + " is missing method."); } this.path = this.path.replace('{format}', 'json'); - this.httpMethod = this.httpMethod.toLowerCase(); - this.isGetMethod = this.httpMethod === "get"; + this.method = this.method.toLowerCase(); + this.isGetMethod = this.method === "get"; this.resourceName = this.resource.name; - if (((_ref = this.responseClass) != null ? _ref.toLowerCase() : void 0) === 'void') { - this.responseClass = void 0; + if (((_ref = this.type) != null ? _ref.toLowerCase() : void 0) === 'void') { + this.type = void 0; } - if (this.responseClass != null) { - this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models); - this.responseSampleJSON = this.getSampleJSON(this.responseClass, this.resource.models); + if (this.type != null) { + this.responseClassSignature = this.getSignature(this.type, this.resource.models); + this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models); } - this.errorResponses = this.errorResponses || []; + this.responseMessages = this.responseMessages || []; _ref1 = this.parameters; for (_i = 0, _len = _ref1.length; _i < _len; _i++) { parameter = _ref1[_i]; - parameter.name = parameter.name || parameter.dataType; - if (parameter.dataType.toLowerCase() === 'boolean') { + parameter.name = parameter.name || parameter.type || parameter.dataType; + type = parameter.type || parameter.dataType; + if (type.toLowerCase() === 'boolean') { parameter.allowableValues = {}; - parameter.allowableValues.values = this.resource.api.booleanValues; + parameter.allowableValues.values = ["true", "false"]; } - parameter.signature = this.getSignature(parameter.dataType, this.resource.models); - parameter.sampleJSON = this.getSampleJSON(parameter.dataType, this.resource.models); + parameter.signature = this.getSignature(type, this.resource.models); + parameter.sampleJSON = this.getSampleJSON(type, this.resource.models); + if (parameter["enum"] != null) { + parameter.isList = true; + parameter.allowableValues = {}; + parameter.allowableValues.descriptiveValues = []; + _ref2 = parameter["enum"]; + for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { + v = _ref2[_j]; + if ((parameter.defaultValue != null) && parameter.defaultValue === v) { + parameter.allowableValues.descriptiveValues.push({ + value: String(v), + isDefault: true + }); + } else { + parameter.allowableValues.descriptiveValues.push({ + value: String(v), + isDefault: false + }); + } + } + } if (parameter.allowableValues != null) { if (parameter.allowableValues.valueType === "RANGE") { parameter.isRange = true; } else { parameter.isList = true; } if (parameter.allowableValues.values != null) { parameter.allowableValues.descriptiveValues = []; - _ref2 = parameter.allowableValues.values; - for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) { - v = _ref2[_j]; + _ref3 = parameter.allowableValues.values; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + v = _ref3[_k]; if ((parameter.defaultValue != null) && parameter.defaultValue === v) { parameter.allowableValues.descriptiveValues.push({ value: v, isDefault: true }); @@ -530,90 +721,142 @@ } } 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) { - if (dataType.indexOf('[') >= 0) { - return dataType.substring(dataType.indexOf('[') + 1, dataType.indexOf(']')); + SwaggerOperation.prototype.isListType = function(type) { + if (type.indexOf('[') >= 0) { + return type.substring(type.indexOf('[') + 1, type.indexOf(']')); } else { return void 0; } }; - SwaggerOperation.prototype.getSignature = function(dataType, models) { + SwaggerOperation.prototype.getSignature = function(type, models) { var isPrimitive, listType; - listType = this.isListType(dataType); - isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true; + listType = this.isListType(type); + isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; if (isPrimitive) { - return dataType; + return type; } else { if (listType != null) { return models[listType].getMockSignature(); } else { - return models[dataType].getMockSignature(); + return models[type].getMockSignature(); } } }; - SwaggerOperation.prototype.getSampleJSON = function(dataType, models) { + SwaggerOperation.prototype.getSampleJSON = function(type, models) { var isPrimitive, listType, val; - listType = this.isListType(dataType); - isPrimitive = ((listType != null) && models[listType]) || (models[dataType] != null) ? false : true; - val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[dataType].createJSONSample()); + listType = this.isListType(type); + isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; + val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample()); if (val) { val = listType ? [val] : val; return JSON.stringify(val, null, 2); } }; - SwaggerOperation.prototype["do"] = function(args, callback, error) { - var body, headers; + SwaggerOperation.prototype["do"] = function(args, opts, callback, error) { + var key, param, params, possibleParams, req, requestContentType, responseContentType, value, _i, _len, _ref; 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); + return log(xhr, textStatus, error); }; } if (callback == null) { - callback = function(data) { - return console.log(data); + callback = function(response) { + var content; + content = null; + if (response != null) { + content = response.data; + } else { + content = "no data"; + } + return log("default callback: " + content); }; } + params = {}; + params.headers = []; if (args.headers != null) { - headers = args.headers; + params.headers = args.headers; delete args.headers; } + _ref = this.parameters; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + param = _ref[_i]; + if (param.paramType === "header") { + if (args[param.name]) { + params.headers[param.name] = args[param.name]; + } + } + } if (args.body != null) { - body = args.body; + params.body = args.body; delete args.body; } - return new SwaggerRequest(this.httpMethod, this.urlify(args), headers, body, callback, error, this); + possibleParams = (function() { + var _j, _len1, _ref1, _results; + _ref1 = this.parameters; + _results = []; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + param = _ref1[_j]; + if (param.paramType === "form" || param.paramType.toLowerCase() === "file") { + _results.push(param); + } + } + return _results; + }).call(this); + if (possibleParams) { + for (key in possibleParams) { + value = possibleParams[key]; + if (args[value.name]) { + params[value.name] = args[value.name]; + } + } + } + req = new SwaggerRequest(this.method, this.urlify(args), params, opts, callback, error, this); + if (opts.mock != null) { + return req; + } else { + return true; + } }; SwaggerOperation.prototype.pathJson = function() { return this.path.replace("{format}", "json"); }; SwaggerOperation.prototype.pathXml = function() { return this.path.replace("{format}", "xml"); }; - SwaggerOperation.prototype.urlify = function(args, includeApiKey) { - var param, queryParams, reg, url, _i, _len, _ref; - if (includeApiKey == null) { - includeApiKey = true; - } + SwaggerOperation.prototype.urlify = function(args) { + var param, queryParams, reg, url, _i, _j, _len, _len1, _ref, _ref1; url = this.resource.basePath + this.pathJson(); _ref = this.parameters; for (_i = 0, _len = _ref.length; _i < _len; _i++) { param = _ref[_i]; if (param.paramType === 'path') { @@ -624,18 +867,23 @@ } else { throw "" + param.name + " is a required path param."; } } } - if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) { - args[this.apiKeyName] = this.resource.api.api_key; + queryParams = ""; + _ref1 = this.parameters; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + param = _ref1[_j]; + if (param.paramType === 'query') { + if (args[param.name]) { + if (queryParams !== "") { + queryParams += "&"; + } + queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]); + } + } } - if (this.supportHeaderParams()) { - queryParams = jQuery.param(this.getQueryParams(args, includeApiKey)); - } else { - queryParams = jQuery.param(this.getQueryAndHeaderParams(args, includeApiKey)); - } if ((queryParams != null) && queryParams.length > 0) { url += "?" + queryParams; } return url; }; @@ -646,80 +894,66 @@ SwaggerOperation.prototype.supportedSubmitMethods = function() { return this.resource.api.supportedSubmitMethods; }; - SwaggerOperation.prototype.getQueryAndHeaderParams = function(args, includeApiKey) { - if (includeApiKey == null) { - includeApiKey = true; - } - return this.getMatchingParams(['query', 'header'], args, includeApiKey); + SwaggerOperation.prototype.getQueryParams = function(args) { + return this.getMatchingParams(['query'], args); }; - SwaggerOperation.prototype.getQueryParams = function(args, includeApiKey) { - if (includeApiKey == null) { - includeApiKey = true; - } - return this.getMatchingParams(['query'], args, includeApiKey); + SwaggerOperation.prototype.getHeaderParams = function(args) { + return this.getMatchingParams(['header'], args); }; - SwaggerOperation.prototype.getHeaderParams = function(args, includeApiKey) { - if (includeApiKey == null) { - includeApiKey = true; - } - return this.getMatchingParams(['header'], args, includeApiKey); - }; - - SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args, includeApiKey) { + SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) { var matchingParams, name, param, value, _i, _len, _ref, _ref1; matchingParams = {}; _ref = this.parameters; for (_i = 0, _len = _ref.length; _i < _len; _i++) { param = _ref[_i]; - if ((jQuery.inArray(param.paramType, paramTypes) >= 0) && args[param.name]) { + if (args && args[param.name]) { matchingParams[param.name] = args[param.name]; } } - if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) { - matchingParams[this.resource.api.apiKeyName] = this.resource.api.api_key; + _ref1 = this.resource.api.headers; + for (name in _ref1) { + value = _ref1[name]; + matchingParams[name] = value; } - if (jQuery.inArray('header', paramTypes) >= 0) { - _ref1 = this.resource.api.headers; - for (name in _ref1) { - value = _ref1[name]; - matchingParams[name] = value; - } - } return matchingParams; }; SwaggerOperation.prototype.help = function() { - var parameter, _i, _len, _ref; + var msg, parameter, _i, _len, _ref; + msg = ""; _ref = this.parameters; for (_i = 0, _len = _ref.length; _i < _len; _i++) { parameter = _ref[_i]; - console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); + if (msg !== "") { + msg += "\n"; + } + msg += "* " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description; } - return this; + return msg; }; return SwaggerOperation; })(); SwaggerRequest = (function() { - - function SwaggerRequest(type, url, headers, body, successCallback, errorCallback, operation) { - var obj, + function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) { + var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, status, urlEncoded, value, values, _this = this; this.type = type; this.url = url; - this.headers = headers; - this.body = body; + this.params = params; + this.opts = opts; this.successCallback = successCallback; this.errorCallback = errorCallback; this.operation = operation; + this.execution = execution; if (this.type == null) { throw "SwaggerRequest type is required (get/post/put/delete)."; } if (this.url == null) { throw "SwaggerRequest url is required."; @@ -731,34 +965,155 @@ throw "SwaggerRequest error callback is required."; } if (this.operation == null) { throw "SwaggerRequest operation is required."; } - if (this.operation.resource.api.verbose) { - console.log(this.asCurl()); + this.type = this.type.toUpperCase(); + headers = params.headers; + myHeaders = {}; + body = params.body; + parent = params["parent"]; + requestContentType = "application/json"; + if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) { + if (this.opts.requestContentType) { + requestContentType = this.opts.requestContentType; + } + } else { + if (((function() { + var _i, _len, _ref, _results; + _ref = this.operation.parameters; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + param = _ref[_i]; + if (param.paramType === "form") { + _results.push(param); + } + } + return _results; + }).call(this)).length > 0) { + type = param.type || param.dataType; + if (((function() { + var _i, _len, _ref, _results; + _ref = this.operation.parameters; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + param = _ref[_i]; + if (type.toLowerCase() === "file") { + _results.push(param); + } + } + return _results; + }).call(this)).length > 0) { + requestContentType = "multipart/form-data"; + } else { + requestContentType = "application/x-www-form-urlencoded"; + } + } else if (this.type !== "DELETE") { + requestContentType = null; + } } - this.headers || (this.headers = {}); - if (this.operation.resource.api.api_key != null) { - this.headers[this.apiKeyName] = this.operation.resource.api.api_key; + 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]; + } + } } - if (this.headers.mock == null) { + responseContentType = null; + if (this.type === "POST" || this.type === "GET" || this.type === "PATCH") { + if (this.opts.responseContentType) { + responseContentType = this.opts.responseContentType; + } else { + responseContentType = "application/json"; + } + } else { + responseContentType = null; + } + 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) { + fields = {}; + possibleParams = (function() { + var _i, _len, _ref, _results; + _ref = this.operation.parameters; + _results = []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + param = _ref[_i]; + if (param.paramType === "form") { + _results.push(param); + } + } + return _results; + }).call(this); + values = {}; + for (key in possibleParams) { + value = possibleParams[key]; + if (this.params[value.name]) { + values[value.name] = this.params[value.name]; + } + } + urlEncoded = ""; + for (key in values) { + value = values[key]; + if (urlEncoded !== "") { + urlEncoded += "&"; + } + urlEncoded += encodeURIComponent(key) + '=' + encodeURIComponent(value); + } + body = urlEncoded; + } + for (name in headers) { + myHeaders[name] = headers[name]; + } + if (requestContentType) { + myHeaders["Content-Type"] = requestContentType; + } + if (responseContentType) { + myHeaders["Accept"] = responseContentType; + } + if (!((headers != null) && (headers.mock != null))) { obj = { - type: this.type, url: this.url, - data: JSON.stringify(this.body), - dataType: 'json', - error: function(xhr, textStatus, error) { - return _this.errorCallback(xhr, textStatus, error); - }, - success: function(data) { - return _this.successCallback(data); + method: this.type, + headers: myHeaders, + body: body, + useJQuery: this.useJQuery, + on: { + error: function(response) { + return _this.errorCallback(response, _this.opts.parent); + }, + redirect: function(response) { + return _this.successCallback(response, _this.opts.parent); + }, + 307: function(response) { + return _this.successCallback(response, _this.opts.parent); + }, + response: function(response) { + return _this.successCallback(response, _this.opts.parent); + } } }; - if (obj.type.toLowerCase() === "post" || obj.type.toLowerCase() === "put") { - obj.contentType = "application/json"; + e = {}; + if (typeof window !== 'undefined') { + e = window; + } else { + e = exports; } - jQuery.ajax(obj); + status = e.authorizations.apply(obj, this.operation.authorizations); + if (opts.mock == null) { + if (status !== false) { + new SwaggerHttp().execute(obj); + } else { + obj.canceled = true; + } + } else { + return obj; + } } } SwaggerRequest.prototype.asCurl = function() { var header_args, k, v; @@ -777,16 +1132,296 @@ return SwaggerRequest; })(); - window.SwaggerApi = SwaggerApi; + SwaggerHttp = (function() { + function SwaggerHttp() {} - window.SwaggerResource = SwaggerResource; + SwaggerHttp.prototype.Shred = null; - window.SwaggerOperation = SwaggerOperation; + SwaggerHttp.prototype.shred = null; - window.SwaggerRequest = SwaggerRequest; + SwaggerHttp.prototype.content = null; - window.SwaggerModelProperty = SwaggerModelProperty; + SwaggerHttp.prototype.initShred = function() { + var identity, toString, + _this = this; + 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(); + }; + if (typeof window !== 'undefined') { + this.content = require("./shred/content"); + return this.content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], { + parser: identity, + stringify: toString + }); + } else { + return this.Shred.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], { + parser: identity, + stringify: toString + }); + } + }; -}).call(this); + SwaggerHttp.prototype.execute = function(obj) { + if (this.isIE() || obj.useJQuery) { + return this.executeWithJQuery(obj); + } else { + return this.executeWithShred(obj); + } + }; + + SwaggerHttp.prototype.executeWithShred = function(obj) { + var cb, res, + _this = this; + if (!this.Shred) { + this.initShred(); + } + cb = obj.on; + res = { + error: function(response) { + if (obj) { + return cb.error(response); + } + }, + redirect: function(response) { + if (obj) { + return cb.redirect(response); + } + }, + 307: function(response) { + if (obj) { + return cb.redirect(response); + } + }, + response: function(raw) { + var headers, out; + if (obj) { + headers = raw._headers; + out = { + headers: headers, + url: raw.request.url, + method: raw.request.method, + status: raw.status, + data: raw.content.data + }; + return cb.response(out); + } + } + }; + if (obj) { + obj.on = res; + } + return this.shred.request(obj); + }; + + SwaggerHttp.prototype.executeWithJQuery = function(obj) { + var beforeSend, cb, request, + _this = this; + cb = obj.on; + request = obj; + obj.type = obj.method; + obj.cache = false; + beforeSend = function(xhr) { + var key, _results; + if (obj.headers) { + _results = []; + for (key in obj.headers) { + if (key.toLowerCase() === "content-type") { + _results.push(obj.contentType = obj.headers[key]); + } else if (key.toLowerCase() === "accept") { + _results.push(obj.accepts = obj.headers[key]); + } else { + _results.push(xhr.setRequestHeader(key, obj.headers[key])); + } + } + return _results; + } + }; + obj.beforeSend = beforeSend; + obj.data = obj.body; + obj.complete = function(response, textStatus, opts) { + var headerArray, headers, i, out, _i, _j, _k, _ref, _ref1, _ref2, _ref3, _results, _results1; + headers = {}; + headerArray = response.getAllResponseHeaders().split(":"); + for (i = _i = 0, _ref = headerArray.length / 2, _ref1 = 2.; _ref1 > 0 ? _i <= _ref : _i >= _ref; i = _i += _ref1) { + headers[headerArray[i]] = headerArray[i + 1]; + } + out = { + headers: headers, + url: request.url, + method: request.method, + status: response.status, + data: response.responseText, + headers: headers + }; + if (_ref2 = response.status, __indexOf.call((function() { + _results = []; + for (_j = 200; _j <= 299; _j++){ _results.push(_j); } + return _results; + }).apply(this), _ref2) >= 0) { + cb.response(out); + } + if ((_ref3 = response.status, __indexOf.call((function() { + _results1 = []; + for (_k = 400; _k <= 599; _k++){ _results1.push(_k); } + return _results1; + }).apply(this), _ref3) >= 0) || response.status === 0) { + cb.error(out); + } + return cb.response(out); + }; + $.support.cors = true; + return $.ajax(obj); + }; + + SwaggerHttp.prototype.isIE = function() { + var isIE, nav, version; + ({ + isIE: false + }); + if (typeof navigator !== 'undefined' && navigator.userAgent) { + nav = navigator.userAgent.toLowerCase(); + if (nav.indexOf('msie') !== -1) { + version = parseInt(nav.split('msie')[1]); + if (version <= 8) { + isIE = true; + } + } + } + return isIE; + }; + + return SwaggerHttp; + + })(); + + SwaggerAuthorizations = (function() { + SwaggerAuthorizations.prototype.authz = null; + + function SwaggerAuthorizations() { + this.authz = {}; + } + + SwaggerAuthorizations.prototype.add = function(name, auth) { + this.authz[name] = auth; + return auth; + }; + + SwaggerAuthorizations.prototype.remove = function(name) { + return delete this.authz[name]; + }; + + SwaggerAuthorizations.prototype.apply = function(obj, authorizations) { + var key, result, status, value, _ref; + status = null; + _ref = this.authz; + for (key in _ref) { + value = _ref[key]; + result = value.apply(obj, authorizations); + if (result === false) { + status = false; + } + if (result === true) { + status = true; + } + } + return status; + }; + + return SwaggerAuthorizations; + + })(); + + ApiKeyAuthorization = (function() { + ApiKeyAuthorization.prototype.type = null; + + ApiKeyAuthorization.prototype.name = null; + + ApiKeyAuthorization.prototype.value = null; + + function ApiKeyAuthorization(name, value, type) { + this.name = name; + this.value = value; + this.type = type; + } + + ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { + if (this.type === "query") { + if (obj.url.indexOf('?') > 0) { + obj.url = obj.url + "&" + this.name + "=" + this.value; + } else { + obj.url = obj.url + "?" + this.name + "=" + this.value; + } + return true; + } else if (this.type === "header") { + obj.headers[this.name] = this.value; + return true; + } + }; + + return ApiKeyAuthorization; + + })(); + + PasswordAuthorization = (function() { + PasswordAuthorization._btoa = null; + + PasswordAuthorization.prototype.name = null; + + PasswordAuthorization.prototype.username = null; + + PasswordAuthorization.prototype.password = null; + + function PasswordAuthorization(name, username, password) { + this.name = name; + this.username = username; + this.password = password; + PasswordAuthorization._ensureBtoa(); + } + + PasswordAuthorization.prototype.apply = function(obj, authorizations) { + obj.headers["Authorization"] = "Basic " + PasswordAuthorization._btoa(this.username + ":" + this.password); + return true; + }; + + PasswordAuthorization._ensureBtoa = function() { + if (typeof window !== 'undefined') { + return this._btoa = btoa; + } else { + return this._btoa = require("btoa"); + } + }; + + return PasswordAuthorization; + + })(); + + this.SwaggerApi = SwaggerApi; + + this.SwaggerResource = SwaggerResource; + + this.SwaggerOperation = SwaggerOperation; + + this.SwaggerRequest = SwaggerRequest; + + this.SwaggerModelProperty = SwaggerModelProperty; + + this.ApiKeyAuthorization = ApiKeyAuthorization; + + this.PasswordAuthorization = PasswordAuthorization; + + this.SwaggerHttp = SwaggerHttp; + + this.authorizations = new SwaggerAuthorizations(); + +}).call(this); \ No newline at end of file