@haddowg/json-api-client 1.30.0 → 1.32.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/ajv.cjs +1 -1
- package/dist/ajv.d.cts +1 -1
- package/dist/ajv.d.mts +1 -1
- package/dist/ajv.mjs +1 -1
- package/dist/index.cjs +338 -28
- package/dist/index.d.cts +109 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +109 -7
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +330 -29
- package/dist/index.mjs.map +1 -1
- package/dist/{validate-B2Dtn9dI.cjs → validate-B9cyfO3p.cjs} +20 -4
- package/dist/{validate-CtXB1MGk.mjs → validate-BuPAPJMi.mjs} +16 -6
- package/dist/validate-BuPAPJMi.mjs.map +1 -0
- package/dist/{validate-ugcQVpb0.d.mts → validate-DyNfTJ52.d.cts} +334 -50
- package/dist/validate-DyNfTJ52.d.cts.map +1 -0
- package/dist/{validate-ugcQVpb0.d.cts → validate-DyNfTJ52.d.mts} +334 -50
- package/dist/validate-DyNfTJ52.d.mts.map +1 -0
- package/package.json +1 -1
- package/dist/validate-CtXB1MGk.mjs.map +0 -1
- package/dist/validate-ugcQVpb0.d.cts.map +0 -1
- package/dist/validate-ugcQVpb0.d.mts.map +0 -1
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as validateDocument, c as JsonApiError, d as hasErrorCode, i as sparseSchemas, l as StructuralGuardError, n as resolveValidator, o as fieldsOfUrl, r as sparseSchema, s as nestedFieldset, t as assertJsonApiDocument, u as findError } from "./validate-
|
|
1
|
+
import { a as validateDocument, c as JsonApiError, d as hasErrorCode, f as errorText, i as sparseSchemas, l as StructuralGuardError, n as resolveValidator, o as fieldsOfUrl, r as sparseSchema, s as nestedFieldset, t as assertJsonApiDocument, u as findError } from "./validate-BuPAPJMi.mjs";
|
|
2
2
|
//#region src/request-stamp.ts
|
|
3
3
|
/**
|
|
4
4
|
* Request stamps: when the request behind a result started, relative to every other request made in
|
|
@@ -595,7 +595,7 @@ function materialiseDocument(doc, ctx, linkage, primaryType, paginatorOverride)
|
|
|
595
595
|
if (doc.jsonapi !== void 0) env.jsonapi = doc.jsonapi;
|
|
596
596
|
if (doc.meta !== void 0) env.meta = doc.meta;
|
|
597
597
|
if (doc.links !== void 0) env.links = doc.links;
|
|
598
|
-
const index = buildIndex(doc.included);
|
|
598
|
+
const index = buildIndex(doc.included, linkage ? void 0 : doc.data);
|
|
599
599
|
const nodes = /* @__PURE__ */ new Map();
|
|
600
600
|
const build = (raw) => buildNode(raw, env, index, nodes, ctx);
|
|
601
601
|
const data = doc.data;
|
|
@@ -610,11 +610,18 @@ function materialiseDocument(doc, ctx, linkage, primaryType, paginatorOverride)
|
|
|
610
610
|
return build(raw);
|
|
611
611
|
}
|
|
612
612
|
}
|
|
613
|
-
/**
|
|
614
|
-
|
|
613
|
+
/**
|
|
614
|
+
* Index `included` and the primary resources by `type:id` so relationship linkage can be hydrated.
|
|
615
|
+
* A server never repeats a primary resource in `included`, so a member that links back to one
|
|
616
|
+
* (`tracks.album` on an album) is found only among the primary data.
|
|
617
|
+
*/
|
|
618
|
+
function buildIndex(included, primary) {
|
|
615
619
|
const index = /* @__PURE__ */ new Map();
|
|
616
|
-
|
|
617
|
-
|
|
620
|
+
const add = (raw) => {
|
|
621
|
+
if (isObject$2(raw) && typeof raw["type"] === "string" && typeof raw["id"] === "string") index.set(key(raw["type"], raw["id"]), raw);
|
|
622
|
+
};
|
|
623
|
+
for (const raw of included ?? []) add(raw);
|
|
624
|
+
for (const raw of Array.isArray(primary) ? primary : [primary]) add(raw);
|
|
618
625
|
return index;
|
|
619
626
|
}
|
|
620
627
|
const key = (type, id) => `${type}:${id}`;
|
|
@@ -632,6 +639,8 @@ function buildNode(raw, env, index, nodes, ctx) {
|
|
|
632
639
|
id: raw.id
|
|
633
640
|
};
|
|
634
641
|
nodes.set(id, node);
|
|
642
|
+
const cycleViews = [];
|
|
643
|
+
UNFINISHED.set(node, cycleViews);
|
|
635
644
|
if (raw.attributes) for (const [k, v] of Object.entries(raw.attributes)) node[k] = v;
|
|
636
645
|
const rels = raw.relationships ?? {};
|
|
637
646
|
const resType = ctx.descriptor[raw.type];
|
|
@@ -641,8 +650,78 @@ function buildNode(raw, env, index, nodes, ctx) {
|
|
|
641
650
|
node[name] = hydrateRelation(rel, relDesc?.cardinality, resolvePaginator(relatedPaginator(relDesc, ctx)), env, index, nodes, ctx);
|
|
642
651
|
}
|
|
643
652
|
attachResourceEnvelope(node, raw, env, rels, nestedFieldset(ctx.fields?.[raw.type]));
|
|
653
|
+
UNFINISHED.delete(node);
|
|
654
|
+
for (const view of cycleViews) readLateRelations(view, node);
|
|
644
655
|
return node;
|
|
645
656
|
}
|
|
657
|
+
/** Nodes still being built, each with the per-edge views a relation cycle copied of it so far. */
|
|
658
|
+
const UNFINISHED = /* @__PURE__ */ new WeakMap();
|
|
659
|
+
/** A back-reference's relations that read through to its node; a property, read on every walk. */
|
|
660
|
+
const LATE = Symbol("late relations");
|
|
661
|
+
/** Nodes some back-reference reads relations through to. */
|
|
662
|
+
const READ_THROUGH = /* @__PURE__ */ new WeakSet();
|
|
663
|
+
/**
|
|
664
|
+
* A relation cycle copied `view` of `node` before the node's later relations were set. Each one it
|
|
665
|
+
* missed reads the node's slot as linkage: the view sits past the end of its include path, and a
|
|
666
|
+
* hydrated value would close the cycle. Non-enumerable, so a spread, `JSON.stringify` and a cache
|
|
667
|
+
* walk meet no cycle.
|
|
668
|
+
*/
|
|
669
|
+
function readLateRelations(view, node) {
|
|
670
|
+
const late = /* @__PURE__ */ new Set();
|
|
671
|
+
for (const name of Object.keys(node)) if (!Object.hasOwn(view, name)) {
|
|
672
|
+
late.add(name);
|
|
673
|
+
defineGetter(view, name, () => linkageOf(node[name]));
|
|
674
|
+
}
|
|
675
|
+
if (late.size > 0) {
|
|
676
|
+
Object.defineProperty(view, LATE, { value: late });
|
|
677
|
+
READ_THROUGH.add(node);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
/** Each slot value's linkage, so a back-reference reads the same value until its node's changes. */
|
|
681
|
+
const LINKAGE = /* @__PURE__ */ new WeakMap();
|
|
682
|
+
/** A relation slot's value as linkage: identifier views, in an array keeping the slot's envelope. */
|
|
683
|
+
function linkageOf(slot) {
|
|
684
|
+
if (typeof slot !== "object" || slot === null) return slot;
|
|
685
|
+
if (LINKAGE.has(slot)) return LINKAGE.get(slot);
|
|
686
|
+
const value = Array.isArray(slot) ? withSlotEnvelope(slot, slot.map(identifierOf)) : identifierOf(slot);
|
|
687
|
+
LINKAGE.set(slot, value);
|
|
688
|
+
return value;
|
|
689
|
+
}
|
|
690
|
+
function identifierOf(member) {
|
|
691
|
+
if (!isObject$2(member) || typeof member["type"] !== "string" || typeof member["id"] !== "string") return member;
|
|
692
|
+
const meta = member.$edge?.meta;
|
|
693
|
+
return buildIdentifierView({
|
|
694
|
+
type: member["type"],
|
|
695
|
+
id: member["id"],
|
|
696
|
+
...meta !== void 0 ? { meta } : {}
|
|
697
|
+
});
|
|
698
|
+
}
|
|
699
|
+
/** `members` carrying `slot`'s non-index members (`$page`, `$links`, `$next()` and the rest). */
|
|
700
|
+
function withSlotEnvelope(slot, members) {
|
|
701
|
+
for (const name of Reflect.ownKeys(slot)) if (name !== "length" && !(typeof name === "string" && /^\d+$/.test(name))) Object.defineProperty(members, name, Object.getOwnPropertyDescriptor(slot, name));
|
|
702
|
+
return members;
|
|
703
|
+
}
|
|
704
|
+
/**
|
|
705
|
+
* The relations of `view` that read through to its node as linkage (a back-reference a relation
|
|
706
|
+
* cycle built before the node's later relations were set), or `undefined`. A cache layer leaves
|
|
707
|
+
* these slots alone: they follow the node.
|
|
708
|
+
* @internal
|
|
709
|
+
*/
|
|
710
|
+
function lateRelationsOf(view) {
|
|
711
|
+
return view[LATE];
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Tell the client a cache layer wrote relation `name` of the cached copy `copy`: a node that
|
|
715
|
+
* back-references read the relation through to takes the value too, so they read it.
|
|
716
|
+
* @internal
|
|
717
|
+
*/
|
|
718
|
+
function relationWritten(copy, name, value) {
|
|
719
|
+
let at = SOURCES.get(copy);
|
|
720
|
+
while (at !== void 0) {
|
|
721
|
+
if (READ_THROUGH.has(at)) at[name] = value;
|
|
722
|
+
at = SOURCES.get(at);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
646
725
|
/**
|
|
647
726
|
* Resolve one relationship's value: an included member hydrates to a per-edge view, a
|
|
648
727
|
* linked-but-not-included member stays an identifier, an empty to-one is `null`, and a
|
|
@@ -703,8 +782,10 @@ function attachResourceEnvelope(node, raw, env, rels, selection) {
|
|
|
703
782
|
*/
|
|
704
783
|
function buildEdgeView(node, member, relationEdge, pick) {
|
|
705
784
|
const view = {};
|
|
706
|
-
if (pick === void 0)
|
|
707
|
-
|
|
785
|
+
if (pick === void 0) {
|
|
786
|
+
Object.assign(view, node);
|
|
787
|
+
UNFINISHED.get(node)?.push(view);
|
|
788
|
+
} else for (const [k, v] of Object.entries(node)) if (k === "type" || k === "id" || pick(k)) view[k] = v;
|
|
708
789
|
SOURCES.set(view, node);
|
|
709
790
|
for (const k of [
|
|
710
791
|
"$meta",
|
|
@@ -714,8 +795,10 @@ function buildEdgeView(node, member, relationEdge, pick) {
|
|
|
714
795
|
"$raw",
|
|
715
796
|
"$nestedFieldset"
|
|
716
797
|
]) delegate(view, node, k);
|
|
717
|
-
|
|
718
|
-
|
|
798
|
+
define(view, "$rel", (name) => {
|
|
799
|
+
const rel = node.$rel;
|
|
800
|
+
return typeof rel === "function" ? rel.call(node, name) : void 0;
|
|
801
|
+
});
|
|
719
802
|
attachOverlayAccessors(view);
|
|
720
803
|
attachEdgeEnvelope(view, member, false, relationEdge);
|
|
721
804
|
return view;
|
|
@@ -943,6 +1026,63 @@ function relatedPaginatorKind(relation, ctx) {
|
|
|
943
1026
|
return resolvePaginator(relatedPaginator(relation, ctx)).kind;
|
|
944
1027
|
}
|
|
945
1028
|
//#endregion
|
|
1029
|
+
//#region src/input-validation.ts
|
|
1030
|
+
/**
|
|
1031
|
+
* Thrown by a create or update whose input fails the schema `validateInput` holds for it, before
|
|
1032
|
+
* any request is sent. `fieldErrors(error)` takes it as it takes a server's 422.
|
|
1033
|
+
*/
|
|
1034
|
+
var InputValidationError = class extends Error {
|
|
1035
|
+
type;
|
|
1036
|
+
operation;
|
|
1037
|
+
issues;
|
|
1038
|
+
/** The failing operation's index in an atomic batch. */
|
|
1039
|
+
opIndex;
|
|
1040
|
+
constructor(type, operation, issues, opIndex) {
|
|
1041
|
+
const first = issues[0]?.message;
|
|
1042
|
+
super(`The ${type} ${operation} input${opIndex === void 0 ? "" : ` of atomic operation ${opIndex}`} is invalid` + (first === void 0 ? "" : `: ${first}`) + (issues.length > 1 ? ` (and ${issues.length - 1} more)` : ""));
|
|
1043
|
+
this.name = "InputValidationError";
|
|
1044
|
+
this.type = type;
|
|
1045
|
+
this.operation = operation;
|
|
1046
|
+
this.issues = issues;
|
|
1047
|
+
if (opIndex !== void 0) this.opIndex = opIndex;
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
/**
|
|
1051
|
+
* The {@link InputValidationError} `check`'s input fails its schema in `schemas` with, or
|
|
1052
|
+
* `undefined` when it passes or has no schema. A Promise only for a schema that validates
|
|
1053
|
+
* asynchronously.
|
|
1054
|
+
*/
|
|
1055
|
+
function inputFailure(schemas, check) {
|
|
1056
|
+
const schema = schemas?.[check.type]?.[check.operation];
|
|
1057
|
+
if (schema === void 0) return;
|
|
1058
|
+
const failure = (result) => result.issues === void 0 ? void 0 : new InputValidationError(check.type, check.operation, result.issues, check.opIndex);
|
|
1059
|
+
const result = schema["~standard"].validate(check.input);
|
|
1060
|
+
return result instanceof Promise ? result.then(failure) : failure(result);
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
* Run `write` once `check`'s input passes its schema, else reject with the
|
|
1064
|
+
* {@link InputValidationError}. A write with no schema, or a schema that validates synchronously,
|
|
1065
|
+
* starts in this tick.
|
|
1066
|
+
*/
|
|
1067
|
+
function withInputCheck(schemas, check, write) {
|
|
1068
|
+
const proceed = (failure) => failure === void 0 ? write() : Promise.reject(failure);
|
|
1069
|
+
const failure = inputFailure(schemas, check);
|
|
1070
|
+
return failure instanceof Promise ? failure.then(proceed) : proceed(failure);
|
|
1071
|
+
}
|
|
1072
|
+
//#endregion
|
|
1073
|
+
//#region src/input-path.ts
|
|
1074
|
+
/**
|
|
1075
|
+
* The flat input-path key format: `.` between names, `[n]` for a to-many member's index, as in
|
|
1076
|
+
* `orderedTracks[0].$pivot.position`. `byPath()` and `fieldErrors()` key errors by it.
|
|
1077
|
+
*/
|
|
1078
|
+
function joinInputPath(segments) {
|
|
1079
|
+
return segments.map((segment, i) => typeof segment === "number" ? `[${segment}]` : i === 0 ? segment : `.${segment}`).join("");
|
|
1080
|
+
}
|
|
1081
|
+
/** The segments of a flat input-path key: `a[0].b` is `['a', 0, 'b']`. */
|
|
1082
|
+
function splitInputPath(key) {
|
|
1083
|
+
return Array.from(key.matchAll(/([^.[\]]+)|\[(\d+)\]/g), ([, name, index]) => index === void 0 ? name : Number(index));
|
|
1084
|
+
}
|
|
1085
|
+
//#endregion
|
|
946
1086
|
//#region src/serialize-write.ts
|
|
947
1087
|
/** Per-edge writable pivot data, supplied on a to-many member as the `$pivot` key. */
|
|
948
1088
|
const PIVOT_KEY = "$pivot";
|
|
@@ -955,7 +1095,11 @@ const META_KEY = "$meta";
|
|
|
955
1095
|
const DOCUMENT_META_KEY = "$documentMeta";
|
|
956
1096
|
/** The flat path of a pointer into the request document's top-level `meta`, else `undefined`. */
|
|
957
1097
|
function documentMetaPath(segments) {
|
|
958
|
-
return segments[0] === "meta" ? [DOCUMENT_META_KEY, ...segments.slice(1)]
|
|
1098
|
+
return segments[0] === "meta" ? pointerPath([DOCUMENT_META_KEY, ...segments.slice(1)]) : void 0;
|
|
1099
|
+
}
|
|
1100
|
+
/** Pointer segments as a flat input path, a numeric segment indexing an array: `tags[0]`. */
|
|
1101
|
+
function pointerPath(segments) {
|
|
1102
|
+
return joinInputPath(segments.map((s) => /^\d+$/.test(s) ? Number(s) : s));
|
|
959
1103
|
}
|
|
960
1104
|
/** The reserved input keys never routed to attributes/relationships. */
|
|
961
1105
|
const RESERVED_INPUT = /* @__PURE__ */ new Set(["type", "id"]);
|
|
@@ -1083,16 +1227,12 @@ const describe = (value) => value === null ? "null" : Array.isArray(value) ? "an
|
|
|
1083
1227
|
function remapPointer(descriptor, type, pointer) {
|
|
1084
1228
|
const segments = pointer.split("/").filter((s) => s !== "");
|
|
1085
1229
|
if (segments[0] !== "data") return documentMetaPath(segments) ?? pointer;
|
|
1086
|
-
if (segments[1] === "meta") return [META_KEY, ...segments.slice(2)]
|
|
1087
|
-
if (segments[1] === "attributes") return
|
|
1230
|
+
if (segments[1] === "meta") return pointerPath([META_KEY, ...segments.slice(2)]);
|
|
1231
|
+
if (segments[1] === "attributes") return pointerPath(segments.slice(2));
|
|
1088
1232
|
if (segments[1] === "id") return "id";
|
|
1089
1233
|
if (segments[1] === "relationships") return remapRelationship(descriptor[type]?.relations ?? {}, segments.slice(2));
|
|
1090
1234
|
return pointer;
|
|
1091
1235
|
}
|
|
1092
|
-
/** `[releaseInfo, label]` -> `releaseInfo.label`; a bare `[title]` -> `title`. */
|
|
1093
|
-
function remapAttribute(rest) {
|
|
1094
|
-
return rest.join(".");
|
|
1095
|
-
}
|
|
1096
1236
|
/**
|
|
1097
1237
|
* Remap a relationship pointer's tail (everything after `relationships/`, or — for a
|
|
1098
1238
|
* relationship-endpoint document — the relation name spliced ahead of the `data` tail). The
|
|
@@ -1105,17 +1245,17 @@ function remapRelationship(relations, rest) {
|
|
|
1105
1245
|
const name = rest[0];
|
|
1106
1246
|
if (name === void 0) return "data/relationships";
|
|
1107
1247
|
const hasPivot = relations[name]?.pivot === true;
|
|
1108
|
-
|
|
1248
|
+
const path = [name];
|
|
1109
1249
|
for (let i = 1; i < rest.length; i++) {
|
|
1110
1250
|
const seg = rest[i];
|
|
1111
1251
|
if (seg === "data") continue;
|
|
1112
|
-
if (/^\d+$/.test(seg)) path
|
|
1252
|
+
if (/^\d+$/.test(seg)) path.push(Number(seg));
|
|
1113
1253
|
else if (seg === "meta" && hasPivot && rest[i + 1] === "pivot") {
|
|
1114
|
-
path
|
|
1254
|
+
path.push("$pivot");
|
|
1115
1255
|
i++;
|
|
1116
|
-
} else path
|
|
1256
|
+
} else path.push(seg);
|
|
1117
1257
|
}
|
|
1118
|
-
return path;
|
|
1258
|
+
return joinInputPath(path);
|
|
1119
1259
|
}
|
|
1120
1260
|
/**
|
|
1121
1261
|
* Invert a relationship-MUTATION error's `source.pointer` to the caller's flat path, for the
|
|
@@ -1236,8 +1376,10 @@ const withOperationMeta = (operation, opts) => opts?.meta === void 0 ? operation
|
|
|
1236
1376
|
const isObject = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
1237
1377
|
/** The deterministic local id for the create at `opIndex` (stable + human-readable). */
|
|
1238
1378
|
const lidFor = (opIndex) => `atomic-${opIndex}`;
|
|
1239
|
-
async function runAtomic(request, materialiseCtx, descriptor, path, build) {
|
|
1379
|
+
async function runAtomic(request, materialiseCtx, descriptor, path, build, inputSchemas) {
|
|
1240
1380
|
const operations = [];
|
|
1381
|
+
/** Each create and update input, checked against `inputSchemas` before the batch is sent. */
|
|
1382
|
+
const checks = [];
|
|
1241
1383
|
/** The wire type of each op, by index — drives the error-pointer remap. */
|
|
1242
1384
|
const typeByOp = [];
|
|
1243
1385
|
/** The relation each relationship op writes, by index, which its pointers are remapped under. */
|
|
@@ -1271,6 +1413,13 @@ async function runAtomic(request, materialiseCtx, descriptor, path, build) {
|
|
|
1271
1413
|
const opIndex = operations.length;
|
|
1272
1414
|
const lid = lidFor(opIndex);
|
|
1273
1415
|
const data = atomicResourceData(descriptor, input, { lid });
|
|
1416
|
+
const { type: _type, ...fields } = input;
|
|
1417
|
+
checks.push({
|
|
1418
|
+
type: input.type,
|
|
1419
|
+
operation: "create",
|
|
1420
|
+
input: fields,
|
|
1421
|
+
opIndex
|
|
1422
|
+
});
|
|
1274
1423
|
operations.push(withOperationMeta({
|
|
1275
1424
|
op: "add",
|
|
1276
1425
|
data
|
|
@@ -1287,6 +1436,14 @@ async function runAtomic(request, materialiseCtx, descriptor, path, build) {
|
|
|
1287
1436
|
const opIndex = operations.length;
|
|
1288
1437
|
const identity = input.id !== void 0 ? { id: input.id } : { lid: input.lid };
|
|
1289
1438
|
const data = atomicResourceData(descriptor, input, identity);
|
|
1439
|
+
const { type: _type, lid: _lid, ...fields } = input;
|
|
1440
|
+
const { id: _id, ...patch } = fields;
|
|
1441
|
+
checks.push({
|
|
1442
|
+
type: input.type,
|
|
1443
|
+
operation: "update",
|
|
1444
|
+
input: descriptor[input.type]?.singleton === true ? fields : patch,
|
|
1445
|
+
opIndex
|
|
1446
|
+
});
|
|
1290
1447
|
operations.push(withOperationMeta({
|
|
1291
1448
|
op: "update",
|
|
1292
1449
|
ref: {
|
|
@@ -1330,6 +1487,10 @@ async function runAtomic(request, materialiseCtx, descriptor, path, build) {
|
|
|
1330
1487
|
};
|
|
1331
1488
|
}
|
|
1332
1489
|
});
|
|
1490
|
+
for (const check of checks) {
|
|
1491
|
+
const failure = await inputFailure(inputSchemas, check);
|
|
1492
|
+
if (failure !== void 0) throw failure;
|
|
1493
|
+
}
|
|
1333
1494
|
const req = {
|
|
1334
1495
|
method: "POST",
|
|
1335
1496
|
path,
|
|
@@ -1961,7 +2122,11 @@ const HANDLE_RESERVED = /* @__PURE__ */ new Set([
|
|
|
1961
2122
|
* the one the body carries.
|
|
1962
2123
|
*/
|
|
1963
2124
|
function runUpdate(ctx, type, vars, id, patch, opts) {
|
|
1964
|
-
return
|
|
2125
|
+
return withInputCheck(ctx.inputSchemas, {
|
|
2126
|
+
type,
|
|
2127
|
+
operation: "update",
|
|
2128
|
+
input: patch
|
|
2129
|
+
}, () => runWrite(ctx, {
|
|
1965
2130
|
type,
|
|
1966
2131
|
operation: "update",
|
|
1967
2132
|
vars,
|
|
@@ -1971,7 +2136,7 @@ function runUpdate(ctx, type, vars, id, patch, opts) {
|
|
|
1971
2136
|
remap: (error) => withRemappedPaths(error, ctx.descriptor, type),
|
|
1972
2137
|
accepted: ctx.descriptor[type]?.accepted?.update,
|
|
1973
2138
|
wait: waitOptions(opts?.wait)
|
|
1974
|
-
});
|
|
2139
|
+
}));
|
|
1975
2140
|
}
|
|
1976
2141
|
/**
|
|
1977
2142
|
* Wrap a handle's verbs in a Proxy resolving each relation not shadowed by a reserved member to
|
|
@@ -2029,7 +2194,11 @@ function typeAccessor(ctx, type) {
|
|
|
2029
2194
|
return {
|
|
2030
2195
|
list: (query) => run(ctx, type, "fetchMany", {}, query),
|
|
2031
2196
|
get: (id, query) => run(ctx, type, "fetchOne", { id }, query),
|
|
2032
|
-
create: (input, opts) =>
|
|
2197
|
+
create: (input, opts) => withInputCheck(ctx.inputSchemas, {
|
|
2198
|
+
type,
|
|
2199
|
+
operation: "create",
|
|
2200
|
+
input
|
|
2201
|
+
}, () => runWrite(ctx, {
|
|
2033
2202
|
type,
|
|
2034
2203
|
operation: "create",
|
|
2035
2204
|
vars: {},
|
|
@@ -2039,7 +2208,7 @@ function typeAccessor(ctx, type) {
|
|
|
2039
2208
|
remap: (error) => withRemappedPaths(error, ctx.descriptor, type),
|
|
2040
2209
|
accepted: ctx.descriptor[type]?.accepted?.create,
|
|
2041
2210
|
wait: waitOptions(opts?.wait)
|
|
2042
|
-
}),
|
|
2211
|
+
})),
|
|
2043
2212
|
id: (id) => resourceHandle(ctx, type, id),
|
|
2044
2213
|
actions: actionsAccessor(ctx, type, "collection", {})
|
|
2045
2214
|
};
|
|
@@ -2063,6 +2232,7 @@ function createClient(descriptor, options) {
|
|
|
2063
2232
|
const ctx = {
|
|
2064
2233
|
request,
|
|
2065
2234
|
descriptor,
|
|
2235
|
+
...options.validateInput !== void 0 ? { inputSchemas: options.validateInput } : {},
|
|
2066
2236
|
materialise: {
|
|
2067
2237
|
descriptor,
|
|
2068
2238
|
navigate: async (url, linkage = false, paginator, signal) => {
|
|
@@ -2080,7 +2250,7 @@ function createClient(descriptor, options) {
|
|
|
2080
2250
|
const atomicDescriptor = options.atomic ?? null;
|
|
2081
2251
|
const atomic = (build) => {
|
|
2082
2252
|
if (atomicDescriptor === null) throw new Error("This API does not expose an Atomic Operations endpoint");
|
|
2083
|
-
return runAtomic(request, ctx.materialise, descriptor, atomicDescriptor.path, build);
|
|
2253
|
+
return runAtomic(request, ctx.materialise, descriptor, atomicDescriptor.path, build, ctx.inputSchemas);
|
|
2084
2254
|
};
|
|
2085
2255
|
const accessors = /* @__PURE__ */ new Map();
|
|
2086
2256
|
return new Proxy(Object.create(null), {
|
|
@@ -2100,6 +2270,52 @@ function createClient(descriptor, options) {
|
|
|
2100
2270
|
});
|
|
2101
2271
|
}
|
|
2102
2272
|
//#endregion
|
|
2273
|
+
//#region src/field-errors.ts
|
|
2274
|
+
function fieldErrors(error) {
|
|
2275
|
+
if (error instanceof InputValidationError) return group(error.issues.map((issue) => fromIssue(issue, error.opIndex)));
|
|
2276
|
+
if (!(error instanceof JsonApiError) || !error.is4xx()) return;
|
|
2277
|
+
const issues = error.errors.map(toFieldError);
|
|
2278
|
+
return issues.some((issue) => issue.key !== "") ? group(issues) : void 0;
|
|
2279
|
+
}
|
|
2280
|
+
function group(issues) {
|
|
2281
|
+
const fields = {};
|
|
2282
|
+
const form = [];
|
|
2283
|
+
for (const issue of issues) if (issue.key === "") form.push(issue);
|
|
2284
|
+
else (fields[issue.key] ??= []).push(issue);
|
|
2285
|
+
return {
|
|
2286
|
+
fields,
|
|
2287
|
+
form,
|
|
2288
|
+
issues,
|
|
2289
|
+
first: (path) => fields[path]?.[0]?.message,
|
|
2290
|
+
messages: (path) => fields[path]?.map((issue) => issue.message) ?? [],
|
|
2291
|
+
op: (index) => group(issues.filter((issue) => issue.opIndex === index))
|
|
2292
|
+
};
|
|
2293
|
+
}
|
|
2294
|
+
function toFieldError(error) {
|
|
2295
|
+
const key = error.path === void 0 || error.path.startsWith("/") ? "" : error.path;
|
|
2296
|
+
return {
|
|
2297
|
+
message: errorText(error, true) ?? "Invalid",
|
|
2298
|
+
path: splitInputPath(key),
|
|
2299
|
+
key,
|
|
2300
|
+
...error.code === void 0 ? {} : { code: error.code },
|
|
2301
|
+
...error.opIndex === void 0 ? {} : { opIndex: error.opIndex },
|
|
2302
|
+
error
|
|
2303
|
+
};
|
|
2304
|
+
}
|
|
2305
|
+
function fromIssue(issue, opIndex) {
|
|
2306
|
+
const path = (issue.path ?? []).map((segment) => {
|
|
2307
|
+
const key = typeof segment === "object" ? segment.key : segment;
|
|
2308
|
+
return typeof key === "number" ? key : String(key);
|
|
2309
|
+
});
|
|
2310
|
+
const key = joinInputPath(path);
|
|
2311
|
+
return {
|
|
2312
|
+
message: issue.message,
|
|
2313
|
+
path,
|
|
2314
|
+
key,
|
|
2315
|
+
...opIndex === void 0 ? {} : { opIndex }
|
|
2316
|
+
};
|
|
2317
|
+
}
|
|
2318
|
+
//#endregion
|
|
2103
2319
|
//#region src/load-all.ts
|
|
2104
2320
|
/**
|
|
2105
2321
|
* Follow `next` links from a first page until the last, and return every member in one plain
|
|
@@ -2126,6 +2342,91 @@ async function loadAll(first, options = {}) {
|
|
|
2126
2342
|
return all;
|
|
2127
2343
|
}
|
|
2128
2344
|
//#endregion
|
|
2129
|
-
|
|
2345
|
+
//#region src/mock.ts
|
|
2346
|
+
/** The members a linkage member carries beside its identity, which never become attributes. */
|
|
2347
|
+
const MEMBER_KEYS = /* @__PURE__ */ new Set([
|
|
2348
|
+
"type",
|
|
2349
|
+
"id",
|
|
2350
|
+
"meta",
|
|
2351
|
+
"$pivot"
|
|
2352
|
+
]);
|
|
2353
|
+
/**
|
|
2354
|
+
* Build a materialised resource from its plain data, as a read returns it: the relations the data
|
|
2355
|
+
* nests hydrate, the identifiers stay identifiers, and each member's `$pivot` lands on its linkage
|
|
2356
|
+
* `meta.pivot`, so `$pivot`, `$edge`, `$rel` and `$count` all work. For tests and stories; the
|
|
2357
|
+
* generated client binds it to its descriptor and types it.
|
|
2358
|
+
*/
|
|
2359
|
+
function mockResource(descriptor, data) {
|
|
2360
|
+
return materialise(mockDocument(descriptor, data), mockContext(descriptor));
|
|
2361
|
+
}
|
|
2362
|
+
/** {@link mockResource} for a collection of type `type`, as a list read returns it. */
|
|
2363
|
+
function mockCollection(descriptor, type, data) {
|
|
2364
|
+
return materialise(mockDocument(descriptor, data), mockContext(descriptor), false, type);
|
|
2365
|
+
}
|
|
2366
|
+
function mockContext(descriptor) {
|
|
2367
|
+
return {
|
|
2368
|
+
descriptor,
|
|
2369
|
+
navigate: () => Promise.reject(/* @__PURE__ */ new Error("A mock resource has no server to follow its links to."))
|
|
2370
|
+
};
|
|
2371
|
+
}
|
|
2372
|
+
/** The JSON:API document plain data stands for: the primary data, and every nested resource in `included`. */
|
|
2373
|
+
function mockDocument(descriptor, data) {
|
|
2374
|
+
const primary = /* @__PURE__ */ new Set();
|
|
2375
|
+
const included = /* @__PURE__ */ new Map();
|
|
2376
|
+
const toResource = (value) => {
|
|
2377
|
+
const relations = descriptor[value.type]?.relations ?? {};
|
|
2378
|
+
const attributes = {};
|
|
2379
|
+
const relationships = {};
|
|
2380
|
+
for (const [key, member] of Object.entries(value)) {
|
|
2381
|
+
if (key === "type" || key === "id") continue;
|
|
2382
|
+
if (key in relations) {
|
|
2383
|
+
if (member !== void 0) relationships[key] = { data: linkageOf(member) };
|
|
2384
|
+
} else if (key !== "meta" && key !== "$pivot") attributes[key] = member;
|
|
2385
|
+
}
|
|
2386
|
+
return {
|
|
2387
|
+
type: value.type,
|
|
2388
|
+
id: value.id,
|
|
2389
|
+
...Object.keys(attributes).length > 0 ? { attributes } : {},
|
|
2390
|
+
...Object.keys(relationships).length > 0 ? { relationships } : {},
|
|
2391
|
+
...value["meta"] !== void 0 ? { meta: value["meta"] } : {}
|
|
2392
|
+
};
|
|
2393
|
+
};
|
|
2394
|
+
const linkageOf = (value) => {
|
|
2395
|
+
if (value === null) return null;
|
|
2396
|
+
if (Array.isArray(value)) return value.map((member) => identifierOf(member));
|
|
2397
|
+
return identifierOf(value);
|
|
2398
|
+
};
|
|
2399
|
+
const identifierOf = (member) => {
|
|
2400
|
+
const id = `${member.type}:${member.id}`;
|
|
2401
|
+
if (isFullResource(member) && !primary.has(id) && !included.has(id)) {
|
|
2402
|
+
included.set(id, {});
|
|
2403
|
+
included.set(id, toResource(member));
|
|
2404
|
+
}
|
|
2405
|
+
const pivot = member.$pivot;
|
|
2406
|
+
const meta = member["meta"];
|
|
2407
|
+
const linkageMeta = pivot === void 0 ? meta : {
|
|
2408
|
+
...meta,
|
|
2409
|
+
pivot
|
|
2410
|
+
};
|
|
2411
|
+
return {
|
|
2412
|
+
type: member.type,
|
|
2413
|
+
id: member.id,
|
|
2414
|
+
...linkageMeta !== void 0 ? { meta: linkageMeta } : {}
|
|
2415
|
+
};
|
|
2416
|
+
};
|
|
2417
|
+
const list = Array.isArray(data) ? data : [data];
|
|
2418
|
+
for (const value of list) primary.add(`${value.type}:${value.id}`);
|
|
2419
|
+
const resources = list.map(toResource);
|
|
2420
|
+
return {
|
|
2421
|
+
data: Array.isArray(data) ? resources : resources[0],
|
|
2422
|
+
...included.size > 0 ? { included: [...included.values()] } : {}
|
|
2423
|
+
};
|
|
2424
|
+
}
|
|
2425
|
+
/** Whether a relation member carries more than its identity, so it stands for an included resource. */
|
|
2426
|
+
function isFullResource(member) {
|
|
2427
|
+
return Object.keys(member).some((key) => !MEMBER_KEYS.has(key));
|
|
2428
|
+
}
|
|
2429
|
+
//#endregion
|
|
2430
|
+
export { ATOMIC_EXT, ATOMIC_MEDIA_TYPE, CANONICAL_PAGE_KEYS, HANDLE_RESERVED, InputValidationError, JSON_API_MEDIA_TYPE, JsonApiError, PollTimeoutError, StructuralGuardError, assertJsonApiDocument, createClient, edgeViewOf, execute, executeRaw, executeResponse, fetchTransport, fieldErrors, findError, hasErrorCode, identifierViewOf, inputFailure, isAccepted, isActionLocation, isMaterialised, isPolymorphic, lateRelationsOf, listFilterMatcher, loadAll, materialise, mediaType, mockCollection, mockDocument, mockResource, nestedFieldset, nextPageParams, nextRequestStamp, parseRetryAfter, prevPageParams, readActionLocation, relatedPaginator, relatedPaginatorKind, relationWritten, remapPointer, remapRelationshipPointer, requestStampOf, resolvePaginator, resolveValidator, runAtomic, serializeQuery, setMaterialisedOverlay, sparseSchema, sparseSchemas, toDocument, toRelationshipDocument, validateDocument, withInputCheck, withRemappedAtomicPaths, withRemappedDocumentMetaPaths, withRemappedPaths, withRemappedRelationshipPaths, withTopLevelMeta };
|
|
2130
2431
|
|
|
2131
2432
|
//# sourceMappingURL=index.mjs.map
|