Skip to content

feat(topic): add getTopic() override for dynamic topic resolution - #30

Merged
jimmy-phantom merged 2 commits into
mainfrom
jimmy/topic-query-get-topic
May 20, 2026
Merged

jimmy-phantom merged 2 commits into
mainfrom
jimmy/topic-query-get-topic

Conversation

@jimmy-phantom

Copy link
Copy Markdown
Collaborator

Summary

  • Adds getTopic?(): string to TopicQuery, mirroring getPath?() on RESTQuery. When defined, it takes precedence over the static topic field and is evaluated at execution time against the resolved params, so a single class can express variable-segment-count or conditionally-shaped topics.
  • Makes the topic field optional (was abstract topic: string), matching path? on RESTQuery. Existing queries that define topic as a static template continue to work unchanged.
  • TopicQueryAdapter.send() and the TopicQuery.getConfig() unsubscribe cleanup now resolve via getTopic() when defined, otherwise topic. send() throws a clear error if neither is set.

Motivation

Consumers that need topics like prefix:a:b vs prefix:a:b:c vs prefix:a:b:c:d had to hand-author one class per shape because the static template is captured at extraction time and resolved via fixed [name] substitution, which can't express variable segment counts, optional trailing segments, or anything conditional on the actual param values. RESTQuery/RESTMutation already solved the same problem with getPath(). This brings TopicQuery to parity.

Example:

class LayoutQuery extends TopicQuery {
  params = { segments: t.array(t.string) };
  result = LayoutResult;

  getTopic() {
    return 'layout:' + this.params.segments.map(encodeURIComponent).join(':');
  }
}

Test plan

  • npm run check-types clean
  • npm run test:unit all 1138 tests pass, including 3 new tests under getTopic() override: basic resolution, variable-segment-count topics handled by one class, and getTopic() taking precedence when both fields are defined
  • npm run lint clean

🤖 Generated with Claude Code

jimmy-phantom and others added 2 commits May 20, 2026 11:32
Mirrors the existing getPath() pattern on RESTQuery. The static `topic`
template only supports fixed-shape topics, so consumers with
variable-segment-count or conditionally-shaped topics had to hand-author
one query class per shape. Subclasses can now override getTopic() to
compute the topic at execution time from resolved params; when defined
it takes precedence over the `topic` field, which becomes optional to
match `path?` on RESTQuery.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Pre-1.0 convention treats patch as backward-compatible (features and
fixes) and minor as breaking. This change is purely additive.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jimmy-phantom
jimmy-phantom requested a review from pzuraq May 20, 2026 18:49
@jimmy-phantom
jimmy-phantom merged commit 117531d into main May 20, 2026
1 check passed
@jimmy-phantom
jimmy-phantom deleted the jimmy/topic-query-get-topic branch May 22, 2026 01:33
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.

2 participants