Repository navigation
Description
Problem
The README lists this as a limitation:
Sparse fieldsets are advisory:
?fields[TYPE]=is decoded, validated and handed to your handler, but attribute projection in responses is handler logic (automatic projection would require post-processing responses against request state).
The reasoning against automatic projection is sound. But there's a middle ground the package doesn't offer: a transform hook on the document builders, so whatever per-resource processing a handler needs is applied once, uniformly, to every resource the document carries — primary data, every member of included, and the parent of a relationship document.
That uniformity is the whole difficulty. ?fields[articles]=title must project the primary resource and any included article; computed per-viewer meta must be stamped on both, or the same resource describes different capabilities depending on which slot it landed in. Today the handler has to remember to do it at each slot, and the builders give it no place to hang the logic.
Proposal
Handlers.data(article, {
included,
self,
onResource: (resource) => withMeta(project(resource, query.fields), viewer)
})applied to data, every deduped included member, and linkage's parent — after dedup, before the linkage check.
Plus the projection itself, which is mechanical and spec-defined rather than application logic:
Handlers.project(resource, fields) // fields: { readonly [type: string]: ReadonlyArray<string> }with the two behaviours every implementation needs: no requested set for this resource's type → return it unchanged (identity, not "project to nothing"), and project attributes only, leaving relationships / links / meta alone. (§Sparse Fieldsets does also cover relationship fields; worth deciding whether to support that or document it as out of scope.)
Why this belongs upstream
All three audited repos (website, preferenceflow, gardeneria) implement an identical projectFields:
const projectFields = <R extends ResourceLike>(resource: R, fields: AssembleOptions["fields"]): R => {
const requested = fields?.[resource.type];
if (!requested || requested.length === 0) return resource;
const requestedSet = new Set(requested);
const projectedAttributes: Record<string, unknown> = {};
for (const key of Object.keys(resource.attributes)) {
if (requestedSet.has(key)) projectedAttributes[key] = resource.attributes[key];
}
return { ...resource, attributes: projectedAttributes };
};…and then each has to thread it, together with a meta hook and a link coercion, through every slot by hand. The primary path:
const enriched = stringifyLinks(withViewerMeta(resource, options.viewer, options.capabilities));
const projected = projectFields(enriched, options.fields);the included path:
const included = Array.from(bucket.values()).map((r) =>
projectFields(stringifyLinks(withViewerMeta(r, options.viewer, options.capabilities)), options.fields)
);the collection path:
const enriched = resources.map((resource) =>
projectFields(stringifyLinks(withViewerMeta(resource, options.viewer, options.capabilities)), options.fields)
);
const included = options.included?.map((resource) => projectFields(stringifyLinks(resource), options.fields));— note that last line: the collection's included members are link-coerced and projected but not meta-stamped, while the single-resource path's are. Whether that's deliberate or a slip, it's the exact class of inconsistency a single hook removes by construction. The repos' own comment on the relationship-document path says the same thing about the parent:
The primary
datalinkage this function emits is bare resource identifiers per §8.4 — they carry norelationshipsof their own…
Five call sites per repo, fifteen in total, each of which has to remember the same three-step pipeline in the same order.
The per-viewer meta hook is worth its own mention: all three repos compute canEdit / canDelete server-side per resource per viewer, which is a completely standard JSON:API pattern (meta is the spec's home for exactly this), and all three discovered the same refinement — an empty merged meta must emit no member rather than meta: {}, since the spec treats an empty object as a present-but-empty meta.
Acceptance criteria
-
onResource(or equivalent) ondata/collection/linkage, applied to primary, every dedupedincludedmember, and a relationship document's parent. - Applied after dedup and before the full-linkage check.
-
Handlers.project(resource, fields)— identity when the type has no requested set;attributesonly. - An empty resulting
metaomits the member rather than emitting{}. - README limitation updated to point at the hook.
Activity
Merge main into the conformance kit