@haddowg/json-api-client 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -15,6 +15,9 @@ var StructuralGuardError = class extends Error {
15
15
  /**
16
16
  * Thrown for any non-2xx JSON:API response. Carries the parsed error document plus
17
17
  * expressive status matchers and pointer-grouping helpers.
18
+ *
19
+ * `C` types each error object's `code` (see {@link JsonApiErrorObject}). It is a compile-time
20
+ * hint only: the runtime never checks a code against it.
18
21
  */
19
22
  var JsonApiError = class extends Error {
20
23
  status;
@@ -79,6 +82,60 @@ var JsonApiError = class extends Error {
79
82
  return out;
80
83
  }
81
84
  };
85
+ /**
86
+ * True when `err` is a {@link JsonApiError} and any of its error objects carries `code`. Takes
87
+ * any string, so it also matches codes the server never documented. A generated client exports a
88
+ * `hasErrorCode` bound to its server's `ErrorCode` union, which rejects a misspelt code at
89
+ * compile time; reach for this one only to match an undocumented code.
90
+ */
91
+ function hasErrorCode(err, code) {
92
+ return err instanceof JsonApiError && err.errors.some((e) => e.code === code);
93
+ }
94
+ //#endregion
95
+ //#region src/pagination.ts
96
+ /** The wire key for each role when the server keeps the default names. */
97
+ const CANONICAL_PAGE_KEYS = {
98
+ page: {
99
+ number: "number",
100
+ size: "size"
101
+ },
102
+ offset: {
103
+ offset: "offset",
104
+ limit: "limit"
105
+ },
106
+ cursor: {
107
+ after: "after",
108
+ before: "before",
109
+ size: "size"
110
+ },
111
+ fixed: { number: "number" },
112
+ none: {}
113
+ };
114
+ /**
115
+ * Resolve the strategy that answers a read from the endpoint's paginator and the `page` the
116
+ * request sends. A menu answers with the arm named by `page.kind`, else its `default`.
117
+ */
118
+ function resolvePaginator(paginator, page) {
119
+ if (paginator === void 0) return {
120
+ kind: "none",
121
+ keys: {}
122
+ };
123
+ if (typeof paginator === "string") return {
124
+ kind: paginator,
125
+ keys: CANONICAL_PAGE_KEYS[paginator]
126
+ };
127
+ if (paginator.strategy !== "multi") return {
128
+ kind: paginator.strategy,
129
+ keys: paginator.keys
130
+ };
131
+ const picked = page?.["kind"];
132
+ const arm = typeof picked === "string" && Object.hasOwn(paginator.arms, picked) ? picked : void 0;
133
+ const resolved = resolvePaginator(paginator.arms[arm ?? paginator.default]);
134
+ return arm === void 0 ? resolved : {
135
+ ...resolved,
136
+ arm
137
+ };
138
+ }
82
139
  //#endregion
83
140
  //#region src/validate.ts
84
141
  /** Whether a `validate` option is the schema-driven config (vs a bare validator function). */
@@ -191,9 +248,10 @@ const defineGetter = (target, key, get) => {
191
248
  * full resource) — a structural guess can't tell them apart for an attribute-less,
192
249
  * relation-less resource, so the caller passes the surface it issued.
193
250
  *
194
- * `paginatorOverride` sets the top-level array's `$page.kind` for a related/relationship read,
195
- * where the paginator is the RELATION's (which may diverge from the related type's collection —
196
- * D6a) and an empty page carries no member to sniff a type from. `primaryType` is preferred for a
251
+ * `paginatorOverride` sets the top-level array's `$page` for a related/relationship read, where the
252
+ * paginator is the RELATION's (which may diverge from the related type's collection — D6a), for a
253
+ * menu read that picked an arm, and where an empty page carries no member to sniff a type from. A
254
+ * bare kind is read with its canonical wire keys. `primaryType` is preferred for a
197
255
  * top-level collection read (statically-known); both override the sniff-from-`data[0]` fallback.
198
256
  */
