merged with swagger.js-2.0-develop
This commit is contained in:
634
dist/lib/swagger.js
vendored
634
dist/lib/swagger.js
vendored
@@ -1,6 +1,6 @@
|
||||
// Generated by CoffeeScript 1.4.0
|
||||
(function() {
|
||||
var SwaggerApi, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource,
|
||||
var ApiKeyAuthorization, SwaggerApi, SwaggerAuthorizations, SwaggerHttp, SwaggerModel, SwaggerModelProperty, SwaggerOperation, SwaggerRequest, SwaggerResource,
|
||||
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
||||
|
||||
SwaggerApi = (function() {
|
||||
@@ -9,10 +9,12 @@
|
||||
|
||||
SwaggerApi.prototype.debug = false;
|
||||
|
||||
SwaggerApi.prototype.api_key = null;
|
||||
|
||||
SwaggerApi.prototype.basePath = null;
|
||||
|
||||
SwaggerApi.prototype.authorizations = null;
|
||||
|
||||
SwaggerApi.prototype.authorizationScheme = null;
|
||||
|
||||
function SwaggerApi(options) {
|
||||
if (options == null) {
|
||||
options = {};
|
||||
@@ -20,97 +22,64 @@
|
||||
if (options.discoveryUrl != null) {
|
||||
this.discoveryUrl = options.discoveryUrl;
|
||||
}
|
||||
if (options.debug != null) {
|
||||
this.debug = options.debug;
|
||||
}
|
||||
this.apiKeyName = options.apiKeyName != null ? options.apiKeyName : 'api_key';
|
||||
if (options.apiKey != null) {
|
||||
this.api_key = options.apiKey;
|
||||
}
|
||||
if (options.api_key != null) {
|
||||
this.api_key = options.api_key;
|
||||
}
|
||||
if (options.verbose != null) {
|
||||
this.verbose = options.verbose;
|
||||
}
|
||||
this.supportHeaderParams = options.supportHeaderParams != null ? options.supportHeaderParams : false;
|
||||
this.supportedSubmitMethods = options.supportedSubmitMethods != null ? options.supportedSubmitMethods : ['get'];
|
||||
if (options.success != null) {
|
||||
this.success = options.success;
|
||||
}
|
||||
this.failure = options.failure != null ? options.failure : function() {};
|
||||
this.progress = options.progress != null ? options.progress : function() {};
|
||||
this.headers = options.headers != null ? options.headers : {};
|
||||
this.booleanValues = options.booleanValues != null ? options.booleanValues : new Array('true', 'false');
|
||||
this.discoveryUrl = this.suffixApiKey(this.discoveryUrl);
|
||||
this.defaultHeaders = options.headers != null ? options.headers : {};
|
||||
if (options.success != null) {
|
||||
this.build();
|
||||
}
|
||||
}
|
||||
|
||||
SwaggerApi.prototype.build = function() {
|
||||
var _this = this;
|
||||
var obj,
|
||||
_this = this;
|
||||
this.progress('fetching resource list: ' + this.discoveryUrl);
|
||||
return jQuery.getJSON(this.discoveryUrl, function(response) {
|
||||
var res, resource, _i, _j, _len, _len1, _ref, _ref1;
|
||||
if (response.apiVersion != null) {
|
||||
_this.apiVersion = response.apiVersion;
|
||||
}
|
||||
if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
|
||||
_this.basePath = response.basePath;
|
||||
if (_this.basePath.match(/^HTTP/i) == null) {
|
||||
_this.fail("discoveryUrl basePath must be a URL.");
|
||||
}
|
||||
_this.basePath = _this.basePath.replace(/\/$/, '');
|
||||
} else {
|
||||
if (_this.discoveryUrl.indexOf('?') > 0) {
|
||||
_this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('?'));
|
||||
} else {
|
||||
_this.basePath = _this.discoveryUrl;
|
||||
}
|
||||
log('derived basepath from discoveryUrl as ' + _this.basePath);
|
||||
}
|
||||
_this.apis = {};
|
||||
_this.apisArray = [];
|
||||
if (response.resourcePath != null) {
|
||||
_this.resourcePath = response.resourcePath;
|
||||
res = null;
|
||||
_ref = response.apis;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
resource = _ref[_i];
|
||||
if (res === null) {
|
||||
res = new SwaggerResource(resource, _this);
|
||||
obj = {
|
||||
url: this.discoveryUrl,
|
||||
method: "get",
|
||||
on: {
|
||||
error: function(response) {
|
||||
if (_this.discoveryUrl.substring(0, 4) !== 'http') {
|
||||
return _this.fail('Please specify the protocol for ' + _this.discoveryUrl);
|
||||
} else if (error.status === 0) {
|
||||
return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
|
||||
} else if (error.status === 404) {
|
||||
return _this.fail('Can\'t read swagger JSON from ' + _this.discoveryUrl);
|
||||
} else {
|
||||
res.addOperations(resource.path, resource.operations);
|
||||
return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl);
|
||||
}
|
||||
}
|
||||
if (res != null) {
|
||||
_this.apis[res.name] = res;
|
||||
_this.apisArray.push(res);
|
||||
res.ready = true;
|
||||
_this.selfReflect();
|
||||
}
|
||||
} else {
|
||||
_ref1 = response.apis;
|
||||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
||||
resource = _ref1[_j];
|
||||
res = new SwaggerResource(resource, _this);
|
||||
_this.apis[res.name] = res;
|
||||
_this.apisArray.push(res);
|
||||
},
|
||||
response: function(rawResponse) {
|
||||
var res, resource, response, _i, _len, _ref;
|
||||
response = JSON.parse(rawResponse.content.data);
|
||||
if (response.apiVersion != null) {
|
||||
_this.apiVersion = response.apiVersion;
|
||||
}
|
||||
_this.apis = {};
|
||||
_this.apisArray = [];
|
||||
if (response.basePath) {
|
||||
_this.basePath = response.basePath;
|
||||
} else if (_this.discoveryUrl.indexOf('?') > 0) {
|
||||
_this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('?'));
|
||||
} else {
|
||||
_this.basePath = _this.discoveryUrl;
|
||||
}
|
||||
_ref = response.apis;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
resource = _ref[_i];
|
||||
res = new SwaggerResource(resource, _this);
|
||||
_this.apis[res.name] = res;
|
||||
_this.apisArray.push(res);
|
||||
}
|
||||
return _this;
|
||||
}
|
||||
}
|
||||
return _this;
|
||||
}).error(function(error) {
|
||||
if (_this.discoveryUrl.substring(0, 4) !== 'http') {
|
||||
return _this.fail('Please specify the protocol for ' + _this.discoveryUrl);
|
||||
} else if (error.status === 0) {
|
||||
return _this.fail('Can\'t read from server. It may not have the appropriate access-control-origin settings.');
|
||||
} else if (error.status === 404) {
|
||||
return _this.fail('Can\'t read swagger JSON from ' + _this.discoveryUrl);
|
||||
} else {
|
||||
return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl);
|
||||
}
|
||||
});
|
||||
};
|
||||
return new SwaggerHttp().execute(obj);
|
||||
};
|
||||
|
||||
SwaggerApi.prototype.selfReflect = function() {
|
||||
@@ -160,16 +129,6 @@
|
||||
return _results;
|
||||
};
|
||||
|
||||
SwaggerApi.prototype.suffixApiKey = function(url) {
|
||||
var sep;
|
||||
if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
|
||||
sep = url.indexOf('?') > 0 ? '&' : '?';
|
||||
return url + sep + this.apiKeyName + '=' + this.api_key;
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
};
|
||||
|
||||
SwaggerApi.prototype.help = function() {
|
||||
var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
|
||||
_ref = this.apis;
|
||||
@@ -196,16 +155,24 @@
|
||||
|
||||
SwaggerResource = (function() {
|
||||
|
||||
SwaggerResource.prototype.api = null;
|
||||
|
||||
SwaggerResource.prototype.produces = null;
|
||||
|
||||
SwaggerResource.prototype.consumes = null;
|
||||
|
||||
function SwaggerResource(resourceObj, api) {
|
||||
var parts,
|
||||
var consumes, obj, parts, produces,
|
||||
_this = this;
|
||||
this.api = api;
|
||||
this.api = this.api;
|
||||
produces = [];
|
||||
consumes = [];
|
||||
this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
|
||||
this.description = resourceObj.description;
|
||||
parts = this.path.split("/");
|
||||
this.name = parts[parts.length - 1].replace('.{format}', '');
|
||||
this.basePath = this.api.basePath;
|
||||
console.log('bp: ' + this.basePath);
|
||||
this.operations = {};
|
||||
this.operationsArray = [];
|
||||
this.modelsArray = [];
|
||||
@@ -219,30 +186,42 @@
|
||||
if (this.path == null) {
|
||||
this.api.fail("SwaggerResources must have a path.");
|
||||
}
|
||||
this.url = this.api.suffixApiKey(this.api.basePath + this.path.replace('{format}', 'json'));
|
||||
console.log('basePath: ' + this.api.basePath);
|
||||
console.log('url: ' + this.url);
|
||||
this.url = this.api.basePath + this.path.replace('{format}', 'json');
|
||||
this.api.progress('fetching resource ' + this.name + ': ' + this.url);
|
||||
jQuery.getJSON(this.url, function(response) {
|
||||
var endpoint, _i, _len, _ref;
|
||||
if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
|
||||
_this.basePath = response.basePath;
|
||||
_this.basePath = _this.basePath.replace(/\/$/, '');
|
||||
}
|
||||
_this.addModels(response.models);
|
||||
if (response.apis) {
|
||||
_ref = response.apis;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
endpoint = _ref[_i];
|
||||
_this.addOperations(endpoint.path, endpoint.operations);
|
||||
obj = {
|
||||
url: this.url,
|
||||
method: "get",
|
||||
on: {
|
||||
error: function(response) {
|
||||
return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + error.statusText + ")");
|
||||
},
|
||||
response: function(rawResponse) {
|
||||
var endpoint, response, _i, _len, _ref;
|
||||
response = JSON.parse(rawResponse.content._body);
|
||||
if (response.produces != null) {
|
||||
_this.produces = response.produces;
|
||||
}
|
||||
if (response.consumes != null) {
|
||||
_this.consumes = response.consumes;
|
||||
}
|
||||
if ((response.basePath != null) && response.basePath.replace(/\s/g, '').length > 0) {
|
||||
_this.basePath = response.basePath;
|
||||
}
|
||||
_this.addModels(response.models);
|
||||
if (response.apis) {
|
||||
_ref = response.apis;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
endpoint = _ref[_i];
|
||||
_this.addOperations(endpoint.path, endpoint.operations);
|
||||
}
|
||||
}
|
||||
_this.api[_this.name] = _this;
|
||||
_this.ready = true;
|
||||
return _this.api.selfReflect();
|
||||
}
|
||||
}
|
||||
_this.api[_this.name] = _this;
|
||||
_this.ready = true;
|
||||
return _this.api.selfReflect();
|
||||
}).error(function(error) {
|
||||
return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + error.statusText + ")");
|
||||
});
|
||||
};
|
||||
new SwaggerHttp().execute(obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,29 +246,35 @@
|
||||
};
|
||||
|
||||
SwaggerResource.prototype.addOperations = function(resource_path, ops) {
|
||||
var consumes, err, errorResponses, o, op, _i, _j, _len, _len1, _results;
|
||||
var consumes, method, o, op, produces, responseMessages, _i, _len, _results;
|
||||
if (ops) {
|
||||
_results = [];
|
||||
for (_i = 0, _len = ops.length; _i < _len; _i++) {
|
||||
o = ops[_i];
|
||||
consumes = o.consumes;
|
||||
consumes = null;
|
||||
produces = null;
|
||||
if (o.consumes != null) {
|
||||
consumes = o.consumes;
|
||||
} else {
|
||||
consumes = this.consumes;
|
||||
}
|
||||
if (o.produces != null) {
|
||||
produces = o.produces;
|
||||
} else {
|
||||
produces = this.produces;
|
||||
}
|
||||
responseMessages = o.responseMessages;
|
||||
method = o.method;
|
||||
if (o.httpMethod) {
|
||||
method = o.httpMethod;
|
||||
}
|
||||
if (o.supportedContentTypes) {
|
||||
consumes = o.supportedContentTypes;
|
||||
}
|
||||
errorResponses = o.responseMessages;
|
||||
if (errorResponses) {
|
||||
for (_j = 0, _len1 = errorResponses.length; _j < _len1; _j++) {
|
||||
err = errorResponses[_j];
|
||||
err.reason = err.message;
|
||||
}
|
||||
}
|
||||
if (o.errorResponses) {
|
||||
errorResponses = o.errorResponses;
|
||||
responseMessages = o.errorResponses;
|
||||
}
|
||||
if (o.method) {
|
||||
o.httpMethod = o.method;
|
||||
}
|
||||
op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, o.responseClass, errorResponses, this, o.consumes, o.produces);
|
||||
op = new SwaggerOperation(o.nickname, resource_path, method, o.parameters, o.summary, o.notes, o.responseClass, responseMessages, this, consumes, produces);
|
||||
this.operations[op.nickname] = op;
|
||||
_results.push(this.operationsArray.push(op));
|
||||
}
|
||||
@@ -298,18 +283,20 @@
|
||||
};
|
||||
|
||||
SwaggerResource.prototype.help = function() {
|
||||
var operation, operation_name, parameter, _i, _len, _ref, _ref1;
|
||||
var msg, operation, operation_name, parameter, _i, _len, _ref, _ref1, _results;
|
||||
_ref = this.operations;
|
||||
_results = [];
|
||||
for (operation_name in _ref) {
|
||||
operation = _ref[operation_name];
|
||||
console.log(" " + operation.nickname);
|
||||
msg = " " + operation.nickname;
|
||||
_ref1 = operation.parameters;
|
||||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||
parameter = _ref1[_i];
|
||||
console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
|
||||
msg.concat(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
|
||||
}
|
||||
_results.push(msg);
|
||||
}
|
||||
return this;
|
||||
return _results;
|
||||
};
|
||||
|
||||
return SwaggerResource;
|
||||
@@ -456,17 +443,17 @@
|
||||
|
||||
SwaggerOperation = (function() {
|
||||
|
||||
function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, responseClass, errorResponses, resource, consumes, produces) {
|
||||
function SwaggerOperation(nickname, path, method, parameters, summary, notes, responseClass, responseMessages, resource, consumes, produces) {
|
||||
var parameter, v, _i, _j, _len, _len1, _ref, _ref1, _ref2,
|
||||
_this = this;
|
||||
this.nickname = nickname;
|
||||
this.path = path;
|
||||
this.httpMethod = httpMethod;
|
||||
this.method = method;
|
||||
this.parameters = parameters != null ? parameters : [];
|
||||
this.summary = summary;
|
||||
this.notes = notes;
|
||||
this.responseClass = responseClass;
|
||||
this.errorResponses = errorResponses;
|
||||
this.responseMessages = responseMessages;
|
||||
this.resource = resource;
|
||||
this.consumes = consumes;
|
||||
this.produces = produces;
|
||||
@@ -478,12 +465,12 @@
|
||||
if (this.path == null) {
|
||||
this.resource.api.fail("SwaggerOperation " + nickname + " is missing path.");
|
||||
}
|
||||
if (this.httpMethod == null) {
|
||||
this.resource.api.fail("SwaggerOperation " + nickname + " is missing httpMethod.");
|
||||
if (this.method == null) {
|
||||
this.resource.api.fail("SwaggerOperation " + nickname + " is missing method.");
|
||||
}
|
||||
this.path = this.path.replace('{format}', 'json');
|
||||
this.httpMethod = this.httpMethod.toLowerCase();
|
||||
this.isGetMethod = this.httpMethod === "get";
|
||||
this.method = this.method.toLowerCase();
|
||||
this.isGetMethod = this.method === "get";
|
||||
this.resourceName = this.resource.name;
|
||||
if (((_ref = this.responseClass) != null ? _ref.toLowerCase() : void 0) === 'void') {
|
||||
this.responseClass = void 0;
|
||||
@@ -492,7 +479,7 @@
|
||||
this.responseClassSignature = this.getSignature(this.responseClass, this.resource.models);
|
||||
this.responseSampleJSON = this.getSampleJSON(this.responseClass, this.resource.models);
|
||||
}
|
||||
this.errorResponses = this.errorResponses || [];
|
||||
this.responseMessages = this.responseMessages || [];
|
||||
_ref1 = this.parameters;
|
||||
for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
|
||||
parameter = _ref1[_i];
|
||||
@@ -569,10 +556,12 @@
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype["do"] = function(args, callback, error) {
|
||||
var body, headers;
|
||||
var key, param, params, possibleParams, requestContentType, responseContentType, value;
|
||||
if (args == null) {
|
||||
args = {};
|
||||
}
|
||||
requestContentType = null;
|
||||
responseContentType = null;
|
||||
if ((typeof args) === "function") {
|
||||
error = callback;
|
||||
callback = args;
|
||||
@@ -585,18 +574,49 @@
|
||||
}
|
||||
if (callback == null) {
|
||||
callback = function(data) {
|
||||
return console.log(data);
|
||||
return console.log("default callback: " + data);
|
||||
};
|
||||
}
|
||||
params = {};
|
||||
if (args.requestContentType) {
|
||||
requestContentType = args.requestContentType;
|
||||
}
|
||||
if (args.responseContentType) {
|
||||
responseContentType = args.responseContentType;
|
||||
}
|
||||
if (args.headers != null) {
|
||||
headers = args.headers;
|
||||
params.headers = args.headers;
|
||||
delete args.headers;
|
||||
}
|
||||
if (args.body != null) {
|
||||
body = args.body;
|
||||
params.body = args.body;
|
||||
delete args.body;
|
||||
}
|
||||
return new SwaggerRequest(this.httpMethod, this.urlify(args), headers, body, callback, error, this);
|
||||
possibleParams = (function() {
|
||||
var _i, _len, _ref, _results;
|
||||
_ref = this.parameters;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
param = _ref[_i];
|
||||
if (param.paramType === "form") {
|
||||
_results.push(param);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
}).call(this);
|
||||
if (possibleParams) {
|
||||
for (key in possibleParams) {
|
||||
value = possibleParams[key];
|
||||
if (args[value.name]) {
|
||||
params[value.name] = args[value.name];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (args.mock != null) {
|
||||
params.mock = args["mock"];
|
||||
}
|
||||
params["parent"] = args["parent"];
|
||||
return new SwaggerRequest(this.method, this.urlify(args), params, requestContentType, responseContentType, callback, error, this);
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.pathJson = function() {
|
||||
@@ -607,11 +627,8 @@
|
||||
return this.path.replace("{format}", "xml");
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.urlify = function(args, includeApiKey) {
|
||||
var param, queryParams, reg, url, _i, _len, _ref;
|
||||
if (includeApiKey == null) {
|
||||
includeApiKey = true;
|
||||
}
|
||||
SwaggerOperation.prototype.urlify = function(args) {
|
||||
var param, queryParams, reg, url, _i, _j, _len, _len1, _ref, _ref1;
|
||||
url = this.resource.basePath + this.pathJson();
|
||||
_ref = this.parameters;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
@@ -626,13 +643,18 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
|
||||
args[this.apiKeyName] = this.resource.api.api_key;
|
||||
}
|
||||
if (this.supportHeaderParams()) {
|
||||
queryParams = jQuery.param(this.getQueryParams(args, includeApiKey));
|
||||
} else {
|
||||
queryParams = jQuery.param(this.getQueryAndHeaderParams(args, includeApiKey));
|
||||
queryParams = "";
|
||||
_ref1 = this.parameters;
|
||||
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
|
||||
param = _ref1[_j];
|
||||
if (param.paramType === 'query') {
|
||||
if (args[param.name]) {
|
||||
if (queryParams !== "") {
|
||||
queryParams += "&";
|
||||
}
|
||||
queryParams += encodeURIComponent(param.name) + '=' + encodeURIComponent(args[param.name]);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((queryParams != null) && queryParams.length > 0) {
|
||||
url += "?" + queryParams;
|
||||
@@ -648,58 +670,44 @@
|
||||
return this.resource.api.supportedSubmitMethods;
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.getQueryAndHeaderParams = function(args, includeApiKey) {
|
||||
if (includeApiKey == null) {
|
||||
includeApiKey = true;
|
||||
}
|
||||
return this.getMatchingParams(['query', 'header'], args, includeApiKey);
|
||||
SwaggerOperation.prototype.getQueryParams = function(args) {
|
||||
return this.getMatchingParams(['query'], args);
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.getQueryParams = function(args, includeApiKey) {
|
||||
if (includeApiKey == null) {
|
||||
includeApiKey = true;
|
||||
}
|
||||
return this.getMatchingParams(['query'], args, includeApiKey);
|
||||
SwaggerOperation.prototype.getHeaderParams = function(args) {
|
||||
return this.getMatchingParams(['header'], args);
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.getHeaderParams = function(args, includeApiKey) {
|
||||
if (includeApiKey == null) {
|
||||
includeApiKey = true;
|
||||
}
|
||||
return this.getMatchingParams(['header'], args, includeApiKey);
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args, includeApiKey) {
|
||||
SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args) {
|
||||
var matchingParams, name, param, value, _i, _len, _ref, _ref1;
|
||||
matchingParams = {};
|
||||
_ref = this.parameters;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
param = _ref[_i];
|
||||
if ((jQuery.inArray(param.paramType, paramTypes) >= 0) && args[param.name]) {
|
||||
if (args && args[param.name]) {
|
||||
matchingParams[param.name] = args[param.name];
|
||||
}
|
||||
}
|
||||
if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
|
||||
matchingParams[this.resource.api.apiKeyName] = this.resource.api.api_key;
|
||||
}
|
||||
if (jQuery.inArray('header', paramTypes) >= 0) {
|
||||
_ref1 = this.resource.api.headers;
|
||||
for (name in _ref1) {
|
||||
value = _ref1[name];
|
||||
matchingParams[name] = value;
|
||||
}
|
||||
_ref1 = this.resource.api.headers;
|
||||
for (name in _ref1) {
|
||||
value = _ref1[name];
|
||||
matchingParams[name] = value;
|
||||
}
|
||||
return matchingParams;
|
||||
};
|
||||
|
||||
SwaggerOperation.prototype.help = function() {
|
||||
var parameter, _i, _len, _ref;
|
||||
var msg, parameter, _i, _len, _ref;
|
||||
msg = "";
|
||||
_ref = this.parameters;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
parameter = _ref[_i];
|
||||
console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
|
||||
if (msg !== "") {
|
||||
msg += "\n";
|
||||
}
|
||||
msg += "* " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description;
|
||||
}
|
||||
return this;
|
||||
return msg;
|
||||
};
|
||||
|
||||
return SwaggerOperation;
|
||||
@@ -708,16 +716,18 @@
|
||||
|
||||
SwaggerRequest = (function() {
|
||||
|
||||
function SwaggerRequest(type, url, headers, body, successCallback, errorCallback, operation) {
|
||||
var obj,
|
||||
function SwaggerRequest(type, url, params, requestContentType, responseContentType, successCallback, errorCallback, operation, execution) {
|
||||
var body, e, fields, headers, key, myHeaders, obj, param, parent, possibleParams, urlEncoded, value, values,
|
||||
_this = this;
|
||||
this.type = type;
|
||||
this.url = url;
|
||||
this.headers = headers;
|
||||
this.body = body;
|
||||
this.params = params;
|
||||
this.requestContentType = requestContentType;
|
||||
this.responseContentType = responseContentType;
|
||||
this.successCallback = successCallback;
|
||||
this.errorCallback = errorCallback;
|
||||
this.operation = operation;
|
||||
this.execution = execution;
|
||||
if (this.type == null) {
|
||||
throw "SwaggerRequest type is required (get/post/put/delete).";
|
||||
}
|
||||
@@ -733,30 +743,133 @@
|
||||
if (this.operation == null) {
|
||||
throw "SwaggerRequest operation is required.";
|
||||
}
|
||||
if (this.operation.resource.api.verbose) {
|
||||
console.log(this.asCurl());
|
||||
this.type = this.type.toUpperCase();
|
||||
headers = params.headers;
|
||||
myHeaders = {};
|
||||
body = params.body;
|
||||
parent = params["parent"];
|
||||
requestContentType = "application/json";
|
||||
if (body && (this.type === "POST" || this.type === "PUT" || this.type === "PATCH")) {
|
||||
if (this.requestContentType) {
|
||||
requestContentType = this.requestContentType;
|
||||
}
|
||||
} else {
|
||||
if (((function() {
|
||||
var _i, _len, _ref, _results;
|
||||
_ref = this.operation.parameters;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
param = _ref[_i];
|
||||
if (param.paramType === "form") {
|
||||
_results.push(param);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
}).call(this)).length > 0) {
|
||||
requestContentType = "application/x-www-form-urlencoded";
|
||||
} else if (this.type !== "DELETE") {
|
||||
requestContentType = null;
|
||||
}
|
||||
}
|
||||
this.headers || (this.headers = {});
|
||||
if (this.operation.resource.api.api_key != null) {
|
||||
this.headers[this.apiKeyName] = this.operation.resource.api.api_key;
|
||||
if (requestContentType && this.operation.consumes) {
|
||||
if (this.operation.consumes.indexOf(requestContentType) === -1) {
|
||||
console.log("server doesn't consume " + requestContentType + ", try " + JSON.stringify(this.operation.consumes));
|
||||
if (this.requestContentType === null) {
|
||||
requestContentType = this.operation.consumes[0];
|
||||
}
|
||||
} else {
|
||||
console.log("it's ok to send " + requestContentType);
|
||||
}
|
||||
}
|
||||
if (this.headers.mock == null) {
|
||||
responseContentType = null;
|
||||
if (this.type === "POST" || this.type === "GET") {
|
||||
if (this.responseContentType) {
|
||||
responseContentType = this.responseContentType;
|
||||
} else {
|
||||
responseContentType = "application/json";
|
||||
}
|
||||
} else {
|
||||
responseContentType = null;
|
||||
}
|
||||
if (responseContentType && this.operation.produces) {
|
||||
if (this.operation.produces.indexOf(responseContentType) === -1) {
|
||||
console.log("server can't produce " + responseContentType);
|
||||
} else {
|
||||
console.log("get ready for " + responseContentType);
|
||||
}
|
||||
}
|
||||
if (requestContentType && requestContentType.indexOf("application/x-www-form-urlencoded") === 0) {
|
||||
console.log("pulling fields");
|
||||
fields = {};
|
||||
possibleParams = (function() {
|
||||
var _i, _len, _ref, _results;
|
||||
_ref = this.operation.parameters;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
param = _ref[_i];
|
||||
if (param.paramType === "form") {
|
||||
_results.push(param);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
}).call(this);
|
||||
console.log(possibleParams);
|
||||
values = {};
|
||||
for (key in possibleParams) {
|
||||
value = possibleParams[key];
|
||||
if (this.params[value.name]) {
|
||||
values[value.name] = this.params[value.name];
|
||||
}
|
||||
}
|
||||
urlEncoded = "";
|
||||
for (key in values) {
|
||||
value = values[key];
|
||||
if (urlEncoded !== "") {
|
||||
urlEncoded += "&";
|
||||
}
|
||||
urlEncoded += encodeURIComponent(key) + '=' + encodeURIComponent(value);
|
||||
}
|
||||
body = urlEncoded;
|
||||
}
|
||||
if (requestContentType) {
|
||||
myHeaders["Content-Type"] = requestContentType;
|
||||
}
|
||||
if (responseContentType) {
|
||||
myHeaders["Accept"] = responseContentType;
|
||||
}
|
||||
if (!((headers != null) && (headers.mock != null))) {
|
||||
obj = {
|
||||
type: this.type,
|
||||
url: this.url,
|
||||
data: JSON.stringify(this.body),
|
||||
dataType: 'json',
|
||||
error: function(xhr, textStatus, error) {
|
||||
return _this.errorCallback(xhr, textStatus, error);
|
||||
},
|
||||
success: function(data) {
|
||||
return _this.successCallback(data);
|
||||
method: this.type,
|
||||
headers: myHeaders,
|
||||
body: body,
|
||||
on: {
|
||||
error: function(response) {
|
||||
return _this.errorCallback(response, _this.params["parent"]);
|
||||
},
|
||||
redirect: function(response) {
|
||||
return _this.successCallback(response, _this.params["parent"]);
|
||||
},
|
||||
307: function(response) {
|
||||
return _this.successCallback(response, _this.params["parent"]);
|
||||
},
|
||||
response: function(response) {
|
||||
return _this.successCallback(response, _this.params["parent"]);
|
||||
}
|
||||
}
|
||||
};
|
||||
if (obj.type.toLowerCase() === "post" || obj.type.toLowerCase() === "put") {
|
||||
obj.contentType = "application/json";
|
||||
e = {};
|
||||
if (typeof window !== 'undefined') {
|
||||
e = window;
|
||||
} else {
|
||||
e = exports;
|
||||
}
|
||||
e.authorizations.apply(obj);
|
||||
if (params.mock == null) {
|
||||
new SwaggerHttp().execute(obj);
|
||||
} else {
|
||||
return obj;
|
||||
}
|
||||
jQuery.ajax(obj);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -779,14 +892,115 @@
|
||||
|
||||
})();
|
||||
|
||||
window.SwaggerApi = SwaggerApi;
|
||||
SwaggerHttp = (function() {
|
||||
|
||||
window.SwaggerResource = SwaggerResource;
|
||||
SwaggerHttp.prototype.shred = null;
|
||||
|
||||
window.SwaggerOperation = SwaggerOperation;
|
||||
function SwaggerHttp() {
|
||||
var Shred;
|
||||
Shred = null;
|
||||
if (typeof window !== 'undefined') {
|
||||
Shred = require("./shred");
|
||||
} else {
|
||||
Shred = require("shred");
|
||||
}
|
||||
this.shred = new Shred();
|
||||
}
|
||||
|
||||
window.SwaggerRequest = SwaggerRequest;
|
||||
SwaggerHttp.prototype.execute = function(obj) {
|
||||
var Content, identity, toString,
|
||||
_this = this;
|
||||
Content = require("./shred/content");
|
||||
identity = function(x) {
|
||||
return x;
|
||||
};
|
||||
toString = function(x) {
|
||||
return x.toString;
|
||||
};
|
||||
Content.registerProcessor(["application/json; charset=utf-8", "application/json", "json"], {
|
||||
parser: identity,
|
||||
stringify: toString
|
||||
});
|
||||
return this.shred.request(obj);
|
||||
};
|
||||
|
||||
window.SwaggerModelProperty = SwaggerModelProperty;
|
||||
return SwaggerHttp;
|
||||
|
||||
})();
|
||||
|
||||
SwaggerAuthorizations = (function() {
|
||||
|
||||
SwaggerAuthorizations.prototype.authz = null;
|
||||
|
||||
function SwaggerAuthorizations() {
|
||||
this.authz = {};
|
||||
}
|
||||
|
||||
SwaggerAuthorizations.prototype.add = function(name, auth) {
|
||||
this.authz[name] = auth;
|
||||
return auth;
|
||||
};
|
||||
|
||||
SwaggerAuthorizations.prototype.apply = function(obj) {
|
||||
var key, value, _ref, _results;
|
||||
_ref = this.authz;
|
||||
_results = [];
|
||||
for (key in _ref) {
|
||||
value = _ref[key];
|
||||
_results.push(value.apply(obj));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return SwaggerAuthorizations;
|
||||
|
||||
})();
|
||||
|
||||
ApiKeyAuthorization = (function() {
|
||||
|
||||
ApiKeyAuthorization.prototype.type = null;
|
||||
|
||||
ApiKeyAuthorization.prototype.name = null;
|
||||
|
||||
ApiKeyAuthorization.prototype.value = null;
|
||||
|
||||
function ApiKeyAuthorization(name, value, type) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
ApiKeyAuthorization.prototype.apply = function(obj) {
|
||||
if (this.type === "query") {
|
||||
if (obj.url.indexOf('?') > 0) {
|
||||
obj.url = obj.url + "&" + this.name + "=" + this.value;
|
||||
} else {
|
||||
obj.url = obj.url + "?" + this.name + "=" + this.value;
|
||||
}
|
||||
return true;
|
||||
} else if (this.type === "header") {
|
||||
return obj.headers[this.name] = this.value;
|
||||
}
|
||||
};
|
||||
|
||||
return ApiKeyAuthorization;
|
||||
|
||||
})();
|
||||
|
||||
this.SwaggerApi = SwaggerApi;
|
||||
|
||||
this.SwaggerResource = SwaggerResource;
|
||||
|
||||
this.SwaggerOperation = SwaggerOperation;
|
||||
|
||||
this.SwaggerRequest = SwaggerRequest;
|
||||
|
||||
this.SwaggerModelProperty = SwaggerModelProperty;
|
||||
|
||||
this.SwaggerAuthorizations = new SwaggerAuthorizations();
|
||||
|
||||
this.ApiKeyAuthorization = ApiKeyAuthorization;
|
||||
|
||||
this.authorizations = new SwaggerAuthorizations();
|
||||
|
||||
}).call(this);
|
||||
|
||||
Reference in New Issue
Block a user