JSON:API mapping guide
Endpoints extending @app/frameworks/jsonapi should keep index.js minimal and express transforms in mapping.js (+ optional helpers/compute.js).
Supported sections (overview)
| Section | Role |
|---|---|
map |
Field-to-field mapping, nested paths, constant shaping |
pluck |
Extract subtrees from source objects |
rewrite |
Rename or restructure keys declaratively |
static |
Inject constant values into output |
group |
Array/collection handling patterns |
compute |
References to functions in helpers/compute.js |
parse |
Pre/post steps, sometimes execute hooks |
Exact keys match internal jsonapi handler expectations — copy structure from a nearby endpoint in the same product area.
Design principles
- Prefer declarative — if it is a straight field rename or pluck, use
map/pluck/rewrite. - Isolate imperative logic — use
computefor rounding, date formatting, or conditional branches that do not fit declarative rules. - One export in
compute.js—utils.importwarns on ambiguous modules. - Variable overrides — for
{version}-style routes, you can add.../{value}/mapping.jsto override base mapping.
Querystrings → backend params
- Put
size→limit,from→offset, filter construction inhelpers/querystrings.jssojsonapi.handlerbuilds consistent upstream requests.
Debugging
Use:
?debugga=mapping— mapping output?debugga=compute— compute traces?debugga=source— raw upstream payload?debugga=request.querystrings— parsed query
Common pitfalls
- Null nested paths — guard in
computeor ensureplucktolerates missing nodes. - Duplicate validation — do not re-check query params in
index.jsifconfig.jsalready defines them. - Large payloads — avoid logging full
sourcein prod; usedebuggaonly when needed.