magento-swagger.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright © Magento, Inc. All rights reserved.
  3. * See COPYING.txt for license details.
  4. */
  5. /*global SwaggerTranslator SwaggerUIBundle SwaggerUIStandalonePreset */
  6. /**
  7. * @api
  8. */
  9. (function () {
  10. 'use strict';
  11. var elementBaseUrl = document.querySelector('#input_baseUrl'),
  12. url = elementBaseUrl.value,
  13. ui;
  14. // Pre load translate...
  15. if (SwaggerTranslator) {
  16. SwaggerTranslator.translate();
  17. }
  18. /**
  19. * Takes token from input and adds it to request header.
  20. */
  21. function addApiKeyAuthorization(e) {
  22. var key = encodeURIComponent(e.target.value).trim();
  23. if (key) {
  24. /**
  25. * Adds Auth token to request header.
  26. *
  27. * @param {Object} req
  28. *
  29. * @returns {Object} req
  30. */
  31. ui.getConfigs().requestInterceptor = function (req) {
  32. req.headers.Authorization = 'Bearer ' + key;
  33. return req;
  34. };
  35. }
  36. }
  37. ui = new SwaggerUIBundle({
  38. url: url,
  39. // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
  40. dom_id: '#swagger-ui-container',
  41. presets: [
  42. SwaggerUIBundle.presets.apis,
  43. SwaggerUIStandalonePreset
  44. ],
  45. plugins: [
  46. SwaggerUIBundle.plugins.DownloadUrl
  47. ],
  48. deepLinking: true,
  49. // jscs:enable requireCamelCaseOrUpperCaseIdentifiers
  50. supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch'],
  51. docExpansion: 'none',
  52. apisSorter: 'alpha',
  53. showRequestHeaders: false,
  54. layout: 'StandaloneLayout'
  55. });
  56. document.querySelector('#input_apiKey').addEventListener('change', addApiKeyAuthorization);
  57. document.querySelector('#explore').addEventListener('click', function () {
  58. ui.specActions.download();
  59. });
  60. })();