Sistema de Gestión Documental
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

5 years ago
  1. // This file is part of the API Platform project.
  2. //
  3. // (c) Kévin Dunglas <dunglas@gmail.com>
  4. //
  5. // For the full copyright and license information, please view the LICENSE
  6. // file that was distributed with this source code.
  7. function loadSwaggerUI(userOptions = {}) {
  8. const data = JSON.parse(document.getElementById('swagger-data').innerText);
  9. const defaultOptions = {
  10. spec: data.spec,
  11. dom_id: '#swagger-ui',
  12. validatorUrl: null,
  13. presets: [
  14. SwaggerUIBundle.presets.apis,
  15. SwaggerUIStandalonePreset
  16. ],
  17. plugins: [
  18. SwaggerUIBundle.plugins.DownloadUrl
  19. ],
  20. layout: 'StandaloneLayout'
  21. };
  22. const options = Object.assign({}, defaultOptions, userOptions);
  23. const ui = SwaggerUIBundle(options);
  24. const storageKey = 'nelmio_api_auth';
  25. // if we have auth in storage use it
  26. if (sessionStorage.getItem(storageKey)) {
  27. try {
  28. ui.authActions.authorize(JSON.parse(sessionStorage.getItem(storageKey)));
  29. } catch (ignored) {
  30. // catch any errors here so it does not stop script execution
  31. }
  32. }
  33. // hook into authorize to store the auth in local storage when user performs authorization
  34. const currentAuthorize = ui.authActions.authorize;
  35. ui.authActions.authorize = function (payload) {
  36. sessionStorage.setItem(storageKey, JSON.stringify(payload));
  37. return currentAuthorize(payload);
  38. };
  39. // hook into logout to clear auth from storage if user logs out
  40. const currentLogout = ui.authActions.logout;
  41. ui.authActions.logout = function (payload) {
  42. sessionStorage.removeItem(storageKey);
  43. return currentLogout(payload);
  44. };
  45. window.ui = ui;
  46. }