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

fix: normalize headers object before onRequest (nuxt/nuxt#27042) #392

Closed
wants to merge 1 commit into from

Conversation

cmgdragon
Copy link

@cmgdragon cmgdragon commented May 4, 2024

πŸ”— Linked issue

❓ Type of change

  • πŸ“– Documentation (updates to the documentation, readme, or JSdoc annotations)
  • 🐞 Bug fix (a non-breaking change that fixes an issue)
  • πŸ‘Œ Enhancement (improving an existing functionality like performance)
  • ✨ New feature (a non-breaking change that adds functionality)
  • 🧹 Chore (updates to the build process or auxiliary tools and libraries)
  • ⚠️ Breaking change (fix or feature that would cause existing functionality to change)

πŸ“š Description

Resolves nuxt/nuxt#27042

It would be convenient to normalize the headers object before running the onRequest interceptor so there's no conflicts between the retried requests

πŸ“ Checklist

  • I have linked an issue or discussion.
  • I have updated the documentation accordingly.

@manniL
Copy link
Member

manniL commented May 5, 2024

Looks reasonable to me! πŸ‘πŸ»

@cmgdragon
Copy link
Author

cmgdragon commented May 5, 2024

Heads up! @manniL , I just realized, the current approach will only work either using the Headers constructor or setting the properties in lowercase. If we set a header in uppercase inside the onRequest, both properties will remain in the headers object and will be merged together after the retry.

For ensuring the headers object always keeps the same format and it isn't case-sensitive, we could add some more code after the intercerpor... Something like shown below πŸ‘‡

utils.ts

export function normalizeHeaders(headers: any) {
  if (headers instanceof Headers) {
    headers = Object.fromEntries(headers)
  }
}

fetch.ts

if (context.options.onRequest) {
  await context.options.onRequest(context);
}

// Normalize & merge headers
if (context.options.headers) {
  normalizeHeaders(context.options.headers);
  context.options.headers = {
    ..._options.headers ? normalizeHeaders(_options.headers) : {},
    ...Object.fromEntries(
      Object.entries(context.options.headers)
        .map(([k, v]) => [k.toLowerCase(), v])
    )
  };
}

But I don't know if we'd complicating the code too much with that, what do you think, should I commit it?

@pi0
Copy link
Member

pi0 commented May 7, 2024

Hi dear @cmgdragon i appreciate the attempt to fix but please first help to make a minimal reproduction of issue using ofetch alone and submit an issue report in this repo πŸ™πŸΌ (we might think to solve this differently after discussion)

@pi0 pi0 closed this May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

$fetch interceptor not modifying the retried request when a body is present
3 participants