diff --git a/dist/lib/swagger.js b/dist/lib/swagger.js index a7b1abcc..7708103d 100644 --- a/dist/lib/swagger.js +++ b/dist/lib/swagger.js @@ -1,1480 +1,1311 @@ -// Generated by CoffeeScript 1.6.3 -(function() { - var ApiKeyAuthorization, PasswordAuthorization, SwaggerApi, SwaggerAuthorizations, SwaggerHttp, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource, log, - __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; }; +// swagger.js +// version 2.0.14 - log = function() { - if (window.console) { - return console.log.apply(console, arguments); +var __bind = function(fn, me){ + return function(){ + return fn.apply(me, arguments); + }; +}; + +log = function(){ + log.history = log.history || []; + log.history.push(arguments); + if(this.console){ + console.log( Array.prototype.slice.call(arguments) ); + } +}; + +var SwaggerApi = function(url, options) { + this.url = null; + this.debug = false; + this.basePath = null; + this.authorizations = null; + this.authorizationScheme = null; + this.info = null; + + options = (options||{}); + if (url) + if (url.url) + options = url; + else + this.url = url; + else + options = url; + + if (options.url != null) + this.url = options.url; + + if (options.success != null) + this.success = options.success; + + this.failure = options.failure != null ? options.failure : function() {}; + this.progress = options.progress != null ? options.progress : function() {}; + if (options.success != null) + this.build(); +} + +SwaggerApi.prototype.build = function() { + var _this = this; + this.progress('fetching resource list: ' + this.url); + var obj = { + useJQuery: this.useJQuery, + url: this.url, + method: "get", + headers: { + accept: "application/json" + }, + 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 { + return _this.fail(response.status + ' : ' + response.statusText + ' ' + _this.url); + } + }, + response: function(resp) { + var responseObj = resp.obj; + _this.swaggerVersion = responseObj.swaggerVersion; + if (_this.swaggerVersion === "1.2") { + return _this.buildFromSpec(responseObj); + } else { + return _this.buildFrom1_1Spec(responseObj); + } + } + } + }; + var e = (typeof window !== 'undefined' ? window : exports); + e.authorizations.apply(obj); + new SwaggerHttp().execute(obj); + return this; +}; + +SwaggerApi.prototype.buildFromSpec = function(response) { + 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; + } + var isApi = false; + for (var i = 0; i < response.apis.length; i++) { + var api = response.apis[i]; + if (api.operations) { + for (var j = 0; j < api.operations.length; j++) { + operation = api.operations[j]; + isApi = true; + } + } + } + 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) { + var newName = response.resourcePath.replace(/\//g, ''); + this.resourcePath = response.resourcePath; + res = new SwaggerResource(response, this); + this.apis[newName] = res; + this.apisArray.push(res); + } else { + for (var k = 0; k < response.apis.length; k++) { + var resource = response.apis[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) { + 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; + } + var isApi = false; + for (var i = 0; i < response.apis.length; i++) { + var api = response.apis[i]; + if (api.operations) { + for (var j = 0; j < api.operations.length; j++) { + operation = api.operations[j]; + isApi = true; + } + } + } + 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) { + var newName = response.resourcePath.replace(/\//g, ''); + this.resourcePath = response.resourcePath; + var res = new SwaggerResource(response, this); + this.apis[newName] = res; + this.apisArray.push(res); + } else { + for (k = 0; k < response.apis.length; k++) { + resource = response.apis[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) { + return false; + } + _ref = this.apis; + for (resource_name in _ref) { + resource = _ref[resource_name]; + if (resource.ready == null) { + return false; + } + } + this.setConsolidatedModels(); + this.ready = true; + if (this.success != null) { + return this.success(); + } +}; + +SwaggerApi.prototype.fail = function(message) { + this.failure(message); + throw 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) { + resource = _ref[resource_name]; + for (modelName in resource.models) { + if (this.models[modelName] == null) { + this.models[modelName] = resource.models[modelName]; + this.modelsArray.push(resource.models[modelName]); + } + } + } + _ref1 = this.modelsArray; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + model = _ref1[_i]; + _results.push(model.setReferencedModels(this.models)); + } + return _results; +}; + +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]; + log(resource_name); + _ref1 = resource.operations; + for (operation_name in _ref1) { + operation = _ref1[operation_name]; + log(" " + operation.nickname); + _ref2 = operation.parameters; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + parameter = _ref2[_i]; + log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); + } + } + } + return this; +}; + +var SwaggerResource = function(resourceObj, api) { + var _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; + + var parts = this.path.split("/"); + this.name = parts[parts.length - 1].replace('.{format}', ''); + this.basePath = this.api.basePath; + this.operations = {}; + this.operationsArray = []; + this.modelsArray = []; + this.models = {}; + this.rawModels = {}; + this.useJQuery = (typeof api.useJQuery !== 'undefined' ? api.useJQuery : null); + + if ((resourceObj.apis != null) && (this.api.resourcePath != null)) { + this.addApiDeclaration(resourceObj); + } else { + if (this.path == null) { + this.api.fail("SwaggerResources must have a path."); + } + 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); + obj = { + url: this.url, + method: "get", + useJQuery: this.useJQuery, + headers: { + accept: "application/json" + }, + on: { + response: function(response) { + var responseObj = response.obj; + return _this.addApiDeclaration(responseObj); + }, + error: function(response) { + return _this.api.fail("Unable to read api '" + + _this.name + "' from path " + _this.url + " (server returned " + response.statusText + ")"); + } + } + }; + var e = typeof window !== 'undefined' ? window : 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) { + 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) { + for (var i = 0 ; i < response.apis.length; i++) { + var endpoint = response.apis[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) { + if (models != null) { + for (modelName in models) { + if (this.models[modelName] == null) { + var swaggerModel = new SwaggerModel(modelName, models[modelName]); + this.modelsArray.push(swaggerModel); + this.models[modelName] = swaggerModel; + this.rawModels[modelName] = models[modelName]; + } + } + var output = []; + for (var i = 0; i < this.modelsArray.length; i++) { + model = this.modelsArray[i]; + output.push(model.setReferencedModels(this.models)); + } + return output; + } +}; + +SwaggerResource.prototype.addOperations = function(resource_path, ops, consumes, produces) { + if (ops) { + output = []; + for (var i = 0; i < ops.length; i++) { + o = ops[i]; + consumes = this.consumes; + produces = this.produces; + if (o.consumes != null) + consumes = o.consumes; + else + consumes = this.consumes; + + 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 + "]"; + } + responseMessages = o.responseMessages; + method = o.method; + if (o.httpMethod) { + method = o.httpMethod; + } + if (o.supportedContentTypes) { + consumes = o.supportedContentTypes; + } + if (o.errorResponses) { + responseMessages = o.errorResponses; + for (j = 0; j < responseMessages.length; 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; + output.push(this.operationsArray.push(op)); + } + return output; + } +}; + +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 op = this.operations; + var output = []; + for (operation_name in op) { + operation = op[operation_name]; + var msg = " " + operation.nickname; + for (var i = 0; i < operation.parameters; i++) { + parameter = operation.parameters[i]; + msg.concat(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); + } + output.push(msg); + } + return output; +}; + +var SwaggerModel = function(modelName, obj) { + this.name = obj.id != null ? obj.id : modelName; + this.properties = []; + for (propertyName in obj.properties) { + 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 results = []; + for (var i = 0; i < this.properties.length; i++) { + var property = this.properties[i]; + var type = property.type || property.dataType; + if (allModels[type] != null) + results.push(property.refModel = allModels[type]); + else if ((property.refDataType != null) && (allModels[property.refDataType] != null)) + results.push(property.refModel = allModels[property.refDataType]); + else + results.push(void 0); + } + return results; +}; + +SwaggerModel.prototype.getMockSignature = function(modelsToIgnore) { + var propertiesStr = []; + for (var i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; + propertiesStr.push(prop.toString()); + } + + var strong = ''; + var stronger = ''; + var strongClose = ''; + var classOpen = strong + this.name + ' {' + strongClose; + var classClose = strong + '}' + strongClose; + var returnVal = classOpen + '
' + propertiesStr.join(',
') + '
' + classClose; + if (!modelsToIgnore) + modelsToIgnore = []; + modelsToIgnore.push(this); + + for (var i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; + if ((prop.refModel != null) && modelsToIgnore.indexOf(prop.refModel) === -1) { + returnVal = returnVal + ('
' + prop.refModel.getMockSignature(modelsToIgnore)); + } + } + return returnVal; +}; + +SwaggerModel.prototype.createJSONSample = function(modelsToIgnore) { + var result = {}; + var modelsToIgnore = (modelsToIgnore||[]) + modelsToIgnore.push(this.name); + for (var i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; + result[prop.name] = prop.getSampleValue(modelsToIgnore); + } + modelsToIgnore.pop(this.name); + return result; +}; + +var SwaggerModelProperty = function(name, obj) { + this.name = name; + 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) { + this.refDataType = obj.items.type; + } + if (obj.items.$ref != null) { + this.refDataType = obj.items.$ref; + } + } + this.dataTypeWithRef = this.refDataType != null ? (this.dataType + '[' + this.refDataType + ']') : this.dataType; + if (obj.allowableValues != null) { + this.valueType = obj.allowableValues.valueType; + 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[this.refModel.name] === undefined)) { + result = this.refModel.createJSONSample(modelsToIgnore); + } else { + if (this.isCollection) { + result = this.toSampleValue(this.refDataType); + } else { + result = this.toSampleValue(this.dataType); + } + } + if (this.isCollection) { + return [result]; + } else { + return result; + } +}; + +SwaggerModelProperty.prototype.toSampleValue = function(value) { + var result; + if (value === "integer") { + result = 0; + } else if (value === "boolean") { + result = false; + } else if (value === "double" || value === "number") { + result = 0.0; + } else if (value === "string") { + result = ""; + } else { + result = value; + } + return result; +}; + +SwaggerModelProperty.prototype.toString = function() { + var req = this.required ? 'propReq' : 'propOpt'; + var str = '' + this.name + ' (' + this.dataTypeWithRef + ''; + if (!this.required) { + str += ', optional'; + } + str += ')'; + if (this.values != null) { + str += " = ['" + this.values.join("' or '") + "']"; + } + if (this.descr != null) { + str += ': ' + this.descr + ''; + } + return str; +}; + +var SwaggerOperation = function(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces, authorizations) { + var _this = this; + + var errors = []; + this.nickname = (nickname||errors.push("SwaggerOperations must have a nickname.")); + this.path = (path||errors.push("SwaggerOperation " + nickname + " is missing path.")); + this.method = (method||errors.push("SwaggerOperation " + nickname + " is missing method.")); + this.parameters = parameters != null ? parameters : []; + this.summary = summary; + this.notes = notes; + this.type = type; + this.responseMessages = (responseMessages||[]); + this.resource = (resource||errors.push("Resource is required")); + this.consumes = consumes; + this.produces = produces; + this.authorizations = authorizations; + this["do"] = __bind(this["do"], this); + + if (errors.length > 0) + this.resource.api.fail(errors); + + this.path = this.path.replace('{format}', 'json'); + this.method = this.method.toLowerCase(); + this.isGetMethod = this.method === "get"; + + this.resourceName = this.resource.name; + if(typeof this.type !== 'undefined' && this.type === 'void') + this.type = null; + else { + this.responseClassSignature = this.getSignature(this.type, this.resource.models); + this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models); + } + + for(var i = 0; i < this.parameters.length; i ++) { + var param = this.parameters[i]; + // might take this away + param.name = param.name || param.type || param.dataType; + + // for 1.1 compatibility + var type = param.type || param.dataType; + + if(type.toLowerCase() === 'boolean') { + param.allowableValues = {}; + param.allowableValues.values = ["true", "false"]; + } + param.signature = this.getSignature(type, this.resource.models); + param.sampleJSON = this.getSampleJSON(type, this.resource.models); + + var enumValue = param["enum"]; + if(enumValue != null) { + param.isList = true; + param.allowableValues = {}; + param.allowableValues.descriptiveValues = []; + + for(j = 0; j < enumValue.length; j++) { + var v = enumValue[j]; + if(param.defaultValue != null) { + param.allowableValues.descriptiveValues.push ({ + value: String(v), + isDefault: (v === param.defaultValue) + }); + } + else { + param.allowableValues.descriptiveValues.push ({ + value: String(v), + isDefault: false + }); + } + } + } + if(param.allowableValues != null) { + if(param.allowableValues.valueType === "RANGE") + param.isRange = true; + else + param.isList = true; + if(param.allowableValues != null) { + param.allowableValues.descriptiveValues = []; + for(j = 0; j < param.allowableValues.values; j++){ + var v = param.allowableValues.values[j]; + if(param.defaultValue != null) { + param.allowableValues.descriptiveValues.push ({ + value: String(v), + isDefault: (v === param.defaultValue) + }); + } + else { + param.allowableValues.descriptiveValues.push ({ + value: String(v), + isDefault: false + }); + } + } + } + } + } + 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(type) { + if (type.indexOf('[') >= 0) { + return type.substring(type.indexOf('[') + 1, type.indexOf(']')); + } else { + return void 0; + } +}; + +SwaggerOperation.prototype.getSignature = function(type, models) { + var isPrimitive, listType; + listType = this.isListType(type); + isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; + if (isPrimitive) { + return type; + } else { + if (listType != null) { + return models[listType].getMockSignature(); + } else { + return models[type].getMockSignature(); + } + } +}; + +SwaggerOperation.prototype.getSampleJSON = function(type, models) { + var isPrimitive, listType, val; + 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, 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 = opts; + callback = args; + args = {}; + } + if ((typeof opts) === "function") { + error = callback; + callback = opts; + } + if (error == null) { + error = function(xhr, textStatus, error) { + return log(xhr, textStatus, error); + }; + } + if (callback == null) { + 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) { + params.headers = args.headers; + delete args.headers; + } + + var possibleParams = []; + for(var i = 0; i < this.parameters.length; i++) { + var param = this.parameters[i]; + if(param.paramType === 'header') { + if(args[param.name]) + params.headers[param.name] = args[param.name]; + } + else if(param.paramType === 'form' || param.paramType.toLowerCase() === 'file') + possibleParams.push(param); + } + + if (args.body != null) { + params.body = args.body; + delete args.body; + } + + 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) { + var url = this.resource.basePath + this.pathJson(); + var params = this.parameters; + for(var i = 0; i < params.length; i ++){ + var param = params[i]; + if (param.paramType === 'path') { + if(args[param.name]) { + // apply path params and remove from args + var reg = new RegExp('\{' + param.name + '[^\}]*\}', 'gi'); + url = url.replace(reg, encodeURIComponent(args[param.name])); + delete args[param.name]; + } + else + throw "" + param.name + " is a required path param."; + } + } + + var queryParams = ""; + for(var i = 0; i < params.length; i ++){ + var param = params[i]; + if(param.paramType === 'query') { + if(queryParams !== '') + queryParams += "&"; + queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]); + } + } + if ((queryParams != null) && queryParams.length > 0) + url += '?' + queryParams; + return url; +}; + +SwaggerOperation.prototype.supportHeaderParams = function() { + return this.resource.api.supportHeaderParams; +}; + +SwaggerOperation.prototype.supportedSubmitMethods = function() { + return this.resource.api.supportedSubmitMethods; +}; + +SwaggerOperation.prototype.getQueryParams = function(args) { + return this.getMatchingParams(['query'], args); +}; + +SwaggerOperation.prototype.getHeaderParams = function(args) { + return this.getMatchingParams(['header'], args); +}; + +SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) { + var matchingParams = {}; + var params = this.parameters; + for (var i = 0; i < params.length; i++) { + param = params[i]; + if (args && args[param.name]) + matchingParams[param.name] = args[param.name]; + } + var headers = this.resource.api.headers; + for (name in headers) { + var value = headers[name]; + matchingParams[name] = value; + } + return matchingParams; +}; + +SwaggerOperation.prototype.help = function() { + var msg = ""; + var params = this.parameters; + for (var i = 0; i < params.length; i++) { + var param = params[i]; + if (msg !== "") + msg += "\n"; + msg += "* " + param.name + (param.required ? ' (required)' : '') + " - " + param.description; + } + return msg; +}; + +var SwaggerRequest = function(type, url, params, opts, successCallback, errorCallback, operation, execution) { + var _this = this; + var errors = []; + this.useJQuery = (typeof operation.useJQuery !== 'undefined' ? operation.useJQuery : null); + this.type = (type||errors.push("SwaggerRequest type is required (get/post/put/delete/patch/options).")); + this.url = (url||errors.push("SwaggerRequest url is required.")); + this.params = params; + this.opts = opts; + this.successCallback = (successCallback||errors.push("SwaggerRequest successCallback is required.")); + this.errorCallback = (errorCallback||errors.push("SwaggerRequest error callback is required.")); + this.operation = (operation||errors.push("SwaggerRequest operation is required.")); + this.execution = execution; + this.headers = (params.headers||{}); + + if(errors.length > 0) { + throw errors; + } + + this.type = this.type.toUpperCase(); + + var myHeaders = {}; + 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") + 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]; + } + } + } + + 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 possibleParams = {}; + var values = {}; + + for(var i = 0; i < formParams.length; i++){ + var param = formParams[i]; + values[param.name] = param; + } + + var encoded = ""; + for(key in values) { + value = this.params[key]; + if(encoded !== "") + encoded += "&"; + encoded += encodeURIComponent(key) + '=' + encodeURIComponent(value); + } + body = encoded + } + for (name in this.headers) + myHeaders[name] = this.headers[name]; + if (requestContentType) + 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, + 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); + } + } + }; + var e; + if (typeof window !== 'undefined') { + e = window; + } else { + e = exports; + } + 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 results = []; + if(this.headers) { + for(key in this.headers) { + results.push("--header \"" + key + ": " + this.headers[v] + "\""); + } + } + return "curl " + (results.join(" ")) + " " + this.url; +}; + +/** + * SwaggerHttp is a wrapper for executing requests + */ +var SwaggerHttp = function() {}; + +SwaggerHttp.prototype.execute = function(obj) { + if(obj && (typeof obj.useJQuery === 'boolean')) + this.useJQuery = obj.useJQuery; + else + this.useJQuery = this.isIE8(); + + if(this.useJQuery) + return new JQueryHttpClient().execute(obj); + else + return new ShredHttpClient().execute(obj); +} + +SwaggerHttp.prototype.isIE8 = function() { + var detectedIE = false; + if (typeof navigator !== 'undefined' && navigator.userAgent) { + nav = navigator.userAgent.toLowerCase(); + if (nav.indexOf('msie') !== -1) { + var version = parseInt(nav.split('msie')[1]); + if (version <= 8) { + detectedIE = true; + } + } + } + return detectedIE; +}; + +/* + * JQueryHttpClient lets a browser take advantage of JQuery's cross-browser magic + */ +var JQueryHttpClient = function(options) {} + +JQueryHttpClient.prototype.execute = function(obj) { + var cb = obj.on; + var request = obj; + + obj.type = obj.method; + obj.cache = false; + + obj.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; } }; - SwaggerApi = (function() { - SwaggerApi.prototype.url = "http://api.wordnik.com/v4/resources.json"; + obj.data = obj.body; + obj.complete = function(response, textStatus, opts) { + headers = {}; + headerArray = response.getAllResponseHeaders().split(":"); - SwaggerApi.prototype.debug = false; + for(var i = 0; i < headerArray.length / 2; i++) + headers[headerArray[i] = headerArray[i+1]]; - SwaggerApi.prototype.basePath = null; + out = { + headers: headers, + url: request.url, + method: request.method, + status: response.status, + data: response.responseText, + headers: headers + }; - SwaggerApi.prototype.authorizations = null; + var contentType = (response._headers["content-type"]||response._headers["Content-Type"]||null) - SwaggerApi.prototype.authorizationScheme = null; - - SwaggerApi.prototype.info = null; - - SwaggerApi.prototype.useJQuery = null; - - function SwaggerApi(url, options) { - if (options == null) { - options = {}; - } - if (url) { - if (url.url) { - options = url; - } else { - this.url = url; - } - } else { - options = url; - } - if (options.url != null) { - this.url = options.url; - } - if (options.success != null) { - this.success = options.success; - } - this.failure = options.failure != null ? options.failure : function() {}; - this.progress = options.progress != null ? options.progress : function() {}; - if (options.success != null) { - this.build(); + if(contentType != null) { + if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(response.responseText && response.responseText !== "") + out.obj = JSON.parse(response.responseText); + else + out.obj = {} } } - SwaggerApi.prototype.build = function() { - var e, obj, - _this = this; - this.progress('fetching resource list: ' + this.url); - obj = { - useJQuery: this.useJQuery, - url: this.url, - method: "get", - headers: { - accept: "application/json" - }, - 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 { - 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); - } - } - } - }; - e = {}; - if (typeof window !== 'undefined') { - e = window; - } else { - e = exports; - } - e.authorizations.apply(obj); - new SwaggerHttp().execute(obj); - return this; + if(response.status >= 200 && response.status < 300) + cb.response(out); + else if(response.status === 0 || (response.status >= 400 && response.status < 599)) + cb.error(out); + else + return cb.response(out); + }; + + $.support.cors = true; + return $.ajax(obj); +} + +/* + * ShredHttpClient is a light-weight, node or browser HTTP client + */ +var ShredHttpClient = function(options) { + this.options = (options||{}); + this.isInitialized = false; + + var identity, toString; + + if (typeof window !== 'undefined') { + this.Shred = require("./shred"); + this.content = require("./shred/content"); + } + else + this.Shred = require("shred"); + this.shred = new this.Shred(); +} + +ShredHttpClient.prototype.initShred = function () { + this.isInitialized = true; + this.registerProcessors(this.shred); +} + +ShredHttpClient.prototype.registerProcessors = function(shred) { + var identity = function(x) { + return x; + }; + var toString = function(x) { + return x.toString(); + }; + + if (typeof window !== 'undefined') { + 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 + }); + } +} + +ShredHttpClient.prototype.execute = function(obj) { + if(!this.isInitialized) + this.initShred(); + + var cb = obj.on, res; + + var transform = function(response) { + var out = { + headers: response._headers, + url: response.request.url, + method: response.request.method, + status: response.status, + data: response.content.data }; - 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; - } - } - } - 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; - }; + var contentType = (response._headers["content-type"]||response._headers["Content-Type"]||null) - 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++) { - operation = _ref1[_j]; - isApi = true; - } - } - } - 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) { - return false; - } - _ref = this.apis; - for (resource_name in _ref) { - resource = _ref[resource_name]; - if (resource.ready == null) { - return false; - } - } - this.setConsolidatedModels(); - this.ready = true; - if (this.success != null) { - return this.success(); - } - }; - - SwaggerApi.prototype.fail = function(message) { - this.failure(message); - throw 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) { - resource = _ref[resource_name]; - for (modelName in resource.models) { - if (this.models[modelName] == null) { - this.models[modelName] = resource.models[modelName]; - this.modelsArray.push(resource.models[modelName]); - } - } - } - _ref1 = this.modelsArray; - _results = []; - for (_i = 0, _len = _ref1.length; _i < _len; _i++) { - model = _ref1[_i]; - _results.push(model.setReferencedModels(this.models)); - } - return _results; - }; - - 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]; - log(resource_name); - _ref1 = resource.operations; - for (operation_name in _ref1) { - operation = _ref1[operation_name]; - log(" " + operation.nickname); - _ref2 = operation.parameters; - for (_i = 0, _len = _ref2.length; _i < _len; _i++) { - parameter = _ref2[_i]; - log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); - } - } - } - return this; - }; - - return SwaggerApi; - - })(); - - SwaggerResource = (function() { - SwaggerResource.prototype.api = null; - - SwaggerResource.prototype.produces = null; - - SwaggerResource.prototype.consumes = null; - - function SwaggerResource(resourceObj, api) { - 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; - this.operations = {}; - this.operationsArray = []; - this.modelsArray = []; - this.models = {}; - 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."); - } - 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); - obj = { - url: this.url, - method: "get", - useJQuery: this.useJQuery, - headers: { - accept: "application/json" - }, - 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); - } - } - }; - e = {}; - if (typeof window !== 'undefined') { - e = window; - } else { - e = exports; - } - e.authorizations.apply(obj); - new SwaggerHttp().execute(obj); + if(contentType != null) { + if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(response.content.data && response.content.data !== "") + out.obj = JSON.parse(response.content.data); + else + out.obj = {} } } + return out; + }; - 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) { - 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++) { - model = _ref[_i]; - _results.push(model.setReferencedModels(this.models)); - } - return _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 = this.consumes; - produces = this.produces; - if (o.consumes != null) { - consumes = o.consumes; - } else { - consumes = this.consumes; - } - 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 + "]"; - } - responseMessages = o.responseMessages; - method = o.method; - if (o.httpMethod) { - method = o.httpMethod; - } - if (o.supportedContentTypes) { - consumes = o.supportedContentTypes; - } - 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 msg, operation, operation_name, parameter, _i, _len, _ref, _ref1, _results; - _ref = this.operations; - _results = []; - for (operation_name in _ref) { - operation = _ref[operation_name]; - msg = " " + operation.nickname; - _ref1 = operation.parameters; - for (_i = 0, _len = _ref1.length; _i < _len; _i++) { - parameter = _ref1[_i]; - msg.concat(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description); - } - _results.push(msg); - } - return _results; - }; - - return SwaggerResource; - - })(); - - SwaggerModel = (function() { - function SwaggerModel(modelName, obj) { - var prop, propertyName, value; - this.name = obj.id != null ? obj.id : modelName; - this.properties = []; - for (propertyName in obj.properties) { - 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); - } + res = { + error: function(response) { + if (obj) + return cb.error(transform(response)); + }, + redirect: function(response) { + if (obj) + return cb.redirect(transform(response)); + }, + 307: function(response) { + if (obj) + return cb.redirect(transform(response)); + }, + response: function(response) { + if (obj) + return cb.response(transform(response)); } - - SwaggerModel.prototype.setReferencedModels = function(allModels) { - var prop, type, _i, _len, _ref, _results; - _ref = this.properties; - _results = []; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - prop = _ref[_i]; - 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); - } - } - return _results; - }; - - SwaggerModel.prototype.getMockSignature = function(modelsToIgnore) { - var classClose, classOpen, prop, propertiesStr, returnVal, strong, strongClose, stronger, _i, _j, _len, _len1, _ref, _ref1; - propertiesStr = []; - _ref = this.properties; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - prop = _ref[_i]; - propertiesStr.push(prop.toString()); - } - strong = ''; - stronger = ''; - strongClose = ''; - classOpen = strong + this.name + ' {' + strongClose; - classClose = strong + '}' + strongClose; - returnVal = classOpen + '
' + propertiesStr.join(',
') + '
' + classClose; - if (!modelsToIgnore) { - modelsToIgnore = []; - } - 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) { - returnVal = returnVal + ('
' + prop.refModel.getMockSignature(modelsToIgnore)); - } - } - return returnVal; - }; - - SwaggerModel.prototype.createJSONSample = function(modelsToIgnore) { - var prop, result, _i, _len, _ref; - result = {}; - modelsToIgnore = modelsToIgnore || []; - modelsToIgnore.push(this.name); - _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 || 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) { - this.refDataType = obj.items.type; - } - if (obj.items.$ref != null) { - this.refDataType = obj.items.$ref; - } - } - this.dataTypeWithRef = this.refDataType != null ? this.dataType + '[' + this.refDataType + ']' : this.dataType; - if (obj.allowableValues != null) { - this.valueType = obj.allowableValues.valueType; - 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[this.refModel.name] === 'undefined')) { - result = this.refModel.createJSONSample(modelsToIgnore); - } else { - if (this.isCollection) { - result = this.toSampleValue(this.refDataType); - } else { - result = this.toSampleValue(this.dataType); - } - } - if (this.isCollection) { - return [result]; - } else { - return result; - } - }; - - SwaggerModelProperty.prototype.toSampleValue = function(value) { - var result; - if (value === "integer") { - result = 0; - } else if (value === "boolean") { - result = false; - } else if (value === "double") { - result = 0.0; - } else if (value === "string") { - result = ""; - } else { - result = value; - } - return result; - }; - - SwaggerModelProperty.prototype.toString = function() { - var req, str; - req = this.required ? 'propReq' : 'propOpt'; - str = '' + this.name + ' (' + this.dataTypeWithRef + ''; - if (!this.required) { - str += ', optional'; - } - str += ')'; - if (this.values != null) { - str += " = ['" + this.values.join("' or '") + "']"; - } - if (this.descr != null) { - str += ': ' + this.descr + ''; - } - return str; - }; - - return SwaggerModelProperty; - - })(); - - SwaggerOperation = (function() { - 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.method = method; - this.parameters = parameters != null ? parameters : []; - this.summary = summary; - this.notes = notes; - 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.method == null) { - this.resource.api.fail("SwaggerOperation " + nickname + " is missing method."); - } - this.path = this.path.replace('{format}', 'json'); - this.method = this.method.toLowerCase(); - this.isGetMethod = this.method === "get"; - this.resourceName = this.resource.name; - if (((_ref = this.type) != null ? _ref.toLowerCase() : void 0) === 'void') { - this.type = void 0; - } - if (this.type != null) { - this.responseClassSignature = this.getSignature(this.type, this.resource.models); - this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models); - } - this.responseMessages = this.responseMessages || []; - _ref1 = this.parameters; - for (_i = 0, _len = _ref1.length; _i < _len; _i++) { - parameter = _ref1[_i]; - parameter.name = parameter.name || parameter.type || parameter.dataType; - type = parameter.type || parameter.dataType; - if (type.toLowerCase() === 'boolean') { - parameter.allowableValues = {}; - parameter.allowableValues.values = ["true", "false"]; - } - 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 = []; - _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 - }); - } else { - parameter.allowableValues.descriptiveValues.push({ - value: v, - isDefault: false - }); - } - } - } - } - } - 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(type) { - if (type.indexOf('[') >= 0) { - return type.substring(type.indexOf('[') + 1, type.indexOf(']')); - } else { - return void 0; - } - }; - - SwaggerOperation.prototype.getSignature = function(type, models) { - var isPrimitive, listType; - listType = this.isListType(type); - isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; - if (isPrimitive) { - return type; - } else { - if (listType != null) { - return models[listType].getMockSignature(); - } else { - return models[type].getMockSignature(); - } - } - }; - - SwaggerOperation.prototype.getSampleJSON = function(type, models) { - var isPrimitive, listType, val; - 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, 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 = opts; - callback = args; - args = {}; - } - if ((typeof opts) === "function") { - error = callback; - callback = opts; - } - if (error == null) { - error = function(xhr, textStatus, error) { - return log(xhr, textStatus, error); - }; - } - if (callback == null) { - 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) { - 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) { - params.body = args.body; - delete args.body; - } - 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) { - 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') { - if (args[param.name]) { - reg = new RegExp('\{' + param.name + '[^\}]*\}', 'gi'); - url = url.replace(reg, encodeURIComponent(args[param.name])); - delete args[param.name]; - } else { - throw "" + param.name + " is a required path param."; - } - } - } - 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 ((queryParams != null) && queryParams.length > 0) { - url += "?" + queryParams; - } - return url; - }; - - SwaggerOperation.prototype.supportHeaderParams = function() { - return this.resource.api.supportHeaderParams; - }; - - SwaggerOperation.prototype.supportedSubmitMethods = function() { - return this.resource.api.supportedSubmitMethods; - }; - - SwaggerOperation.prototype.getQueryParams = function(args) { - return this.getMatchingParams(['query'], args); - }; - - SwaggerOperation.prototype.getHeaderParams = function(args) { - return this.getMatchingParams(['header'], args); - }; - - 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 (args && args[param.name]) { - matchingParams[param.name] = args[param.name]; - } - } - _ref1 = this.resource.api.headers; - for (name in _ref1) { - value = _ref1[name]; - matchingParams[name] = value; - } - return matchingParams; - }; - - SwaggerOperation.prototype.help = function() { - var msg, parameter, _i, _len, _ref; - msg = ""; - _ref = this.parameters; - for (_i = 0, _len = _ref.length; _i < _len; _i++) { - parameter = _ref[_i]; - if (msg !== "") { - msg += "\n"; - } - msg += "* " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description; - } - return msg; - }; - - return SwaggerOperation; - - })(); - - SwaggerRequest = (function() { - 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.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."; - } - if (this.successCallback == null) { - throw "SwaggerRequest successCallback is required."; - } - if (this.errorCallback == null) { - throw "SwaggerRequest error callback is required."; - } - if (this.operation == null) { - throw "SwaggerRequest operation is required."; - } - 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 (typeof type !== 'undefined' && 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; - } - } - 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]; - } - } - } - 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 = { - url: this.url, - 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); - } - } - }; - e = {}; - if (typeof window !== 'undefined') { - e = window; - } else { - e = exports; - } - 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; - header_args = (function() { - var _ref, _results; - _ref = this.headers; - _results = []; - for (k in _ref) { - v = _ref[k]; - _results.push("--header \"" + k + ": " + v + "\""); - } - return _results; - }).call(this); - return "curl " + (header_args.join(" ")) + " " + this.url; - }; - - return SwaggerRequest; - - })(); - - SwaggerHttp = (function() { - function SwaggerHttp() {} - - SwaggerHttp.prototype.Shred = null; - - SwaggerHttp.prototype.shred = null; - - SwaggerHttp.prototype.content = null; - - 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 - }); - } - }; - - 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(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.error(out); - } - }, - redirect: 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.redirect(out); - } - }, - 307: 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.redirect(out); - } - }, - 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); + }; + if (obj) { + obj.on = res; + } + return this.shred.request(obj); +}; + +/** + * SwaggerAuthorizations applys the correct authorization to an operation being executed + */ +var SwaggerAuthorizations = function() { + 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) { + status = null; + for (key in this.authz) { + value = this.authz[key]; + result = value.apply(obj, authorizations); + if (result === false) + status = false; + if (result === true) + status = true; + } + return status; +}; + +/** + * ApiKeyAuthorization allows a query param or header to be injected + */ +var ApiKeyAuthorization = function(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; + } +}; + +/** + * Password Authorization is a basic auth implementation + */ +var PasswordAuthorization = function(name, username, password) { + this.name = name; + this.username = username; + this.password = password; + this._btoa = null; + + if (typeof window !== 'undefined') + return this._btoa = btoa; + else + return this._btoa = require("btoa"); +}; + +PasswordAuthorization.prototype.apply = function(obj, authorizations) { + obj.headers["Authorization"] = "Basic " + this._btoa(this.username + ":" + this.password); + return true; +}; + +var e = (typeof window !== 'undefined' ? window : exports); + +e.SwaggerHttp = SwaggerHttp; +e.SwaggerRequest = SwaggerRequest; +e.authorizations = new SwaggerAuthorizations(); +e.JQueryHttpClient = JQueryHttpClient; +e.ShredHttpClient = ShredHttpClient; +e.SwaggerOperation = SwaggerOperation; +e.SwaggerModel = SwaggerModel; +e.SwaggerModelProperty = SwaggerModelProperty; +e.SwaggerResource = SwaggerResource; +e.SwaggerApi = SwaggerApi; diff --git a/lib/swagger.js b/lib/swagger.js index e2e1058a..7708103d 100644 --- a/lib/swagger.js +++ b/lib/swagger.js @@ -1,5 +1,5 @@ // swagger.js -// version 2.0.13 +// version 2.0.14 var __bind = function(fn, me){ return function(){ @@ -350,8 +350,8 @@ SwaggerResource.prototype.addModels = function(models) { } } var output = []; - for (var i = 0; i < this.modelsArray; i++) { - model = this.modelsArray[_i]; + for (var i = 0; i < this.modelsArray.length; i++) { + model = this.modelsArray[i]; output.push(model.setReferencedModels(this.models)); } return output; @@ -450,7 +450,7 @@ var SwaggerModel = function(modelName, obj) { SwaggerModel.prototype.setReferencedModels = function(allModels) { var results = []; - for (i = 0; i < this.properties.length; i++) { + for (var i = 0; i < this.properties.length; i++) { var property = this.properties[i]; var type = property.type || property.dataType; if (allModels[type] != null) @@ -465,7 +465,7 @@ SwaggerModel.prototype.setReferencedModels = function(allModels) { SwaggerModel.prototype.getMockSignature = function(modelsToIgnore) { var propertiesStr = []; - for (i = 0; i < this.properties.length; i++) { + for (var i = 0; i < this.properties.length; i++) { prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -480,7 +480,7 @@ SwaggerModel.prototype.getMockSignature = function(modelsToIgnore) { modelsToIgnore = []; modelsToIgnore.push(this); - for (i = 0; i < this.properties.length; i++) { + for (var i = 0; i < this.properties.length; i++) { prop = this.properties[i]; if ((prop.refModel != null) && modelsToIgnore.indexOf(prop.refModel) === -1) { returnVal = returnVal + ('
' + prop.refModel.getMockSignature(modelsToIgnore)); @@ -493,7 +493,7 @@ SwaggerModel.prototype.createJSONSample = function(modelsToIgnore) { var result = {}; var modelsToIgnore = (modelsToIgnore||[]) modelsToIgnore.push(this.name); - for (i = 0; i < this.properties.length; i++) { + for (var i = 0; i < this.properties.length; i++) { prop = this.properties[i]; result[prop.name] = prop.getSampleValue(modelsToIgnore); } @@ -534,7 +534,7 @@ var SwaggerModelProperty = function(name, obj) { SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) { var result; - if ((this.refModel != null) && (modelsToIgnore[this.refModel.name] === 'undefined')) { + if ((this.refModel != null) && (modelsToIgnore[this.refModel.name] === undefined)) { result = this.refModel.createJSONSample(modelsToIgnore); } else { if (this.isCollection) { @@ -615,8 +615,8 @@ var SwaggerOperation = function(nickname, path, method, parameters, summary, not this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models); } - for(i = 0; i < parameters.length; i ++) { - var param = parameters[i]; + for(var i = 0; i < this.parameters.length; i ++) { + var param = this.parameters[i]; // might take this away param.name = param.name || param.type || param.dataType; @@ -763,7 +763,7 @@ SwaggerOperation.prototype["do"] = function(args, opts, callback, error) { } var possibleParams = []; - for(i = 0; i < this.parameters.length; i++) { + for(var i = 0; i < this.parameters.length; i++) { var param = this.parameters[i]; if(param.paramType === 'header') { if(args[param.name]) @@ -806,7 +806,7 @@ SwaggerOperation.prototype.pathXml = function() { SwaggerOperation.prototype.urlify = function(args) { var url = this.resource.basePath + this.pathJson(); var params = this.parameters; - for(i = 0; i < params.length; i ++){ + for(var i = 0; i < params.length; i ++){ var param = params[i]; if (param.paramType === 'path') { if(args[param.name]) { @@ -821,7 +821,7 @@ SwaggerOperation.prototype.urlify = function(args) { } var queryParams = ""; - for(i = 0; i < params.length; i ++){ + for(var i = 0; i < params.length; i ++){ var param = params[i]; if(param.paramType === 'query') { if(queryParams !== '') @@ -853,7 +853,7 @@ SwaggerOperation.prototype.getHeaderParams = function(args) { SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) { var matchingParams = {}; var params = this.parameters; - for (i = 0; i < params.length; i++) { + for (var i = 0; i < params.length; i++) { param = params[i]; if (args && args[param.name]) matchingParams[param.name] = args[param.name]; @@ -869,7 +869,7 @@ SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) { SwaggerOperation.prototype.help = function() { var msg = ""; var params = this.parameters; - for (i = 0; i < params.length; i++) { + for (var i = 0; i < params.length; i++) { var param = params[i]; if (msg !== "") msg += "\n"; @@ -908,7 +908,7 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal var params = this.operation.parameters; - for(i = 0; i < params.length; i++) { + for(var i = 0; i < params.length; i++) { var param = params[i]; if(param.paramType === "form") formParams.push(param); @@ -958,7 +958,7 @@ var SwaggerRequest = function(type, url, params, opts, successCallback, errorCal var possibleParams = {}; var values = {}; - for(i = 0; i < formParams.length; i++){ + for(var i = 0; i < formParams.length; i++){ var param = formParams[i]; values[param.name] = param; } @@ -1095,7 +1095,7 @@ JQueryHttpClient.prototype.execute = function(obj) { headers = {}; headerArray = response.getAllResponseHeaders().split(":"); - for(i = 0; i < headerArray.length / 2; i++) + for(var i = 0; i < headerArray.length / 2; i++) headers[headerArray[i] = headerArray[i+1]]; out = { @@ -1107,9 +1107,15 @@ JQueryHttpClient.prototype.execute = function(obj) { headers: headers }; - if(response._headers["Content-Type"] && response._headers["Content-Type"].indexOf("application/json") == 0 && - response.content.data && response.content.data !== "") { - out.obj = JSON.parse(response.responseText); + var contentType = (response._headers["content-type"]||response._headers["Content-Type"]||null) + + if(contentType != null) { + if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(response.responseText && response.responseText !== "") + out.obj = JSON.parse(response.responseText); + else + out.obj = {} + } } if(response.status >= 200 && response.status < 300) @@ -1186,7 +1192,7 @@ ShredHttpClient.prototype.execute = function(obj) { var contentType = (response._headers["content-type"]||response._headers["Content-Type"]||null) if(contentType != null) { - if(contentType.indexOf("application/json") == 0) { + if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { if(response.content.data && response.content.data !== "") out.obj = JSON.parse(response.content.data); else diff --git a/package.json b/package.json index a7ca031b..0da87c4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "swagger-ui", - "version": "2.0.7", + "version": "2.0.8", "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", @@ -19,7 +19,7 @@ "readmeFilename": "README.md", "dependencies": { "coffee-script": "~1.5.0", - "swagger-client": "2.0.13", + "swagger-client": "2.0.14", "handlebars": "~1.0.10", "less": "~1.4.2" }