/usr/share/perl5/Swagger2/swagger2-client.js is in libswagger2-perl 0.89-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | (function() {
/**
* @file Generate object for communicating with a Swagger backend.
* @version 0.70.0
* @author Jan Henning Thorsen
* @copyright Jan Henning Thorsen 2016
* @license Artistic License version 2.0.
*/
/**
* Generates a new client from a Swagger specification.
* Note that the specification specification cannot contain "$ref" and need
* to be in JSON format.
* @class
* @param {(string|Object)} spec - Either an object representing a Swagger spec or the URL to the Swagger specification file.
* @param {function} [cb] - Function to call when the spec URL is downloaded.
* @example
* var client = new swaggerClient().load(url, function(err) {
* if (err) throw err;
* this.listPets({limit: 10}, function(err, xhr) {
* console.log(xhr.code, xhr.body);
* });
* });
*/
window.swaggerClient = function(spec, cb) {
this._id = 1;
this._xhr = {};
if (typeof spec == "object") return this._generate(spec);
if (typeof spec == "string") return this.load(spec, cb);
};
var proto = window.swaggerClient.prototype;
var makeErr;
/**
* Used to force running a request over plain HTTP instead of WebSocket.
* @return {Object} client - Self.
* @example
* client = client.http().listPets({}, function(err, xhr) { });
*/
proto.http = function() {
this._http = true;
return this;
};
/**
* Used to load a specification and generate methods.
* @return {Object} client - Self.
* @example
* client = client.load(url, function(err) { });
*/
proto.load = function(url, cb) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (xhr.status != 200) return cb.call(this, xhr.status);
if (window.DEBUG == 1) console.log("[Swagger] Generate methods from " + url);
this._generate(xhr.responseText.match(/^[\{\[]/) ? JSON.parse(xhr.responseText) : {});
cb.call(this, "");
}.bind(this);
xhr.send(null);
return this;
};
/**
* Used to set a WebSocket object which can talk Swagger.
* The WebSocket object passed on need to either fire a "json" event
* or the "onmessage" event will be taken over.
* @return {Object} client - Self.
* @example
* client = client.ws(ws);
* ws = client.ws();
*/
proto.ws = function(ws) {
var self = this;
if (!ws) return self._ws;
self._ws = ws;
var handleResponse = function(json) {
if (!json || !json.id || !json.code) return;
var xhr = self._xhr[json.id];
if (!xhr) return;
delete self._xhr[json.id];
xhr.status = json.code;
xhr.body = json.body;
if (window.DEBUG) console.log("[Swagger] " + xhr.op + " " + xhr.status + " " + JSON.stringify(xhr.body));
xhr.call(self, makeErr(xhr), xhr);
}.bind(this);
if (typeof ws.on == "function") {
ws.on("json", handleResponse);
} else {
ws.onmessage = function(e) {
var json = e.data.match(/^[\{\[]/) ? JSON.parse(e.data) : null;
handleResponse(json);
};
}
return self;
};
// Generate methods from spec
proto._generate = function(spec) {
var self = this;
this.baseUrl = (spec.basePath || "").replace(/\/$/, "");
Object.keys(spec.paths).forEach(function(path) {
if (path.indexOf("/") != 0) return;
Object.keys(spec.paths[path]).forEach(function(httpMethod) {
if (!httpMethod.match(/^\w+$/)) return;
var opSpec = spec.paths[path][httpMethod];
var pathList = path.split("/");
httpMethod = httpMethod.toUpperCase();
pathList.shift(); // first element is empty string
if (window.DEBUG == 2) console.log("[Swagger] Add method " + opSpec.operationId);
self[opSpec.operationId] = function(input, cb) {
var http = this._http;
delete this._http; // reset on each request
if (!http && this._ws && this._ws.readyState == WebSocket.OPEN) {
this._ws.send(JSON.stringify({
id: this._id,
op: opSpec.operationId,
params: input
}));
this._xhr[this._id++] = cb;
cb.op = opSpec.operationId;
} else {
var xhr = this._xhrReq(httpMethod, pathList, input, opSpec.parameters || []);
if (xhr.errors) {
setTimeout(function() {
cb.call(this, xhr.errors, xhr);
}.bind(this), 0);
} else {
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
if (window.DEBUG) console.log("[Swagger] " + xhr.url + " " + xhr.status + " " + xhr.responseText);
xhr.body = xhr.responseText.match(/^[\{\[]/) ? JSON.parse(xhr.responseText) : xhr.responseText;
cb.call(this, makeErr(xhr), xhr);
}.bind(this);
xhr.send(xhr.body);
delete xhr.body;
}
}
};
});
});
return this;
};
// Create a request
proto._xhrReq = function(httpMethod, pathList, input, parameters) {
var xhr = new XMLHttpRequest();
var form = [],
headers = [],
json = null,
query = [],
str;
var url = [this.baseUrl];
var errors = [];
pathList.forEach(function(p) {
url.push(p.replace(/\{(\w+)\}/, function(m, n) {
if (typeof input[n] == "undefined") errors.push({
message: "Missing input: " + n,
path: "/" + n
});
return input[n];
}));
});
xhr.body = null;
xhr.url = url.join("/");
for (i = 0; i < parameters.length; i++) {
var p = parameters[i];
var name = p.name;
var value = input[name];
if (typeof value == "undefined") {
value = p["default"];
}
if (typeof value == "undefined") {
if (p.required) errors.push({
message: "Missing input: " + name,
path: "/" + name
});
continue;
}
switch (p["in"]) {
case "body":
json = value;
break;
case "file":
xhr.body = value;
break;
case "formData":
form.push([name, value]);
break;
case "header":
headers.push([name, value]);
break;
case "query":
query.push([name, value]);
break;
}
}
if (errors.length) {
if (window.DEBUG) console.log("[Swagger] " + xhr.url + " = " + JSON.stringify(errors));
xhr.errors = errors;
return xhr;
}
if (query.length) {
str = [];
query.forEach(function(i) {
str.push(encodeURIComponent(i[0]) + "=" + encodeURIComponent(i[1]));
});
xhr.url += "?" + str.join("&");
}
if (json) {
headers.unshift(["Content-Type", "application/json"]);
xhr.body = JSON.stringify(json);
if (window.DEBUG == 2) console.log("[Swagger] " + xhr.url + " <<< " + xhr.body);
} else if (form.length) {
str = [];
headers.unshift(["Content-Type", "application/x-www-form-urlencoded"]);
form.forEach(function(i) {
str.push(encodeURIComponent(i[0]) + "=" + encodeURIComponent(i[1]));
});
xhr.body = str.join("&");
if (window.DEBUG == 2) console.log("[Swagger] " + xhr.url + " <<< " + xhr.body);
}
xhr.open(httpMethod, xhr.url);
headers.forEach(function(i) {
xhr.setRequestHeader(i[0], i[1]);
});
return xhr;
};
var makeErr = function(xhr) {
var errors = xhr.body.errors || [];
if (xhr.status == 200) return null;
if (errors.length) return errors;
if (!xhr.status)
xhr.status = 408;
return [{
message: "Request failed! Try again later. (" + xhr.status + ")",
path: xhr.url
}];
};
})();
|