diff --git a/dist/lib/swagger-client.js b/dist/lib/swagger-client.js index 3b2c6e44..07117fb8 100644 --- a/dist/lib/swagger-client.js +++ b/dist/lib/swagger-client.js @@ -1,6 +1,6 @@ /** * swagger-client - swagger.js is a javascript client for use with swaggering APIs. - * @version v2.1.0-alpha.5 + * @version v2.1.0-alpha.6 * @link http://swagger.io * @license apache 2.0 */ @@ -9,9 +9,7 @@ var ArrayModel = function(definition) { this.name = "name"; this.definition = definition || {}; this.properties = []; - this.type; - this.ref; - + var requiredFields = definition.enum || []; var items = definition.items; if(items) { @@ -20,7 +18,7 @@ var ArrayModel = function(definition) { this.type = typeFromJsonSchema(type.type, type.format); } else { - this.ref = items['$ref']; + this.ref = items.$ref; } } }; @@ -157,7 +155,7 @@ var PasswordAuthorization = function(name, username, password) { PasswordAuthorization.prototype.apply = function(obj, authorizations) { var base64encoder = this._btoa; - obj.headers["Authorization"] = "Basic " + base64encoder(this.username + ":" + this.password); + obj.headers.Authorization = "Basic " + base64encoder(this.username + ":" + this.password); return true; }; var __bind = function(fn, me){ @@ -217,7 +215,6 @@ var PrimitiveModel = function(definition) { this.name = "name"; this.definition = definition || {}; this.properties = []; - this.type; var requiredFields = definition.enum || []; this.type = typeFromJsonSchema(definition.type, definition.format); @@ -235,9 +232,9 @@ PrimitiveModel.prototype.getSampleValue = function() { PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) { var propertiesStr = []; - var i; + var i, prop; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -251,10 +248,9 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) { if (!modelsToIgnore) modelsToIgnore = {}; modelsToIgnore[this.name] = this; - var i; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; - var ref = prop['$ref']; + prop = this.properties[i]; + var ref = prop.$ref; var model = models[ref]; if (model && typeof modelsToIgnore[ref] === 'undefined') { returnVal = returnVal + ('
' + model.getMockSignature(modelsToIgnore)); @@ -262,6 +258,9 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) { } return returnVal; }; +/** + * Provides support for 1.x versions of swagger + */ var SwaggerApi = function (url, options) { this.isBuilt = false; this.url = null; @@ -272,7 +271,7 @@ var SwaggerApi = function (url, options) { this.info = null; this.useJQuery = false; this.modelsArray = []; - this.isValid; + this.isValid = false; options = (options || {}); if (url) @@ -283,14 +282,14 @@ var SwaggerApi = function (url, options) { else options = url; - if (options.url != null) + if (typeof options.url === 'string') this.url = options.url; this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json;charset=utf-8,*/*'; this.defaultSuccessCallback = options.defaultSuccessCallback || null; this.defaultErrorCallback = options.defaultErrorCallback || null; - if (options.success != null) + if (typeof options.success === 'function') this.success = options.success; if (typeof options.useJQuery === 'boolean') @@ -304,9 +303,9 @@ var SwaggerApi = function (url, options) { } this.supportedSubmitMethods = options.supportedSubmitMethods || []; - this.failure = options.failure != null ? options.failure : function () { }; - this.progress = options.progress != null ? options.progress : function () { }; - if (options.success != null) { + this.failure = typeof options.failure === 'function' ? options.failure : function () { }; + this.progress = typeof options.progress === 'function' ? options.progress : function () { }; + if (typeof options.success === 'function') { this.build(); this.isBuilt = true; } @@ -368,8 +367,7 @@ SwaggerApi.prototype.buildFromSpec = function (response) { if (response.info != null) { this.info = response.info; } - var isApi = false; - var i; + var isApi = false, i, res; for (i = 0; i < response.apis.length; i++) { var api = response.apis[i]; if (api.operations) { @@ -390,20 +388,20 @@ SwaggerApi.prototype.buildFromSpec = function (response) { if (isApi) { var newName = response.resourcePath.replace(/\//g, ''); this.resourcePath = response.resourcePath; - var res = new SwaggerResource(response, this); + res = new SwaggerResource(response, this); this.apis[newName] = res; this.apisArray.push(res); } else { var k; for (k = 0; k < response.apis.length; k++) { var resource = response.apis[k]; - var res = new SwaggerResource(resource, this); + res = new SwaggerResource(resource, this); this.apis[res.name] = res; this.apisArray.push(res); } } this.isValid = true; - if (this.success) { + if (typeof this.success === 'function') { this.success(); } return this; @@ -419,7 +417,7 @@ SwaggerApi.prototype.buildFrom1_1Spec = function (response) { if (response.info != null) { this.info = response.info; } - var isApi = false; + var isApi = false, res; for (var i = 0; i < response.apis.length; i++) { var api = response.apis[i]; if (api.operations) { @@ -439,13 +437,13 @@ SwaggerApi.prototype.buildFrom1_1Spec = function (response) { if (isApi) { var newName = response.resourcePath.replace(/\//g, ''); this.resourcePath = response.resourcePath; - var res = new SwaggerResource(response, this); + 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]; - var res = new SwaggerResource(resource, this); + res = new SwaggerResource(resource, this); this.apis[res.name] = res; this.apisArray.push(res); } @@ -459,19 +457,19 @@ SwaggerApi.prototype.buildFrom1_1Spec = function (response) { SwaggerApi.prototype.selfReflect = function () { var resource, resource_name, ref; - if (this.apis == null) { + if (this.apis === null) { return false; } ref = this.apis; for (resource_name in ref) { resource = ref[resource_name]; - if (resource.ready == null) { + if (resource.ready === null) { return false; } } this.setConsolidatedModels(); this.ready = true; - if (this.success != null) { + if (typeof this.success === 'function') { return this.success(); } }; @@ -488,7 +486,7 @@ SwaggerApi.prototype.setConsolidatedModels = function () { for (resource_name in _ref) { resource = _ref[resource_name]; for (modelName in resource.models) { - if (this.models[modelName] == null) { + if (typeof this.models[modelName] === 'undefined') { this.models[modelName] = resource.models[modelName]; this.modelsArray.push(resource.models[modelName]); } @@ -527,7 +525,7 @@ var SwaggerResource = function (resourceObj, api) { var _this = this; this.api = api; this.swaggerRequstHeaders = api.swaggerRequstHeaders; - this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path; + this.path = (typeof this.api.resourcePath === 'string') ? this.api.resourcePath : resourceObj.path; this.description = resourceObj.description; this.authorizations = (resourceObj.authorizations || {}); @@ -539,12 +537,12 @@ var SwaggerResource = function (resourceObj, api) { this.modelsArray = []; this.models = {}; this.rawModels = {}; - this.useJQuery = (typeof api.useJQuery !== 'undefined' ? api.useJQuery : null); + this.useJQuery = (typeof api.useJQuery !== 'undefined') ? api.useJQuery : null; - if ((resourceObj.apis != null) && (this.api.resourcePath != null)) { + if ((resourceObj.apis) && this.api.resourcePath) { this.addApiDeclaration(resourceObj); } else { - if (this.path == null) { + if (typeof this.path === 'undefined') { this.api.fail('SwaggerResources must have a path.'); } if (this.path.substring(0, 4) === 'http') { @@ -593,7 +591,7 @@ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) { return rootUrl + relativeBasePath; } else { - var pos = this.basePath.lastIndexOf('/'); + pos = this.basePath.lastIndexOf('/'); var base = this.basePath.substring(0, pos); if (base.substring(base.length - 1) == '/') return base + relativeBasePath; @@ -603,11 +601,11 @@ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) { }; SwaggerResource.prototype.addApiDeclaration = function (response) { - if (response.produces != null) + if (typeof response.produces === 'string') this.produces = response.produces; - if (response.consumes != null) + if (typeof response.consumes === 'string') this.consumes = response.consumes; - if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) + if ((typeof response.basePath === 'string') && response.basePath.replace(/\s/g, '').length > 0) this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath; this.addModels(response.models); @@ -623,10 +621,10 @@ SwaggerResource.prototype.addApiDeclaration = function (response) { }; SwaggerResource.prototype.addModels = function (models) { - if (models != null) { + if (typeof models === 'object') { var modelName; for (modelName in models) { - if (this.models[modelName] == null) { + if (typeof this.models[modelName] === 'undefined') { var swaggerModel = new SwaggerModel(modelName, models[modelName]); this.modelsArray.push(swaggerModel); this.models[modelName] = swaggerModel; @@ -649,12 +647,12 @@ SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes var o = ops[i]; consumes = this.consumes; produces = this.produces; - if (o.consumes != null) + if (typeof o.consumes !== 'undefined') consumes = o.consumes; else consumes = this.consumes; - if (o.produces != null) + if (typeof o.produces !== 'undefined') produces = o.produces; else produces = this.produces; @@ -663,7 +661,7 @@ SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes if (type === 'array') { ref = null; if (o.items) - ref = o.items['type'] || o.items['$ref']; + ref = o.items.type || o.items.$ref; type = 'array[' + ref + ']'; } var responseMessages = o.responseMessages; @@ -701,11 +699,11 @@ SwaggerResource.prototype.sanitize = function (nickname) { }; var SwaggerModel = function (modelName, obj) { - this.name = obj.id != null ? obj.id : modelName; + this.name = typeof obj.id !== 'undefined' ? obj.id : modelName; this.properties = []; var propertyName; for (propertyName in obj.properties) { - if (obj.required != null) { + if (obj.required) { var value; for (value in obj.required) { if (propertyName === obj.required[value]) { @@ -723,9 +721,9 @@ SwaggerModel.prototype.setReferencedModels = function (allModels) { for (var i = 0; i < this.properties.length; i++) { var property = this.properties[i]; var type = property.type || property.dataType; - if (allModels[type] != null) + if (allModels[type]) results.push(property.refModel = allModels[type]); - else if ((property.refDataType != null) && (allModels[property.refDataType] != null)) + else if ((property.refDataType) && (allModels[property.refDataType])) results.push(property.refModel = allModels[property.refDataType]); else results.push(void 0); @@ -734,9 +732,9 @@ SwaggerModel.prototype.setReferencedModels = function (allModels) { }; SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) { - var propertiesStr = []; - for (var i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + var i, prop, propertiesStr = []; + for (i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -749,9 +747,9 @@ SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) { modelsToIgnore = []; modelsToIgnore.push(this.name); - for (var i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; - if ((prop.refModel != null) && modelsToIgnore.indexOf(prop.refModel.name) === -1) { + for (i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; + if ((prop.refModel) && modelsToIgnore.indexOf(prop.refModel.name) === -1) { returnVal = returnVal + ('
' + prop.refModel.getMockSignature(modelsToIgnore)); } } @@ -764,7 +762,7 @@ SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) { } else { var result = {}; - var modelsToIgnore = (modelsToIgnore || []); + modelsToIgnore = (modelsToIgnore || []); modelsToIgnore.push(this.name); for (var i = 0; i < this.properties.length; i++) { var prop = this.properties[i]; @@ -777,31 +775,31 @@ SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) { var SwaggerModelProperty = function (name, obj, model) { this.name = name; - this.dataType = obj.type || obj.dataType || obj['$ref']; + 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; this.defaultValue = applyModelPropertyMacro(obj, model); - if (obj.items != null) { - if (obj.items.type != null) { + if (obj.items) { + if (obj.items.type) { this.refDataType = obj.items.type; } - if (obj.items.$ref != null) { + if (obj.items.$ref) { this.refDataType = obj.items.$ref; } } - this.dataTypeWithRef = this.refDataType != null ? (this.dataType + '[' + this.refDataType + ']') : this.dataType; - if (obj.allowableValues != null) { + this.dataTypeWithRef = this.refDataType ? (this.dataType + '[' + this.refDataType + ']') : this.dataType; + if (obj.allowableValues) { this.valueType = obj.allowableValues.valueType; this.values = obj.allowableValues.values; - if (this.values != null) { + if (this.values) { this.valuesString = '\'' + this.values.join('\' or \'') + '\''; } } - if (obj['enum'] != null) { + if (obj['enum']) { this.valueType = 'string'; this.values = obj['enum']; - if (this.values != null) { + if (this.values) { this.valueString = '\'' + this.values.join('\' or \'') + '\''; } } @@ -809,7 +807,7 @@ var SwaggerModelProperty = function (name, obj, model) { SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) { var result; - if ((this.refModel != null) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) { + if ((this.refModel) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) { result = this.refModel.createJSONSample(modelsToIgnore); } else { if (this.isCollection) { @@ -827,7 +825,7 @@ SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) { SwaggerModelProperty.prototype.toSampleValue = function (value) { var result; - if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) { + if ((typeof this.defaultValue !== 'undefined') && this.defaultValue) { result = this.defaultValue; } else if (value === 'integer') { result = 0; @@ -850,10 +848,10 @@ SwaggerModelProperty.prototype.toString = function () { str += ', optional'; } str += ')'; - if (this.values != null) { + if (this.values) { str += ' = ["' + this.values.join('\' or \'') + '\']'; } - if (this.descr != null) { + if (this.descr) { str += ': ' + this.descr + ''; } return str; @@ -866,7 +864,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no 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.parameters = parameters ? parameters : []; this.summary = summary; this.notes = notes; this.type = type; @@ -887,6 +885,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.method = this.method.toLowerCase(); this.isGetMethod = this.method === 'GET'; + var i, j, v; this.resourceName = this.resource.name; if (typeof this.type !== 'undefined' && this.type === 'void') this.type = null; @@ -895,13 +894,13 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models); } - for (var i = 0; i < this.parameters.length; i++) { + for (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; + type = param.type || param.dataType; if (type === 'array') { type = 'array[' + (param.items.$ref ? param.items.$ref : param.items.type) + ']'; } @@ -915,14 +914,14 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no param.sampleJSON = this.getSampleJSON(type, this.resource.models); var enumValue = param['enum']; - if (enumValue != null) { + if (typeof enumValue !== 'undefined') { param.isList = true; param.allowableValues = {}; param.allowableValues.descriptiveValues = []; - for (var j = 0; j < enumValue.length; j++) { - var v = enumValue[j]; - if (param.defaultValue != null) { + for (j = 0; j < enumValue.length; j++) { + v = enumValue[j]; + if (param.defaultValue) { param.allowableValues.descriptiveValues.push({ value: String(v), isDefault: (v === param.defaultValue) @@ -944,8 +943,8 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no if (param.allowableValues != null) { param.allowableValues.descriptiveValues = []; if (param.allowableValues.values) { - for (var j = 0; j < param.allowableValues.values.length; j++) { - var v = param.allowableValues.values[j]; + for (j = 0; j < param.allowableValues.values.length; j++) { + v = param.allowableValues.values[j]; if (param.defaultValue != null) { param.allowableValues.descriptiveValues.push({ value: String(v), @@ -969,13 +968,15 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.resource[this.nickname] = function (args, opts, callback, error) { var arg1, arg2, arg3, arg4; - if(typeof args === 'function') // right shift 3 - arg1 = {}, arg2 = {}, arg3 = args, arg4 = opts; - else if(typeof args === 'object' && typeof opts === 'function') // right shift 2 - arg1 = args, arg2 = {}, arg3 = opts, arg4 = callback; - else - arg1 = args, arg2 = opts, arg3 = callback, arg4 = error; - + if(typeof args === 'function') { // right shift 3 + arg1 = {}; arg2 = {}; arg3 = args; arg4 = opts; + } + else if(typeof args === 'object' && typeof opts === 'function') { // right shift 2 + arg1 = args; arg2 = {}; arg3 = opts; arg4 = callback; + } + else { + arg1 = args; arg2 = opts; arg3 = callback; arg4 = error; + } return _this['do'](arg1 || {}, arg2 || {}, arg3 || defaultSuccessCallback, arg4 || defaultErrorCallback); }; @@ -985,7 +986,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.resource[this.nickname].asCurl = function (args) { return _this.asCurl(args); }; -} +}; SwaggerOperation.prototype.isListType = function (type) { if (type && type.indexOf('[') >= 0) { @@ -998,11 +999,11 @@ SwaggerOperation.prototype.isListType = function (type) { SwaggerOperation.prototype.getSignature = function (type, models) { var isPrimitive, listType; listType = this.isListType(type); - isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; + isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true; if (isPrimitive) { return type; } else { - if (listType != null) { + if (typeof listType !== 'undefined') { return models[listType].getMockSignature(); } else { return models[type].getMockSignature(); @@ -1013,7 +1014,7 @@ SwaggerOperation.prototype.getSignature = function (type, models) { SwaggerOperation.prototype.getSampleJSON = function (type, models) { var isPrimitive, listType, val; listType = this.isListType(type); - isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; + isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true; val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample()); if (val) { val = listType ? [val] : val; @@ -1037,15 +1038,15 @@ SwaggerOperation.prototype.getSampleJSON = function (type, models) { }; SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { - var key, param, params, possibleParams, req, value; + var key, param, params, possibleParams = [], req, value; - if (error == null) { + if (typeof error !== 'function') { error = function (xhr, textStatus, error) { return log(xhr, textStatus, error); }; } - if (callback == null) { + if (typeof callback !== 'function') { callback = function (response) { var content; content = null; @@ -1057,6 +1058,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { return log('default callback: ' + content); }; } + params = {}; params.headers = []; if (args.headers != null) { @@ -1068,12 +1070,11 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { params.headers['Content-Type'] = opts.responseContentType; } if(opts && opts.requestContentType) { - params.headers['Accept'] = opts.requestContentType; + params.headers.Accept = opts.requestContentType; } - var possibleParams = []; for (var i = 0; i < this.parameters.length; i++) { - var param = this.parameters[i]; + param = this.parameters[i]; if (param.paramType === 'header') { if (typeof args[param.name] !== 'undefined') params.headers[param.name] = args[param.name]; @@ -1088,22 +1089,22 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { } } - if (args.body != null) { + if (typeof args.body !== 'undefined') { params.body = args.body; delete args.body; } if (possibleParams) { - var key; for (key in possibleParams) { - var value = possibleParams[key]; + 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) { + if (opts.mock) { return req; } else { return true; @@ -1135,10 +1136,11 @@ SwaggerOperation.prototype.encodePathParam = function (pathParam) { }; SwaggerOperation.prototype.urlify = function (args) { + var i, j, param; var url = this.resource.basePath + this.pathJson(); var params = this.parameters; - for (var i = 0; i < params.length; i++) { - var param = params[i]; + for (i = 0; i < params.length; i++) { + param = params[i]; if (param.paramType === 'path') { if (typeof args[param.name] !== 'undefined') { // apply path params and remove from args @@ -1152,13 +1154,12 @@ SwaggerOperation.prototype.urlify = function (args) { } var queryParams = ''; - for (var i = 0; i < params.length; i++) { - var param = params[i]; + for (i = 0; i < params.length; i++) { + param = params[i]; if(param.paramType === 'query') { if (queryParams !== '') queryParams += '&'; if (Array.isArray(param)) { - var j; var output = ''; for(j = 0; j < param.length; j++) { if(j > 0) @@ -1307,9 +1308,9 @@ SwaggerOperation.prototype.formatXml = function (xml) { return _results; })()).join(''); if (fromTo === 'opening->closing') { - return formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'; + formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'; } else { - return formatted += padding + ln + '\n'; + formatted += padding + ln + '\n'; } }; for (_i = 0, _len = lines.length; _i < _len; _i++) { @@ -1322,6 +1323,7 @@ SwaggerOperation.prototype.formatXml = function (xml) { var SwaggerRequest = function (type, url, params, opts, successCallback, errorCallback, operation, execution) { var _this = this; var errors = []; + this.useJQuery = (typeof operation.resource.useJQuery !== 'undefined' ? operation.resource.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.')); @@ -1345,8 +1347,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa // encode the body for form submits if (headers['Content-Type']) { - var values = {}; - var i; + var key, value, values = {}, i; var operationParams = this.operation.parameters; for (i = 0; i < operationParams.length; i++) { var param = operationParams[i]; @@ -1356,7 +1357,6 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa if (headers['Content-Type'].indexOf('application/x-www-form-urlencoded') === 0) { var encoded = ''; - var key, value; for (key in values) { value = this.params[key]; if (typeof value !== 'undefined') { @@ -1371,7 +1371,6 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa // encode the body for form submits var data = ''; var boundary = '----SwaggerFormBoundary' + Date.now(); - var key, value; for (key in values) { value = this.params[key]; if (typeof value !== 'undefined') { @@ -1426,7 +1425,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa status = e.authorizations.apply(obj, this.operation.authorizations); } - if (opts.mock == null) { + if (!opts.mock) { if (status !== false) { new SwaggerHttp().execute(obj); } else { @@ -1504,9 +1503,9 @@ SwaggerRequest.prototype.setHeaders = function (params, opts, operation) { if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded')) headers['Content-Type'] = consumes; if (accepts) - headers['Accept'] = accepts; + headers.Accept = accepts; return headers; -} +}; var SwaggerClient = function(url, options) { this.isBuilt = false; @@ -1518,7 +1517,7 @@ var SwaggerClient = function(url, options) { this.isValid = false; this.info = null; this.useJQuery = false; - this.models = models; + this.models = {}; options = (options||{}); if (url) @@ -1526,21 +1525,21 @@ var SwaggerClient = function(url, options) { else this.url = url; else options = url; - if (options.url !== null) + if (typeof options.url === 'string') this.url = options.url; - if (options.success !== null) + if (typeof options.success === 'function') this.success = options.success; - if (typeof options.useJQuery === 'boolean') + if (options.useJQuery) this.useJQuery = options.useJQuery; this.supportedSubmitMethods = options.supportedSubmitMethods || []; - this.failure = options.failure != null ? options.failure : function() {}; - this.progress = options.progress != null ? options.progress : function() {}; + this.failure = options.failure || function() {}; + this.progress = options.progress || function() {}; this.spec = options.spec; - if (options.success !== null) + if (typeof options.success === 'function') this.build(); }; @@ -1582,7 +1581,6 @@ SwaggerClient.prototype.build = function() { } }; if(this.spec) { - var self = this; setTimeout(function() { self.buildFromSpec(self.spec); }, 10); } else { @@ -1601,7 +1599,6 @@ SwaggerClient.prototype.buildFromSpec = function(response) { this.title = response.title || ''; this.host = response.host || ''; this.schemes = response.schemes || []; - this.scheme; this.basePath = response.basePath || ''; this.apis = {}; this.apisArray = []; @@ -1768,7 +1765,7 @@ var Operation = function(parent, operationId, httpMethod, path, args, definition this.nickname = (operationId||errors.push('Operations must have a nickname.')); this.method = (httpMethod||errors.push('Operation ' + operationId + ' is missing method.')); this.path = (path||errors.push('Operation ' + this.nickname + ' is missing path.')); - this.parameters = args != null ? (args.parameters||[]) : {}; + this.parameters = args !== null ? (args.parameters||[]) : {}; this.summary = args.summary || ''; this.responses = (args.responses||{}); this.type = null; @@ -1929,7 +1926,7 @@ Operation.prototype.getType = function (param) { Operation.prototype.resolveModel = function (schema, definitions) { if(typeof schema.$ref !== 'undefined') { var ref = schema.$ref; - if(ref.indexOf('#/definitions/') == 0) + if(ref.indexOf('#/definitions/') === 0) ref = ref.substring('#/definitions/'.length); if(definitions[ref]) { return new Model(ref, definitions[ref]); @@ -1964,11 +1961,11 @@ Operation.prototype.getSignature = function(type, models) { if(type === 'string') isPrimitive = true; else - isPrimitive = ((listType !== null) && models[listType]) || (models[type] != null) ? false : true; + isPrimitive = (listType && models[listType]) || (models[type]) ? false : true; if (isPrimitive) { return type; } else { - if (listType !== null) + if (listType) return models[type].getMockSignature(); else return models[type].getMockSignature(); @@ -2100,9 +2097,8 @@ Operation.prototype.getSampleJSON = function(type, models) { var isPrimitive, listType, sampleJson; listType = (type instanceof Array); - isPrimitive = (models[type] != null) ? false : true; + isPrimitive = models[type] ? false : true; sampleJson = isPrimitive ? void 0 : models[type].createJSONSample(); - if (sampleJson) { sampleJson = listType ? [sampleJson] : sampleJson; if(typeof sampleJson == 'string') @@ -2261,7 +2257,7 @@ Operation.prototype.setContentTypes = function(args, opts) { if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded')) headers['Content-Type'] = consumes; if (accepts) - headers['Accept'] = accepts; + headers.Accept = accepts; return headers; }; @@ -2290,7 +2286,7 @@ Operation.prototype.encodePathCollection = function(type, name, value) { separator = ','; for(i = 0; i < value.length; i++) { - if(i == 0) + if(i === 0) encoded = this.encodeQueryParam(value[i]); else encoded += separator + this.encodeQueryParam(value[i]); @@ -2326,7 +2322,7 @@ Operation.prototype.encodeQueryCollection = function(type, name, value) { } if(separator !== '') { for(i = 0; i < value.length; i++) { - if(i == 0) + if(i === 0) encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]); else encoded += separator + this.encodeQueryParam(value[i]); @@ -2405,9 +2401,9 @@ Model.prototype.getSampleValue = function(modelsToIgnore) { Model.prototype.getMockSignature = function(modelsToIgnore) { var propertiesStr = []; - var i; + var i, prop; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -2421,9 +2417,8 @@ Model.prototype.getMockSignature = function(modelsToIgnore) { modelsToIgnore = {}; modelsToIgnore[this.name] = this; - var i; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + prop = this.properties[i]; var ref = prop.$ref; var model = models[ref]; if (model && typeof modelsToIgnore[model.name] === 'undefined') { @@ -2509,11 +2504,14 @@ Property.prototype.sampleValue = function(isArray, ignoredModels) { getStringSignature = function(obj) { var str = ''; - if(obj.type === 'array') { - obj = (obj.items || obj.$ref || {}); + if(typeof obj.type === 'undefined') + str += obj; + else if(obj.type === 'array') { str += 'Array['; + str += getStringSignature((obj.items || obj.$ref || {})); + str += ']'; } - if(obj.type === 'integer' && obj.format === 'int32') + else if(obj.type === 'integer' && obj.format === 'int32') str += 'integer'; else if(obj.type === 'integer' && obj.format === 'int64') str += 'long'; @@ -2523,6 +2521,8 @@ getStringSignature = function(obj) { str += 'date-time'; else if(obj.type === 'string' && obj.format === 'date') str += 'date'; + else if(obj.type === 'string' && typeof obj.format === 'undefined') + str += 'string'; else if(obj.type === 'number' && obj.format === 'float') str += 'float'; else if(obj.type === 'number' && obj.format === 'double') @@ -2535,8 +2535,6 @@ getStringSignature = function(obj) { str += simpleRef(obj.$ref); else str += obj.type; - if(obj.type === 'array') - str += ']'; return str; }; @@ -2594,6 +2592,7 @@ typeFromJsonSchema = function(type, format) { var sampleModels = {}; var cookies = {}; var models = {}; + /** * SwaggerHttp is a wrapper for executing requests */ @@ -2652,7 +2651,6 @@ JQueryHttpClient.prototype.execute = function(obj) { var key, results; if (obj.headers) { results = []; - var key; for (key in obj.headers) { if (key.toLowerCase() === "content-type") { results.push(obj.contentType = obj.headers[key]); @@ -2695,8 +2693,8 @@ JQueryHttpClient.prototype.execute = function(obj) { }; var contentType = (headers["content-type"]||headers["Content-Type"]||null); - if(contentType != null) { - if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(contentType) { + if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) { try { out.obj = response.responseJSON || {}; } catch (ex) { @@ -2767,7 +2765,6 @@ ShredHttpClient.prototype.execute = function(obj) { this.initShred(); var cb = obj.on, res; - var transform = function(response) { var out = { headers: response._headers, @@ -2780,8 +2777,8 @@ ShredHttpClient.prototype.execute = function(obj) { var headers = response._headers.normalized || response._headers; var contentType = (headers["content-type"]||headers["Content-Type"]||null); - if(contentType != null) { - if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(contentType) { + if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) { if(response.content.data && response.content.data !== "") try{ out.obj = JSON.parse(response.content.data); @@ -2815,7 +2812,7 @@ ShredHttpClient.prototype.execute = function(obj) { return out; }; - var res = { + res = { error: function (response) { if (obj) return cb.error(transform(response)); @@ -2826,8 +2823,9 @@ ShredHttpClient.prototype.execute = function(obj) { return cb.error(transformError(err)); }, response: function (response) { - if (obj) + if (obj) { return cb.response(transform(response)); + } } }; if (obj) { diff --git a/lib/swagger-client.js b/lib/swagger-client.js index 3b2c6e44..07117fb8 100644 --- a/lib/swagger-client.js +++ b/lib/swagger-client.js @@ -1,6 +1,6 @@ /** * swagger-client - swagger.js is a javascript client for use with swaggering APIs. - * @version v2.1.0-alpha.5 + * @version v2.1.0-alpha.6 * @link http://swagger.io * @license apache 2.0 */ @@ -9,9 +9,7 @@ var ArrayModel = function(definition) { this.name = "name"; this.definition = definition || {}; this.properties = []; - this.type; - this.ref; - + var requiredFields = definition.enum || []; var items = definition.items; if(items) { @@ -20,7 +18,7 @@ var ArrayModel = function(definition) { this.type = typeFromJsonSchema(type.type, type.format); } else { - this.ref = items['$ref']; + this.ref = items.$ref; } } }; @@ -157,7 +155,7 @@ var PasswordAuthorization = function(name, username, password) { PasswordAuthorization.prototype.apply = function(obj, authorizations) { var base64encoder = this._btoa; - obj.headers["Authorization"] = "Basic " + base64encoder(this.username + ":" + this.password); + obj.headers.Authorization = "Basic " + base64encoder(this.username + ":" + this.password); return true; }; var __bind = function(fn, me){ @@ -217,7 +215,6 @@ var PrimitiveModel = function(definition) { this.name = "name"; this.definition = definition || {}; this.properties = []; - this.type; var requiredFields = definition.enum || []; this.type = typeFromJsonSchema(definition.type, definition.format); @@ -235,9 +232,9 @@ PrimitiveModel.prototype.getSampleValue = function() { PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) { var propertiesStr = []; - var i; + var i, prop; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -251,10 +248,9 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) { if (!modelsToIgnore) modelsToIgnore = {}; modelsToIgnore[this.name] = this; - var i; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; - var ref = prop['$ref']; + prop = this.properties[i]; + var ref = prop.$ref; var model = models[ref]; if (model && typeof modelsToIgnore[ref] === 'undefined') { returnVal = returnVal + ('
' + model.getMockSignature(modelsToIgnore)); @@ -262,6 +258,9 @@ PrimitiveModel.prototype.getMockSignature = function(modelsToIgnore) { } return returnVal; }; +/** + * Provides support for 1.x versions of swagger + */ var SwaggerApi = function (url, options) { this.isBuilt = false; this.url = null; @@ -272,7 +271,7 @@ var SwaggerApi = function (url, options) { this.info = null; this.useJQuery = false; this.modelsArray = []; - this.isValid; + this.isValid = false; options = (options || {}); if (url) @@ -283,14 +282,14 @@ var SwaggerApi = function (url, options) { else options = url; - if (options.url != null) + if (typeof options.url === 'string') this.url = options.url; this.swaggerRequstHeaders = options.swaggerRequstHeaders || 'application/json;charset=utf-8,*/*'; this.defaultSuccessCallback = options.defaultSuccessCallback || null; this.defaultErrorCallback = options.defaultErrorCallback || null; - if (options.success != null) + if (typeof options.success === 'function') this.success = options.success; if (typeof options.useJQuery === 'boolean') @@ -304,9 +303,9 @@ var SwaggerApi = function (url, options) { } this.supportedSubmitMethods = options.supportedSubmitMethods || []; - this.failure = options.failure != null ? options.failure : function () { }; - this.progress = options.progress != null ? options.progress : function () { }; - if (options.success != null) { + this.failure = typeof options.failure === 'function' ? options.failure : function () { }; + this.progress = typeof options.progress === 'function' ? options.progress : function () { }; + if (typeof options.success === 'function') { this.build(); this.isBuilt = true; } @@ -368,8 +367,7 @@ SwaggerApi.prototype.buildFromSpec = function (response) { if (response.info != null) { this.info = response.info; } - var isApi = false; - var i; + var isApi = false, i, res; for (i = 0; i < response.apis.length; i++) { var api = response.apis[i]; if (api.operations) { @@ -390,20 +388,20 @@ SwaggerApi.prototype.buildFromSpec = function (response) { if (isApi) { var newName = response.resourcePath.replace(/\//g, ''); this.resourcePath = response.resourcePath; - var res = new SwaggerResource(response, this); + res = new SwaggerResource(response, this); this.apis[newName] = res; this.apisArray.push(res); } else { var k; for (k = 0; k < response.apis.length; k++) { var resource = response.apis[k]; - var res = new SwaggerResource(resource, this); + res = new SwaggerResource(resource, this); this.apis[res.name] = res; this.apisArray.push(res); } } this.isValid = true; - if (this.success) { + if (typeof this.success === 'function') { this.success(); } return this; @@ -419,7 +417,7 @@ SwaggerApi.prototype.buildFrom1_1Spec = function (response) { if (response.info != null) { this.info = response.info; } - var isApi = false; + var isApi = false, res; for (var i = 0; i < response.apis.length; i++) { var api = response.apis[i]; if (api.operations) { @@ -439,13 +437,13 @@ SwaggerApi.prototype.buildFrom1_1Spec = function (response) { if (isApi) { var newName = response.resourcePath.replace(/\//g, ''); this.resourcePath = response.resourcePath; - var res = new SwaggerResource(response, this); + 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]; - var res = new SwaggerResource(resource, this); + res = new SwaggerResource(resource, this); this.apis[res.name] = res; this.apisArray.push(res); } @@ -459,19 +457,19 @@ SwaggerApi.prototype.buildFrom1_1Spec = function (response) { SwaggerApi.prototype.selfReflect = function () { var resource, resource_name, ref; - if (this.apis == null) { + if (this.apis === null) { return false; } ref = this.apis; for (resource_name in ref) { resource = ref[resource_name]; - if (resource.ready == null) { + if (resource.ready === null) { return false; } } this.setConsolidatedModels(); this.ready = true; - if (this.success != null) { + if (typeof this.success === 'function') { return this.success(); } }; @@ -488,7 +486,7 @@ SwaggerApi.prototype.setConsolidatedModels = function () { for (resource_name in _ref) { resource = _ref[resource_name]; for (modelName in resource.models) { - if (this.models[modelName] == null) { + if (typeof this.models[modelName] === 'undefined') { this.models[modelName] = resource.models[modelName]; this.modelsArray.push(resource.models[modelName]); } @@ -527,7 +525,7 @@ var SwaggerResource = function (resourceObj, api) { var _this = this; this.api = api; this.swaggerRequstHeaders = api.swaggerRequstHeaders; - this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path; + this.path = (typeof this.api.resourcePath === 'string') ? this.api.resourcePath : resourceObj.path; this.description = resourceObj.description; this.authorizations = (resourceObj.authorizations || {}); @@ -539,12 +537,12 @@ var SwaggerResource = function (resourceObj, api) { this.modelsArray = []; this.models = {}; this.rawModels = {}; - this.useJQuery = (typeof api.useJQuery !== 'undefined' ? api.useJQuery : null); + this.useJQuery = (typeof api.useJQuery !== 'undefined') ? api.useJQuery : null; - if ((resourceObj.apis != null) && (this.api.resourcePath != null)) { + if ((resourceObj.apis) && this.api.resourcePath) { this.addApiDeclaration(resourceObj); } else { - if (this.path == null) { + if (typeof this.path === 'undefined') { this.api.fail('SwaggerResources must have a path.'); } if (this.path.substring(0, 4) === 'http') { @@ -593,7 +591,7 @@ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) { return rootUrl + relativeBasePath; } else { - var pos = this.basePath.lastIndexOf('/'); + pos = this.basePath.lastIndexOf('/'); var base = this.basePath.substring(0, pos); if (base.substring(base.length - 1) == '/') return base + relativeBasePath; @@ -603,11 +601,11 @@ SwaggerResource.prototype.getAbsoluteBasePath = function (relativeBasePath) { }; SwaggerResource.prototype.addApiDeclaration = function (response) { - if (response.produces != null) + if (typeof response.produces === 'string') this.produces = response.produces; - if (response.consumes != null) + if (typeof response.consumes === 'string') this.consumes = response.consumes; - if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) + if ((typeof response.basePath === 'string') && response.basePath.replace(/\s/g, '').length > 0) this.basePath = response.basePath.indexOf('http') === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath; this.addModels(response.models); @@ -623,10 +621,10 @@ SwaggerResource.prototype.addApiDeclaration = function (response) { }; SwaggerResource.prototype.addModels = function (models) { - if (models != null) { + if (typeof models === 'object') { var modelName; for (modelName in models) { - if (this.models[modelName] == null) { + if (typeof this.models[modelName] === 'undefined') { var swaggerModel = new SwaggerModel(modelName, models[modelName]); this.modelsArray.push(swaggerModel); this.models[modelName] = swaggerModel; @@ -649,12 +647,12 @@ SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes var o = ops[i]; consumes = this.consumes; produces = this.produces; - if (o.consumes != null) + if (typeof o.consumes !== 'undefined') consumes = o.consumes; else consumes = this.consumes; - if (o.produces != null) + if (typeof o.produces !== 'undefined') produces = o.produces; else produces = this.produces; @@ -663,7 +661,7 @@ SwaggerResource.prototype.addOperations = function (resource_path, ops, consumes if (type === 'array') { ref = null; if (o.items) - ref = o.items['type'] || o.items['$ref']; + ref = o.items.type || o.items.$ref; type = 'array[' + ref + ']'; } var responseMessages = o.responseMessages; @@ -701,11 +699,11 @@ SwaggerResource.prototype.sanitize = function (nickname) { }; var SwaggerModel = function (modelName, obj) { - this.name = obj.id != null ? obj.id : modelName; + this.name = typeof obj.id !== 'undefined' ? obj.id : modelName; this.properties = []; var propertyName; for (propertyName in obj.properties) { - if (obj.required != null) { + if (obj.required) { var value; for (value in obj.required) { if (propertyName === obj.required[value]) { @@ -723,9 +721,9 @@ SwaggerModel.prototype.setReferencedModels = function (allModels) { for (var i = 0; i < this.properties.length; i++) { var property = this.properties[i]; var type = property.type || property.dataType; - if (allModels[type] != null) + if (allModels[type]) results.push(property.refModel = allModels[type]); - else if ((property.refDataType != null) && (allModels[property.refDataType] != null)) + else if ((property.refDataType) && (allModels[property.refDataType])) results.push(property.refModel = allModels[property.refDataType]); else results.push(void 0); @@ -734,9 +732,9 @@ SwaggerModel.prototype.setReferencedModels = function (allModels) { }; SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) { - var propertiesStr = []; - for (var i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + var i, prop, propertiesStr = []; + for (i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -749,9 +747,9 @@ SwaggerModel.prototype.getMockSignature = function (modelsToIgnore) { modelsToIgnore = []; modelsToIgnore.push(this.name); - for (var i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; - if ((prop.refModel != null) && modelsToIgnore.indexOf(prop.refModel.name) === -1) { + for (i = 0; i < this.properties.length; i++) { + prop = this.properties[i]; + if ((prop.refModel) && modelsToIgnore.indexOf(prop.refModel.name) === -1) { returnVal = returnVal + ('
' + prop.refModel.getMockSignature(modelsToIgnore)); } } @@ -764,7 +762,7 @@ SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) { } else { var result = {}; - var modelsToIgnore = (modelsToIgnore || []); + modelsToIgnore = (modelsToIgnore || []); modelsToIgnore.push(this.name); for (var i = 0; i < this.properties.length; i++) { var prop = this.properties[i]; @@ -777,31 +775,31 @@ SwaggerModel.prototype.createJSONSample = function (modelsToIgnore) { var SwaggerModelProperty = function (name, obj, model) { this.name = name; - this.dataType = obj.type || obj.dataType || obj['$ref']; + 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; this.defaultValue = applyModelPropertyMacro(obj, model); - if (obj.items != null) { - if (obj.items.type != null) { + if (obj.items) { + if (obj.items.type) { this.refDataType = obj.items.type; } - if (obj.items.$ref != null) { + if (obj.items.$ref) { this.refDataType = obj.items.$ref; } } - this.dataTypeWithRef = this.refDataType != null ? (this.dataType + '[' + this.refDataType + ']') : this.dataType; - if (obj.allowableValues != null) { + this.dataTypeWithRef = this.refDataType ? (this.dataType + '[' + this.refDataType + ']') : this.dataType; + if (obj.allowableValues) { this.valueType = obj.allowableValues.valueType; this.values = obj.allowableValues.values; - if (this.values != null) { + if (this.values) { this.valuesString = '\'' + this.values.join('\' or \'') + '\''; } } - if (obj['enum'] != null) { + if (obj['enum']) { this.valueType = 'string'; this.values = obj['enum']; - if (this.values != null) { + if (this.values) { this.valueString = '\'' + this.values.join('\' or \'') + '\''; } } @@ -809,7 +807,7 @@ var SwaggerModelProperty = function (name, obj, model) { SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) { var result; - if ((this.refModel != null) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) { + if ((this.refModel) && (modelsToIgnore.indexOf(this.refModel.name) === -1)) { result = this.refModel.createJSONSample(modelsToIgnore); } else { if (this.isCollection) { @@ -827,7 +825,7 @@ SwaggerModelProperty.prototype.getSampleValue = function (modelsToIgnore) { SwaggerModelProperty.prototype.toSampleValue = function (value) { var result; - if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) { + if ((typeof this.defaultValue !== 'undefined') && this.defaultValue) { result = this.defaultValue; } else if (value === 'integer') { result = 0; @@ -850,10 +848,10 @@ SwaggerModelProperty.prototype.toString = function () { str += ', optional'; } str += ')'; - if (this.values != null) { + if (this.values) { str += ' = ["' + this.values.join('\' or \'') + '\']'; } - if (this.descr != null) { + if (this.descr) { str += ': ' + this.descr + ''; } return str; @@ -866,7 +864,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no 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.parameters = parameters ? parameters : []; this.summary = summary; this.notes = notes; this.type = type; @@ -887,6 +885,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.method = this.method.toLowerCase(); this.isGetMethod = this.method === 'GET'; + var i, j, v; this.resourceName = this.resource.name; if (typeof this.type !== 'undefined' && this.type === 'void') this.type = null; @@ -895,13 +894,13 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.responseSampleJSON = this.getSampleJSON(this.type, this.resource.models); } - for (var i = 0; i < this.parameters.length; i++) { + for (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; + type = param.type || param.dataType; if (type === 'array') { type = 'array[' + (param.items.$ref ? param.items.$ref : param.items.type) + ']'; } @@ -915,14 +914,14 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no param.sampleJSON = this.getSampleJSON(type, this.resource.models); var enumValue = param['enum']; - if (enumValue != null) { + if (typeof enumValue !== 'undefined') { param.isList = true; param.allowableValues = {}; param.allowableValues.descriptiveValues = []; - for (var j = 0; j < enumValue.length; j++) { - var v = enumValue[j]; - if (param.defaultValue != null) { + for (j = 0; j < enumValue.length; j++) { + v = enumValue[j]; + if (param.defaultValue) { param.allowableValues.descriptiveValues.push({ value: String(v), isDefault: (v === param.defaultValue) @@ -944,8 +943,8 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no if (param.allowableValues != null) { param.allowableValues.descriptiveValues = []; if (param.allowableValues.values) { - for (var j = 0; j < param.allowableValues.values.length; j++) { - var v = param.allowableValues.values[j]; + for (j = 0; j < param.allowableValues.values.length; j++) { + v = param.allowableValues.values[j]; if (param.defaultValue != null) { param.allowableValues.descriptiveValues.push({ value: String(v), @@ -969,13 +968,15 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.resource[this.nickname] = function (args, opts, callback, error) { var arg1, arg2, arg3, arg4; - if(typeof args === 'function') // right shift 3 - arg1 = {}, arg2 = {}, arg3 = args, arg4 = opts; - else if(typeof args === 'object' && typeof opts === 'function') // right shift 2 - arg1 = args, arg2 = {}, arg3 = opts, arg4 = callback; - else - arg1 = args, arg2 = opts, arg3 = callback, arg4 = error; - + if(typeof args === 'function') { // right shift 3 + arg1 = {}; arg2 = {}; arg3 = args; arg4 = opts; + } + else if(typeof args === 'object' && typeof opts === 'function') { // right shift 2 + arg1 = args; arg2 = {}; arg3 = opts; arg4 = callback; + } + else { + arg1 = args; arg2 = opts; arg3 = callback; arg4 = error; + } return _this['do'](arg1 || {}, arg2 || {}, arg3 || defaultSuccessCallback, arg4 || defaultErrorCallback); }; @@ -985,7 +986,7 @@ var SwaggerOperation = function (nickname, path, method, parameters, summary, no this.resource[this.nickname].asCurl = function (args) { return _this.asCurl(args); }; -} +}; SwaggerOperation.prototype.isListType = function (type) { if (type && type.indexOf('[') >= 0) { @@ -998,11 +999,11 @@ SwaggerOperation.prototype.isListType = function (type) { SwaggerOperation.prototype.getSignature = function (type, models) { var isPrimitive, listType; listType = this.isListType(type); - isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; + isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true; if (isPrimitive) { return type; } else { - if (listType != null) { + if (typeof listType !== 'undefined') { return models[listType].getMockSignature(); } else { return models[type].getMockSignature(); @@ -1013,7 +1014,7 @@ SwaggerOperation.prototype.getSignature = function (type, models) { SwaggerOperation.prototype.getSampleJSON = function (type, models) { var isPrimitive, listType, val; listType = this.isListType(type); - isPrimitive = ((listType != null) && models[listType]) || (models[type] != null) ? false : true; + isPrimitive = ((typeof listType !== 'undefined') && models[listType]) || (typeof models[type] !== 'undefined') ? false : true; val = isPrimitive ? void 0 : (listType != null ? models[listType].createJSONSample() : models[type].createJSONSample()); if (val) { val = listType ? [val] : val; @@ -1037,15 +1038,15 @@ SwaggerOperation.prototype.getSampleJSON = function (type, models) { }; SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { - var key, param, params, possibleParams, req, value; + var key, param, params, possibleParams = [], req, value; - if (error == null) { + if (typeof error !== 'function') { error = function (xhr, textStatus, error) { return log(xhr, textStatus, error); }; } - if (callback == null) { + if (typeof callback !== 'function') { callback = function (response) { var content; content = null; @@ -1057,6 +1058,7 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { return log('default callback: ' + content); }; } + params = {}; params.headers = []; if (args.headers != null) { @@ -1068,12 +1070,11 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { params.headers['Content-Type'] = opts.responseContentType; } if(opts && opts.requestContentType) { - params.headers['Accept'] = opts.requestContentType; + params.headers.Accept = opts.requestContentType; } - var possibleParams = []; for (var i = 0; i < this.parameters.length; i++) { - var param = this.parameters[i]; + param = this.parameters[i]; if (param.paramType === 'header') { if (typeof args[param.name] !== 'undefined') params.headers[param.name] = args[param.name]; @@ -1088,22 +1089,22 @@ SwaggerOperation.prototype['do'] = function (args, opts, callback, error) { } } - if (args.body != null) { + if (typeof args.body !== 'undefined') { params.body = args.body; delete args.body; } if (possibleParams) { - var key; for (key in possibleParams) { - var value = possibleParams[key]; + 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) { + if (opts.mock) { return req; } else { return true; @@ -1135,10 +1136,11 @@ SwaggerOperation.prototype.encodePathParam = function (pathParam) { }; SwaggerOperation.prototype.urlify = function (args) { + var i, j, param; var url = this.resource.basePath + this.pathJson(); var params = this.parameters; - for (var i = 0; i < params.length; i++) { - var param = params[i]; + for (i = 0; i < params.length; i++) { + param = params[i]; if (param.paramType === 'path') { if (typeof args[param.name] !== 'undefined') { // apply path params and remove from args @@ -1152,13 +1154,12 @@ SwaggerOperation.prototype.urlify = function (args) { } var queryParams = ''; - for (var i = 0; i < params.length; i++) { - var param = params[i]; + for (i = 0; i < params.length; i++) { + param = params[i]; if(param.paramType === 'query') { if (queryParams !== '') queryParams += '&'; if (Array.isArray(param)) { - var j; var output = ''; for(j = 0; j < param.length; j++) { if(j > 0) @@ -1307,9 +1308,9 @@ SwaggerOperation.prototype.formatXml = function (xml) { return _results; })()).join(''); if (fromTo === 'opening->closing') { - return formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'; + formatted = formatted.substr(0, formatted.length - 1) + ln + '\n'; } else { - return formatted += padding + ln + '\n'; + formatted += padding + ln + '\n'; } }; for (_i = 0, _len = lines.length; _i < _len; _i++) { @@ -1322,6 +1323,7 @@ SwaggerOperation.prototype.formatXml = function (xml) { var SwaggerRequest = function (type, url, params, opts, successCallback, errorCallback, operation, execution) { var _this = this; var errors = []; + this.useJQuery = (typeof operation.resource.useJQuery !== 'undefined' ? operation.resource.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.')); @@ -1345,8 +1347,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa // encode the body for form submits if (headers['Content-Type']) { - var values = {}; - var i; + var key, value, values = {}, i; var operationParams = this.operation.parameters; for (i = 0; i < operationParams.length; i++) { var param = operationParams[i]; @@ -1356,7 +1357,6 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa if (headers['Content-Type'].indexOf('application/x-www-form-urlencoded') === 0) { var encoded = ''; - var key, value; for (key in values) { value = this.params[key]; if (typeof value !== 'undefined') { @@ -1371,7 +1371,6 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa // encode the body for form submits var data = ''; var boundary = '----SwaggerFormBoundary' + Date.now(); - var key, value; for (key in values) { value = this.params[key]; if (typeof value !== 'undefined') { @@ -1426,7 +1425,7 @@ var SwaggerRequest = function (type, url, params, opts, successCallback, errorCa status = e.authorizations.apply(obj, this.operation.authorizations); } - if (opts.mock == null) { + if (!opts.mock) { if (status !== false) { new SwaggerHttp().execute(obj); } else { @@ -1504,9 +1503,9 @@ SwaggerRequest.prototype.setHeaders = function (params, opts, operation) { if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded')) headers['Content-Type'] = consumes; if (accepts) - headers['Accept'] = accepts; + headers.Accept = accepts; return headers; -} +}; var SwaggerClient = function(url, options) { this.isBuilt = false; @@ -1518,7 +1517,7 @@ var SwaggerClient = function(url, options) { this.isValid = false; this.info = null; this.useJQuery = false; - this.models = models; + this.models = {}; options = (options||{}); if (url) @@ -1526,21 +1525,21 @@ var SwaggerClient = function(url, options) { else this.url = url; else options = url; - if (options.url !== null) + if (typeof options.url === 'string') this.url = options.url; - if (options.success !== null) + if (typeof options.success === 'function') this.success = options.success; - if (typeof options.useJQuery === 'boolean') + if (options.useJQuery) this.useJQuery = options.useJQuery; this.supportedSubmitMethods = options.supportedSubmitMethods || []; - this.failure = options.failure != null ? options.failure : function() {}; - this.progress = options.progress != null ? options.progress : function() {}; + this.failure = options.failure || function() {}; + this.progress = options.progress || function() {}; this.spec = options.spec; - if (options.success !== null) + if (typeof options.success === 'function') this.build(); }; @@ -1582,7 +1581,6 @@ SwaggerClient.prototype.build = function() { } }; if(this.spec) { - var self = this; setTimeout(function() { self.buildFromSpec(self.spec); }, 10); } else { @@ -1601,7 +1599,6 @@ SwaggerClient.prototype.buildFromSpec = function(response) { this.title = response.title || ''; this.host = response.host || ''; this.schemes = response.schemes || []; - this.scheme; this.basePath = response.basePath || ''; this.apis = {}; this.apisArray = []; @@ -1768,7 +1765,7 @@ var Operation = function(parent, operationId, httpMethod, path, args, definition this.nickname = (operationId||errors.push('Operations must have a nickname.')); this.method = (httpMethod||errors.push('Operation ' + operationId + ' is missing method.')); this.path = (path||errors.push('Operation ' + this.nickname + ' is missing path.')); - this.parameters = args != null ? (args.parameters||[]) : {}; + this.parameters = args !== null ? (args.parameters||[]) : {}; this.summary = args.summary || ''; this.responses = (args.responses||{}); this.type = null; @@ -1929,7 +1926,7 @@ Operation.prototype.getType = function (param) { Operation.prototype.resolveModel = function (schema, definitions) { if(typeof schema.$ref !== 'undefined') { var ref = schema.$ref; - if(ref.indexOf('#/definitions/') == 0) + if(ref.indexOf('#/definitions/') === 0) ref = ref.substring('#/definitions/'.length); if(definitions[ref]) { return new Model(ref, definitions[ref]); @@ -1964,11 +1961,11 @@ Operation.prototype.getSignature = function(type, models) { if(type === 'string') isPrimitive = true; else - isPrimitive = ((listType !== null) && models[listType]) || (models[type] != null) ? false : true; + isPrimitive = (listType && models[listType]) || (models[type]) ? false : true; if (isPrimitive) { return type; } else { - if (listType !== null) + if (listType) return models[type].getMockSignature(); else return models[type].getMockSignature(); @@ -2100,9 +2097,8 @@ Operation.prototype.getSampleJSON = function(type, models) { var isPrimitive, listType, sampleJson; listType = (type instanceof Array); - isPrimitive = (models[type] != null) ? false : true; + isPrimitive = models[type] ? false : true; sampleJson = isPrimitive ? void 0 : models[type].createJSONSample(); - if (sampleJson) { sampleJson = listType ? [sampleJson] : sampleJson; if(typeof sampleJson == 'string') @@ -2261,7 +2257,7 @@ Operation.prototype.setContentTypes = function(args, opts) { if ((consumes && body !== '') || (consumes === 'application/x-www-form-urlencoded')) headers['Content-Type'] = consumes; if (accepts) - headers['Accept'] = accepts; + headers.Accept = accepts; return headers; }; @@ -2290,7 +2286,7 @@ Operation.prototype.encodePathCollection = function(type, name, value) { separator = ','; for(i = 0; i < value.length; i++) { - if(i == 0) + if(i === 0) encoded = this.encodeQueryParam(value[i]); else encoded += separator + this.encodeQueryParam(value[i]); @@ -2326,7 +2322,7 @@ Operation.prototype.encodeQueryCollection = function(type, name, value) { } if(separator !== '') { for(i = 0; i < value.length; i++) { - if(i == 0) + if(i === 0) encoded = this.encodeQueryParam(name) + '=' + this.encodeQueryParam(value[i]); else encoded += separator + this.encodeQueryParam(value[i]); @@ -2405,9 +2401,9 @@ Model.prototype.getSampleValue = function(modelsToIgnore) { Model.prototype.getMockSignature = function(modelsToIgnore) { var propertiesStr = []; - var i; + var i, prop; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + prop = this.properties[i]; propertiesStr.push(prop.toString()); } @@ -2421,9 +2417,8 @@ Model.prototype.getMockSignature = function(modelsToIgnore) { modelsToIgnore = {}; modelsToIgnore[this.name] = this; - var i; for (i = 0; i < this.properties.length; i++) { - var prop = this.properties[i]; + prop = this.properties[i]; var ref = prop.$ref; var model = models[ref]; if (model && typeof modelsToIgnore[model.name] === 'undefined') { @@ -2509,11 +2504,14 @@ Property.prototype.sampleValue = function(isArray, ignoredModels) { getStringSignature = function(obj) { var str = ''; - if(obj.type === 'array') { - obj = (obj.items || obj.$ref || {}); + if(typeof obj.type === 'undefined') + str += obj; + else if(obj.type === 'array') { str += 'Array['; + str += getStringSignature((obj.items || obj.$ref || {})); + str += ']'; } - if(obj.type === 'integer' && obj.format === 'int32') + else if(obj.type === 'integer' && obj.format === 'int32') str += 'integer'; else if(obj.type === 'integer' && obj.format === 'int64') str += 'long'; @@ -2523,6 +2521,8 @@ getStringSignature = function(obj) { str += 'date-time'; else if(obj.type === 'string' && obj.format === 'date') str += 'date'; + else if(obj.type === 'string' && typeof obj.format === 'undefined') + str += 'string'; else if(obj.type === 'number' && obj.format === 'float') str += 'float'; else if(obj.type === 'number' && obj.format === 'double') @@ -2535,8 +2535,6 @@ getStringSignature = function(obj) { str += simpleRef(obj.$ref); else str += obj.type; - if(obj.type === 'array') - str += ']'; return str; }; @@ -2594,6 +2592,7 @@ typeFromJsonSchema = function(type, format) { var sampleModels = {}; var cookies = {}; var models = {}; + /** * SwaggerHttp is a wrapper for executing requests */ @@ -2652,7 +2651,6 @@ JQueryHttpClient.prototype.execute = function(obj) { var key, results; if (obj.headers) { results = []; - var key; for (key in obj.headers) { if (key.toLowerCase() === "content-type") { results.push(obj.contentType = obj.headers[key]); @@ -2695,8 +2693,8 @@ JQueryHttpClient.prototype.execute = function(obj) { }; var contentType = (headers["content-type"]||headers["Content-Type"]||null); - if(contentType != null) { - if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(contentType) { + if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) { try { out.obj = response.responseJSON || {}; } catch (ex) { @@ -2767,7 +2765,6 @@ ShredHttpClient.prototype.execute = function(obj) { this.initShred(); var cb = obj.on, res; - var transform = function(response) { var out = { headers: response._headers, @@ -2780,8 +2777,8 @@ ShredHttpClient.prototype.execute = function(obj) { var headers = response._headers.normalized || response._headers; var contentType = (headers["content-type"]||headers["Content-Type"]||null); - if(contentType != null) { - if(contentType.indexOf("application/json") == 0 || contentType.indexOf("+json") > 0) { + if(contentType) { + if(contentType.indexOf("application/json") === 0 || contentType.indexOf("+json") > 0) { if(response.content.data && response.content.data !== "") try{ out.obj = JSON.parse(response.content.data); @@ -2815,7 +2812,7 @@ ShredHttpClient.prototype.execute = function(obj) { return out; }; - var res = { + res = { error: function (response) { if (obj) return cb.error(transform(response)); @@ -2826,8 +2823,9 @@ ShredHttpClient.prototype.execute = function(obj) { return cb.error(transformError(err)); }, response: function (response) { - if (obj) + if (obj) { return cb.response(transform(response)); + } } }; if (obj) { diff --git a/package.json b/package.json index 22dca07e..c124deff 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ "readmeFilename": "README.md", "dependencies": { "coffee-script": "~1.6.3", - "swagger-client": "2.1.0-alpha.5", + "swagger-client": "2.1.0-alpha.7", "handlebars": "~1.0.10", "less": "~1.4.2" },