Skip to content

feat(builders): typed factories for 5 discriminated unions (#1386) - #1598

Merged
bokelley merged 4 commits into
mainfrom
bokelley/issue-1386-typed-factories
May 8, 2026
Merged

bokelley merged 4 commits into
mainfrom
bokelley/issue-1386-typed-factories

Conversation

@bokelley

@bokelley bokelley commented May 8, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Five new typed-factory namespaces, mirroring the existing
asset-builders /
render-builders /
format-asset-slot-builders
pattern. Each injects the discriminator at the typed-builder level using the
spread-order trick from asset-builders.ts:34, so adopters can't write a
missing-discriminator value at compile time and a runtime cast that smuggles
a conflicting discriminator via fields cannot clobber it.

Factory Wire shape Variants SHAPE-GOTCHAS
activationKey ActivationKey (type discriminator) .segment / .keyValue §1
signalId SignalID (source discriminator) .catalog / .agent §2
previewCreative 3-way oneOf on response_type .single / .batch / .variant §4
buildCreativeReturn 4-arm BuildCreativeReturn union .single / .multi / .singleEnveloped / .multiEnveloped §5
mediaBuyDeliveryNotification webhook notification_type on GetMediaBuyDeliveryResponse .scheduled / .final / .delayed / .adjusted / .windowUpdate (new)

Adapter migrations

Reference adapters now use the new factories where applicable (4 files,
several call sites each):

  • examples/signals-agent.ts — signal_id × 3, activation_key × 2
  • examples/hello_signals_adapter_marketplace.ts — signal_id × 1, activation_key × 2
  • examples/hello_creative_adapter_template.ts — buildCreative 2-arm + previewCreative.single
  • examples/hello_creative_adapter_ad_server.ts — buildCreative shaped envelope + previewCreative.single

The two as unknown as PreviewCreativeResponse / as unknown as BuildCreativeSuccess casts in the
ad-server adapter are gone — the typed factories return the wire shape directly.

Naming: avoid collision with v5 server helper

The new factory namespace is previewCreative (not previewCreativeResponse)
to avoid colliding with the v5 server-helper function of the same name that
is still re-exported at the top level of @adcp/sdk. Following the existing
urlRender / htmlRender precedent, the namespace is named after the wire
concept (no Response suffix) and reads naturally:
previewCreative.single({...}) — "build a preview-creative single response".

The top-level previewCreativeResponse v5 server-helper export is retained
as-is. Adopters who imported previewCreativeResponse and called it as a
function continue to work unchanged. The new factory namespace is a strictly
additive export under a non-colliding name.

Type-only regression tests

src/lib/server/decisioning/decisioning.type-checks.ts gains five new
@ts-expect-error blocks — one per factory — that pin the discriminator
as non-overridable. If the spread-order trick regresses (e.g. someone
removes the Omit<> from the parameter type), tsc --noEmit fails.

SHAPE-GOTCHAS recommendations

§1, §2, §4, §5 each gain a "Prevent at write time" block referencing the
new factory as the prevent-at-write-time fix.

Test plan

  • npx tsc --noEmit clean
  • npm run format:check clean
  • npm test — full suite passes
  • Type-checks regression: each factory's @ts-expect-error directive verified to fire when removed
  • Reference-adapter migrations compile and pass their existing storyboard tests
  • No top-level export collision: previewCreativeResponse (v5 helper function) and previewCreative (new factory namespace) coexist

Closes #1386.

🤖 Generated with Claude Code

bokelley added 3 commits May 8, 2026 09:36
…1386)

Adapter migrations, top-level exports, type-only regression tests, SHAPE-GOTCHAS recommendations, and changeset for the five typed factory namespaces shipped in 001d6ab.
@bokelley bokelley changed the title feat(builders): typed factories for ActivationKey, BuildCreativeReturn, MediaBuyDeliveryNotification, PreviewCreativeResponse, SignalID (#1386) feat(builders): typed factories for 5 discriminated unions (#1386) May 8, 2026
…oid v5 collision

The new factory namespace `previewCreativeResponse` collided with the
existing v5 server-helper *function* `previewCreativeResponse` re-exported
from `@adcp/sdk` top-level. Adopters who imported `previewCreativeResponse`
from the top level and called it as a function would have hit a silent
runtime TypeError after this PR.

Resolution:
- Rename the factory namespace `previewCreativeResponse` → `previewCreative`
  (no `Response` suffix), matching the existing `urlRender` / `htmlRender`
  precedent of naming after the wire concept.
- Restore the v5 `previewCreativeResponse` top-level re-export from
  `@adcp/sdk` for backwards compatibility. Adopters mid-migration who
  imported it and called it as a function continue to work unchanged.
- Rename source file `preview-creative-response-builders.ts` →
  `preview-creative-builders.ts`.
- Update SHAPE-GOTCHAS §4, the four reference-adapter migrations, the
  type-check regression block, the changeset, and the PR body.

The 3 underlying typed factory functions (`singlePreviewCreativeResponse`,
`batchPreviewCreativeResponse`, `variantPreviewCreativeResponse`) keep
their full names — only the grouped `.single` / `.batch` / `.variant`
accessor is renamed.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@bokelley
bokelley merged commit a0ad369 into main May 8, 2026
11 checks passed
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.

feat(builders): typed factories for ActivationKey, BuildCreativeReturn envelopes, MediaBuyDeliveryNotification

1 participant