Skip to content

feat: warn about deprecated operations at runtime via onDeprecation - #314

Open
mfal wants to merge 4 commits into
feat/emit-deprecated-operationsfrom
feat/runtime-deprecation-warnings
Open

mfal wants to merge 4 commits into
feat/emit-deprecated-operationsfrom
feat/runtime-deprecation-warnings

Conversation

@mfal

@mfal mfal commented Oct 15, 2026

Copy link
Copy Markdown
Member

Refs #166. Stacked on #313: the base branch is feat/emit-deprecated-operations. Retarget to master once #313 is merged.

#313 emits deprecated operations and marks them @deprecated. That only helps people whose editor shows JSDoc. This PR adds a runtime signal: calling a deprecated operation warns once per operation by default, and consumers can handle the deprecation themselves through a structured onDeprecation hook.

const api = MittwaldAPIClient.newWithToken(token, {
  onDeprecation: Commons.oncePerOperation((event) => logger.warn("deprecated API call", event)),
});

What changes

Generator

  • Operation.compileDescriptor adds deprecated: true to the runtime descriptor of deprecated operations. Active descriptors stay as they are.

Commons

  • OpenAPIOperation gets an optional deprecated?: boolean.
  • ApiClientBase takes an optional second constructor argument ApiClientOptions with onDeprecation.
  • The hook receives a DeprecationEvent: operationId, method, path, source: "spec" | "response-header", and optional deprecation and sunset dates.
  • Default behaviour: defaultDeprecationHandler warns once per operation, process-wide.
    • In Node it calls process.emitWarning(msg, { type: "DeprecationWarning", code: "MITTWALD_API_DEPRECATED_OPERATION" }). So --no-deprecation, --throw-deprecation and process.on("warning") work as they do for Node's own deprecations.
    • Without process.emitWarning (browsers) it calls console.warn.
  • Response headers: Deprecation (RFC 9745) and Sunset (RFC 8594) are detected on every response.
    • Supported Deprecation values: the RFC 9745 structured date (@1688169599) and the older draft values (true, HTTP-date).
    • A Sunset header alone also counts as a deprecation.
    • Dates that can't be parsed are dropped, but the signal is kept.
    • A spec-deprecated operation gets one event with source: "spec", plus the header dates if present.
  • When the hook runs: after the response settles and before the caller's await resumes. A spec-deprecated operation whose request fails, e.g. with a network error, still reports.
  • Errors in the hook: anything the hook throws, and any promise it rejects, is swallowed. Deprecation reporting never fails an API call.
  • New exports: DeprecationEvent, DeprecationHandler, ApiClientOptions, defaultDeprecationHandler, emitDeprecationWarning, oncePerOperation, parseDeprecationHeaders, formatDeprecationMessage, deprecationWarningCode.

@mittwald/api-client

  • newUnauthenticated(options?), newWithToken(token, options?) and newWithCredentials(email, password, options?) take an optional MittwaldAPIClientOptions argument (v2 and v3-next). newWithCredentials passes it to both internal clients. Existing call sites are unchanged.
  • README: new section "Deprecated operations".

Generated client (feat: update generated client)

Does the mittwald API send Deprecation / Sunset today?

No. I checked this on 2026-10-15 against api.mittwald.de:

  • The v2 spec marks 206 operations as deprecated but declares neither header.
  • Authenticated 200 responses on four deprecated operations carry no Deprecation, Sunset, Link or Warning header. The four operations are GET /v2/user, GET /v2/users/self/personal-information, GET /v2/user/settings and GET /v2/user/feedback.
  • Error and redirect responses on other deprecated operations don't carry them either.
  • Browser caveat: the API sends no Access-Control-Expose-Headers. Even once it sends Deprecation/Sunset, browser clients can't read them until the API also exposes them, e.g. Access-Control-Expose-Headers: Deprecation, Sunset, Link. Node clients aren't affected.

The header detection is still part of this PR, because the API is likely to send these headers later. Until then every event has source: "spec".

Why an onDeprecation callback

The question is what a consumer needs when they call a deprecated operation. There are two cases:

  1. Most people should simply notice it.
  2. Some want to act on it: count it in their metrics, fail a CI run, log it to their own logger with context, or ignore it during a planned migration.

