fix(typescript): keep only declared response fields in typed error data$

Merged
AS
10h ago
+137-52

Why

Typed error classes in generated TypeScript SDKs keep the parsed error object in their enumerable data$ field. The inbound schema that produces that object also carries the Request, Response and raw body under request$, response$ and body$, so the transform could build the error's HTTP metadata. The transform passed the whole object to the constructor, so data$.request$ held the authenticated Request. Node's default inspection of a caught error, for example console.error(err), therefore printed the request headers, including Authorization.

data$ is typed and documented as the declared response fields only, so nothing public exposed those keys.

What changed

  • templates/templates/typescriptv2/includes/zod-error-templating.ts: the inbound transform destructures request$, response$ and body$ out of the parsed value and passes only the remaining fields to the constructor. The remap path remaps that stripped object. Empty error types keep passing {}. The HTTP metadata still reaches the error through the existing constructor argument and accessors (rawResponse, headers, statusCode, body, and httpMeta in the envelope format).
  • templates/templates/typescriptv2/tests/primary/errors.test.ts: asserts data$ holds exactly the declared fields (error plus the envelope's httpMeta).
  • templates/templates/typescriptv2/tests/secondary/auth_additional.test.ts: new case in the flat-format variant. The SDK is built with basic auth and a random password, the call fails with a typed error, and util.inspect(err) must contain neither the password nor its Base64 Authorization value, while rawResponse is still set. This is the user-visible contract; it fails on main because data$.request$.headers printed the credential.
  • internal/features/tests.go: declares the new test ID; go generate adds it to the other targets' skip lists.
  • Review TypeScript SDK regenerated: the eight typed error files gain the destructure, plus gen.lock checksums.
  • Changeset: fix, patch bump, errors feature, typescriptv2 target.

Observable change for SDK users: Object.keys(err.data$) and inspection output no longer include request$, response$ or body$. These keys were absent from the generated XData type and the README, so TypeScript callers could not have read them without a cast.

Testing

  • Parsed errors through the rebuilt review SDK's $inboundSchema for both a plain and a remapped error type: data$ holds only the declared fields and util.inspect(err) no longer contains the bearer token, while rawResponse is still populated.
  • make lint, make check-template-typescriptv2, TARGET=review make build-typescriptv2 pass.
  • Ran the secondary auth_additional.test.ts file against the regenerated secondary SDK: 5 passed. Temporarily restored the old transform in the generated error class and re-ran: the new case fails on the Base64 credential, the other 4 pass.
  • Not run locally: the full primary and secondary suites; the primary change is a key-set assertion on an already exercised path and runs in CI.
  • Note on scope: in the envelope-http format the documented err.httpMeta.request still carries the request, so an inspect-level assertion can only hold in the flat format; that is why the end-to-end case lives in the secondary variant.

Public-safety check

  • This change contains no credentials, customer documents, private repository URLs, private filesystem paths, or unredacted private logs.
  • Title, body, comments, and commit messages name no customers or customer-derived identifiers, private paths or trackers, or workflow provenance, and are understandable without private context (.claude/skills/public-repo-communication/SKILL.md).
  • Generated fixtures and review SDK changes are public-safe.
  • I reviewed git diff --check.

Summary by cubic

Stops typed error classes in generated TypeScript SDKs from leaking the authenticated Request—including its Authorization header—when an error is logged.

The inbound transform previously passed the whole parsed object to the error constructor, which stored it as the enumerable data$ field. That object carried the transport keys request$, response$, and body$ alongside the parsed response fields, so console.error(err) or util.inspect(err) printed request headers. The transform now strips those three keys and passes only the declared response fields, while HTTP metadata still reaches the error through rawResponse, headers, statusCode, body, and httpMeta.

Bug Fixes

  • Destructures transport keys out of parsed values in the typescriptv2 error template, regenerated SDK error models, and the error schema snapshot.
  • Asserts data$ holds only the declared fields and that inspecting a basic-auth SDK error reveals neither the password nor its Base64 Authorization value.
  • Adds a patch-bump changeset for the errors feature.

Behavior change for SDK users: Object.keys(err.data$) and inspection output no longer include request$, response$, or body$. The generated XData type never exposed these keys, so TypeScript callers could only reach them via a cast.