nodejs / nodejs/undici

Fetch types are incompatible with "lib/dom" definitions

Open
#1,943 7 comments 7 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug Types
Dominant language
JavaScript
Stars
7.7k
Forks
880
Avg merge
2d 16h
Merged PRs (30d)
68

Description

Bug Description

The Request interface exposed by Undici does not satisfy the same type from the global type definitions (lib/dom), and also missing properties according to the Fetch API specification:

https://github.com/nodejs/undici/blob/d8d9a96b6b8f856d9e01a1edfe22c3d83c6996ec/types/fetch.d.ts#L138

Reproducible By

As an example, consider the following code:

import { Request } from 'undicit'

function transform(req: globalThis.Request) {}

transform(new Request('/hello'))
// ^^^ Property 'referrer' is missing in type 'import(".../node_modules/undici/types/fetch").Request' but required in type 'Request'.ts(2345)

The referrer property is not present in the Undici's Request type definition while it's both required in lib/dom definitions and the Fetch API specification.

readonly attribute [USVString](https://webidl.spec.whatwg.org/#idl-USVString) [referrer](https://fetch.spec.whatwg.org/#dom-request-referrer);

Expected Behavior

Fetch API primitives such as Request, Response and Headers, are compatible with the global

Logs & Screenshots

No applicable logs.

Environment

  • Node: v16.16.0
  • Undici: ^5.20.0
  • TypeScript: ^4.9.4
Additional context

I understand that I'm setting lib/dom as the source of truth here but I expect that shouldn't matter since:

  1. Both Undici and lib/node aim to implement the Fetch API specification.
  2. Undici is objectively missing some properties in its type definitions defined in the spec, like referrer.

Also, for context, I'm spotting this while building Interceptors (https://github.com/mswjs/interceptors/pull/340) where I try using Undici as a Fetch polyfill to guarantee fetch primitives can be created in Node versions prior to global fetch.

I'd like to open a pull request to ensure this compliance after discussing this with the maintainers of this lib.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with types/fetch.d.ts around line 138 and compare Undici's Request shape with TypeScript's lib.dom Request and the linked Fetch specification. Confirm the reported transform(new Request('/hello')) example, then verify that the relevant Fetch primitives are compatible and type-check cleanly.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.