@haddowg/json-api-client 1.1.0 → 1.2.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/README.md +5 -1
- package/dist/ajv.d.cts +1 -1
- package/dist/ajv.d.mts +1 -1
- package/dist/index.cjs +111 -33
- package/dist/index.d.cts +31 -9
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +31 -9
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +108 -34
- package/dist/index.mjs.map +1 -1
- package/dist/{validate-CfrCMZkA.d.cts → validate-DgCUbzgT.d.cts} +270 -51
- package/dist/validate-DgCUbzgT.d.cts.map +1 -0
- package/dist/{validate-CfrCMZkA.d.mts → validate-DgCUbzgT.d.mts} +270 -51
- package/dist/validate-DgCUbzgT.d.mts.map +1 -0
- package/package.json +1 -1
- package/dist/validate-CfrCMZkA.d.cts.map +0 -1
- package/dist/validate-CfrCMZkA.d.mts.map +0 -1
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
|
|
195
|
-
*
|
|
196
|
-
*
|
|
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,
|
|
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
|
-
* `
|
|
260
|
-
*
|
|
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,
|
|
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,
|
|
265
|
-
if (data === void 0 && cardinality === "many") return augmentRelationArray([], rel,
|
|
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
|
|
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.
|
|
377
|
-
* is the
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
398
|
-
define(arr, "$prev", (options) => navigateLink(page.links.prev, ctx, linkage,
|
|
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
|
|
426
|
-
*
|
|
427
|
-
*
|
|
428
|
-
*
|
|
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
|
|
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
|
|
493
|
+
return type !== void 0 ? ctx.descriptor[type]?.paginator : void 0;
|
|
434
494
|
}
|
|
435
495
|
/**
|
|
436
|
-
* A to-many relation's paginator
|
|
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
|
|
504
|
+
function relatedPaginator(relation, ctx) {
|
|
445
505
|
if (relation?.paginator !== void 0) return relation.paginator;
|
|
446
506
|
const relatedType = relation?.types[0];
|
|
447
|
-
|
|
448
|
-
|
|
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 (
|
|
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;
|
|
@@ -969,8 +1040,10 @@ async function run(ctx, type, operation, vars, query, linkage = false, relationF
|
|
|
969
1040
|
negotiateProfiles(ctx, type, params, req, relationForCount);
|
|
970
1041
|
}
|
|
971
1042
|
const doc = await execute(ctx.request, req);
|
|
1043
|
+
const relation = (operation === "fetchRelated" || operation === "fetchRelationship") && vars["rel"] !== void 0 ? ctx.descriptor[type]?.relations[vars["rel"]] : void 0;
|
|
1044
|
+
const paginator = operation === "fetchMany" ? ctx.descriptor[type]?.paginator : relation !== void 0 ? relatedPaginator(relation, ctx.materialise) : void 0;
|
|
972
1045
|
const primaryType = operation === "fetchMany" ? type : void 0;
|
|
973
|
-
const paginatorOverride =
|
|
1046
|
+
const paginatorOverride = paginator === void 0 ? void 0 : resolvePaginator(paginator, query?.page);
|
|
974
1047
|
return doc === void 0 ? void 0 : materialise(doc, ctx.materialise, linkage, primaryType, paginatorOverride);
|
|
975
1048
|
}
|
|
976
1049
|
/**
|
|
@@ -1187,7 +1260,7 @@ function typeAccessor(ctx, type) {
|
|
|
1187
1260
|
* not per-type codegen: `client.<type>` yields a {@link TypeAccessor}, `.id(id)` a handle,
|
|
1188
1261
|
* and a handle's relation accessors drive the relationship/related endpoints. Navigation
|
|
1189
1262
|
* (`$next()`/`$prev()`, related links) is wired through `materialise`'s `navigate` seam,
|
|
1190
|
-
* which
|
|
1263
|
+
* which resolves the server's link against `baseUrl`, executes it and re-materialises.
|
|
1191
1264
|
*/
|
|
1192
1265
|
function createClient(descriptor, options) {
|
|
1193
1266
|
const request = {
|
|
@@ -1205,6 +1278,7 @@ function createClient(descriptor, options) {
|
|
|
1205
1278
|
const doc = await execute(request, {
|
|
1206
1279
|
method: "GET",
|
|
1207
1280
|
path: url,
|
|
1281
|
+
link: true,
|
|
1208
1282
|
...signal !== void 0 ? { signal } : {}
|
|
1209
1283
|
});
|
|
1210
1284
|
return doc === void 0 ? void 0 : materialise(doc, ctx.materialise, linkage, void 0, paginator);
|
|
@@ -1235,6 +1309,6 @@ function createClient(descriptor, options) {
|
|
|
1235
1309
|
});
|
|
1236
1310
|
}
|
|
1237
1311
|
//#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 };
|
|
1312
|
+
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
1313
|
|
|
1240
1314
|
//# sourceMappingURL=index.mjs.map
|