Repository navigation
Description
Reproduction
Steps to reproduce the behavior:
- Create a proxy function that takes a Request object and forwards it to another URL
- Try to use node-fetch types (RequestInit, BodyInit) with native fetch API
- Observe TypeScript compilation error regarding incompatible body types
Example code:
import type { RequestInit, BodyInit } from 'node-fetch';
export async function proxy(
request: Request,
baseUrl = 'https://api.sampleapis.com'
): Promise<Response> {
const { method, headers, body } = request;
const url = new URL(request.url);
const proxyUrl = new URL(`${baseUrl}${url.pathname}`);
const requestConfig: RequestInit = {
method,
headers: new Headers(headers),
body
};
return fetch(proxyUrl, requestConfig);
}Expected behavior
The RequestInit and BodyInit types from node-fetch should be compatible with the native fetch API types, allowing for seamless usage between the two. According to the Fetch API specification, the body should be properly typed and compatible across implementations.
Error message received:
Error: No overload matches this call.
Overload 1 of 2, '(input: string | URL | Request, init?: RequestInit | undefined): Promise<Response>', gave the following error.
Argument of type 'import("node_modules/node-fetch/@types/index").RequestInit' is not assignable to parameter of type 'RequestInit'.
Types of property 'body' are incompatible.
Type 'import("node_modules/node-fetch/@types/index").BodyInit | null | undefined' is not assignable to type 'BodyInit | null | undefined'.
Type 'ReadableStream' is not assignable to type 'BodyInit | null | undefined'.
Overload 2 of 2, '(input: URL | RequestInfo, init?: RequestInit | undefined): Promise<Response>', gave the following error.
Argument of type 'import("node_modules/node-fetch/@types/index").RequestInit' is not assignable to parameter of type 'RequestInit'.
Types of property 'body' are incompatible.
Type 'import("node_modules/node-fetch/@types/index").BodyInit | null | undefined' is not assignable to type 'BodyInit | null | undefined'.
Type 'ReadableStream' is not assignable to type 'BodyInit | null | undefined'.
return fetch(proxyUrl, requestConfig);
}
Your Environment
| software | version |
|---|---|
| node-fetch | 3.3.2 |
| node | 20.18.0 |
| npm | 10.8.2 |
| Operating System | macOS 15.3 |
Additional context
This appears to be a type compatibility issue between node-fetch's type definitions and the native fetch API types. The specific problem occurs when trying to use node-fetch's RequestInit and BodyInit types with the native fetch implementation, particularly around the handling of ReadableStream in the body property. This makes it difficult to create proxy functions that need to work with both node-fetch and native fetch API types.
The issue specifically manifests when:
- Using TypeScript with strict type checking
- Attempting to forward requests between node-fetch and native fetch implementations
- Working with request bodies, especially streaming bodies
A potential workaround might be to avoid using node-fetch types altogether and use native fetch types, but this isn't always possible in projects that need to maintain compatibility with both APIs.
Activity