Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting server variable using js #10238

Open
siemandalorian opened this issue Dec 12, 2024 · 0 comments
Open

Setting server variable using js #10238

siemandalorian opened this issue Dec 12, 2024 · 0 comments

Comments

@siemandalorian
Copy link

Q&A (please complete the following information)

  • OS: macOS
  • Method of installation: dist
  • Swagger-UI version: 5.18.2

Content & configuration

Swagger/OpenAPI definition:

openapi: 3.0.2
info:
  description: |
    My API
servers:
  - url: https://my-url.com/{my-server-variable}
    variables:
      my-server-variable:
        default: default-value

Swagger-UI configuration options:

document.querySelectorAll(".openapi"), item => {
          const ui = SwaggerUIBundle({
                url: item.getAttribute("data-openapi-url"),
                domNode: item,
                plugins: [SwaggerUIBundle.plugins.DownloadUrl],
                requestInterceptor: (request) => {
                  var inputField = document.getElementById("auth-bearer-value");
                  var inputValue = inputField.value;
                  request.headers['authorization'] = 'Bearer ' + inputValue;
                  return request;
                },
              })

How can we help?

Problem:
I have multiple openAPI specs rendered on single page using swagger-ui. I don't want to authorize and write server variables to each of the API separately but do it once and use in all APIs. I was able to do the authorization part by using requestInterceptor and inserting the token based on the input inserted by the user but I struggle with server variables.

Is there a way to set a server variable using javascript based on user input coming from a field ?
Currently after the user sets the value in general field I set the value programatically in each server variable inputs coming from swagger-ui and trigger the change event on them but the computedUrl is not changing. Also when I click on any endpoint at the API the default value appears back again.

Code how setting the value looks like:

document.getElementById("my_input").addEventListener("input", function () {
        const newValue = this.value;
        document.querySelectorAll('input[data-variable="my-server-variable"]').forEach(input => {
          input.value = newValue;
          input.setAttribute("value", newValue);
          const changeEvent = new Event('change', {
                    bubbles: true,
                    cancelable: true
                });
          input.dispatchEvent(changeEvent);
        });
      })

I would appreciate any hints how to achieve that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant