[typescript-fetch] Type the fetch runtime without DOM globals

CP
2h ago
+246-104

I changed the typescript-fetch runtime so a Node project can typecheck a generated client without the DOM library.

The runtime named WindowOrWorkerGlobalScope and RequestCredentials. Node fetch types do not declare those names. They do declare fetch, RequestInit, Response, and FormData.

FetchAPI is now a method type that takes string | URL | Request and an optional RequestInit, so callers and middleware can use it as they did typeof fetch. Because it is a method type, the middleware wrapper and a user fetch that takes only a string URL or a required init still fit it. URL and Request are declared by Node fetch types too.

FetchCredentials is NonNullable<RequestInit['credentials']>. With the DOM library loaded, that union matches RequestCredentials. Node fetch types accept the same indexed access on their own.

Changed files:

  • modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache
  • modules/openapi-generator/src/test/java/org/openapitools/codegen/typescript/fetch/TypeScriptFetchClientCodegenTest.java
  • 25 runtime.ts samples from the typescript-fetch configs under samples/client/petstore/typescript-fetch and samples/client/others/typescript-fetch

I regenerated those samples with ./bin/generate-samples.sh ./bin/configs/typescript-fetch*.

On commit 4ae5151867da84be7cb596d7842b97649aeef20a, TypeScriptFetchClientCodegenTest.testRuntimeUsesPlatformFetchTypes failed: 1 test, 1 failure. After the fix that test passed: 1 test, 0 failures. The full class then passed: 60 tests, 0 failures, 0 errors, 0 skipped.

Before the fix, tsc 6.0.2 with @types/node 24.19.1, target ES2023, module NodeNext, lib ES2023, types node, strict true, and skipLibCheck false reported 6 errors on the generated runtime and exited 2. After the fix, the same check on the generated client exited 0. A Node-only project that consumed declarations emitted with the DOM library also exited 0. tsc --noEmit on the es6-target sample and the with-npm-version sample exited 0. npm test in samples/client/petstore/typescript-fetch/tests/isolated-declarations (TypeScript 5.9.3) exited 0.

Fixes #25244

TypeScript technical committee: @TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka @joscha @KannaKim

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.