A structured callback covers both:

  • The data is structured. The event carries operationId, method, path, source and sunset as typed fields. Consumers can filter, aggregate or alert without parsing a message string.
  • It has one purpose and a clear contract. The hook means exactly "a deprecated operation was used". Unrelated warnings can't end up in it, and TypeScript can add fields later, such as a Link to the successor.
  • The client stays logger-agnostic. Logging is one line inside the handler, with whatever logger the app already uses. The client doesn't need a logging interface, levels or formatting.
  • Every occurrence reaches the hook. Without dedupe, call counts are correct for metrics. Dedupe is opt-in through oncePerOperation, and the default handler uses it, so the default stays quiet.
  • Opting out is trivial. onDeprecation: () => {}, which is explicit, works per client and is easy to find in code review.
  • It matches the existing style. It works like RequestOptions.onBeforeRequest: a plain function in an options object, with no new concepts.

Alternatives considered and rejected

Alternative Why rejected
Injected logger (logger: { warn(...) }) The client would decide level and message format, and consumers would get a string back. Metrics or failing CI would require parsing log lines. It would also add a logging interface we'd have to keep stable. A callback can call any logger, but a logger can't do everything a callback can.
Generic onWarn(message) hook String-based and untyped: sunset and operationId would sit inside the message. It would mix deprecations with any future warning, so every handler would need to tell them apart. Its contract would be vague ("some warning"), which makes later changes risky.
Axios interceptor, in the style of withEventConsistencyHandling An interceptor only sees the URL, not the operation descriptor, so it doesn't know operationId or the spec's deprecated flag without passing the descriptor through the Axios config. As an opt-in wrapper it would also be off by default, which defeats the purpose. Detection therefore lives in requestFunctionFactory, where the descriptor is at hand. The existing interceptors are unchanged.
Plain console.warn in Node as well It bypasses Node's warning system: --no-deprecation, --throw-deprecation, --trace-deprecation and process.on("warning") wouldn't apply. process.emitWarning with type: "DeprecationWarning" is the idiomatic Node signal.
No default, i.e. silent unless a hook is set Nobody would notice deprecations, and spotting them is the reason for this feature.
Dedupe in the client before the hook Custom hooks would lose the call count, and "once per client instance" isn't "once per process": newWithCredentials alone creates two clients. Dedupe in the default handler plus the oncePerOperation helper serves both use cases.
Per-request option in RequestOptions Deprecation handling is an app-wide policy. A per-request setting would mean passing the same option on every call for no benefit.
Letting errors from the hook bubble up A bug in telemetry code would turn a working API call into a failure. With --throw-deprecation Node still throws, because emitWarning throws on the next tick, outside our try.
JSDoc @deprecated only (#313) It doesn't reach plain JavaScript consumers, and it can't reflect deprecations the server announces at runtime through headers.

Review notes

  • Behaviour change: existing code that calls an operation that is now deprecated gets one DeprecationWarning per operation and process. That is intentional. Before feat(generator): emit deprecated operations with an @deprecated tag #313, deprecated operations didn't exist in the client, so this mainly affects operations that become deprecated in a future spec.
  • Compatible signature: ApiClientBase's constructor only gained an optional second parameter. The generated clients don't define their own constructor, so they inherit it.
  • Unchanged error timing: requestFunctionFactory still throws synchronously if onBeforeRequest throws. Only the execute promise is wrapped.
  • Tests: new deprecation.test.ts and ApiClientBase.deprecation.test.ts in commons, and two descriptor tests in Paths.test.ts.
    • They cover dedupe once per operation (across calls and clients), opting out with () => {}, a throwing hook, a rejecting async hook, header parsing, Sunset on spec-deprecated operations, and network errors.
    • Commons 129/129, generator 217/217. yarn nx run-many -t build lint passes.

🤖 Generated with Claude Code

mfal and others added 4 commits October 15, 2026 17:33
…hook

ApiClientBase takes an optional second argument with an onDeprecation
handler. It is called for operations whose descriptor carries
deprecated: true and for responses with a Deprecation (RFC 9745) or
Sunset (RFC 8594) header. The default handler warns once per operation
via process.emitWarning (DeprecationWarning) or console.warn in browsers.
Handler errors never affect the request.

Refs #166

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refs #166

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
newUnauthenticated, newWithToken and newWithCredentials take an optional
options argument that is passed to ApiClientBase, e.g. onDeprecation.

Refs #166

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Refs #166

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mfal mfal self-assigned this Oct 15, 2026
@mfal
mfal added this pull request to stack #315 October 15, 2026 15:35
@mfal
mfal marked this pull request as ready for review October 15, 2026 15:44
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