updates to develop branch

This commit is contained in:
Tony Tam
2014-09-12 16:09:42 -07:00
parent 963c009870
commit 66718f23dd
3 changed files with 109 additions and 18 deletions

View File

@@ -186,6 +186,7 @@ var SwaggerClient = function(url, options) {
this.authorizationScheme = null; this.authorizationScheme = null;
this.info = null; this.info = null;
this.useJQuery = false; this.useJQuery = false;
this.blocked = false;
options = (options||{}); options = (options||{});
if (url) if (url)
@@ -210,6 +211,7 @@ var SwaggerClient = function(url, options) {
SwaggerClient.prototype.build = function() { SwaggerClient.prototype.build = function() {
var self = this; var self = this;
this.blocked = false;
this.progress('fetching resource list: ' + this.url); this.progress('fetching resource list: ' + this.url);
var obj = { var obj = {
useJQuery: this.useJQuery, useJQuery: this.useJQuery,
@@ -231,6 +233,7 @@ SwaggerClient.prototype.build = function() {
}, },
response: function(resp) { response: function(resp) {
var responseObj = resp.obj || JSON.parse(resp.data); var responseObj = resp.obj || JSON.parse(resp.data);
self.blocked = false;
self.swaggerVersion = responseObj.swaggerVersion; self.swaggerVersion = responseObj.swaggerVersion;
if(responseObj.swagger && responseObj.swagger === 2.0) { if(responseObj.swagger && responseObj.swagger === 2.0) {
@@ -242,10 +245,20 @@ SwaggerClient.prototype.build = function() {
}; };
var e = (typeof window !== 'undefined' ? window : exports); var e = (typeof window !== 'undefined' ? window : exports);
e.authorizations.apply(obj); e.authorizations.apply(obj);
blocked = true;
new SwaggerHttp().execute(obj); new SwaggerHttp().execute(obj);
function waitABit(obj) {
if(obj.blocked)
setTimeout(waitABit(obj), 5);
};
setTimeout(waitABit(this), 5);
return this; return this;
}; };
function blocking(obj) {
}
SwaggerClient.prototype.buildFromSpec = function(response) { SwaggerClient.prototype.buildFromSpec = function(response) {
if(this.isBuilt) if(this.isBuilt)
return this; return this;

55
dist/lib/swagger.js vendored
View File

@@ -1,5 +1,5 @@
// swagger.js // swagger.js
// version 2.0.37 // version 2.0.38
var __bind = function(fn, me){ var __bind = function(fn, me){
return function(){ return function(){
@@ -80,6 +80,7 @@ Object.keys = Object.keys || (function () {
})(); })();
var SwaggerApi = function(url, options) { var SwaggerApi = function(url, options) {
this.isBuilt = false;
this.url = null; this.url = null;
this.debug = false; this.debug = false;
this.basePath = null; this.basePath = null;
@@ -108,11 +109,15 @@ var SwaggerApi = function(url, options) {
this.failure = options.failure != null ? options.failure : function() {}; this.failure = options.failure != null ? options.failure : function() {};
this.progress = options.progress != null ? options.progress : function() {}; this.progress = options.progress != null ? options.progress : function() {};
if (options.success != null) if (options.success != null) {
this.build(); this.build();
this.isBuilt = true;
}
} }
SwaggerApi.prototype.build = function() { SwaggerApi.prototype.build = function() {
if(this.isBuilt)
return this;
var _this = this; var _this = this;
this.progress('fetching resource list: ' + this.url); this.progress('fetching resource list: ' + this.url);
var obj = { var obj = {
@@ -120,7 +125,7 @@ SwaggerApi.prototype.build = function() {
url: this.url, url: this.url,
method: "get", method: "get",
headers: { headers: {
accept: "application/json" accept: "application/json,application/json;charset=\"utf-8\",*/*"
}, },
on: { on: {
error: function(response) { error: function(response) {
@@ -353,7 +358,7 @@ var SwaggerResource = function(resourceObj, api) {
method: "get", method: "get",
useJQuery: this.useJQuery, useJQuery: this.useJQuery,
headers: { headers: {
accept: "application/json" accept: "application/json,application/json;charset=\"utf-8\",*/*"
}, },
on: { on: {
response: function(resp) { response: function(resp) {
@@ -593,6 +598,7 @@ var SwaggerModelProperty = function(name, obj) {
this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set'); this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
this.descr = obj.description; this.descr = obj.description;
this.required = obj.required; this.required = obj.required;
this.defaultValue = obj.defaultValue;
if (obj.items != null) { if (obj.items != null) {
if (obj.items.type != null) { if (obj.items.type != null) {
this.refDataType = obj.items.type; this.refDataType = obj.items.type;
@@ -638,7 +644,9 @@ SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
SwaggerModelProperty.prototype.toSampleValue = function(value) { SwaggerModelProperty.prototype.toSampleValue = function(value) {
var result; var result;
if (value === "integer") { if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) {
result = this.defaultValue;
} else if (value === "integer") {
result = 0; result = 0;
} else if (value === "boolean") { } else if (value === "boolean") {
result = false; result = false;
@@ -1218,7 +1226,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) {
// if there's a body, need to set the accepts header via requestContentType // if there's a body, need to set the accepts header via requestContentType
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH" || this.type === "DELETE")) { if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH" || this.type === "DELETE")) {
if (this.opts.requestContentType) if (this.opts.requestContentType)
accepts = this.opts.requestContentType; consumes = this.opts.requestContentType;
} else { } else {
// if any form params, content type must be set // if any form params, content type must be set
if(definedFormParams.length > 0) { if(definedFormParams.length > 0) {
@@ -1227,7 +1235,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) {
else else
consumes = "application/x-www-form-urlencoded"; consumes = "application/x-www-form-urlencoded";
} }
else if (this.type == "DELETE") else if (this.type === "DELETE")
body = "{}"; body = "{}";
else if (this.type != "DELETE") else if (this.type != "DELETE")
accepts = null; accepts = null;
@@ -1461,11 +1469,36 @@ ShredHttpClient.prototype.execute = function(obj) {
return out; return out;
}; };
// Transform an error into a usable response-like object
var transformError = function(error) {
var out = {
// Default to a status of 0 - The client will treat this as a generic permissions sort of error
status: 0,
data: error.message || error
};
if(error.code) {
out.obj = error;
if(error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED' ) {
// We can tell the client that this should be treated as a missing resource and not as a permissions thing
out.status = 404;
}
}
return out;
};
res = { res = {
error: function(response) { error: function(response) {
if (obj) if (obj)
return cb.error(transform(response)); return cb.error(transform(response));
}, },
// Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming)
request_error: function(err) {
if(obj)
return cb.error(transformError(err));
},
redirect: function(response) { redirect: function(response) {
if (obj) if (obj)
return cb.redirect(transform(response)); return cb.redirect(transform(response));
@@ -1533,10 +1566,11 @@ SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
/** /**
* ApiKeyAuthorization allows a query param or header to be injected * ApiKeyAuthorization allows a query param or header to be injected
*/ */
var ApiKeyAuthorization = function(name, value, type) { var ApiKeyAuthorization = function(name, value, type, delimiter) {
this.name = name; this.name = name;
this.value = value; this.value = value;
this.type = type; this.type = type;
this.delimiter = delimiter;
}; };
ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
@@ -1547,6 +1581,11 @@ ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
obj.url = obj.url + "?" + this.name + "=" + this.value; obj.url = obj.url + "?" + this.name + "=" + this.value;
return true; return true;
} else if (this.type === "header") { } else if (this.type === "header") {
if(typeof obj.headers[this.name] !== 'undefined') {
if(typeof this.delimiter !== 'undefined')
obj.headers[this.name] = obj.headers[this.name] + this.delimiter + this.value;
}
else
obj.headers[this.name] = this.value; obj.headers[this.name] = this.value;
return true; return true;
} }

View File

@@ -1,5 +1,5 @@
// swagger.js // swagger.js
// version 2.0.37 // version 2.0.38
var __bind = function(fn, me){ var __bind = function(fn, me){
return function(){ return function(){
@@ -80,6 +80,7 @@ Object.keys = Object.keys || (function () {
})(); })();
var SwaggerApi = function(url, options) { var SwaggerApi = function(url, options) {
this.isBuilt = false;
this.url = null; this.url = null;
this.debug = false; this.debug = false;
this.basePath = null; this.basePath = null;
@@ -108,11 +109,15 @@ var SwaggerApi = function(url, options) {
this.failure = options.failure != null ? options.failure : function() {}; this.failure = options.failure != null ? options.failure : function() {};
this.progress = options.progress != null ? options.progress : function() {}; this.progress = options.progress != null ? options.progress : function() {};
if (options.success != null) if (options.success != null) {
this.build(); this.build();
this.isBuilt = true;
}
} }
SwaggerApi.prototype.build = function() { SwaggerApi.prototype.build = function() {
if(this.isBuilt)
return this;
var _this = this; var _this = this;
this.progress('fetching resource list: ' + this.url); this.progress('fetching resource list: ' + this.url);
var obj = { var obj = {
@@ -120,7 +125,7 @@ SwaggerApi.prototype.build = function() {
url: this.url, url: this.url,
method: "get", method: "get",
headers: { headers: {
accept: "application/json" accept: "application/json,application/json;charset=\"utf-8\",*/*"
}, },
on: { on: {
error: function(response) { error: function(response) {
@@ -353,7 +358,7 @@ var SwaggerResource = function(resourceObj, api) {
method: "get", method: "get",
useJQuery: this.useJQuery, useJQuery: this.useJQuery,
headers: { headers: {
accept: "application/json" accept: "application/json,application/json;charset=\"utf-8\",*/*"
}, },
on: { on: {
response: function(resp) { response: function(resp) {
@@ -593,6 +598,7 @@ var SwaggerModelProperty = function(name, obj) {
this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set'); this.isCollection = this.dataType && (this.dataType.toLowerCase() === 'array' || this.dataType.toLowerCase() === 'list' || this.dataType.toLowerCase() === 'set');
this.descr = obj.description; this.descr = obj.description;
this.required = obj.required; this.required = obj.required;
this.defaultValue = obj.defaultValue;
if (obj.items != null) { if (obj.items != null) {
if (obj.items.type != null) { if (obj.items.type != null) {
this.refDataType = obj.items.type; this.refDataType = obj.items.type;
@@ -638,7 +644,9 @@ SwaggerModelProperty.prototype.getSampleValue = function(modelsToIgnore) {
SwaggerModelProperty.prototype.toSampleValue = function(value) { SwaggerModelProperty.prototype.toSampleValue = function(value) {
var result; var result;
if (value === "integer") { if ((typeof this.defaultValue !== 'undefined') && this.defaultValue !== null) {
result = this.defaultValue;
} else if (value === "integer") {
result = 0; result = 0;
} else if (value === "boolean") { } else if (value === "boolean") {
result = false; result = false;
@@ -1218,7 +1226,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) {
// if there's a body, need to set the accepts header via requestContentType // if there's a body, need to set the accepts header via requestContentType
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH" || this.type === "DELETE")) { if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH" || this.type === "DELETE")) {
if (this.opts.requestContentType) if (this.opts.requestContentType)
accepts = this.opts.requestContentType; consumes = this.opts.requestContentType;
} else { } else {
// if any form params, content type must be set // if any form params, content type must be set
if(definedFormParams.length > 0) { if(definedFormParams.length > 0) {
@@ -1227,7 +1235,7 @@ SwaggerRequest.prototype.setHeaders = function(params, operation) {
else else
consumes = "application/x-www-form-urlencoded"; consumes = "application/x-www-form-urlencoded";
} }
else if (this.type == "DELETE") else if (this.type === "DELETE")
body = "{}"; body = "{}";
else if (this.type != "DELETE") else if (this.type != "DELETE")
accepts = null; accepts = null;
@@ -1461,11 +1469,36 @@ ShredHttpClient.prototype.execute = function(obj) {
return out; return out;
}; };
// Transform an error into a usable response-like object
var transformError = function(error) {
var out = {
// Default to a status of 0 - The client will treat this as a generic permissions sort of error
status: 0,
data: error.message || error
};
if(error.code) {
out.obj = error;
if(error.code === 'ENOTFOUND' || error.code === 'ECONNREFUSED' ) {
// We can tell the client that this should be treated as a missing resource and not as a permissions thing
out.status = 404;
}
}
return out;
};
res = { res = {
error: function(response) { error: function(response) {
if (obj) if (obj)
return cb.error(transform(response)); return cb.error(transform(response));
}, },
// Catch the Shred error raised when the request errors as it is made (i.e. No Response is coming)
request_error: function(err) {
if(obj)
return cb.error(transformError(err));
},
redirect: function(response) { redirect: function(response) {
if (obj) if (obj)
return cb.redirect(transform(response)); return cb.redirect(transform(response));
@@ -1533,10 +1566,11 @@ SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
/** /**
* ApiKeyAuthorization allows a query param or header to be injected * ApiKeyAuthorization allows a query param or header to be injected
*/ */
var ApiKeyAuthorization = function(name, value, type) { var ApiKeyAuthorization = function(name, value, type, delimiter) {
this.name = name; this.name = name;
this.value = value; this.value = value;
this.type = type; this.type = type;
this.delimiter = delimiter;
}; };
ApiKeyAuthorization.prototype.apply = function(obj, authorizations) { ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
@@ -1547,6 +1581,11 @@ ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
obj.url = obj.url + "?" + this.name + "=" + this.value; obj.url = obj.url + "?" + this.name + "=" + this.value;
return true; return true;
} else if (this.type === "header") { } else if (this.type === "header") {
if(typeof obj.headers[this.name] !== 'undefined') {
if(typeof this.delimiter !== 'undefined')
obj.headers[this.name] = obj.headers[this.name] + this.delimiter + this.value;
}
else
obj.headers[this.name] = this.value; obj.headers[this.name] = this.value;
return true; return true;
} }