Skip to content

Generate binding types from API documentation - #316

Merged
tnkuehne merged 2 commits into
mainfrom
feat/service-binding-types
Nov 23, 2026
Merged

tnkuehne merged 2 commits into
mainfrom
feat/service-binding-types

Conversation

@tnkuehne

@tnkuehne tnkuehne commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Why

An app that calls another app's API writes the binding's type by hand, for example INVENTORY: { status(): Promise<{ configured: boolean }> }. Cloudflare's Service<T> needs the provider's source, which the caller does not have. When the provider changes a method, the caller's compiler does not notice; the error appears at runtime. Generated APIs cannot be copied by hand at all: the Linear template has 424 methods with generic selection types.

What changes

  • API: GET /api/v1/app-services/{service}/types?binding=LINEAR&entrypoint=default returns declarations generated from the active deployment's documentation, with the same authorization as describe.
  • CLI: widefleet services types linear --as LINEAR [--entrypoint NAME] [--output FILE] [--check]. Without --output it prints the file; --check fails with a usage error when the file differs from the active deployment, so CI notices provider changes, including renamed types. Line endings do not matter.
  • Generator (serviceBindingTypes in contracts):
    • The binding is a plain interface with every documented signature of the entrypoint, including type parameters, overloads and TSDoc, not Service<T>, so recursive types stay within the compiler's instantiation limits.
    • Results are typed as callers receive them: LINEAR.Result<R> replaces a question's requestState with the sealed string, and withInput(answer) returns the methods without fetch. fetch is declared only for HTTP entrypoints.
    • Documented types are declared as interface or type by their kind, with their type parameters, sorted by name so document order does not change the file. Field descriptions move onto their members where the definition is one object type.
    • Same-named types from different modules are named after their module, such as src_a_record_Entry; a name already taken gains underscores.
    • References the documentation does not declare, such as package classes, become unknown /* import("…").Token */ and are listed in the header.
    • A documented type named like the binding, or Promise, is refused with a message.
  • Rewriter: shortenTypeNames and the generator share one reference scanner (rewriteTypeReferences); shortening is unchanged.

Why the server generates the file

The documentation format is a storage contract of the platform (format 3) and the sealing of requestState is runtime behavior. Generating on the server keeps both in one place, so CLI releases do not depend on the stored format and the API offers the same types to every client. The CLI only downloads, writes and compares. Raw documentation access through the API is not added; no client needs it for this.

Verification

  • pnpm typecheck, oxfmt --check ., oxlint --type-aware --deny-warnings on changed files, cargo clippy, cargo test -p platform-core -p platform-cli
  • RUN_CLI_TESTS=1 service-binding-types.test.ts: a fixture app with a generic declaration with constraint and default, recursive types, same-named types from two modules, a package class, a question with requestState and an HTTP entrypoint is documented by the native extractor, generated, and a caller compiles against it with @tsconfig/strictest and skipLibCheck: false, including @ts-expect-error cases for constraints, missing arguments, unresolved types and withInput.
  • app-services-cli.test.ts: printing, --output, --check with a matching, CRLF, changed and missing file.
  • app-services.integration.test.ts: undocumented app, unknown entrypoint, taken binding name, other organization.
  • Contracts unit tests: naming of same-named types, unresolved references with type arguments and typeof, field descriptions, refused names.
  • Linear template (828 types, 1.5 MB documentation, extracted locally): generation takes about 0.2 s and yields 1.48 MB. templates/linear/tests/types.ts compiles against the generated file with @tsconfig/strictest and skipLibCheck: false, with only its import line changed, including all @ts-expect-error cases.
  • A small app that used four Linear methods through a hand-written binding type compiles with the generated file in place of its own and only its import line changed.

Documentation: #317 (draft).

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the previous compilation issue is addressed.

Summary

Adds generated TypeScript declarations for app service bindings through the API and CLI.

  • App service bindings now get TypeScript types from active API documentation.
  • The CLI can write or check generated app-service binding types.

Reviews (2) · Last reviewed commit: "Reject binding names that cannot name an..." · Reviewed by Greptile

Callers of another app's API wrote the binding's type by hand, because
they do not have the provider's source for Cloudflare's `Service<T>`.
The compiler could not notice when the provider changed a method, and
generated APIs with hundreds of generic methods could not be copied.

`GET /api/v1/app-services/{service}/types` turns the active deployment's
documentation into declarations for one entrypoint and binding name, and
`widefleet services types APP --as BINDING` prints or writes them.
`--check` fails when the file no longer matches, for CI.

The binding is a plain interface with each documented signature,
including type parameters and TSDoc, so recursive types stay within the
compiler's instantiation limits. Results are typed as callers receive
them: a question's `requestState` arrives sealed, and `withInput` returns
the methods. Documented types keep their declarations, with field
descriptions on their members. Same-named types from different modules
are named after their modules. References the documentation does not
declare become `unknown`, keep their text in a comment and are listed in
the header.
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Updated (UTC)
✅ Deployment successful!
View logs
widefleet-docs 8b2ff52 Nov 22 2026, 11:32 PM

@tnkuehne
tnkuehne merged commit 11f7870 into main Nov 23, 2026
17 checks passed
@tnkuehne
tnkuehne deleted the feat/service-binding-types branch November 23, 2026 08:42
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.

1 participant