199
257
  function materialise(doc, ctx, linkage = false, primaryType, paginatorOverride) {
@@ -246,7 +304,7 @@ function buildNode(raw, env, index, nodes, ctx) {
246
304
  for (const [name, rel] of Object.entries(rels)) {
247
305
  if (!("data" in rel)) continue;
248
306
  const relDesc = resType?.relations[name];
249
- node[name] = hydrateRelation(rel, relDesc?.cardinality, relatedPaginatorKind(relDesc, ctx), env, index, nodes, ctx);
307
+ node[name] = hydrateRelation(rel, relDesc?.cardinality, resolvePaginator(relatedPaginator(relDesc, ctx)), env, index, nodes, ctx);
250
308
  }
251
309
  attachResourceEnvelope(node, raw, env, rels);
252
310
  return node;
@@ -256,13 +314,13 @@ function buildNode(raw, env, index, nodes, ctx) {
256
314
  * linked-but-not-included member stays an identifier, an empty to-one is `null`, and a
257
315
  * to-many is always an augmented array (of views and/or identifiers).
258
316
  *
259
- * `relatedKind` is the related type's paginator kind (from the descriptor), threaded so a
260
- * to-many array reports the same `$page.kind` it would on its dedicated endpoint.
317
+ * `related` is the relation's paginator (from the descriptor), threaded so a to-many array
318
+ * reports the same `$page` it would on its dedicated endpoint without a `page` argument.
261
319
  */
262
- function hydrateRelation(rel, cardinality, relatedKind, env, index, nodes, ctx) {
320
+ function hydrateRelation(rel, cardinality, related, env, index, nodes, ctx) {
263
321
  const data = rel.data;
264
- if (Array.isArray(data)) return augmentRelationArray(data.map((member) => resolveMember(member, env, index, nodes, ctx)), rel, relatedKind, ctx);
265
- if (data === void 0 && cardinality === "many") return augmentRelationArray([], rel, relatedKind, ctx);
322
+ if (Array.isArray(data)) return augmentRelationArray(data.map((member) => resolveMember(member, env, index, nodes, ctx)), rel, related, ctx);
323
+ if (data === void 0 && cardinality === "many") return augmentRelationArray([], rel, related, ctx);
266
324
  if (data === null || data === void 0) return data === null ? null : void 0;
267
325
  if (isObject$2(data)) return resolveMember(data, env, index, nodes, ctx, {
268
326
  links: rel.links,
@@ -369,33 +427,35 @@ function augmentArray(members, doc, ctx, linkage, primaryType, paginatorOverride
369
427
  const links = doc.links;
370
428
  defineGetter(arr, "$links", () => links);
371
429
  defineGetter(arr, "$meta", () => doc.meta);
372
- attachPage(arr, paginatorOverride ?? paginatorKind(doc, ctx, primaryType), pageMeta(doc.meta), links, ctx, linkage);
430
+ attachPage(arr, paginatorOverride === void 0 ? resolvePaginator(primaryPaginator(doc, ctx, primaryType)) : typeof paginatorOverride === "string" ? resolvePaginator(paginatorOverride) : paginatorOverride, pageMeta(doc.meta), links, ctx, linkage);
373
431
  return arr;
374
432
  }
375
433
  /**
376
- * A to-many relationship array: `$page`/links/meta come from the relationship object. `kind`
377
- * is the related type's paginator (so an in-document compound-include to-many reports the same
434
+ * A to-many relationship array: `$page`/links/meta come from the relationship object.
435
+ * `paginator` is the relation's (so an in-document compound-include to-many reports the same
378
436
  * discriminant it would when fetched at its own relationship/related endpoint — "one model,
379
437
  * three surfaces").
380
438
  */
381
- function augmentRelationArray(members, rel, kind, ctx) {
439
+ function augmentRelationArray(members, rel, paginator, ctx) {
382
440
  const arr = [...members];
383
441
  const links = rel.links;
384
442
  defineGetter(arr, "$links", () => links);
385
443
  defineGetter(arr, "$meta", () => rel.meta);
386
- attachPage(arr, kind, pageMeta(rel.meta), links, ctx, false);
444
+ attachPage(arr, paginator, pageMeta(rel.meta), links, ctx, false);
387
445
  return arr;
388
446
  }
389
447
  /** Attach `$page` + `$next()`/`$prev()` driven by link presence (count-free-safe). */
390
- function attachPage(arr, kind, meta, links, ctx, linkage) {
448
+ function attachPage(arr, paginator, meta, links, ctx, linkage) {
391
449
  const page = {
392
- kind,
450
+ kind: paginator.kind,
451
+ keys: paginator.keys,
393
452
  links: pageLinks(links)
394
453
  };
454
+ if (paginator.arm !== void 0) page.arm = paginator.arm;
395
455
  if (meta !== void 0) page.meta = meta;
396
456
  defineGetter(arr, "$page", () => page);
397
- define(arr, "$next", (options) => navigateLink(page.links.next, ctx, linkage, kind, options?.signal));
398
- define(arr, "$prev", (options) => navigateLink(page.links.prev, ctx, linkage, kind, options?.signal));
457
+ define(arr, "$next", (options) => navigateLink(page.links.next, ctx, linkage, paginator, options?.signal));
458
+ define(arr, "$prev", (options) => navigateLink(page.links.prev, ctx, linkage, paginator, options?.signal));
399
459
  }
400
460
  const navigateLink = (link, ctx, linkage, paginator, signal) => {
401
461
  if (link === void 0) return Promise.resolve(void 0);
@@ -422,18 +482,18 @@ const linkHref = (link) => {
422
482
  };
423
483
  const pageMeta = (meta) => isObject$2(meta?.["page"]) ? meta["page"] : void 0;
424
484
  /**
425
- * The document's paginator kind (from the primary type's descriptor; default `none`). Prefers the
426
- * statically-known `primaryType` (passed by the descriptor binding at the call site) so an EMPTY
427
- * page still reports the collection's real kind; falls back to sniffing the first member's type
428
- * (for a re-materialised `$next()` page or a related read, where the primary type isn't threaded).
485
+ * The document's paginator (from the primary type's descriptor). Prefers the statically-known
486
+ * `primaryType` (passed by the descriptor binding at the call site) so an EMPTY page still reports
487
+ * the collection's real kind; falls back to sniffing the first member's type (for a re-materialised
488
+ * `$next()` page or a related read, where the primary type isn't threaded).
429
489
  */
430
- function paginatorKind(doc, ctx, primaryType) {
490
+ function primaryPaginator(doc, ctx, primaryType) {
431
491
  const first = Array.isArray(doc.data) ? doc.data[0] : doc.data;
432
492
  const type = primaryType ?? (isObject$2(first) && typeof first["type"] === "string" ? first["type"] : void 0);
433
- return type !== void 0 ? ctx.descriptor[type]?.paginator ?? "none" : "none";
493
+ return type !== void 0 ? ctx.descriptor[type]?.paginator : void 0;
434
494
  }
435
495
  /**
436
- * A to-many relation's paginator kind. The relation's OWN related/relationship endpoint may
496
+ * A to-many relation's paginator. The relation's OWN related/relationship endpoint may
437
497
  * paginate differently than the related type's top-level collection (D6a), so prefer the
438
498
  * per-relation `paginator` (carried in the descriptor only when it diverges); otherwise fall back
439
499
  * to the related type's own paginator (the relationship and related endpoints both paginate that
@@ -441,11 +501,14 @@ function paginatorKind(doc, ctx, primaryType) {
441
501
  * resolves off its first declared related type (its members span types, matching the relationship
442
502
  * endpoint's first-member resolution).
443
503
  */
444
- function relatedPaginatorKind(relation, ctx) {
504
+ function relatedPaginator(relation, ctx) {
445
505
  if (relation?.paginator !== void 0) return relation.paginator;
446
506
  const relatedType = relation?.types[0];
447
- if (relatedType === void 0) return "none";
448
- return ctx.descriptor[relatedType]?.paginator ?? "none";
507
+ return relatedType === void 0 ? void 0 : ctx.descriptor[relatedType]?.paginator;
508
+ }
509
+ /** The kind a relation's related read answers with when it sends no `page`. See {@link relatedPaginator}. */
510
+ function relatedPaginatorKind(relation, ctx) {
511
+ return resolvePaginator(relatedPaginator(relation, ctx)).kind;
449
512
  }
450
513
  //#endregion
451
514
  //#region src/types.ts
@@ -475,6 +538,14 @@ const ATOMIC_MEDIA_TYPE = mediaType({ ext: [ATOMIC_EXT] });
475
538
  //#endregion
476
539
  //#region src/request.ts
477
540
  const isAbsolute = (path) => /^https?:\/\//i.test(path);
541
+ const RELATIVE_ORIGIN = "http://relative.invalid";
542
+ /** Resolve a server link against `baseUrl` per RFC 3986. An absolute link is kept verbatim. */
543
+ function resolveLink(link, baseUrl) {
544
+ if (isAbsolute(link)) return link;
545
+ if (isAbsolute(baseUrl)) return new URL(link, baseUrl).href;
546
+ const resolved = new URL(link, new URL(baseUrl, RELATIVE_ORIGIN));
547
+ return resolved.origin === RELATIVE_ORIGIN ? resolved.pathname + resolved.search + resolved.hash : resolved.href;
548
+ }
478
549
  const append = (parts, key, value) => {
479
550
  if (value === void 0 || value === null || value === "") return;
480
551
  parts.push(`${key}=${encodeURIComponent(String(value))}`);
@@ -511,14 +582,14 @@ function serializeQuery(query) {
511
582
  return parts.join("&");
512
583
  }
513
584
  /**
514
- * Execute a request: build the URL (verbatim when already absolute — for page links —
515
- * else `baseUrl + path`), apply content negotiation and any async per-request headers,
585
+ * Execute a request: build the URL (a server link resolved against `baseUrl`, an absolute path
586
+ * verbatim, else `baseUrl + path`), apply content negotiation and any async per-request headers,
516
587
  * then drive the transport. A 2xx with a body parses to a {@link Document}; an empty /
517
588
  * 204 body resolves to `undefined`. A non-2xx throws a {@link JsonApiError} carrying the
518
589
  * parsed error objects (tolerating a non-JSON error body with an empty list).
519
590
  */
520
591
  async function execute(ctx, req) {
521
- let url = isAbsolute(req.path) ? req.path : ctx.baseUrl + req.path;
592
+ let url = req.link ? resolveLink(req.path, ctx.baseUrl) : isAbsolute(req.path) ? req.path : ctx.baseUrl + req.path;
522
593
  if (req.query) {
523
594
  const qs = serializeQuery(req.query);
524
595
  if (qs) url += (url.includes("?") ? "&" : "?") + qs;
@@ -948,6 +1019,18 @@ const fetchTransport = async (req) => {
948
1019
  /** Substitute `{key}` placeholders in a path template (`/albums/{id}/{rel}` -> `/albums/1/tracks`). */
949
1020
  const fill = (template, vars) => template.replace(/\{(\w+)\}/g, (_, key) => vars[key] ?? `{${key}}`);
950
1021
  /**
1022
+ * Fill a type's path template, putting a relation's URI segment (its `uriField`, else its name)
1023
+ * in place of `{rel}`. `vars.rel` stays the relation's name everywhere else.
1024
+ */
1025
+ function fillPath(ctx, type, template, vars) {
1026
+ const rel = vars["rel"];
1027
+ const uriField = rel === void 0 ? void 0 : ctx.descriptor[type]?.relations[rel]?.uriField;
1028
+ return fill(template, uriField === void 0 ? vars : {
1029
+ ...vars,
1030
+ rel: uriField
1031
+ });
1032
+ }
1033
+ /**
951
1034
  * Run an operation: resolve the path template from the type's descriptor, fill its
952
1035
  * placeholders, execute, and materialise the resulting document. A no-body / 204 response
953
1036
  * materialises to `undefined`; an operation the type does not declare (no path template) throws
@@ -960,7 +1043,7 @@ async function run(ctx, type, operation, vars, query, linkage = false, relationF
960
1043
  if (template === void 0) throw new Error(`No "${operation}" path declared for type "${type}"`);
961
1044
  const req = {
962
1045
  method: "GET",
963
- path: fill(template, vars)
1046
+ path: fillPath(ctx, type, template, vars)
964
1047
  };
965
1048
  if (query !== void 0) {
966
1049
  const { signal, ...params } = query;
@@ -969,8 +1052,10 @@ async function run(ctx, type, operation, vars, query, linkage = false, relationF
969
1052
  negotiateProfiles(ctx, type, params, req, relationForCount);
970
1053
  }
971
1054
  const doc = await execute(ctx.request, req);
1055
+ const relation = (operation === "fetchRelated" || operation === "fetchRelationship") && vars["rel"] !== void 0 ? ctx.descriptor[type]?.relations[vars["rel"]] : void 0;
1056
+ const paginator = operation === "fetchMany" ? ctx.descriptor[type]?.paginator : relation !== void 0 ? relatedPaginator(relation, ctx.materialise) : void 0;
972
1057
  const primaryType = operation === "fetchMany" ? type : void 0;
973
- const paginatorOverride = (operation === "fetchRelated" || operation === "fetchRelationship") && vars["rel"] !== void 0 ? relatedPaginatorKind(ctx.descriptor[type]?.relations[vars["rel"]], ctx.materialise) : void 0;
1058
+ const paginatorOverride = paginator === void 0 ? void 0 : resolvePaginator(paginator, query?.page);
974
1059
  return doc === void 0 ? void 0 : materialise(doc, ctx.materialise, linkage, primaryType, paginatorOverride);
975
1060
  }
976
1061
  /**
@@ -1008,7 +1093,7 @@ async function runWrite(ctx, spec) {
1008
1093
  if (template === void 0) throw new Error(`No "${spec.operation}" path declared for type "${spec.type}"`);
1009
1094
  const req = {
1010
1095
  method: spec.method,
1011
- path: fill(template, spec.vars),
1096
+ path: fillPath(ctx, spec.type, template, spec.vars),
1012
1097
  body: spec.body
1013
1098
  };
1014
1099
  if (spec.query !== void 0) req.query = spec.query;
@@ -1107,8 +1192,9 @@ function relationshipAccessor(ctx, type, id, rel) {
1107
1192
  return mutations === void 0 ? true : mutations[verb] === true;
1108
1193
  };
1109
1194
  const accessor = {};
1110
- if (relation?.relationship !== false) accessor["get"] = (query) => run(ctx, type, "fetchRelationship", vars, query, true, rel);
1111
- if (relation?.related !== false) accessor["related"] = (query) => run(ctx, type, "fetchRelated", vars, query, false, rel);
1195
+ const readable = relation?.writeOnly !== true;
1196
+ if (readable && relation?.relationship !== false) accessor["get"] = (query) => run(ctx, type, "fetchRelationship", vars, query, true, rel);
1197
+ if (readable && relation?.related !== false) accessor["related"] = (query) => run(ctx, type, "fetchRelated", vars, query, false, rel);
1112
1198
  if (cardinality === "many") {
1113
1199
  if (advertises("add")) accessor["add"] = (refs) => mutate("POST", refs);
1114
1200
  if (advertises("remove")) accessor["remove"] = (refs) => mutate("DELETE", refs);
@@ -1187,7 +1273,7 @@ function typeAccessor(ctx, type) {
1187
1273
  * not per-type codegen: `client.<type>` yields a {@link TypeAccessor}, `.id(id)` a handle,
1188
1274
  * and a handle's relation accessors drive the relationship/related endpoints. Navigation
1189
1275
  * (`$next()`/`$prev()`, related links) is wired through `materialise`'s `navigate` seam,
1190
- * which executes the (absolute) link and re-materialises.
1276
+ * which resolves the server's link against `baseUrl`, executes it and re-materialises.
1191
1277
  */
1192
1278
  function createClient(descriptor, options) {
1193
1279
  const request = {
@@ -1205,6 +1291,7 @@ function createClient(descriptor, options) {
1205
1291
  const doc = await execute(request, {
1206
1292
  method: "GET",
1207
1293
  path: url,
1294
+ link: true,
1208
1295
  ...signal !== void 0 ? { signal } : {}
1209
1296
  });
1210
1297
  return doc === void 0 ? void 0 : materialise(doc, ctx.materialise, linkage, void 0, paginator);
@@ -1235,6 +1322,6 @@ function createClient(descriptor, options) {
1235
1322
  });
1236
1323
  }
1237
1324
  //#endregion
1238
- export { ATOMIC_EXT, ATOMIC_MEDIA_TYPE, HANDLE_RESERVED, JSON_API_MEDIA_TYPE, JsonApiError, StructuralGuardError, assertJsonApiDocument, createClient, execute, fetchTransport, materialise, mediaType, relatedPaginatorKind, remapPointer, remapRelationshipPointer, resolveValidator, runAtomic, serializeQuery, toDocument, toRelationshipDocument, validateDocument, withRemappedAtomicPaths, withRemappedPaths, withRemappedRelationshipPaths };
1325
+ export { ATOMIC_EXT, ATOMIC_MEDIA_TYPE, CANONICAL_PAGE_KEYS, HANDLE_RESERVED, JSON_API_MEDIA_TYPE, JsonApiError, StructuralGuardError, assertJsonApiDocument, createClient, execute, fetchTransport, hasErrorCode, materialise, mediaType, relatedPaginator, relatedPaginatorKind, remapPointer, remapRelationshipPointer, resolvePaginator, resolveValidator, runAtomic, serializeQuery, toDocument, toRelationshipDocument, validateDocument, withRemappedAtomicPaths, withRemappedPaths, withRemappedRelationshipPaths };
1239
1326
 
1240
1327
  //# sourceMappingURL=index.mjs.map