Skip to content

Add a success override to the primary-data endpoints - #70

Merged
thomasfosterau merged 1 commit into
mainfrom
claude/effect-jsonapi-success-override-t7bf2w
Aug 16, 2026
Merged

thomasfosterau merged 1 commit into
mainfrom
claude/effect-jsonapi-success-override-t7bf2w

Conversation

@thomasfosterau

@thomasfosterau thomasfosterau commented Aug 16, 2026 •

Copy link
Copy Markdown
Owner

Completion note for thomasfosterau/website#1274 (Phase 0b). This note was meant to be posted
as a comment on that issue, but this session's GitHub access is scoped to thomasfosterau/effect-jsonapi
and attaching another repository needs an interactive approval that a non-interactive session can't
give — so it lives here instead. Please copy it over.

What landed. Endpoint.get / list / create / update now take an optional success
schema, the last handler-layer override Phase 0b listed as blocking Group.resource adoption. Any
Schema.Top is accepted — like WriteConfig.payload (#61), not just document-shaped derivations —
and the default is exactly today's behaviour, resource.document() / resource.collection().
Threaded through Endpoint.resource / Group.resource's per-endpoint get / list / create /
update config, so the generated surface can adopt it without being spelled out by hand.

Shape of the new surface.

// constructors — a trailing `Success` type parameter, defaulting to the resource's document
Endpoint.get(Article,    { success?: Schema.Top })   // default: Article.document()
Endpoint.list(Article,   { success?: Schema.Top })   // default: Article.collection()
Endpoint.create(Article, { success?: Schema.Top })   // default: Article.document(), still 201
Endpoint.update(Article, { success?: Schema.Top })   // default: Article.document()

// per-endpoint config: GetConfig / ListConfig / WriteConfig each gain `readonly success?: Schema.Top`
Group.resource(Article, {
  endpoints: {
    get:    { success: Document.DataDocument(WireArticle) },
    list:   { success: Document.CollectionDocument(WireArticle) },
    create: { success: Document.DataDocument(WireArticle) },
    update: { success: Document.DataDocument(WireArticle) }
  }
})

For the wire-variant case: const WireArticle = Schema.Struct({ ...Article.fields, links: Schema.optionalKey(Schema.Struct({ self: Schema.optionalKey(Schema.String) })) }) — links.self
then decodes as a plain string on every generated-client call site instead of URL. An override
is served as application/vnd.api+json at the same status as before, and the document envelope is
untouched, so Handlers.offsetPaginationLinks still populates an overridden list's top-level
links (covered by a round-trip test). payload and success compose independently.

Release status. Merged to main (this PR, #70) with CI green on Node 20.x and 22.x. The
changesets "Version Packages" PR #71 is open and bumps to 0.10.0 — I could not merge it:
branch protection requires two status checks, and CI never runs on that PR because it is opened by
the changesets action with GITHUB_TOKEN, and bot-authored PRs don't trigger pull_request
workflows. So merging #71 needs @thomasfosterau (admin bypass of the required checks); the
release.yml run on the resulting push to main is what publishes to npm. Until then the
published version is still 0.9.0 — everything above is on main, not yet on the registry.


Endpoint.get, list, create and update bound their response unconditionally to the resource's
document() / collection(), so the only shape a handler could answer with was the resource
itself, and GetConfig / ListConfig / WriteConfig carried no seam for anything else — verified
against main at c5f7064 (published 0.9.0).

The failing case

A consumer whose resources declare links.self as Schema.URLFromString — as the package's own
Document.Link does, decoding an absolute reference to a URL — but whose assembler stringifies
every link before the document leaves the server needs a wire variant of the resource in its
success documents: plain-string link members. With the success schema hard-bound to
resource.document(), links.self decodes as URL where every generated-client call site consumes
a string, and there is no seam to supply the wire variant.

The change

An optional success schema on all four constructors, defaulting to today's documents, mirroring
the two escape hatches already accepted — WriteConfig.payload (#61) and DeleteConfig.success
(#68). Any Schema.Top is accepted, like payload — not just document-shaped derivations.

const WireArticle = Schema.Struct({
  ...Article.fields,
  links: Schema.optionalKey(Schema.Struct({ self: Schema.optionalKey(Schema.String) }))
})

Endpoint.get(Article, { success: Document.DataDocument(WireArticle) })
Endpoint.list(Article, { success: Document.CollectionDocument(WireArticle), page: Query.Page.Offset })
Endpoint.create(Article, { success: Document.DataDocument(WireArticle) })   // still 201
Endpoint.update(Article, { payload: Article.updateInput, success: Document.DataDocument(WireArticle) })

Threaded through Endpoint.resource / Group.resource's per-endpoint config, so a generated group
can adopt the wire variant without being spelled out by hand:

Group.resource(Article, {
  endpoints: {
    get: { success: Document.DataDocument(WireArticle) },
    list: { success: Document.CollectionDocument(WireArticle) },
    create: { success: Document.DataDocument(WireArticle) },
    update: { success: Document.DataDocument(WireArticle) }
  }
})

An override is served as application/vnd.api+json like every other body in the package, at the
same status as before (201 for create). The document envelope is untouched, so
Handlers.offsetPaginationLinks still populates the top-level links of an overridden list. Only
the response changes — path, params, query parameters, request payload, errors and middleware are
unaffected, and payload and success compose independently.

Surface

  • Endpoint.get / list / create / update: readonly success?: Schema.Top (a new trailing
    Success type parameter, defaulting to the resource's document / collection at the effective
    document meta).
  • GetConfig, ListConfig, WriteConfig: readonly success?: Schema.Top, @since 0.10.0.
  • No breaking changes: the defaults are exactly resource.document() / resource.collection().

Tests

12 new tests in src/Endpoint.test.ts, in the shape of the #61 / #68 suites:

  • regression pins on the defaults — links.self still decodes to a URL for get / create /
    update / list, both hand-written and via Endpoint.resource;
  • the wire variant decodes links.self to a plain string, at the endpoint and through
    Endpoint.resource / Group.resource;
  • type-level assertions that the overridden success is the supplied schema and the default is
    Article.document() / Article.collection(), and that payload and success compose;
  • an HTTP round trip over get / list / create: the client receives links.self as a string
    (expectTypeOf(...).toEqualTypeOf<string | undefined>()), list carries
    Handlers.offsetPaginationLinks output in its top-level links, and create still answers 201;
  • name / method / path / middleware are unchanged by the override.

pnpm check (typecheck, lint, format, 436 unit tests + the three example suites) and pnpm docgen
pass. Changeset: minor. README gains an "Overriding the success document" section.

🤖 Generated with Claude Code

https://claude.ai/code/session_019cwVHMAmfnFV1PQzuXaNcA

`get`, `list`, `create` and `update` bound their response unconditionally to
the resource's `document()` / `collection()`, so the only shape a handler could
answer with was the resource itself, and `GetConfig` / `ListConfig` /
`WriteConfig` had no seam for anything else. Apis whose assembler emits a
narrower shape than the resource's decoded types have one to declare: a wire
variant whose `links.self` is a plain string, where the resource's own
`Document.Link` decodes an absolute reference to a `URL` and hands every
generated-client call site the wrong type.

Add an optional `success` schema — any `Schema.Top`, like `payload` — to all
four constructors, defaulting to today's documents. An override is served as
`application/vnd.api+json` at the same status as before (201 for `create`), and
the document envelope is untouched, so `Handlers.offsetPaginationLinks` still
applies to an overridden `list`. Thread it through `Endpoint.resource` /
`Group.resource`'s per-endpoint config. Only the response changes; path,
params, query parameters, request payload, errors and middleware are untouched.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019cwVHMAmfnFV1PQzuXaNcA
@thomasfosterau
thomasfosterau merged commit 76ebe8f into main Aug 16, 2026
2 checks passed
@thomasfosterau
thomasfosterau deleted the claude/effect-jsonapi-success-override-t7bf2w branch September 15, 2026 07:11
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