This commit is contained in:
Tony Tam
2014-01-02 19:54:10 -08:00
parent 54d0a8ec09
commit 15acb15af6
3 changed files with 170 additions and 112 deletions

112
dist/lib/swagger.js vendored
View File

@@ -63,17 +63,9 @@
} }
}, },
response: function(rawResponse) { response: function(rawResponse) {
if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { var response;
try { response = JSON.parse(rawResponse.content.data);
var response = JSON.parse(rawResponse.content.data);
} catch (e) {
var response = rawResponse.content.data;
}
} else {
var response = rawResponse.content.data;
}
_this.swaggerVersion = response.swaggerVersion; _this.swaggerVersion = response.swaggerVersion;
if (_this.swaggerVersion === "1.2") { if (_this.swaggerVersion === "1.2") {
return _this.buildFromSpec(response); return _this.buildFromSpec(response);
} else { } else {
@@ -313,16 +305,7 @@
}, },
response: function(rawResponse) { response: function(rawResponse) {
var response; var response;
if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { response = JSON.parse(rawResponse.content.data);
try {
var response = JSON.parse(rawResponse.content.data);
} catch (e) {
var response = rawResponse.content.data;
}
} else {
var response = rawResponse.content.data;
}
return _this.addApiDeclaration(response); return _this.addApiDeclaration(response);
} }
} }
@@ -338,6 +321,25 @@
} }
} }
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) { SwaggerResource.prototype.addApiDeclaration = function(response) {
var endpoint, _i, _len, _ref; var endpoint, _i, _len, _ref;
if (response.produces != null) { if (response.produces != null) {
@@ -347,7 +349,7 @@
this.consumes = response.consumes; this.consumes = response.consumes;
} }
if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) { if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) {
this.basePath = response.basePath; this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
} }
this.addModels(response.models); this.addModels(response.models);
if (response.apis) { if (response.apis) {
@@ -425,7 +427,7 @@
} }
} }
o.nickname = this.sanitize(o.nickname); 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); 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; this.operations[op.nickname] = op;
_results.push(this.operationsArray.push(op)); _results.push(this.operationsArray.push(op));
} }
@@ -617,7 +619,7 @@
})(); })();
SwaggerOperation = (function() { SwaggerOperation = (function() {
function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces) { 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, var parameter, v, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3,
_this = this; _this = this;
this.nickname = nickname; this.nickname = nickname;
@@ -631,6 +633,7 @@
this.resource = resource; this.resource = resource;
this.consumes = consumes; this.consumes = consumes;
this.produces = produces; this.produces = produces;
this.authorizations = authorizations;
this["do"] = __bind(this["do"], this); this["do"] = __bind(this["do"], this);
if (this.nickname == null) { if (this.nickname == null) {
this.resource.api.fail("SwaggerOperations must have a nickname."); this.resource.api.fail("SwaggerOperations must have a nickname.");
@@ -658,7 +661,7 @@
parameter = _ref1[_i]; parameter = _ref1[_i];
parameter.name = parameter.name || parameter.type || parameter.dataType; parameter.name = parameter.name || parameter.type || parameter.dataType;
type = parameter.type || parameter.dataType; type = parameter.type || parameter.dataType;
if (typeof(type) != 'undefined' && type.toLowerCase() === 'boolean') { if (type.toLowerCase() === 'boolean') {
parameter.allowableValues = {}; parameter.allowableValues = {};
parameter.allowableValues.values = ["true", "false"]; parameter.allowableValues.values = ["true", "false"];
} }
@@ -673,12 +676,12 @@
v = _ref2[_j]; v = _ref2[_j];
if ((parameter.defaultValue != null) && parameter.defaultValue === v) { if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
parameter.allowableValues.descriptiveValues.push({ parameter.allowableValues.descriptiveValues.push({
value: v, value: String(v),
isDefault: true isDefault: true
}); });
} else { } else {
parameter.allowableValues.descriptiveValues.push({ parameter.allowableValues.descriptiveValues.push({
value: v, value: String(v),
isDefault: false isDefault: false
}); });
} }
@@ -719,7 +722,7 @@
} }
SwaggerOperation.prototype.isListType = function(type) { SwaggerOperation.prototype.isListType = function(type) {
if (typeof(type) != 'undefined' && type.indexOf('[') >= 0) { if (type.indexOf('[') >= 0) {
return type.substring(type.indexOf('[') + 1, type.indexOf(']')); return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
} else { } else {
return void 0; return void 0;
@@ -932,7 +935,7 @@
SwaggerRequest = (function() { SwaggerRequest = (function() {
function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) { function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) {
var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, urlEncoded, value, values, var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, status, urlEncoded, value, values,
_this = this; _this = this;
this.type = type; this.type = type;
this.url = url; this.url = url;
@@ -981,18 +984,18 @@
return _results; return _results;
}).call(this)).length > 0) { }).call(this)).length > 0) {
type = param.type || param.dataType; type = param.type || param.dataType;
if (((function() { if ((function() {
var _i, _len, _ref, _results; var _i, _len, _ref, _results;
_ref = this.operation.parameters; _ref = this.operation.parameters;
_results = []; _results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
param = _ref[_i]; param = _ref[_i];
if (type.toLowerCase() === "file") { if ((typeof type !== 'undefined' && type.toLowerCase() === "file").length > 0) {
_results.push(param); _results.push(param);
} }
} }
return _results; return _results;
}).call(this)).length > 0) { }).call(this)) {
requestContentType = "multipart/form-data"; requestContentType = "multipart/form-data";
} else { } else {
requestContentType = "application/x-www-form-urlencoded"; requestContentType = "application/x-www-form-urlencoded";
@@ -1091,9 +1094,13 @@
} else { } else {
e = exports; e = exports;
} }
e.authorizations.apply(obj); status = e.authorizations.apply(obj, this.operation.authorizations);
if (opts.mock == null) { if (opts.mock == null) {
if (status !== false) {
new SwaggerHttp().execute(obj); new SwaggerHttp().execute(obj);
} else {
obj.canceled = true;
}
} else { } else {
console.log(obj); console.log(obj);
return obj; return obj;
@@ -1176,15 +1183,25 @@
return auth; return auth;
}; };
SwaggerAuthorizations.prototype.apply = function(obj) { SwaggerAuthorizations.prototype.remove = function(name) {
var key, value, _ref, _results; return delete this.authz[name];
};
SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
var key, result, status, value, _ref;
status = null;
_ref = this.authz; _ref = this.authz;
_results = [];
for (key in _ref) { for (key in _ref) {
value = _ref[key]; value = _ref[key];
_results.push(value.apply(obj)); result = value.apply(obj, authorizations);
if (result === false) {
status = false;
} }
return _results; if (result === true) {
status = true;
}
}
return status;
}; };
return SwaggerAuthorizations; return SwaggerAuthorizations;
@@ -1204,7 +1221,7 @@
this.type = type; this.type = type;
} }
ApiKeyAuthorization.prototype.apply = function(obj) { ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
if (this.type === "query") { if (this.type === "query") {
if (obj.url.indexOf('?') > 0) { if (obj.url.indexOf('?') > 0) {
obj.url = obj.url + "&" + this.name + "=" + this.value; obj.url = obj.url + "&" + this.name + "=" + this.value;
@@ -1213,7 +1230,8 @@
} }
return true; return true;
} else if (this.type === "header") { } else if (this.type === "header") {
return obj.headers[this.name] = this.value; obj.headers[this.name] = this.value;
return true;
} }
}; };
@@ -1222,6 +1240,8 @@
})(); })();
PasswordAuthorization = (function() { PasswordAuthorization = (function() {
PasswordAuthorization._btoa = null;
PasswordAuthorization.prototype.name = null; PasswordAuthorization.prototype.name = null;
PasswordAuthorization.prototype.username = null; PasswordAuthorization.prototype.username = null;
@@ -1232,10 +1252,20 @@
this.name = name; this.name = name;
this.username = username; this.username = username;
this.password = password; this.password = password;
PasswordAuthorization._ensureBtoa();
} }
PasswordAuthorization.prototype.apply = function(obj) { PasswordAuthorization.prototype.apply = function(obj, authorizations) {
return obj.headers["Authorization"] = "Basic " + btoa(this.username + ":" + this.password); 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; return PasswordAuthorization;

6
dist/swagger-ui.js vendored
View File

@@ -1511,14 +1511,12 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
for (_i = 0, _len = _ref5.length; _i < _len; _i++) { for (_i = 0, _len = _ref5.length; _i < _len; _i++) {
param = _ref5[_i]; param = _ref5[_i];
type = param.type || param.dataType; type = param.type || param.dataType;
if (type.toLowerCase() === 'file') {
if (typeof(type) != 'undefined' && type.toLowerCase() === 'file') {
if (!contentTypeModel.consumes) { if (!contentTypeModel.consumes) {
console.log("set content type "); console.log("set content type ");
contentTypeModel.consumes = 'multipart/form-data'; contentTypeModel.consumes = 'multipart/form-data';
} }
} }
} }
responseContentTypeView = new ResponseContentTypeView({ responseContentTypeView = new ResponseContentTypeView({
model: contentTypeModel model: contentTypeModel
@@ -1906,7 +1904,7 @@ helpers = this.merge(helpers, Handlebars.helpers); data = data || {};
if (this.model.paramType === 'body') { if (this.model.paramType === 'body') {
this.model.isBody = true; this.model.isBody = true;
} }
if (typeof(type) != 'undefined' && type.toLowerCase() === 'file') { if (type.toLowerCase() === 'file') {
this.model.isFile = true; this.model.isFile = true;
} }
template = this.template(); template = this.template();

View File

@@ -63,17 +63,9 @@
} }
}, },
response: function(rawResponse) { response: function(rawResponse) {
if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { var response;
try { response = JSON.parse(rawResponse.content.data);
var response = JSON.parse(rawResponse.content.data);
} catch (e) {
var response = rawResponse.content.data;
}
} else {
var response = rawResponse.content.data;
}
_this.swaggerVersion = response.swaggerVersion; _this.swaggerVersion = response.swaggerVersion;
if (_this.swaggerVersion === "1.2") { if (_this.swaggerVersion === "1.2") {
return _this.buildFromSpec(response); return _this.buildFromSpec(response);
} else { } else {
@@ -313,16 +305,7 @@
}, },
response: function(rawResponse) { response: function(rawResponse) {
var response; var response;
if (/^[\],:{}\s]*$/.test(rawResponse.content.data.replace(/\\["\\\/bfnrtu]/g, '@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) { response = JSON.parse(rawResponse.content.data);
try {
var response = JSON.parse(rawResponse.content.data);
} catch (e) {
var response = rawResponse.content.data;
}
} else {
var response = rawResponse.content.data;
}
return _this.addApiDeclaration(response); return _this.addApiDeclaration(response);
} }
} }
@@ -338,6 +321,25 @@
} }
} }
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) { SwaggerResource.prototype.addApiDeclaration = function(response) {
var endpoint, _i, _len, _ref; var endpoint, _i, _len, _ref;
if (response.produces != null) { if (response.produces != null) {
@@ -347,7 +349,7 @@
this.consumes = response.consumes; this.consumes = response.consumes;
} }
if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) { if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) {
this.basePath = response.basePath; this.basePath = response.basePath.indexOf("http") === -1 ? this.getAbsoluteBasePath(response.basePath) : response.basePath;
} }
this.addModels(response.models); this.addModels(response.models);
if (response.apis) { if (response.apis) {
@@ -425,7 +427,7 @@
} }
} }
o.nickname = this.sanitize(o.nickname); 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); 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; this.operations[op.nickname] = op;
_results.push(this.operationsArray.push(op)); _results.push(this.operationsArray.push(op));
} }
@@ -617,7 +619,7 @@
})(); })();
SwaggerOperation = (function() { SwaggerOperation = (function() {
function SwaggerOperation(nickname, path, method, parameters, summary, notes, type, responseMessages, resource, consumes, produces) { 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, var parameter, v, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3,
_this = this; _this = this;
this.nickname = nickname; this.nickname = nickname;
@@ -631,6 +633,7 @@
this.resource = resource; this.resource = resource;
this.consumes = consumes; this.consumes = consumes;
this.produces = produces; this.produces = produces;
this.authorizations = authorizations;
this["do"] = __bind(this["do"], this); this["do"] = __bind(this["do"], this);
if (this.nickname == null) { if (this.nickname == null) {
this.resource.api.fail("SwaggerOperations must have a nickname."); this.resource.api.fail("SwaggerOperations must have a nickname.");
@@ -658,7 +661,7 @@
parameter = _ref1[_i]; parameter = _ref1[_i];
parameter.name = parameter.name || parameter.type || parameter.dataType; parameter.name = parameter.name || parameter.type || parameter.dataType;
type = parameter.type || parameter.dataType; type = parameter.type || parameter.dataType;
if (typeof(type) != 'undefined' && type.toLowerCase() === 'boolean') { if (typeof type !== 'undefined' && type.toLowerCase() === 'boolean') {
parameter.allowableValues = {}; parameter.allowableValues = {};
parameter.allowableValues.values = ["true", "false"]; parameter.allowableValues.values = ["true", "false"];
} }
@@ -673,12 +676,12 @@
v = _ref2[_j]; v = _ref2[_j];
if ((parameter.defaultValue != null) && parameter.defaultValue === v) { if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
parameter.allowableValues.descriptiveValues.push({ parameter.allowableValues.descriptiveValues.push({
value: v, value: String(v),
isDefault: true isDefault: true
}); });
} else { } else {
parameter.allowableValues.descriptiveValues.push({ parameter.allowableValues.descriptiveValues.push({
value: v, value: String(v),
isDefault: false isDefault: false
}); });
} }
@@ -719,7 +722,7 @@
} }
SwaggerOperation.prototype.isListType = function(type) { SwaggerOperation.prototype.isListType = function(type) {
if (typeof(type) != 'undefined' && type.indexOf('[') >= 0) { if (type.indexOf('[') >= 0) {
return type.substring(type.indexOf('[') + 1, type.indexOf(']')); return type.substring(type.indexOf('[') + 1, type.indexOf(']'));
} else { } else {
return void 0; return void 0;
@@ -932,7 +935,7 @@
SwaggerRequest = (function() { SwaggerRequest = (function() {
function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) { function SwaggerRequest(type, url, params, opts, successCallback, errorCallback, operation, execution) {
var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, urlEncoded, value, values, var body, e, fields, headers, key, myHeaders, name, obj, param, parent, possibleParams, requestContentType, responseContentType, status, urlEncoded, value, values,
_this = this; _this = this;
this.type = type; this.type = type;
this.url = url; this.url = url;
@@ -987,7 +990,7 @@
_results = []; _results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) { for (_i = 0, _len = _ref.length; _i < _len; _i++) {
param = _ref[_i]; param = _ref[_i];
if (type.toLowerCase() === "file") { if (typeof type !== 'undefined' && type.toLowerCase() === "file") {
_results.push(param); _results.push(param);
} }
} }
@@ -1091,9 +1094,13 @@
} else { } else {
e = exports; e = exports;
} }
e.authorizations.apply(obj); status = e.authorizations.apply(obj, this.operation.authorizations);
if (opts.mock == null) { if (opts.mock == null) {
if (status !== false) {
new SwaggerHttp().execute(obj); new SwaggerHttp().execute(obj);
} else {
obj.canceled = true;
}
} else { } else {
console.log(obj); console.log(obj);
return obj; return obj;
@@ -1176,15 +1183,25 @@
return auth; return auth;
}; };
SwaggerAuthorizations.prototype.apply = function(obj) { SwaggerAuthorizations.prototype.remove = function(name) {
var key, value, _ref, _results; return delete this.authz[name];
};
SwaggerAuthorizations.prototype.apply = function(obj, authorizations) {
var key, result, status, value, _ref;
status = null;
_ref = this.authz; _ref = this.authz;
_results = [];
for (key in _ref) { for (key in _ref) {
value = _ref[key]; value = _ref[key];
_results.push(value.apply(obj)); result = value.apply(obj, authorizations);
if (result === false) {
status = false;
} }
return _results; if (result === true) {
status = true;
}
}
return status;
}; };
return SwaggerAuthorizations; return SwaggerAuthorizations;
@@ -1204,7 +1221,7 @@
this.type = type; this.type = type;
} }
ApiKeyAuthorization.prototype.apply = function(obj) { ApiKeyAuthorization.prototype.apply = function(obj, authorizations) {
if (this.type === "query") { if (this.type === "query") {
if (obj.url.indexOf('?') > 0) { if (obj.url.indexOf('?') > 0) {
obj.url = obj.url + "&" + this.name + "=" + this.value; obj.url = obj.url + "&" + this.name + "=" + this.value;
@@ -1213,7 +1230,8 @@
} }
return true; return true;
} else if (this.type === "header") { } else if (this.type === "header") {
return obj.headers[this.name] = this.value; obj.headers[this.name] = this.value;
return true;
} }
}; };
@@ -1222,6 +1240,8 @@
})(); })();
PasswordAuthorization = (function() { PasswordAuthorization = (function() {
PasswordAuthorization._btoa = null;
PasswordAuthorization.prototype.name = null; PasswordAuthorization.prototype.name = null;
PasswordAuthorization.prototype.username = null; PasswordAuthorization.prototype.username = null;
@@ -1232,10 +1252,20 @@
this.name = name; this.name = name;
this.username = username; this.username = username;
this.password = password; this.password = password;
PasswordAuthorization._ensureBtoa();
} }
PasswordAuthorization.prototype.apply = function(obj) { PasswordAuthorization.prototype.apply = function(obj, authorizations) {
return obj.headers["Authorization"] = "Basic " + btoa(this.username + ":" + this.password); 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; return PasswordAuthorization;