Skip to content

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

Open
cpruijsen wants to merge 4 commits into
OpenAPITools:masterfrom
cpruijsen:fix/ts-fetch-native-fetch-dom-types
Open

cpruijsen wants to merge 4 commits into
OpenAPITools:masterfrom
cpruijsen:fix/ts-fetch-native-fetch-dom-types

Conversation

@cpruijsen

@cpruijsen cpruijsen commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

…ithout DOM globals

Node fetch types declare RequestInit and Response, but not RequestCredentials
or WindowOrWorkerGlobalScope. The runtime now takes those shapes from
RequestInit so a Node-only project can typecheck the generated client.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 27 files

Reply with feedback, questions, or to request a fix.

View guided diff | Re-trigger cubic

FetchAPI is now a method type that takes string, URL or Request and an
optional init. Middleware can call context.fetch(url) or pass a URL or
Request again, and a fetch with a string-only URL or a required init still
fits Configuration.fetchApi. None of these names need the DOM lib.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 27 files (changes from recent commits).

Requires human review: Auto-approval blocked because this review re-detected 1 unresolved issue already reported by Cubic.

View guided diff | Re-trigger cubic

This branch has not been deployed

No deployments
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.

[BUG][typescript-fetch] Generated runtime requires DOM types in native-fetch Node consumers

1 participant