Add ky HTTP client support

TO
4mo ago
+2030-9

Problem

PR #1601 added initial ky support but has been stale for a while and is now in a conflicting state. Its template implementation was also incomplete — it returned a KyResponse type instead of the project's HttpResponse<D,E> wrapper, and dropped support for format, unwrapResponseData, cancelToken, customFetch, and disableThrowOnError.

Separately, the generate command had a latent bug where any truthy --http-client value was silently coerced to axios rather than being used as the literal string.

Solution

Redone from scratch with full feature parity against the Fetch client:

  • templates/base/http-clients/ky-http-client.ejs — new template backed by ky.create({ fetch: customFetch ?? fetch, throwHttpErrors: false }). Preserves the HttpResponse<D,E> response wrapper, ResponseFormat, manual query serialization (not ky's searchParams), secure/securityWorker, cancelToken/AbortController, format parsing, disableThrowOnError, unwrapResponseData, and baseApiParams/mergeRequestParams. Drop-in compatible with procedure-call.ejs.
  • src/constants.ts — added KY: "ky" to HTTP_CLIENT.
  • types/index.ts — widened the hard-coded httpClientType union to include "ky".
  • index.ts — fixed the --http-client CLI resolution bug; the value is now validated against Object.values(HTTP_CLIENT) and honored directly, with the legacy --axios boolean as a fallback.
  • templates/default/procedure-call.ejs and templates/modular/procedure-call.ejs — changed the FORM_DATA response-format predicate from isFetchTemplate ? to !== HTTP_CLIENT.AXIOS ? so ky (like fetch) uses "formData" rather than "document".
  • package.json — added ky as a devDependency so generated output typechecks in CI.
  • README.md — documented ky alongside Fetch and Axios.
  • .changeset/add-ky-http-client.md — minor version bump.

Verification

bun install
bun run build
bun run test          # 93 files, 277 tests — all passing, no type errors
bun run format:check  # clean

# CLI smoke test
sta generate -p tests/spec/ky/schema.json -o /tmp/out -n schema.ts --http-client ky

Three new spec test dirs cover the ky client: tests/spec/ky/ (base), tests/spec/kySingleHttpClient/ (--single-http-client), and tests/spec/jsKy/ (--js / JS translation), mirroring the existing axios test coverage.

Closes #1601


Summary by cubic

Adds ky as a first-class HTTP client with full Fetch parity and fixes --http-client handling so the chosen client is respected. Disables ky’s default retry/timeout for parity and restores the sta CLI alias.

  • New Features

    • Support --http-client ky and httpClientType: "ky"; generated client uses ky.create({ fetch: customFetch ?? fetch, throwHttpErrors: false, timeout: false, retry: 0 }) with the same HttpResponse<D,E>/unwrapResponseData behavior as Fetch.
    • Parity with Fetch: secure/securityWorker, cancelToken (AbortController), manual query serialization, format, disableThrowOnError, customFetch, and baseApiParams merge.
    • Non-axios clients now use "formData" for form-data responses.
    • Docs updated to include ky; added ky as a devDependency; added TS/JS and single-http-client tests for ky.
  • Bug Fixes

    • CLI now validates and honors --http-client ("fetch" | "axios" | "ky"); invalid values throw with a clear message; legacy --axios still works.
    • Restored sta bin alias so both sta and swagger-typescript-api CLIs work.
    • Ensured AbortController cleanup runs on errors via .finally().