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 optional function to modify headers #307

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

chdanielmueller
Copy link

@chdanielmueller chdanielmueller commented Nov 19, 2024

Adding an optional function which can modify the headers before they are being sent.
It allows to add additional headers or modify existing headers like Authorization.

It can be used like this:

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;
  }
});

closes #308

Copy link

changeset-bot bot commented Nov 19, 2024

🦋 Changeset detected

Latest commit: 67b7f60

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/sandbox Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 67b7f60:

Sandbox Source
maya-test-embed-next Configuration
maya-test-embed-next Configuration

@chdanielmueller
Copy link
Author

Hi @hwillson,
Do I need to do something for the circleci tests to run?
Thanks,
Daniel

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 this pull request may close these issues.

Add or modify headers async before query, mutation or subscription is being sent
1 participant