2
0

swagger.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  1. // Generated by CoffeeScript 1.3.3
  2. (function() {
  3. var SwaggerApi, SwaggerOperation, SwaggerRequest, SwaggerResource,
  4. __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  5. SwaggerApi = (function() {
  6. SwaggerApi.prototype.discoveryUrl = "http://api.wordnik.com/v4/resources.json";
  7. SwaggerApi.prototype.debug = false;
  8. SwaggerApi.prototype.api_key = null;
  9. SwaggerApi.prototype.basePath = null;
  10. function SwaggerApi(options) {
  11. if (options == null) {
  12. options = {};
  13. }
  14. if (options.discoveryUrl != null) {
  15. this.discoveryUrl = options.discoveryUrl;
  16. }
  17. if (options.debug != null) {
  18. this.debug = options.debug;
  19. }
  20. this.apiKeyName = options.apiKeyName != null ? options.apiKeyName : 'api_key';
  21. if (options.apiKey != null) {
  22. this.api_key = options.apiKey;
  23. }
  24. if (options.api_key != null) {
  25. this.api_key = options.api_key;
  26. }
  27. if (options.verbose != null) {
  28. this.verbose = options.verbose;
  29. }
  30. this.supportHeaderParams = options.supportHeaderParams != null ? options.supportHeaderParams : false;
  31. this.supportedSubmitMethods = options.supportedSubmitMethods != null ? options.supportedSubmitMethods : ['get'];
  32. if (options.success != null) {
  33. this.success = options.success;
  34. }
  35. this.failure = options.failure != null ? options.failure : function() {};
  36. this.progress = options.progress != null ? options.progress : function() {};
  37. this.discoveryUrl = this.suffixApiKey(this.discoveryUrl);
  38. if (options.success != null) {
  39. this.build();
  40. }
  41. }
  42. SwaggerApi.prototype.build = function() {
  43. var _this = this;
  44. this.progress('fetching resource list: ' + this.discoveryUrl);
  45. return jQuery.getJSON(this.discoveryUrl, function(response) {
  46. var res, resource, _i, _j, _len, _len1, _ref, _ref1;
  47. if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
  48. _this.basePath = response.basePath;
  49. if (_this.basePath.match(/^HTTP/i) == null) {
  50. _this.fail("discoveryUrl basePath must be a URL.");
  51. }
  52. _this.basePath = _this.basePath.replace(/\/$/, '');
  53. } else {
  54. _this.basePath = _this.discoveryUrl.substring(0, _this.discoveryUrl.lastIndexOf('/'));
  55. log('derived basepath from discoveryUrl as ' + _this.basePath);
  56. }
  57. _this.resources = {};
  58. _this.resourcesArray = [];
  59. if (response.resourcePath != null) {
  60. _this.resourcePath = response.resourcePath;
  61. res = null;
  62. _ref = response.apis;
  63. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  64. resource = _ref[_i];
  65. if (res === null) {
  66. res = new SwaggerResource(resource, _this);
  67. } else {
  68. res.addOperations(resource.path, resource.operations);
  69. }
  70. }
  71. if (res != null) {
  72. _this.resources[res.name] = res;
  73. _this.resourcesArray.push(res);
  74. res.ready = true;
  75. _this.selfReflect();
  76. }
  77. } else {
  78. _ref1 = response.apis;
  79. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  80. resource = _ref1[_j];
  81. res = new SwaggerResource(resource, _this);
  82. _this.resources[res.name] = res;
  83. _this.resourcesArray.push(res);
  84. }
  85. }
  86. return _this;
  87. }).error(function(error) {
  88. return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.discoveryUrl);
  89. });
  90. };
  91. SwaggerApi.prototype.selfReflect = function() {
  92. var resource, resource_name, _ref;
  93. if (this.resources == null) {
  94. return false;
  95. }
  96. _ref = this.resources;
  97. for (resource_name in _ref) {
  98. resource = _ref[resource_name];
  99. if (resource.ready == null) {
  100. return false;
  101. }
  102. }
  103. this.ready = true;
  104. if (this.success != null) {
  105. return this.success();
  106. }
  107. };
  108. SwaggerApi.prototype.fail = function(message) {
  109. this.failure(message);
  110. throw message;
  111. };
  112. SwaggerApi.prototype.suffixApiKey = function(url) {
  113. var sep;
  114. if ((this.api_key != null) && jQuery.trim(this.api_key).length > 0 && (url != null)) {
  115. sep = url.indexOf('?') > 0 ? '&' : '?';
  116. return url + sep + this.apiKeyName + '=' + this.api_key;
  117. } else {
  118. return url;
  119. }
  120. };
  121. SwaggerApi.prototype.help = function() {
  122. var operation, operation_name, parameter, resource, resource_name, _i, _len, _ref, _ref1, _ref2;
  123. _ref = this.resources;
  124. for (resource_name in _ref) {
  125. resource = _ref[resource_name];
  126. console.log(resource_name);
  127. _ref1 = resource.operations;
  128. for (operation_name in _ref1) {
  129. operation = _ref1[operation_name];
  130. console.log(" " + operation.nickname);
  131. _ref2 = operation.parameters;
  132. for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
  133. parameter = _ref2[_i];
  134. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  135. }
  136. }
  137. }
  138. return this;
  139. };
  140. return SwaggerApi;
  141. })();
  142. SwaggerResource = (function() {
  143. function SwaggerResource(resourceObj, api) {
  144. var parts,
  145. _this = this;
  146. this.api = api;
  147. this.path = this.api.resourcePath != null ? this.api.resourcePath : resourceObj.path;
  148. this.description = resourceObj.description;
  149. parts = this.path.split("/");
  150. this.name = parts[parts.length - 1].replace('.{format}', '');
  151. this.basePath = this.api.basePath;
  152. this.operations = {};
  153. this.operationsArray = [];
  154. if ((resourceObj.operations != null) && (this.api.resourcePath != null)) {
  155. this.api.progress('reading resource ' + this.name + ' operations');
  156. this.addOperations(resourceObj.path, resourceObj.operations);
  157. this.api[this.name] = this;
  158. } else {
  159. if (this.path == null) {
  160. this.api.fail("SwaggerResources must have a path.");
  161. }
  162. this.url = this.api.suffixApiKey(this.api.basePath + this.path.replace('{format}', 'json'));
  163. this.api.progress('fetching resource ' + this.name + ': ' + this.url);
  164. jQuery.getJSON(this.url, function(response) {
  165. var endpoint, _i, _len, _ref;
  166. if ((response.basePath != null) && jQuery.trim(response.basePath).length > 0) {
  167. _this.basePath = response.basePath;
  168. _this.basePath = _this.basePath.replace(/\/$/, '');
  169. }
  170. if (response.apis) {
  171. _ref = response.apis;
  172. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  173. endpoint = _ref[_i];
  174. _this.addOperations(endpoint.path, endpoint.operations);
  175. }
  176. }
  177. _this.api[_this.name] = _this;
  178. _this.ready = true;
  179. return _this.api.selfReflect();
  180. }).error(function(error) {
  181. return _this.fail(error.status + ' : ' + error.statusText + ' ' + _this.url);
  182. });
  183. }
  184. }
  185. SwaggerResource.prototype.addOperations = function(resource_path, ops) {
  186. var o, op, _i, _len, _results;
  187. if (ops) {
  188. _results = [];
  189. for (_i = 0, _len = ops.length; _i < _len; _i++) {
  190. o = ops[_i];
  191. op = new SwaggerOperation(o.nickname, resource_path, o.httpMethod, o.parameters, o.summary, o.notes, this);
  192. this.operations[op.nickname] = op;
  193. _results.push(this.operationsArray.push(op));
  194. }
  195. return _results;
  196. }
  197. };
  198. SwaggerResource.prototype.help = function() {
  199. var operation, operation_name, parameter, _i, _len, _ref, _ref1;
  200. _ref = this.operations;
  201. for (operation_name in _ref) {
  202. operation = _ref[operation_name];
  203. console.log(" " + operation.nickname);
  204. _ref1 = operation.parameters;
  205. for (_i = 0, _len = _ref1.length; _i < _len; _i++) {
  206. parameter = _ref1[_i];
  207. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  208. }
  209. }
  210. return this;
  211. };
  212. return SwaggerResource;
  213. })();
  214. SwaggerOperation = (function() {
  215. function SwaggerOperation(nickname, path, httpMethod, parameters, summary, notes, resource) {
  216. var parameter, v, _i, _j, _len, _len1, _ref, _ref1,
  217. _this = this;
  218. this.nickname = nickname;
  219. this.path = path;
  220. this.httpMethod = httpMethod;
  221. this.parameters = parameters != null ? parameters : [];
  222. this.summary = summary;
  223. this.notes = notes;
  224. this.resource = resource;
  225. this["do"] = __bind(this["do"], this);
  226. if (this.nickname == null) {
  227. this.resource.api.fail("SwaggerOperations must have a nickname.");
  228. }
  229. if (this.path == null) {
  230. this.resource.api.fail("SwaggerOperation " + nickname + " is missing path.");
  231. }
  232. if (this.httpMethod == null) {
  233. this.resource.api.fail("SwaggerOperation " + nickname + " is missing httpMethod.");
  234. }
  235. this.path = this.path.replace('{format}', 'json');
  236. this.httpMethod = this.httpMethod.toLowerCase();
  237. this.isGetMethod = this.httpMethod === "get";
  238. this.resourceName = this.resource.name;
  239. _ref = this.parameters;
  240. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  241. parameter = _ref[_i];
  242. parameter.name = parameter.name || parameter.dataType;
  243. if (parameter.allowableValues != null) {
  244. if (parameter.allowableValues.valueType === "RANGE") {
  245. parameter.isRange = true;
  246. } else {
  247. parameter.isList = true;
  248. }
  249. if (parameter.allowableValues.values != null) {
  250. parameter.allowableValues.descriptiveValues = [];
  251. _ref1 = parameter.allowableValues.values;
  252. for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
  253. v = _ref1[_j];
  254. if ((parameter.defaultValue != null) && parameter.defaultValue === v) {
  255. parameter.allowableValues.descriptiveValues.push({
  256. value: v,
  257. isDefault: true
  258. });
  259. } else {
  260. parameter.allowableValues.descriptiveValues.push({
  261. value: v,
  262. isDefault: false
  263. });
  264. }
  265. }
  266. }
  267. }
  268. }
  269. this.resource[this.nickname] = function(args, callback, error) {
  270. return _this["do"](args, callback, error);
  271. };
  272. }
  273. SwaggerOperation.prototype["do"] = function(args, callback, error) {
  274. var body, headers;
  275. if (args == null) {
  276. args = {};
  277. }
  278. if ((typeof args) === "function") {
  279. error = callback;
  280. callback = args;
  281. args = {};
  282. }
  283. if (error == null) {
  284. error = function(xhr, textStatus, error) {
  285. return console.log(xhr, textStatus, error);
  286. };
  287. }
  288. if (callback == null) {
  289. callback = function(data) {
  290. return console.log(data);
  291. };
  292. }
  293. if (args.headers != null) {
  294. headers = args.headers;
  295. delete args.headers;
  296. }
  297. if (args.body != null) {
  298. body = args.body;
  299. delete args.body;
  300. }
  301. return new SwaggerRequest(this.httpMethod, this.urlify(args), headers, body, callback, error, this);
  302. };
  303. SwaggerOperation.prototype.pathJson = function() {
  304. return this.path.replace("{format}", "json");
  305. };
  306. SwaggerOperation.prototype.pathXml = function() {
  307. return this.path.replace("{format}", "xml");
  308. };
  309. SwaggerOperation.prototype.urlify = function(args, includeApiKey) {
  310. var param, queryParams, url, _i, _len, _ref;
  311. if (includeApiKey == null) {
  312. includeApiKey = true;
  313. }
  314. url = this.resource.basePath + this.pathJson();
  315. _ref = this.parameters;
  316. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  317. param = _ref[_i];
  318. if (param.paramType === 'path') {
  319. if (args[param.name]) {
  320. url = url.replace("{" + param.name + "}", encodeURIComponent(args[param.name]));
  321. delete args[param.name];
  322. } else {
  323. throw "" + param.name + " is a required path param.";
  324. }
  325. }
  326. }
  327. if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
  328. args[this.apiKeyName] = this.resource.api.api_key;
  329. }
  330. if (this.supportHeaderParams()) {
  331. queryParams = jQuery.param(this.getQueryParams(args));
  332. } else {
  333. queryParams = jQuery.param(this.getQueryAndHeaderParams(args));
  334. }
  335. if ((queryParams != null) && queryParams.length > 0) {
  336. url += "?" + queryParams;
  337. }
  338. return url;
  339. };
  340. SwaggerOperation.prototype.supportHeaderParams = function() {
  341. return this.resource.api.supportHeaderParams;
  342. };
  343. SwaggerOperation.prototype.supportedSubmitMethods = function() {
  344. return this.resource.api.supportedSubmitMethods;
  345. };
  346. SwaggerOperation.prototype.getQueryAndHeaderParams = function(args, includeApiKey) {
  347. if (includeApiKey == null) {
  348. includeApiKey = true;
  349. }
  350. return this.getMatchingParams(['query', 'header'], args, includeApiKey);
  351. };
  352. SwaggerOperation.prototype.getQueryParams = function(args, includeApiKey) {
  353. if (includeApiKey == null) {
  354. includeApiKey = true;
  355. }
  356. return this.getMatchingParams(['query'], args, includeApiKey);
  357. };
  358. SwaggerOperation.prototype.getHeaderParams = function(args, includeApiKey) {
  359. if (includeApiKey == null) {
  360. includeApiKey = true;
  361. }
  362. return this.getMatchingParams(['header'], args, includeApiKey);
  363. };
  364. SwaggerOperation.prototype.getMatchingParams = function(paramTypes, args, includeApiKey) {
  365. var matchingParams, param, _i, _len, _ref;
  366. matchingParams = {};
  367. _ref = this.parameters;
  368. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  369. param = _ref[_i];
  370. if ((jQuery.inArray(param.paramType, paramTypes) >= 0) && args[param.name]) {
  371. matchingParams[param.name] = args[param.name];
  372. }
  373. }
  374. if (includeApiKey && (this.resource.api.api_key != null) && this.resource.api.api_key.length > 0) {
  375. matchingParams[this.resource.api.apiKeyName] = this.resource.api.api_key;
  376. }
  377. return matchingParams;
  378. };
  379. SwaggerOperation.prototype.help = function() {
  380. var parameter, _i, _len, _ref;
  381. _ref = this.parameters;
  382. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  383. parameter = _ref[_i];
  384. console.log(" " + parameter.name + (parameter.required ? ' (required)' : '') + " - " + parameter.description);
  385. }
  386. return this;
  387. };
  388. return SwaggerOperation;
  389. })();
  390. SwaggerRequest = (function() {
  391. function SwaggerRequest(type, url, headers, body, successCallback, errorCallback, operation) {
  392. var obj,
  393. _this = this;
  394. this.type = type;
  395. this.url = url;
  396. this.headers = headers;
  397. this.body = body;
  398. this.successCallback = successCallback;
  399. this.errorCallback = errorCallback;
  400. this.operation = operation;
  401. if (this.type == null) {
  402. throw "SwaggerRequest type is required (get/post/put/delete).";
  403. }
  404. if (this.url == null) {
  405. throw "SwaggerRequest url is required.";
  406. }
  407. if (this.successCallback == null) {
  408. throw "SwaggerRequest successCallback is required.";
  409. }
  410. if (this.errorCallback == null) {
  411. throw "SwaggerRequest error callback is required.";
  412. }
  413. if (this.operation == null) {
  414. throw "SwaggerRequest operation is required.";
  415. }
  416. if (this.operation.resource.api.verbose) {
  417. console.log(this.asCurl());
  418. }
  419. this.headers || (this.headers = {});
  420. if (this.operation.resource.api.api_key != null) {
  421. this.headers[this.apiKeyName] = this.operation.resource.api.api_key;
  422. }
  423. if (this.headers.mock == null) {
  424. obj = {
  425. type: this.type,
  426. url: this.url,
  427. data: JSON.stringify(this.body),
  428. dataType: 'json',
  429. error: function(xhr, textStatus, error) {
  430. return _this.errorCallback(xhr, textStatus, error);
  431. },
  432. success: function(data) {
  433. return _this.successCallback(data);
  434. }
  435. };
  436. if (obj.type.toLowerCase() === "post" || obj.type.toLowerCase() === "put") {
  437. obj.contentType = "application/json";
  438. }
  439. jQuery.ajax(obj);
  440. }
  441. }
  442. SwaggerRequest.prototype.asCurl = function() {
  443. var header_args, k, v;
  444. header_args = (function() {
  445. var _ref, _results;
  446. _ref = this.headers;
  447. _results = [];
  448. for (k in _ref) {
  449. v = _ref[k];
  450. _results.push("--header \"" + k + ": " + v + "\"");
  451. }
  452. return _results;
  453. }).call(this);
  454. return "curl " + (header_args.join(" ")) + " " + this.url;
  455. };
  456. return SwaggerRequest;
  457. })();
  458. window.SwaggerApi = SwaggerApi;
  459. window.SwaggerResource = SwaggerResource;
  460. window.SwaggerOperation = SwaggerOperation;
  461. window.SwaggerRequest = SwaggerRequest;
  462. }).call(this);