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

Add or modify headers async before query, mutation or subscription is being sent #308

Open
chdanielmueller opened this issue Nov 19, 2024 · 0 comments · May be fixed by #307
Open

Add or modify headers async before query, mutation or subscription is being sent #308

chdanielmueller opened this issue Nov 19, 2024 · 0 comments · May be fixed by #307

Comments

@chdanielmueller
Copy link

chdanielmueller commented Nov 19, 2024

I would like to add a Bearer token to the requests automatically.
An optional function which can modify the headers would solve this issue.

Sample:

new window.EmbeddedSandbox({
  target: '#sandbox',
  initialEndpoint: window.location.href,
  initialState: {
    sharedHeaders: {
      'X-SpecialHeader': 'WILL_BE_REPLACED_AUTOMATICALLY'
    }
  },
  modifyHeaders: async (url, headers) => {
    // Add Authorization header if not provided
    if (!headers.Authorization) {
      const accessToken = await window.getAccessToken();
      headers.Authorization = 'Bearer ' + accessToken;
    }
    // Modify special header if shared header is not unchecked
    if (headers['X-SpecialHeader'] === 'WILL_BE_REPLACED_AUTOMATICALLY') {
      const special = await window.getTheValue();
      headers['X-SpecialHeader'] = special;
    }
    return headers;
  }
});

I already provided a PR for this. #307

@chdanielmueller chdanielmueller linked a pull request Nov 19, 2024 that will close this issue
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

Successfully merging a pull request may close this issue.

1 participant