Skip to content

Add write status/media-type overrides and a get query override - #72

Merged
thomasfosterau merged 3 commits into
mainfrom
claude/effect-jsonapi-handler-blockers-so173v
Aug 16, 2026
Merged

thomasfosterau merged 3 commits into
mainfrom
claude/effect-jsonapi-handler-blockers-so173v

Conversation

@thomasfosterau

Copy link
Copy Markdown
Owner

Closes the three gaps blocking the downstream consumer's handler-layer adoption, recorded as blockers E, F and G in thomasfosterau/website#1274's Phase 1 verdict (2026-08-16). One commit per ask, three minor changesets, so the release lands as 0.11.0 — the @since tags on the new surface say 0.11.0.

All three were re-verified as still present against main (cd0d517) before implementing — the verdict inspected published 0.10.0, and nothing since touched them. Measured, not inferred:

Gap Verified on cd0d517
E Endpoint.create(Article, { success: … }) → success statuses [201] (the caller's schema re-stamped)
F POST /articles with Content-Type: application/json → 415 Unsupported content-type; application/vnd.api+json → 201
G include: ["author","author"] → decode Failure; "author,author" → Success

Each is opt-in and defaults to today's behaviour, so a ^0.10.0 → ^0.11.0 bump is a no-op until a call site chooses to use one. Every ask carries a regression test pinning the default.


1. status on Endpoint.create / Endpoint.update — blocker E

create stamped asJsonApi(success, 201) unconditionally, and asJsonApi pipes HttpApiSchema.status last, so even a caller-supplied success was re-stamped 201. WriteConfig carried no status, where DeleteConfig has had one since 0.9.0.

Endpoint.create(Article, { status: 200 })
Endpoint.update(Article, { status: 202 })

Group.resource(Article, { endpoints: { create: { status: 200 }, update: { status: 200 } } })

Defaults to 201 for create and 200 for update, whether or not success is given. update takes it for symmetry — its default is the status HttpApi gives any success schema carrying a body, so passing nothing is exactly today. Only the status changes; path, params, payload, success schema, errors and middleware are untouched.

2. payloadMediaType on Endpoint.create / Endpoint.update — blocker F

Both constructors wrapped their payload — the override included — in asJsonApi, so the router's request registration was always application/vnd.api+json with no seam. Effect's router matches an incoming Content-Type against that registration and answers 415 on a mismatch, so a host that enforces §6 at its own seam and then hands the router a write relabelled application/json had it rejected before any handler saw it.

Endpoint.create(Article, { payload: Article.createInput, payloadMediaType: "application/json" })

Group.resource(Article, { endpoints: { create: { payloadMediaType: "application/json" } } })

The registration moves rather than widens, so opting in is a statement of which label the host dispatches. Only the request registration changes: the payload schema, the response media type, path, params, errors and middleware are untouched. Pair it with Middleware.layerHostNegotiated, or the package's own §5 check rejects what the host admitted — the request-half counterpart to the negotiation half ContentNegotiationPassthrough already covers. asMediaType in internal/media.ts is the seam; everything the package emits itself still goes through asJsonApi, so no other body moves.

3. Repeated ?include= keys, and GetConfig.query — blocker G

Both halves of the ask, since they are the same gap on the read path.

The include parameter is widened, no opt-in. Query.schema declared include's wire form as Schema.optionalKey(Schema.String), so ?include=a&include=b — which UrlParams.toRecord decodes to an array — answered 400 while ?include=a,b, denoting the same set, answered 200. That is a spec bug in its own right: JSON:API treats the two spellings as equivalent, so identical requests were answered differently. The flat leaf is now a Repeatable codec accepting either and joining an array back to the canonical comma form the item codec already splits. Encoding still emits the single comma form, so nothing a client is handed changes shape, and an unknown or unlisted path (including under a constrained paths allow-list) is still the same 400 in either spelling.

sort and fields[TYPE] deliberately keep their single-string wire form: the failing case is include, and widening the rest is a wire-contract change nobody has asked for. Say the word and they can follow.

Endpoint.get gains query, the override Endpoint.list has carried since 0.9.0 — any Schema.Top, defaulting to the composed include / fields parameters, with those options ignored once it is given:

Endpoint.get(Article, {
  query: Schema.Struct({
    include: Schema.optionalKey(Schema.String),
    includeDeleted: Schema.optionalKey(Schema.Literals(["true", "false"]))
  })
})

Group.resource(Article, { endpoints: { get: { query: GetArticle } } })

Only the query changes; the :id path param, success document, errors and middleware are untouched.

One type-level note: Endpoint.get's type parameter list gains QuerySchema before Success, mirroring list's order. That is visible only to code instantiating typeof Endpoint.get<…> explicitly — never to a normal call site, where the options are inferred.


Symmetry

This is the same escape-hatch shape the package has now accepted four times, applied to the endpoints that still lacked it:

This PR Precedent
WriteConfig.status DeleteConfig.status (#68)
GetConfig.query ListConfig.query (#68)
WriteConfig.payloadMediaType WriteConfig.payload (#61) / ContentNegotiationPassthrough (#61), on the request half

with the success overrides (#70) as the fourth. Each is opt-in, each defaults to today's behaviour, each is threaded through Group.resource's per-endpoint config.

Tests

pnpm run check green locally — 467 package tests plus the three example suites (39 / 36 / 33). The new coverage is on the wire wherever the wire is what changed:

  • Status — a real web handler, POST and PATCH, pinning 201/200 without the option (with and without a success override) and 200/202 with it, for the constructors and for Group.resource.
  • Media type — a real web handler at both labels, both directions: default registration accepts vnd.api+json and 415s application/json, the override does the reverse, and the response media type stays vnd.api+json.
  • Include — decode-level equivalence of the two spellings (including the mixed form), the encode direction, the 400s that must survive, and a real web handler serving 200 for ?include=author&include=comments and 400 for ?include=author&include=nope.
  • get query — the composed default, the replacement, the feature options being ignored, the type-level identity, and both Endpoint.resource and Group.resource threading.

Generated by Claude Code

claude added 3 commits August 16, 2026 23:04
`Endpoint.create` stamped `asJsonApi(success, 201)` unconditionally, and
`asJsonApi` pipes `HttpApiSchema.status` last, so even a caller-supplied
`success` was re-stamped 201. `WriteConfig` carried `name` / `path` / `errors` /
`meta` / `payload` / `success` and no `status`, where `DeleteConfig` has had one
since 0.9.0. An api whose whole write surface answers 200 — or that accepts a
write for later application and answers 202 — could not adopt the constructors
without a wire change on every create endpoint.

Add an optional `status` to `Endpoint.create` and `Endpoint.update`, defaulting
to today's 201 and 200 whether or not `success` is given, and thread it through
`Endpoint.resource` / `Group.resource`'s per-endpoint `create` / `update`
config. `update` takes it for symmetry: its default is the status `HttpApi`
gives any success schema carrying a body, so passing nothing is exactly today.

Only the status changes; path, params, request payload, success schema, errors
and middleware are untouched. Regression tests drive a real web handler and
pin 201/200 for both constructors and for the generated group, with and
without a `success` override.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NcH6RU1HzkH6U665DjDQug
`Endpoint.create` and `Endpoint.update` wrapped their `payload` — the override
included — in `asJsonApi`, so the router's *request* registration was always
`application/vnd.api+json` with no way out. Effect's router matches an incoming
`Content-Type` against that registration and answers 415 on a mismatch, so an
api whose host enforces JSON:API §6 at its own seam and then dispatches writes
to the router relabelled `application/json` had every package-built write
rejected before a handler saw it — the situation
`Middleware.ContentNegotiationPassthrough` already exists for, on the request
half rather than the negotiation half.

Add an optional `payloadMediaType` to both constructors, defaulting to today's
JSON:API media type, and thread it through `Endpoint.resource` /
`Group.resource`'s per-endpoint `create` / `update` config. `asMediaType` in
`internal/media.ts` is the seam; everything the package emits itself still goes
through `asJsonApi`, so no other body moves.

Only the request registration changes: the payload schema, the response media
type, path, params, errors and middleware are untouched — and the registration
moves rather than widens, so an api that opts in is stating which label its
host dispatches. Round-trip tests drive a real web handler at both labels and
pin the vnd.api+json default, for the constructors and the generated group.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NcH6RU1HzkH6U665DjDQug
Two halves of the same gap on the read path.

`Query.schema` declared `include`'s wire form as
`Schema.optionalKey(Schema.String)`, so the repeated-key spelling
`?include=a&include=b` — which `UrlParams.toRecord` decodes to an array —
failed to decode and answered 400, while `?include=a,b`, which denotes the same
set, answered 200. JSON:API treats the two spellings as equivalent, so that was
a bug in its own right: identical requests answered differently. The flat leaf
is now a `Repeatable` codec accepting either, joining an array back to the
canonical comma form the item codec already splits. Encoding still emits the
single comma form, so nothing a client is handed changes shape, and an unknown
or unlisted path is still the 400 it was in either spelling. `sort` and
`fields[TYPE]` keep their single-string wire form — the failing case is
`include`, and widening the rest is a wire-contract change nobody has asked for.

`Endpoint.get` gains the `query` override `Endpoint.list` has carried since
0.9.0: any `Schema.Top`, defaulting to the composed `include` / `fields`
parameters, with those options ignored once it is given. Threaded through
`Endpoint.resource` / `Group.resource`'s per-endpoint `get` config, with
`GetConfig.query` alongside `ListConfig`'s. `get`'s type parameter list gains
`QuerySchema` before `Success`, mirroring `list`.

Only the query changes; the `:id` path param, success document, errors and
middleware are untouched. Regression tests pin the composed default for both
spellings and drive a real web handler over `?include=a&include=b`.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NcH6RU1HzkH6U665DjDQug
@thomasfosterau
thomasfosterau merged commit cfe8fc3 into main Aug 16, 2026
2 checks passed
@thomasfosterau
thomasfosterau deleted the claude/effect-jsonapi-handler-blockers-so173v 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