undici icon indicating copy to clipboard operation
undici copied to clipboard

feat(fetch): introduce `HttpMethod` type and update `RequestInit` method typing

Open plvo opened this issue 7 months ago • 2 comments

This relates to...

This PR enhances the developer experience by adding proper TypeScript typing for HTTP methods and missing RequestInit properties to match the standard Fetch API specification. https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods

Added a new HttpMethod type to define valid HTTP methods and updated the method property in RequestInit to use this type. Updated tests to reflect these changes.

Rationale

Changes

Enhanced TypeScript Definitions

  • Added HttpMethod type: Union of standard HTTP methods + extensible string type using (string & {}) pattern
  • Updated RequestInit.method: Now typed as HttpMethod instead of generic string

Test Coverage

  • Added comprehensive TypeScript tests for all new properties and types
  • Enhanced method testing to verify HttpMethod typing works correctly
  • Maintained backward compatibility - all existing code continues to work

Features

  • HTTP Method Autocompletion: IntelliSense now suggests standard HTTP methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH, TRACE, CONNECT)
  • Extensible Method Typing: Custom HTTP methods still work via (string & {}) pattern
  • Improved Developer Experience: Better type safety and IDE support

Bug Fixes

Breaking Changes and Deprecations

None - This is a purely additive change that maintains full backward compatibility. All existing code will continue to work without modification.

Status

  • [x] I have read and agreed to the Developer's Certificate of Origin
  • [x] Tested
  • [ ] Benchmarked (optional)
  • [ ] Documented
  • [ ] Review ready
  • [ ] In review
  • [ ] Merge ready

plvo avatar Jun 16 '25 11:06 plvo

Breaking Changes and Deprecations None - This is a purely additive change that maintains full backward compatibility. All existing code will continue to work without modification.

Isn't this a breaking change? It won't be possible to merge of declarations of the following declare module 'undici' {interface RequestInit {method?: string;}} .

tsctx avatar Jun 16 '25 13:06 tsctx

@tsctx You're right about the issue. I've found this is a fundamental TS limitation after testing:

  • method?: string → Module augmentation works but no autocompletion
  • method?: HttpMethod → Autocompletion works but breaks module augmentation

But it works like this

import "undici"
import type { HttpMethod } from "undici";

declare module "undici" {
  interface RequestInit {
    method?: HttpMethod;
  }
}

plvo avatar Jun 16 '25 14:06 plvo