Skip to content

feat: add install() function for global WHATWG fetch classes #4286

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

Merged
merged 2 commits into from
Jun 20, 2025

Conversation

mcollina
Copy link
Member

@mcollina mcollina commented Jun 19, 2025

Summary

  • Add a top-level install() export that installs all WHATWG fetch classes on globalThis
  • Include comprehensive test coverage and documentation

Changes

  • New API: install() function in index.js that sets globalThis properties
  • Classes included: fetch, Headers, Response, Request, FormData, WebSocket, events, EventSource
  • Testing: Added test/install.js with comprehensive test coverage
  • Documentation: Updated README.md with usage examples and added API docs

Use Cases

This is useful for:

  • Polyfilling environments that don't have fetch
  • Ensuring consistent fetch behavior across different Node.js versions
  • Making undici's implementations available globally for libraries that expect them

Test Plan

  • New test file test/install.js verifies function installs all classes correctly
  • Tests verify installed classes are functional
  • Tests properly restore original globals after execution
  • All existing tests pass
  • Linting passes

Fixes #4285

🤖 Generated with Claude Code

Add a top-level install() export that installs all WHATWG fetch classes
on globalThis, making them available globally without imports.

- Add install() function to index.js that sets globalThis properties
- Include fetch, Headers, Response, Request, FormData, WebSocket, events, and EventSource
- Add comprehensive test coverage for the install function
- Document the feature in README.md with usage examples
- Add API documentation for the docs website

This is useful for polyfilling environments, ensuring consistent fetch
behavior across Node.js versions, and making undici's implementations
available globally for libraries that expect them.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina requested review from KhafraDev and Uzlopak and removed request for KhafraDev June 19, 2025 16:41
Refactor the install() function to be declared as a top-level function
instead of an anonymous function assigned to module.exports.install.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina mcollina requested a review from KhafraDev June 19, 2025 16:43
@anonrig
Copy link
Member

anonrig commented Jun 20, 2025

Thanks!

@mcollina mcollina merged commit d28d5ba into main Jun 20, 2025
35 of 37 checks passed
@mcollina mcollina deleted the add-global-install-function branch June 20, 2025 15:19
@github-actions github-actions bot mentioned this pull request Jun 26, 2025
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.

Undici v7(+?) Request cannot parse FormData from Undici v5
5 participants