@bjornharrtell/json-api 5.2.2 → 5.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/README.md +13 -8
- package/dist/docs/enums/RelationshipType.html +2 -2
- package/dist/docs/functions/camel.html +1 -1
- package/dist/docs/functions/useJsonApi.html +1 -1
- package/dist/docs/index.html +6 -3
- package/dist/docs/interfaces/AtomicOperation.html +2 -2
- package/dist/docs/interfaces/BaseEntity.html +2 -2
- package/dist/docs/interfaces/FetchOptions.html +2 -2
- package/dist/docs/interfaces/FetchParams.html +1 -1
- package/dist/docs/interfaces/JsonApiAtomicDocument.html +2 -2
- package/dist/docs/interfaces/JsonApiAtomicOperation.html +2 -2
- package/dist/docs/interfaces/JsonApiAtomicResult.html +2 -2
- package/dist/docs/interfaces/JsonApiConfig.html +4 -4
- package/dist/docs/interfaces/JsonApiDocument.html +2 -2
- package/dist/docs/interfaces/JsonApiError.html +2 -2
- package/dist/docs/interfaces/JsonApiLinkObject.html +2 -2
- package/dist/docs/interfaces/JsonApiLinks.html +2 -2
- package/dist/docs/interfaces/JsonApiMeta.html +2 -2
- package/dist/docs/interfaces/JsonApiRelationship.html +2 -2
- package/dist/docs/interfaces/JsonApiResource.html +2 -2
- package/dist/docs/interfaces/JsonApiResourceIdentifier.html +2 -2
- package/dist/docs/interfaces/ModelDefinition.html +3 -3
- package/dist/docs/interfaces/PageOption.html +2 -2
- package/dist/docs/interfaces/Relationship.html +3 -3
- package/dist/docs/types/JsonApi.html +1 -1
- package/dist/docs/types/JsonApiFetcher.html +1 -1
- package/dist/docs/types/JsonApiLink.html +1 -1
- package/dist/lib.d.ts +1 -0
- package/dist/lib.js +178 -150
- package/dist/lib.js.map +1 -1
- package/package.json +12 -5
package/dist/lib.js
CHANGED
|
@@ -1,235 +1,263 @@
|
|
|
1
|
-
function O(...
|
|
2
|
-
return new URL(
|
|
1
|
+
function O(...s) {
|
|
2
|
+
return new URL(s.join("/")).href;
|
|
3
3
|
}
|
|
4
|
-
class
|
|
4
|
+
class x extends Error {
|
|
5
5
|
constructor(t, n, d) {
|
|
6
6
|
super(t), this.status = n, this.body = d, this.name = "HttpError";
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
-
async function
|
|
9
|
+
async function P(s) {
|
|
10
10
|
try {
|
|
11
|
-
return await
|
|
11
|
+
return await s.json();
|
|
12
12
|
} catch {
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
|
-
async function
|
|
16
|
-
const { headers: n, searchParams: d, method: r, signal:
|
|
15
|
+
async function v(s, t) {
|
|
16
|
+
const { headers: n, searchParams: d, method: r, signal: p, body: i } = t, f = d ? `?${d}` : "", g = s.replace(/(?:\?.*?)?(?=#|$)/, f), T = await fetch(g, {
|
|
17
17
|
method: r,
|
|
18
18
|
headers: n,
|
|
19
|
-
signal:
|
|
20
|
-
body:
|
|
21
|
-
}),
|
|
22
|
-
if (!
|
|
23
|
-
throw new
|
|
24
|
-
return
|
|
19
|
+
signal: p,
|
|
20
|
+
body: i
|
|
21
|
+
}), j = await P(T);
|
|
22
|
+
if (!T.ok)
|
|
23
|
+
throw new x(`HTTP error! status: ${T.status} ${T.statusText}`, T.status, j);
|
|
24
|
+
return j;
|
|
25
25
|
}
|
|
26
|
-
async function C(
|
|
27
|
-
const { signal: n, body: d } = t, r = "POST",
|
|
28
|
-
|
|
29
|
-
const
|
|
26
|
+
async function C(s, t) {
|
|
27
|
+
const { signal: n, body: d } = t, r = "POST", p = new Headers(t.headers ?? {});
|
|
28
|
+
p.append("Accept", 'application/vnd.api+json; ext="https://jsonapi.org/ext/atomic"'), p.append("Content-Type", 'application/vnd.api+json; ext="https://jsonapi.org/ext/atomic"');
|
|
29
|
+
const i = await fetch(s, {
|
|
30
30
|
method: r,
|
|
31
|
-
headers:
|
|
31
|
+
headers: p,
|
|
32
32
|
signal: n,
|
|
33
33
|
body: d
|
|
34
|
-
}),
|
|
35
|
-
if (!
|
|
36
|
-
throw new
|
|
37
|
-
return
|
|
34
|
+
}), f = await P(i);
|
|
35
|
+
if (!i.ok)
|
|
36
|
+
throw new x(`HTTP error! status: ${i.status} ${i.statusText}`, i.status, f);
|
|
37
|
+
return i.status === 204 ? void 0 : f;
|
|
38
38
|
}
|
|
39
39
|
class J {
|
|
40
40
|
constructor(t) {
|
|
41
41
|
this.endpoint = t;
|
|
42
42
|
}
|
|
43
43
|
createOptions(t = {}, n = {}, d) {
|
|
44
|
-
const r = new URLSearchParams(),
|
|
45
|
-
|
|
46
|
-
const
|
|
44
|
+
const r = new URLSearchParams(), p = new Headers(t.headers ?? {});
|
|
45
|
+
p.append("Accept", "application/vnd.api+json");
|
|
46
|
+
const i = { searchParams: r, headers: p, body: d };
|
|
47
47
|
if (t.fields)
|
|
48
|
-
for (const [
|
|
48
|
+
for (const [f, g] of Object.entries(t.fields)) r.append(`fields[${f}]`, g.join(","));
|
|
49
49
|
t.page?.size && r.append("page[size]", t.page.size.toString()), t.page?.number && r.append("page[number]", t.page.number.toString()), t.include && r.append("include", t.include.join(",")), t.filter && r.append("filter", t.filter);
|
|
50
|
-
for (const [
|
|
51
|
-
return
|
|
50
|
+
for (const [f, g] of Object.entries(n)) r.append(f, g);
|
|
51
|
+
return i;
|
|
52
52
|
}
|
|
53
53
|
async fetchDocument(t, n, d, r) {
|
|
54
|
-
const
|
|
55
|
-
n &&
|
|
56
|
-
const
|
|
57
|
-
return await
|
|
54
|
+
const p = [this.endpoint, t];
|
|
55
|
+
n && p.push(n);
|
|
56
|
+
const i = O(...p);
|
|
57
|
+
return await v(i, this.createOptions(d, r));
|
|
58
58
|
}
|
|
59
59
|
async fetchAll(t, n, d) {
|
|
60
60
|
const r = O(this.endpoint, t);
|
|
61
|
-
return (await
|
|
61
|
+
return (await v(r, this.createOptions(n, d))).data;
|
|
62
62
|
}
|
|
63
63
|
async fetchOne(t, n, d, r) {
|
|
64
|
-
const
|
|
65
|
-
return (await
|
|
64
|
+
const p = O(this.endpoint, t, n);
|
|
65
|
+
return (await v(p, this.createOptions(d, r))).data;
|
|
66
66
|
}
|
|
67
|
-
async fetchHasMany(t, n, d, r,
|
|
68
|
-
const
|
|
69
|
-
return await
|
|
67
|
+
async fetchHasMany(t, n, d, r, p) {
|
|
68
|
+
const i = O(this.endpoint, t, n, d);
|
|
69
|
+
return await v(i, this.createOptions(r, p));
|
|
70
70
|
}
|
|
71
|
-
async fetchBelongsTo(t, n, d, r,
|
|
72
|
-
const
|
|
73
|
-
return await
|
|
71
|
+
async fetchBelongsTo(t, n, d, r, p) {
|
|
72
|
+
const i = O(this.endpoint, t, n, d);
|
|
73
|
+
return await v(i, this.createOptions(r, p));
|
|
74
74
|
}
|
|
75
75
|
async post(t, n) {
|
|
76
|
-
const d = O(this.endpoint, t.type),
|
|
76
|
+
const d = O(this.endpoint, t.type), p = JSON.stringify({
|
|
77
77
|
data: t
|
|
78
|
-
}),
|
|
79
|
-
return
|
|
78
|
+
}), i = this.createOptions(n, {}, p);
|
|
79
|
+
return i.method = "POST", i.headers.set("Content-Type", "application/vnd.api+json"), await v(d, i);
|
|
80
|
+
}
|
|
81
|
+
async patch(t, n) {
|
|
82
|
+
if (!t.id) throw new Error("Resource must have an id to be patched");
|
|
83
|
+
const d = O(this.endpoint, t.type, t.id), p = JSON.stringify({
|
|
84
|
+
data: t
|
|
85
|
+
}), i = new Headers(n?.headers ?? {});
|
|
86
|
+
i.set("Content-Type", "application/vnd.api+json"), i.append("Accept", "application/vnd.api+json");
|
|
87
|
+
const f = await fetch(d, {
|
|
88
|
+
method: "PATCH",
|
|
89
|
+
headers: i,
|
|
90
|
+
signal: n?.signal,
|
|
91
|
+
body: p
|
|
92
|
+
}), g = await P(f);
|
|
93
|
+
if (!f.ok)
|
|
94
|
+
throw new x(
|
|
95
|
+
`HTTP error! status: ${f.status} ${f.statusText}`,
|
|
96
|
+
f.status,
|
|
97
|
+
g
|
|
98
|
+
);
|
|
99
|
+
return f.status === 204 ? void 0 : g;
|
|
80
100
|
}
|
|
81
101
|
async postAtomic(t, n = {}) {
|
|
82
102
|
const d = new URL([this.endpoint, "operations"].join("/")).href;
|
|
83
103
|
return n.body = JSON.stringify(t), await C(d, n);
|
|
84
104
|
}
|
|
85
105
|
}
|
|
86
|
-
function F(
|
|
87
|
-
return
|
|
106
|
+
function F(s) {
|
|
107
|
+
return s.replace(/[-][a-z\u00E0-\u00F6\u00F8-\u00FE]/g, (t) => t.slice(1).toUpperCase());
|
|
88
108
|
}
|
|
89
|
-
var L = /* @__PURE__ */ ((
|
|
90
|
-
function
|
|
91
|
-
|
|
109
|
+
var L = /* @__PURE__ */ ((s) => (s[s.HasMany = 0] = "HasMany", s[s.BelongsTo = 1] = "BelongsTo", s))(L || {});
|
|
110
|
+
function $(s, t, n) {
|
|
111
|
+
s[t] = n;
|
|
92
112
|
}
|
|
93
|
-
function
|
|
94
|
-
const t = { type:
|
|
95
|
-
return
|
|
113
|
+
function H(s) {
|
|
114
|
+
const t = { type: s.type };
|
|
115
|
+
return s.lid ? t.lid = s.lid : s.id && (t.id = s.id), t;
|
|
96
116
|
}
|
|
97
|
-
function I(
|
|
98
|
-
const n = t ?? new J(
|
|
99
|
-
for (const e of
|
|
117
|
+
function I(s, t) {
|
|
118
|
+
const n = t ?? new J(s.endpoint), d = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
|
|
119
|
+
for (const e of s.modelDefinitions)
|
|
100
120
|
d.set(e.type, e), e.relationships && r.set(e.type, e.relationships);
|
|
101
|
-
function u(e) {
|
|
102
|
-
return o.kebabCase ? F(e) : e;
|
|
103
|
-
}
|
|
104
121
|
function p(e) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
122
|
+
return s.kebabCase ? F(e) : e;
|
|
123
|
+
}
|
|
124
|
+
function i(e) {
|
|
125
|
+
const o = r.get(e.type), l = H(e);
|
|
126
|
+
l.attributes = {}, o && (l.relationships = {});
|
|
127
|
+
for (const [a, u] of Object.entries(e))
|
|
128
|
+
if (!(a === "id" || a === "lid" || a === "type" || u === void 0))
|
|
129
|
+
if (o && a in o && l.relationships) {
|
|
130
|
+
const y = o[a];
|
|
131
|
+
if (y.relationshipType === 0) {
|
|
132
|
+
const m = u;
|
|
113
133
|
l.relationships[a] = {
|
|
114
|
-
data:
|
|
134
|
+
data: m.map(H)
|
|
115
135
|
};
|
|
116
|
-
} else if (
|
|
117
|
-
const
|
|
136
|
+
} else if (y.relationshipType === 1) {
|
|
137
|
+
const m = u;
|
|
118
138
|
l.relationships[a] = {
|
|
119
|
-
data:
|
|
139
|
+
data: H(m)
|
|
120
140
|
};
|
|
121
141
|
} else
|
|
122
142
|
throw new Error(`Unknown relationship type for ${a}`);
|
|
123
143
|
} else
|
|
124
|
-
l.attributes[a] =
|
|
144
|
+
l.attributes[a] = u;
|
|
125
145
|
return l;
|
|
126
146
|
}
|
|
127
|
-
function
|
|
147
|
+
function f(e, o) {
|
|
128
148
|
if (!d.get(e)) throw new Error(`Model type ${e} not defined`);
|
|
129
|
-
const a =
|
|
130
|
-
if (
|
|
131
|
-
const
|
|
132
|
-
for (const [
|
|
133
|
-
|
|
134
|
-
return
|
|
149
|
+
const a = o.id ?? crypto.randomUUID(), u = { id: a, type: e, ...o };
|
|
150
|
+
if (s.kebabCase) {
|
|
151
|
+
const y = { id: a, type: e };
|
|
152
|
+
for (const [m, h] of Object.entries(o))
|
|
153
|
+
m !== "id" && h !== void 0 && (y[p(m)] = h);
|
|
154
|
+
return y;
|
|
135
155
|
}
|
|
136
|
-
return
|
|
156
|
+
return u;
|
|
137
157
|
}
|
|
138
|
-
function g(e,
|
|
139
|
-
function l(
|
|
140
|
-
return
|
|
141
|
-
id:
|
|
142
|
-
...
|
|
158
|
+
function g(e, o) {
|
|
159
|
+
function l(c) {
|
|
160
|
+
return f(c.type, {
|
|
161
|
+
id: c.id,
|
|
162
|
+
...c.attributes
|
|
143
163
|
});
|
|
144
164
|
}
|
|
145
|
-
function a(
|
|
146
|
-
|
|
165
|
+
function a(c, w) {
|
|
166
|
+
c.has(w.type) || c.set(w.type, /* @__PURE__ */ new Map()), c.get(w.type)?.set(w.id, w);
|
|
147
167
|
}
|
|
148
|
-
function c
|
|
149
|
-
if (
|
|
150
|
-
return
|
|
168
|
+
function u(c, w) {
|
|
169
|
+
if (w.id === void 0) throw new Error("Resource identifier must have an id");
|
|
170
|
+
return c.has(w.type) || c.set(w.type, /* @__PURE__ */ new Map()), c.get(w.type)?.get(w.id);
|
|
151
171
|
}
|
|
152
|
-
const
|
|
153
|
-
if (
|
|
154
|
-
const
|
|
155
|
-
for (const
|
|
156
|
-
function
|
|
157
|
-
const
|
|
158
|
-
if (!
|
|
159
|
-
if (!
|
|
160
|
-
const R = r.get(
|
|
172
|
+
const y = /* @__PURE__ */ new Map();
|
|
173
|
+
if (o) for (const c of o) a(y, l(c));
|
|
174
|
+
const m = e.map(l), h = /* @__PURE__ */ new Map();
|
|
175
|
+
for (const c of m) a(h, c);
|
|
176
|
+
function b(c) {
|
|
177
|
+
const w = u(h, c) ?? u(y, c);
|
|
178
|
+
if (!w) throw new Error("Unexpected not found record");
|
|
179
|
+
if (!c.relationships) return;
|
|
180
|
+
const R = r.get(c.type);
|
|
161
181
|
if (R)
|
|
162
|
-
for (const [E,
|
|
163
|
-
const
|
|
164
|
-
if (!
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
182
|
+
for (const [E, M] of Object.entries(c.relationships)) {
|
|
183
|
+
const z = p(E), D = R[z];
|
|
184
|
+
if (!D || !M.data) continue;
|
|
185
|
+
const S = (D.relationshipType === 0 ? M.data : [M.data]).filter((A) => A?.type === D.type).map((A) => u(y, A) || u(h, A)).filter(Boolean);
|
|
186
|
+
$(
|
|
187
|
+
w,
|
|
188
|
+
z,
|
|
189
|
+
D.relationshipType === 0 ? S : S[0]
|
|
170
190
|
);
|
|
171
191
|
}
|
|
172
192
|
}
|
|
173
|
-
if (
|
|
174
|
-
for (const
|
|
175
|
-
for (const
|
|
193
|
+
if (o) {
|
|
194
|
+
for (const c of e) b(c);
|
|
195
|
+
for (const c of o) b(c);
|
|
176
196
|
}
|
|
177
|
-
return
|
|
197
|
+
return m;
|
|
178
198
|
}
|
|
179
|
-
async function
|
|
180
|
-
const a = await n.fetchDocument(e, void 0,
|
|
181
|
-
return { doc: a, records:
|
|
199
|
+
async function T(e, o, l) {
|
|
200
|
+
const a = await n.fetchDocument(e, void 0, o, l), u = a.data, y = g(u, a.included);
|
|
201
|
+
return { doc: a, records: y };
|
|
182
202
|
}
|
|
183
|
-
async function
|
|
184
|
-
const
|
|
185
|
-
if (!
|
|
186
|
-
return
|
|
203
|
+
async function j(e, o, l, a) {
|
|
204
|
+
const u = await n.fetchDocument(e, o, l, a), y = u.data, h = g([y], u.included)[0];
|
|
205
|
+
if (!h) throw new Error(`Record with id ${o} not found`);
|
|
206
|
+
return h;
|
|
187
207
|
}
|
|
188
|
-
async function
|
|
189
|
-
const
|
|
190
|
-
if (!
|
|
191
|
-
const
|
|
192
|
-
if (!
|
|
193
|
-
if (
|
|
194
|
-
const
|
|
208
|
+
async function k(e, o, l, a) {
|
|
209
|
+
const u = e.type, y = r.get(u);
|
|
210
|
+
if (!y) throw new Error(`Model ${u} has no relationships`);
|
|
211
|
+
const m = y[o];
|
|
212
|
+
if (!m) throw new Error(`Relationship ${o} not defined`);
|
|
213
|
+
if (m.relationshipType === 1) {
|
|
214
|
+
const w = await n.fetchBelongsTo(u, e.id, o, l, a), R = w.data, E = f(m.type, {
|
|
195
215
|
id: R.id,
|
|
196
216
|
...R.attributes
|
|
197
217
|
});
|
|
198
|
-
return
|
|
218
|
+
return $(e, o, E), w;
|
|
199
219
|
}
|
|
200
|
-
const
|
|
201
|
-
(
|
|
202
|
-
id:
|
|
203
|
-
...
|
|
220
|
+
const h = await n.fetchHasMany(u, e.id, o, l, a), c = (m.relationshipType === 0 ? h.data : [h.data]).map(
|
|
221
|
+
(w) => f(m.type, {
|
|
222
|
+
id: w.id,
|
|
223
|
+
...w.attributes
|
|
204
224
|
})
|
|
205
225
|
);
|
|
206
|
-
return
|
|
226
|
+
return $(
|
|
207
227
|
e,
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
),
|
|
228
|
+
o,
|
|
229
|
+
m.relationshipType === 0 ? c : c[0]
|
|
230
|
+
), h;
|
|
211
231
|
}
|
|
212
|
-
async function
|
|
213
|
-
const l =
|
|
232
|
+
async function U(e, o) {
|
|
233
|
+
const l = i(e);
|
|
234
|
+
let a;
|
|
235
|
+
if (e.lid || !e.id)
|
|
236
|
+
a = await n.post(l, o);
|
|
237
|
+
else if (a = await n.patch(l, o), !a) {
|
|
238
|
+
if (!e.id) throw new Error("Cannot refetch record without id");
|
|
239
|
+
return await j(e.type, e.id, o);
|
|
240
|
+
}
|
|
214
241
|
return g([a.data])[0];
|
|
215
242
|
}
|
|
216
|
-
async function B(e,
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
243
|
+
async function B(e, o) {
|
|
244
|
+
function l(h) {
|
|
245
|
+
const b = { op: h.op };
|
|
246
|
+
return h.data && (b.data = i(h.data), h.op === "update" && delete b.data.relationships), h.ref && (b.ref = h.ref), b;
|
|
247
|
+
}
|
|
248
|
+
const u = {
|
|
249
|
+
"atomic:operations": e.map(l)
|
|
250
|
+
}, y = await n.postAtomic(u, o);
|
|
251
|
+
if (!y) return;
|
|
252
|
+
const m = y["atomic:results"] ? g(y["atomic:results"].map((h) => h.data)) : [];
|
|
253
|
+
return { doc: y, records: m };
|
|
226
254
|
}
|
|
227
255
|
return {
|
|
228
|
-
findAll:
|
|
229
|
-
findRecord:
|
|
230
|
-
findRelated:
|
|
231
|
-
createRecord:
|
|
232
|
-
saveRecord:
|
|
256
|
+
findAll: T,
|
|
257
|
+
findRecord: j,
|
|
258
|
+
findRelated: k,
|
|
259
|
+
createRecord: f,
|
|
260
|
+
saveRecord: U,
|
|
233
261
|
saveAtomic: B
|
|
234
262
|
};
|
|
235
263
|
}
|
package/dist/lib.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"lib.js","sources":["../src/json-api-fetcher.ts","../src/util.ts","../src/json-api.ts"],"sourcesContent":["import type { JsonApiAtomicDocument, JsonApiDocument, JsonApiResource } from './json-api.ts'\n\nfunction resolvePath(...segments: string[]): string {\n return new URL(segments.join('/')).href\n}\n\nexport interface PageOption {\n size?: number\n number?: number\n}\n\nexport interface FetchOptions {\n fields?: Record<string, string[]>\n page?: PageOption\n include?: string[]\n filter?: string\n headers?: HeadersInit\n body?: BodyInit\n signal?: AbortSignal\n}\n\nexport interface FetchParams {\n [key: string]: string\n}\n\nexport interface Options {\n searchParams?: URLSearchParams\n headers: Headers\n method?: string\n body?: BodyInit\n signal?: AbortSignal\n}\n\nclass HttpError extends Error {\n constructor(\n message: string,\n public status: number,\n public body?: unknown,\n ) {\n super(message)\n this.name = 'HttpError'\n }\n}\n\nasync function tryJson(response: Response) {\n try {\n return await response.json()\n } catch {\n // Ignore JSON parsing errors\n }\n}\n\nasync function req(url: string, options: Options) {\n const { headers, searchParams, method, signal, body } = options\n const textSearchParams = searchParams ? `?${searchParams}` : ''\n const finalUrl = url.replace(/(?:\\?.*?)?(?=#|$)/, textSearchParams)\n const response = await fetch(finalUrl, {\n method,\n headers,\n signal,\n body,\n })\n const responseBody = await tryJson(response)\n if (!response.ok)\n throw new HttpError(`HTTP error! status: ${response.status} ${response.statusText}`, response.status, responseBody)\n const data = responseBody as JsonApiDocument\n return data\n}\n\nasync function postAtomic(url: string, options: FetchOptions) {\n const { signal, body } = options\n const method = 'POST'\n const headers = new Headers(options.headers ?? {})\n headers.append('Accept', 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"')\n headers.append('Content-Type', 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"')\n const response = await fetch(url, {\n method,\n headers,\n signal,\n body,\n })\n const responseBody = await tryJson(response)\n if (!response.ok)\n throw new HttpError(`HTTP error! status: ${response.status} ${response.statusText}`, response.status, responseBody)\n if (response.status === 204) return\n const data = responseBody as JsonApiAtomicDocument\n return data\n}\n\nexport type JsonApiFetcher = InstanceType<typeof JsonApiFetcherImpl>\n\nexport class JsonApiFetcherImpl implements JsonApiFetcher {\n constructor(public endpoint: string) {}\n createOptions(options: FetchOptions = {}, params: FetchParams = {}, body?: BodyInit): Options {\n const searchParams = new URLSearchParams()\n const headers = new Headers(options.headers ?? {})\n headers.append('Accept', 'application/vnd.api+json')\n const requestOptions = { searchParams, headers, body }\n if (options.fields)\n for (const [key, value] of Object.entries(options.fields)) searchParams.append(`fields[${key}]`, value.join(','))\n if (options.page?.size) searchParams.append('page[size]', options.page.size.toString())\n if (options.page?.number) searchParams.append('page[number]', options.page.number.toString())\n if (options.include) searchParams.append('include', options.include.join(','))\n if (options.filter) searchParams.append('filter', options.filter)\n for (const [key, value] of Object.entries(params)) searchParams.append(key, value)\n return requestOptions\n }\n async fetchDocument(type: string, id?: string, options?: FetchOptions, params?: FetchParams) {\n const segments = [this.endpoint, type]\n if (id) segments.push(id)\n const url = resolvePath(...segments)\n const doc = await req(url, this.createOptions(options, params))\n return doc\n }\n async fetchAll(type: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type)\n const doc = await req(url, this.createOptions(options, params))\n const resources = doc.data as JsonApiResource[]\n return resources\n }\n async fetchOne(type: string, id: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type, id)\n const doc = await req(url, this.createOptions(options, params))\n const resource = doc.data as JsonApiResource\n return resource\n }\n async fetchHasMany(type: string, id: string, name: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type, id, name)\n const doc = await req(url, this.createOptions(options, params))\n return doc\n }\n async fetchBelongsTo(type: string, id: string, name: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type, id, name)\n const doc = await req(url, this.createOptions(options, params))\n return doc\n }\n async post(resource: JsonApiResource, options?: FetchOptions) {\n const url = resolvePath(this.endpoint, resource.type)\n const postDoc: JsonApiDocument = {\n data: resource,\n }\n const body = JSON.stringify(postDoc)\n const newOptions = this.createOptions(options, {}, body)\n newOptions.method = 'POST'\n newOptions.headers.set('Content-Type', 'application/vnd.api+json')\n const doc = await req(url, newOptions)\n return doc\n }\n async postAtomic(doc: JsonApiAtomicDocument, options: FetchOptions = {}) {\n const url = new URL([this.endpoint, 'operations'].join('/')).href\n options.body = JSON.stringify(doc)\n const results = await postAtomic(url, options)\n return results\n }\n}\n","/**\n * Convert str from kebab-case to camelCase\n */\nexport function camel(str: string) {\n return str.replace(/[-][a-z\\u00E0-\\u00F6\\u00F8-\\u00FE]/g, (match) => match.slice(1).toUpperCase())\n}\n","import { type FetchOptions, type FetchParams, type JsonApiFetcher, JsonApiFetcherImpl } from './json-api-fetcher.ts'\nimport { camel } from './util.ts'\n\nexport interface JsonApiResourceIdentifier {\n id?: string\n lid?: string\n type: string\n}\n\nexport interface JsonApiRelationship {\n data: null | [] | JsonApiResourceIdentifier | JsonApiResourceIdentifier[]\n}\n\nexport interface JsonApiResource {\n id?: string\n lid?: string\n type: string\n attributes: Record<string, unknown>\n relationships?: Record<string, JsonApiRelationship>\n}\n\nexport interface JsonApiMeta {\n // Pagination\n totalPages?: number\n totalItems?: number\n currentPage?: number\n itemsPerPage?: number\n\n // Common metadata\n timestamp?: string | number\n version?: string\n copyright?: string\n\n // Allow additional custom properties\n [key: string]: unknown\n}\n\nexport interface JsonApiLinkObject {\n href: string\n rel?: string\n describedby?: JsonApiLink\n title?: string\n type?: string\n hreflang?: string | string[]\n meta?: JsonApiMeta\n}\n\nexport type JsonApiLink = null | string | JsonApiLinkObject\n\nexport interface JsonApiLinks {\n self?: JsonApiLink\n related?: JsonApiLink\n describedby?: JsonApiLink\n first?: JsonApiLink\n last?: JsonApiLink\n prev?: JsonApiLink\n next?: JsonApiLink\n}\n\nexport interface JsonApiDocument {\n links?: JsonApiLinks\n data?: JsonApiResource | JsonApiResource[]\n errors?: JsonApiError[]\n included?: JsonApiResource[]\n meta?: JsonApiMeta\n}\n\nexport interface JsonApiReference extends JsonApiResourceIdentifier {\n relationship?: string\n}\n\nexport interface JsonApiError {\n id: string\n status: string\n code?: string\n title: string\n detail?: string\n meta?: JsonApiMeta\n}\n\nexport interface JsonApiAtomicOperation {\n op: 'add' | 'update' | 'remove'\n data?: JsonApiResource | JsonApiResourceIdentifier[]\n ref?: JsonApiReference\n}\n\nexport interface JsonApiAtomicResult {\n data: JsonApiResource\n meta?: JsonApiMeta\n}\n\nexport interface JsonApiAtomicDocument {\n 'atomic:operations'?: JsonApiAtomicOperation[]\n 'atomic:results'?: JsonApiAtomicResult[]\n errors?: JsonApiError[]\n}\n\nexport interface AtomicOperation {\n op: 'add' | 'update' | 'remove'\n data?: BaseEntity\n ref?: JsonApiReference\n}\n\nexport interface BaseEntity {\n id: string\n lid?: string\n type: string\n}\n\n/**\n * Model definition\n */\nexport interface ModelDefinition {\n /**\n * The JSON:API type for the model\n */\n type: string\n /**\n * Optional relationships for the model\n */\n relationships?: Record<string, Relationship>\n}\n\nexport interface JsonApiConfig {\n /**\n * The URL for the JSON:API endpoint\n */\n endpoint: string\n /**\n * Model definitions for the store\n */\n modelDefinitions: ModelDefinition[]\n /**\n * Whether to convert kebab-case names from JSON:API (older convention) to camelCase\n */\n kebabCase?: boolean\n}\n\nexport enum RelationshipType {\n HasMany = 0,\n BelongsTo = 1,\n}\n\n/**\n * Relationship definition\n */\nexport interface Relationship {\n /** The JSON:API type name of the related model */\n type: string\n /** The relationship type */\n relationshipType: RelationshipType\n}\n\nfunction setRelationship(record: BaseEntity, name: string, value: unknown): void {\n ;(record as unknown as Record<string, unknown>)[name] = value\n}\n\nexport type JsonApi = ReturnType<typeof useJsonApi>\n\nfunction serializeRid(entity: BaseEntity): JsonApiResourceIdentifier {\n const rid: JsonApiResourceIdentifier = { type: entity.type }\n if (entity.lid) rid.lid = entity.lid\n else if (entity.id) rid.id = entity.id\n return rid\n}\n\nexport function useJsonApi(config: JsonApiConfig, fetcher?: JsonApiFetcher) {\n const _fetcher = fetcher ?? new JsonApiFetcherImpl(config.endpoint)\n\n // Map type names to their definitions\n const modelDefinitions = new Map<string, ModelDefinition>()\n const relationshipDefinitions = new Map<string, Record<string, Relationship>>()\n\n for (const modelDef of config.modelDefinitions) {\n modelDefinitions.set(modelDef.type, modelDef)\n if (modelDef.relationships) relationshipDefinitions.set(modelDef.type, modelDef.relationships)\n }\n\n function normalize(str: string) {\n return config.kebabCase ? camel(str) : str\n }\n\n function serialize(record: BaseEntity): JsonApiResource {\n const relationships = relationshipDefinitions.get(record.type)\n const resource: JsonApiResource = serializeRid(record) as JsonApiResource\n resource.attributes = {}\n if (relationships) resource.relationships = {}\n for (const [key, value] of Object.entries(record)) {\n if (key === 'id' || key === 'lid' || key === 'type' || value === undefined) continue\n if (relationships && key in relationships && resource.relationships) {\n const rel = relationships[key]\n if (rel.relationshipType === RelationshipType.HasMany) {\n const entities = value as unknown as BaseEntity[]\n resource.relationships[key] = {\n data: entities.map(serializeRid),\n }\n } else if (rel.relationshipType === RelationshipType.BelongsTo) {\n const entity = value as unknown as BaseEntity\n resource.relationships[key] = {\n data: serializeRid(entity),\n }\n } else {\n throw new Error(`Unknown relationship type for ${key}`)\n }\n } else {\n resource.attributes[key] = value\n }\n }\n return resource\n }\n\n function createRecord<T extends BaseEntity>(type: string, properties: Partial<T> & { id?: string }): T {\n const modelDef = modelDefinitions.get(type)\n if (!modelDef) throw new Error(`Model type ${type} not defined`)\n\n const id = properties.id ?? crypto.randomUUID()\n\n const record = { id, type, ...properties } as T\n\n // Normalize property keys if needed\n if (config.kebabCase) {\n const normalizedRecord = { id, type } as BaseEntity & Record<string, unknown>\n for (const [key, value] of Object.entries(properties))\n if (key !== 'id' && value !== undefined) normalizedRecord[normalize(key)] = value\n return normalizedRecord as T\n }\n\n return record\n }\n\n function resourcesToRecords(resources: JsonApiResource[], included?: JsonApiResource[]): BaseEntity[] {\n function resourceToRecord(resource: JsonApiResource): BaseEntity {\n return createRecord(resource.type, {\n id: resource.id,\n ...resource.attributes,\n })\n }\n\n function setRecord(map: Map<string, Map<string, BaseEntity>>, record: BaseEntity) {\n if (!map.has(record.type)) map.set(record.type, new Map())\n map.get(record.type)?.set(record.id, record)\n }\n\n function getRecord(map: Map<string, Map<string, BaseEntity>>, rid: JsonApiResourceIdentifier) {\n if (rid.id === undefined) throw new Error('Resource identifier must have an id')\n if (!map.has(rid.type)) map.set(rid.type, new Map())\n return map.get(rid.type)?.get(rid.id)\n }\n\n const includedMap = new Map<string, Map<string, BaseEntity>>()\n if (included) for (const resource of included) setRecord(includedMap, resourceToRecord(resource))\n\n const records = resources.map(resourceToRecord)\n const recordsMap = new Map<string, Map<string, BaseEntity>>()\n for (const record of records) setRecord(recordsMap, record)\n\n function populateRelationships(resource: JsonApiResource) {\n const record = getRecord(recordsMap, resource) ?? getRecord(includedMap, resource)\n if (!record) throw new Error('Unexpected not found record')\n\n if (!resource.relationships) return\n\n const rels = relationshipDefinitions.get(resource.type)\n if (!rels) return\n\n for (const [name, reldoc] of Object.entries(resource.relationships)) {\n const normalizedName = normalize(name)\n const rel = rels[normalizedName]\n if (!rel) continue\n if (!reldoc.data) continue\n const rids =\n rel.relationshipType === RelationshipType.HasMany\n ? (reldoc.data as JsonApiResourceIdentifier[])\n : [reldoc.data as JsonApiResourceIdentifier]\n const relatedRecords = rids\n .filter((d) => d && d.type === rel.type)\n .map((d) => getRecord(includedMap, d) || getRecord(recordsMap, d))\n .filter(Boolean)\n setRelationship(\n record,\n normalizedName,\n rel.relationshipType === RelationshipType.HasMany ? relatedRecords : relatedRecords[0],\n )\n }\n }\n\n if (included) {\n for (const r of resources) populateRelationships(r)\n for (const r of included) populateRelationships(r)\n }\n\n return records\n }\n\n async function findAll<T extends BaseEntity>(\n type: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<{ doc: JsonApiDocument; records: T[] }> {\n const doc = await _fetcher.fetchDocument(type, undefined, options, params)\n const resources = doc.data as JsonApiResource[]\n const records = resourcesToRecords(resources, doc.included) as T[]\n return { doc, records }\n }\n\n async function findRecord<T extends BaseEntity>(\n type: string,\n id: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<T> {\n const doc = await _fetcher.fetchDocument(type, id, options, params)\n const resource = doc.data as JsonApiResource\n const records = resourcesToRecords([resource], doc.included) as T[]\n const record = records[0]\n if (!record) throw new Error(`Record with id ${id} not found`)\n return record\n }\n\n async function findRelated(\n record: BaseEntity,\n relationshipName: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument> {\n const type = record.type\n const rels = relationshipDefinitions.get(type)\n if (!rels) throw new Error(`Model ${type} has no relationships`)\n\n const rel = rels[relationshipName]\n if (!rel) throw new Error(`Relationship ${relationshipName} not defined`)\n\n if (rel.relationshipType === RelationshipType.BelongsTo) {\n const doc = await _fetcher.fetchBelongsTo(type, record.id, relationshipName, options, params)\n const related = doc.data as JsonApiResource\n const relatedRecord = createRecord(rel.type, {\n id: related.id,\n ...related.attributes,\n })\n setRelationship(record, relationshipName, relatedRecord)\n return doc\n }\n\n const doc = await _fetcher.fetchHasMany(type, record.id, relationshipName, options, params)\n const related =\n rel.relationshipType === RelationshipType.HasMany\n ? (doc.data as JsonApiResource[])\n : [doc.data as JsonApiResource]\n\n const relatedRecords = related.map((r) =>\n createRecord(rel.type, {\n id: r.id,\n ...r.attributes,\n }),\n )\n\n setRelationship(\n record,\n relationshipName,\n rel.relationshipType === RelationshipType.HasMany ? relatedRecords : relatedRecords[0],\n )\n\n return doc\n }\n\n async function saveRecord<T extends BaseEntity>(record: BaseEntity, options?: FetchOptions): Promise<T> {\n const resource = serialize(record)\n const doc = await _fetcher.post(resource, options)\n const records = resourcesToRecords([doc.data] as JsonApiResource[])\n return records[0] as T\n }\n\n async function saveAtomic(\n operations: AtomicOperation[],\n options?: FetchOptions,\n ): Promise<{ doc: JsonApiAtomicDocument; records: BaseEntity[] } | undefined> {\n const atomicOperations = operations.map((op) => {\n const jsonApiOp: JsonApiAtomicOperation = { op: op.op }\n if (op.data) jsonApiOp.data = serialize(op.data)\n if (op.ref) jsonApiOp.ref = op.ref\n return jsonApiOp\n })\n const atomicDoc: JsonApiAtomicDocument = {\n 'atomic:operations': atomicOperations,\n }\n const doc = await _fetcher.postAtomic(atomicDoc, options)\n if (!doc) return\n const records = doc['atomic:results'] ? resourcesToRecords(doc['atomic:results'].map((r) => r.data)) : []\n return { doc, records }\n }\n\n return {\n findAll,\n findRecord,\n findRelated,\n createRecord,\n saveRecord,\n saveAtomic,\n }\n}\n"],"names":["resolvePath","segments","HttpError","message","status","body","tryJson","response","req","url","options","headers","searchParams","method","signal","textSearchParams","finalUrl","responseBody","postAtomic","JsonApiFetcherImpl","endpoint","params","requestOptions","key","value","type","id","name","resource","newOptions","doc","camel","str","match","RelationshipType","setRelationship","record","serializeRid","entity","rid","useJsonApi","config","fetcher","_fetcher","modelDefinitions","relationshipDefinitions","modelDef","normalize","serialize","relationships","rel","entities","createRecord","properties","normalizedRecord","resourcesToRecords","resources","included","resourceToRecord","setRecord","map","getRecord","includedMap","records","recordsMap","populateRelationships","rels","reldoc","normalizedName","relatedRecords","d","r","findAll","findRecord","findRelated","relationshipName","related","relatedRecord","saveRecord","saveAtomic","operations","atomicDoc","op","jsonApiOp"],"mappings":"AAEA,SAASA,KAAeC,GAA4B;AAClD,SAAO,IAAI,IAAIA,EAAS,KAAK,GAAG,CAAC,EAAE;AACrC;AA6BA,MAAMC,UAAkB,MAAM;AAAA,EAC5B,YACEC,GACOC,GACAC,GACP;AACA,UAAMF,CAAO,GAHN,KAAA,SAAAC,GACA,KAAA,OAAAC,GAGP,KAAK,OAAO;AAAA,EACd;AACF;AAEA,eAAeC,EAAQC,GAAoB;AACzC,MAAI;AACF,WAAO,MAAMA,EAAS,KA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AAMA,EAAE,IAAI,CAAC,IAAI,CAAA;AACvG,WAAO,EAAE,KAAAzC,GAAK,SAAAiC,EAAA;AAAA,EAChB;AAEA,SAAO;AAAA,IACL,SAAAS;AAAA,IACA,YAAAC;AAAA,IACA,aAAAC;AAAA,IACA,cAAAtB;AAAA,IACA,YAAA0B;AAAA,IACA,YAAAC;AAAA,EAAA;AAEJ;"}
|
|
1
|
+
{"version":3,"file":"lib.js","sources":["../src/json-api-fetcher.ts","../src/util.ts","../src/json-api.ts"],"sourcesContent":["import type { JsonApiAtomicDocument, JsonApiDocument, JsonApiResource } from './json-api.ts'\n\nfunction resolvePath(...segments: string[]): string {\n return new URL(segments.join('/')).href\n}\n\nexport interface PageOption {\n size?: number\n number?: number\n}\n\nexport interface FetchOptions {\n fields?: Record<string, string[]>\n page?: PageOption\n include?: string[]\n filter?: string\n headers?: HeadersInit\n body?: BodyInit\n signal?: AbortSignal\n}\n\nexport interface FetchParams {\n [key: string]: string\n}\n\nexport interface Options {\n searchParams?: URLSearchParams\n headers: Headers\n method?: string\n body?: BodyInit\n signal?: AbortSignal\n}\n\nclass HttpError extends Error {\n constructor(\n message: string,\n public status: number,\n public body?: unknown,\n ) {\n super(message)\n this.name = 'HttpError'\n }\n}\n\nasync function tryJson(response: Response) {\n try {\n return await response.json()\n } catch {\n // Ignore JSON parsing errors\n }\n}\n\nasync function req(url: string, options: Options) {\n const { headers, searchParams, method, signal, body } = options\n const textSearchParams = searchParams ? `?${searchParams}` : ''\n const finalUrl = url.replace(/(?:\\?.*?)?(?=#|$)/, textSearchParams)\n const response = await fetch(finalUrl, {\n method,\n headers,\n signal,\n body,\n })\n const responseBody = await tryJson(response)\n if (!response.ok)\n throw new HttpError(`HTTP error! status: ${response.status} ${response.statusText}`, response.status, responseBody)\n const data = responseBody as JsonApiDocument\n return data\n}\n\nasync function postAtomic(url: string, options: FetchOptions) {\n const { signal, body } = options\n const method = 'POST'\n const headers = new Headers(options.headers ?? {})\n headers.append('Accept', 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"')\n headers.append('Content-Type', 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"')\n const response = await fetch(url, {\n method,\n headers,\n signal,\n body,\n })\n const responseBody = await tryJson(response)\n if (!response.ok)\n throw new HttpError(`HTTP error! status: ${response.status} ${response.statusText}`, response.status, responseBody)\n if (response.status === 204) return\n const data = responseBody as JsonApiAtomicDocument\n return data\n}\n\nexport type JsonApiFetcher = InstanceType<typeof JsonApiFetcherImpl>\n\nexport class JsonApiFetcherImpl implements JsonApiFetcher {\n constructor(public endpoint: string) {}\n createOptions(options: FetchOptions = {}, params: FetchParams = {}, body?: BodyInit): Options {\n const searchParams = new URLSearchParams()\n const headers = new Headers(options.headers ?? {})\n headers.append('Accept', 'application/vnd.api+json')\n const requestOptions = { searchParams, headers, body }\n if (options.fields)\n for (const [key, value] of Object.entries(options.fields)) searchParams.append(`fields[${key}]`, value.join(','))\n if (options.page?.size) searchParams.append('page[size]', options.page.size.toString())\n if (options.page?.number) searchParams.append('page[number]', options.page.number.toString())\n if (options.include) searchParams.append('include', options.include.join(','))\n if (options.filter) searchParams.append('filter', options.filter)\n for (const [key, value] of Object.entries(params)) searchParams.append(key, value)\n return requestOptions\n }\n async fetchDocument(type: string, id?: string, options?: FetchOptions, params?: FetchParams) {\n const segments = [this.endpoint, type]\n if (id) segments.push(id)\n const url = resolvePath(...segments)\n const doc = await req(url, this.createOptions(options, params))\n return doc\n }\n async fetchAll(type: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type)\n const doc = await req(url, this.createOptions(options, params))\n const resources = doc.data as JsonApiResource[]\n return resources\n }\n async fetchOne(type: string, id: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type, id)\n const doc = await req(url, this.createOptions(options, params))\n const resource = doc.data as JsonApiResource\n return resource\n }\n async fetchHasMany(type: string, id: string, name: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type, id, name)\n const doc = await req(url, this.createOptions(options, params))\n return doc\n }\n async fetchBelongsTo(type: string, id: string, name: string, options?: FetchOptions, params?: FetchParams) {\n const url = resolvePath(this.endpoint, type, id, name)\n const doc = await req(url, this.createOptions(options, params))\n return doc\n }\n async post(resource: JsonApiResource, options?: FetchOptions) {\n const url = resolvePath(this.endpoint, resource.type)\n const postDoc: JsonApiDocument = {\n data: resource,\n }\n const body = JSON.stringify(postDoc)\n const newOptions = this.createOptions(options, {}, body)\n newOptions.method = 'POST'\n newOptions.headers.set('Content-Type', 'application/vnd.api+json')\n const doc = await req(url, newOptions)\n return doc\n }\n async patch(resource: JsonApiResource, options?: FetchOptions) {\n if (!resource.id) throw new Error('Resource must have an id to be patched')\n const url = resolvePath(this.endpoint, resource.type, resource.id)\n const patchDoc: JsonApiDocument = {\n data: resource,\n }\n const body = JSON.stringify(patchDoc)\n const headers = new Headers(options?.headers ?? {})\n headers.set('Content-Type', 'application/vnd.api+json')\n headers.append('Accept', 'application/vnd.api+json')\n const response = await fetch(url, {\n method: 'PATCH',\n headers,\n signal: options?.signal,\n body,\n })\n const responseBody = await tryJson(response)\n if (!response.ok)\n throw new HttpError(\n `HTTP error! status: ${response.status} ${response.statusText}`,\n response.status,\n responseBody,\n )\n if (response.status === 204) return\n const doc = responseBody as JsonApiDocument\n return doc\n }\n async postAtomic(doc: JsonApiAtomicDocument, options: FetchOptions = {}) {\n const url = new URL([this.endpoint, 'operations'].join('/')).href\n options.body = JSON.stringify(doc)\n const results = await postAtomic(url, options)\n return results\n }\n}\n","/**\n * Convert str from kebab-case to camelCase\n */\nexport function camel(str: string) {\n return str.replace(/[-][a-z\\u00E0-\\u00F6\\u00F8-\\u00FE]/g, (match) => match.slice(1).toUpperCase())\n}\n","import { type FetchOptions, type FetchParams, type JsonApiFetcher, JsonApiFetcherImpl } from './json-api-fetcher.ts'\nimport { camel } from './util.ts'\n\nexport interface JsonApiResourceIdentifier {\n id?: string\n lid?: string\n type: string\n}\n\nexport interface JsonApiRelationship {\n data: null | [] | JsonApiResourceIdentifier | JsonApiResourceIdentifier[]\n}\n\nexport interface JsonApiResource {\n id?: string\n lid?: string\n type: string\n attributes: Record<string, unknown>\n relationships?: Record<string, JsonApiRelationship>\n}\n\nexport interface JsonApiMeta {\n // Pagination\n totalPages?: number\n totalItems?: number\n currentPage?: number\n itemsPerPage?: number\n\n // Common metadata\n timestamp?: string | number\n version?: string\n copyright?: string\n\n // Allow additional custom properties\n [key: string]: unknown\n}\n\nexport interface JsonApiLinkObject {\n href: string\n rel?: string\n describedby?: JsonApiLink\n title?: string\n type?: string\n hreflang?: string | string[]\n meta?: JsonApiMeta\n}\n\nexport type JsonApiLink = null | string | JsonApiLinkObject\n\nexport interface JsonApiLinks {\n self?: JsonApiLink\n related?: JsonApiLink\n describedby?: JsonApiLink\n first?: JsonApiLink\n last?: JsonApiLink\n prev?: JsonApiLink\n next?: JsonApiLink\n}\n\nexport interface JsonApiDocument {\n links?: JsonApiLinks\n data?: JsonApiResource | JsonApiResource[]\n errors?: JsonApiError[]\n included?: JsonApiResource[]\n meta?: JsonApiMeta\n}\n\nexport interface JsonApiReference extends JsonApiResourceIdentifier {\n relationship?: string\n}\n\nexport interface JsonApiError {\n id: string\n status: string\n code?: string\n title: string\n detail?: string\n meta?: JsonApiMeta\n}\n\nexport interface JsonApiAtomicOperation {\n op: 'add' | 'update' | 'remove'\n data?: JsonApiResource | JsonApiResourceIdentifier[]\n ref?: JsonApiReference\n}\n\nexport interface JsonApiAtomicResult {\n data: JsonApiResource\n meta?: JsonApiMeta\n}\n\nexport interface JsonApiAtomicDocument {\n 'atomic:operations'?: JsonApiAtomicOperation[]\n 'atomic:results'?: JsonApiAtomicResult[]\n errors?: JsonApiError[]\n}\n\nexport interface AtomicOperation {\n op: 'add' | 'update' | 'remove'\n data?: BaseEntity\n ref?: JsonApiReference\n}\n\nexport interface BaseEntity {\n id: string\n lid?: string\n type: string\n}\n\n/**\n * Model definition\n */\nexport interface ModelDefinition {\n /**\n * The JSON:API type for the model\n */\n type: string\n /**\n * Optional relationships for the model\n */\n relationships?: Record<string, Relationship>\n}\n\nexport interface JsonApiConfig {\n /**\n * The URL for the JSON:API endpoint\n */\n endpoint: string\n /**\n * Model definitions for the store\n */\n modelDefinitions: ModelDefinition[]\n /**\n * Whether to convert kebab-case names from JSON:API (older convention) to camelCase\n */\n kebabCase?: boolean\n}\n\nexport enum RelationshipType {\n HasMany = 0,\n BelongsTo = 1,\n}\n\n/**\n * Relationship definition\n */\nexport interface Relationship {\n /** The JSON:API type name of the related model */\n type: string\n /** The relationship type */\n relationshipType: RelationshipType\n}\n\nfunction setRelationship(record: BaseEntity, name: string, value: unknown): void {\n ;(record as unknown as Record<string, unknown>)[name] = value\n}\n\nexport type JsonApi = ReturnType<typeof useJsonApi>\n\nfunction serializeRid(entity: BaseEntity): JsonApiResourceIdentifier {\n const rid: JsonApiResourceIdentifier = { type: entity.type }\n if (entity.lid) rid.lid = entity.lid\n else if (entity.id) rid.id = entity.id\n return rid\n}\n\nexport function useJsonApi(config: JsonApiConfig, fetcher?: JsonApiFetcher) {\n const _fetcher = fetcher ?? new JsonApiFetcherImpl(config.endpoint)\n\n // Map type names to their definitions\n const modelDefinitions = new Map<string, ModelDefinition>()\n const relationshipDefinitions = new Map<string, Record<string, Relationship>>()\n\n for (const modelDef of config.modelDefinitions) {\n modelDefinitions.set(modelDef.type, modelDef)\n if (modelDef.relationships) relationshipDefinitions.set(modelDef.type, modelDef.relationships)\n }\n\n function normalize(str: string) {\n return config.kebabCase ? camel(str) : str\n }\n\n function serialize(record: BaseEntity): JsonApiResource {\n const relationships = relationshipDefinitions.get(record.type)\n const resource: JsonApiResource = serializeRid(record) as JsonApiResource\n resource.attributes = {}\n if (relationships) resource.relationships = {}\n for (const [key, value] of Object.entries(record)) {\n if (key === 'id' || key === 'lid' || key === 'type' || value === undefined) continue\n if (relationships && key in relationships && resource.relationships) {\n const rel = relationships[key]\n if (rel.relationshipType === RelationshipType.HasMany) {\n const entities = value as unknown as BaseEntity[]\n resource.relationships[key] = {\n data: entities.map(serializeRid),\n }\n } else if (rel.relationshipType === RelationshipType.BelongsTo) {\n const entity = value as unknown as BaseEntity\n resource.relationships[key] = {\n data: serializeRid(entity),\n }\n } else {\n throw new Error(`Unknown relationship type for ${key}`)\n }\n } else {\n resource.attributes[key] = value\n }\n }\n return resource\n }\n\n function createRecord<T extends BaseEntity>(type: string, properties: Partial<T> & { id?: string }): T {\n const modelDef = modelDefinitions.get(type)\n if (!modelDef) throw new Error(`Model type ${type} not defined`)\n\n const id = properties.id ?? crypto.randomUUID()\n\n const record = { id, type, ...properties } as T\n\n // Normalize property keys if needed\n if (config.kebabCase) {\n const normalizedRecord = { id, type } as BaseEntity & Record<string, unknown>\n for (const [key, value] of Object.entries(properties))\n if (key !== 'id' && value !== undefined) normalizedRecord[normalize(key)] = value\n return normalizedRecord as T\n }\n\n return record\n }\n\n function resourcesToRecords(resources: JsonApiResource[], included?: JsonApiResource[]): BaseEntity[] {\n function resourceToRecord(resource: JsonApiResource): BaseEntity {\n return createRecord(resource.type, {\n id: resource.id,\n ...resource.attributes,\n })\n }\n\n function setRecord(map: Map<string, Map<string, BaseEntity>>, record: BaseEntity) {\n if (!map.has(record.type)) map.set(record.type, new Map())\n map.get(record.type)?.set(record.id, record)\n }\n\n function getRecord(map: Map<string, Map<string, BaseEntity>>, rid: JsonApiResourceIdentifier) {\n if (rid.id === undefined) throw new Error('Resource identifier must have an id')\n if (!map.has(rid.type)) map.set(rid.type, new Map())\n return map.get(rid.type)?.get(rid.id)\n }\n\n const includedMap = new Map<string, Map<string, BaseEntity>>()\n if (included) for (const resource of included) setRecord(includedMap, resourceToRecord(resource))\n\n const records = resources.map(resourceToRecord)\n const recordsMap = new Map<string, Map<string, BaseEntity>>()\n for (const record of records) setRecord(recordsMap, record)\n\n function populateRelationships(resource: JsonApiResource) {\n const record = getRecord(recordsMap, resource) ?? getRecord(includedMap, resource)\n if (!record) throw new Error('Unexpected not found record')\n\n if (!resource.relationships) return\n\n const rels = relationshipDefinitions.get(resource.type)\n if (!rels) return\n\n for (const [name, reldoc] of Object.entries(resource.relationships)) {\n const normalizedName = normalize(name)\n const rel = rels[normalizedName]\n if (!rel) continue\n if (!reldoc.data) continue\n const rids =\n rel.relationshipType === RelationshipType.HasMany\n ? (reldoc.data as JsonApiResourceIdentifier[])\n : [reldoc.data as JsonApiResourceIdentifier]\n const relatedRecords = rids\n .filter((d) => d?.type === rel.type)\n .map((d) => getRecord(includedMap, d) || getRecord(recordsMap, d))\n .filter(Boolean)\n setRelationship(\n record,\n normalizedName,\n rel.relationshipType === RelationshipType.HasMany ? relatedRecords : relatedRecords[0],\n )\n }\n }\n\n if (included) {\n for (const r of resources) populateRelationships(r)\n for (const r of included) populateRelationships(r)\n }\n\n return records\n }\n\n async function findAll<T extends BaseEntity>(\n type: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<{ doc: JsonApiDocument; records: T[] }> {\n const doc = await _fetcher.fetchDocument(type, undefined, options, params)\n const resources = doc.data as JsonApiResource[]\n const records = resourcesToRecords(resources, doc.included) as T[]\n return { doc, records }\n }\n\n async function findRecord<T extends BaseEntity>(\n type: string,\n id: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<T> {\n const doc = await _fetcher.fetchDocument(type, id, options, params)\n const resource = doc.data as JsonApiResource\n const records = resourcesToRecords([resource], doc.included) as T[]\n const record = records[0]\n if (!record) throw new Error(`Record with id ${id} not found`)\n return record\n }\n\n async function findRelated(\n record: BaseEntity,\n relationshipName: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument> {\n const type = record.type\n const rels = relationshipDefinitions.get(type)\n if (!rels) throw new Error(`Model ${type} has no relationships`)\n\n const rel = rels[relationshipName]\n if (!rel) throw new Error(`Relationship ${relationshipName} not defined`)\n\n if (rel.relationshipType === RelationshipType.BelongsTo) {\n const doc = await _fetcher.fetchBelongsTo(type, record.id, relationshipName, options, params)\n const related = doc.data as JsonApiResource\n const relatedRecord = createRecord(rel.type, {\n id: related.id,\n ...related.attributes,\n })\n setRelationship(record, relationshipName, relatedRecord)\n return doc\n }\n\n const doc = await _fetcher.fetchHasMany(type, record.id, relationshipName, options, params)\n const related =\n rel.relationshipType === RelationshipType.HasMany\n ? (doc.data as JsonApiResource[])\n : [doc.data as JsonApiResource]\n\n const relatedRecords = related.map((r) =>\n createRecord(rel.type, {\n id: r.id,\n ...r.attributes,\n }),\n )\n\n setRelationship(\n record,\n relationshipName,\n rel.relationshipType === RelationshipType.HasMany ? relatedRecords : relatedRecords[0],\n )\n\n return doc\n }\n\n async function saveRecord<T extends BaseEntity>(record: BaseEntity, options?: FetchOptions): Promise<T> {\n const resource = serialize(record)\n let doc: JsonApiDocument | undefined\n\n if (record.lid || !record.id) {\n // Creating a new record (with local id or without id)\n doc = await _fetcher.post(resource, options)\n } else {\n // Updating an existing record\n doc = await _fetcher.patch(resource, options)\n // If server returns 204 No Content, fetch the updated record\n if (!doc) {\n if (!record.id) throw new Error('Cannot refetch record without id')\n return await findRecord<T>(record.type, record.id, options)\n }\n }\n\n const records = resourcesToRecords([doc.data] as JsonApiResource[])\n return records[0] as T\n }\n\n async function saveAtomic(\n operations: AtomicOperation[],\n options?: FetchOptions,\n ): Promise<{ doc: JsonApiAtomicDocument; records: BaseEntity[] } | undefined> {\n function toJsonApiOperation(op: AtomicOperation): JsonApiAtomicOperation {\n const jsonApiOp: JsonApiAtomicOperation = { op: op.op }\n if (op.data) {\n jsonApiOp.data = serialize(op.data)\n if (op.op === 'update') delete jsonApiOp.data.relationships\n }\n if (op.ref) jsonApiOp.ref = op.ref\n return jsonApiOp\n }\n const atomicOperations = operations.map(toJsonApiOperation)\n const atomicDoc: JsonApiAtomicDocument = {\n 'atomic:operations': atomicOperations,\n }\n const doc = await _fetcher.postAtomic(atomicDoc, options)\n if (!doc) return\n const records = doc['atomic:results'] ? resourcesToRecords(doc['atomic:results'].map((r) => r.data)) : []\n return { doc, records }\n }\n\n return {\n findAll,\n findRecord,\n findRelated,\n createRecord,\n saveRecord,\n saveAtomic,\n }\n}\n"],"names":["resolvePath","segments","HttpError","message","status","body","tryJson","response","req","url","options","headers","searchParams","method","signal","textSearchParams","finalUrl","responseBody","postAtomic","JsonApiFetcherImpl","endpoint","params","requestOptions","key","value","type","id","name","resource","newOptions","doc","camel","str","match","RelationshipType","setRelationship","record","serializeRid","entity","rid","useJsonApi","config","fetcher","_fetcher","modelDefinitions","relationshipDefinitions","modelDef","normalize","serialize","relationships","rel","entities","createRecord","properties","normalizedRecord","resourcesToRecords","resources","included","resourceToRecord","setRecord","map","getRecord","includedMap","records","recordsMap","populateRelationships","rels","reldoc","normalizedName","relatedRecords","d","r","findAll","findRecord","findRelated","relationshipName","related","relatedRecord","saveRecord","saveAtomic","operations","toJsonApiOperation","op","jsonApiOp","atomicDoc"],"mappings":"AAEA,SAASA,KAAeC,GAA4B;AAClD,SAAO,IAAI,IAAIA,EAAS,KAAK,GAAG,CAAC,EAAE;AACrC;AA6BA,MAAMC,UAAkB,MAAM;AAAA,EAC5B,YACEC,GACOC,GACAC,GACP;AACA,UAAMF,CAAO,GAHN,KAAA,SAAAC,GACA,KAAA,OAAAC,GAGP,KAAK,OAAO;AAAA,EACd;AACF;AAEA,eAAeC,EAAQC,GAAoB;AACzC,MAAI;AACF,WAAO,MAAMA,EAAS,KAAA;AAAA,EACxB,QAAQ;AAAA,EAER;AACF;AAEA,eAAeC,EAAIC,GAAaC,GAAkB;AAChD,QAAM,EAAE,SAAAC,GAAS,cAAAC,GAAc,QAAAC,GAAQ,QAAAC,GAAQ,MAAAT,MAASK,GAClDK,IAAmBH,IAAe,IAAIA,CAAY,KAAK,IACvDI,IAAWP,EAAI,QAAQ,qBAAqBM,CAAgB,GAC5DR,IAAW,MAAM,MAAMS,GAAU;AAAA,IACrC,QAAAH;AAAA,IACA,SAAAF;AAAA,IACA,QAAAG;AAAA,IACA,MAAAT;AAAA,EAAA,CACD,GACKY,IAAe,MA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E,aAAO,MAAMqC,EAAcrC,EAAO,MAAMA,EAAO,IAAI1B,CAAO;AAAA,IAC5D;AAIF,WADgB6C,EAAmB,CAACzB,EAAI,IAAI,CAAsB,EACnD,CAAC;AAAA,EAClB;AAEA,iBAAeiD,EACbC,GACAtE,GAC4E;AAC5E,aAASuE,EAAmBC,GAA6C;AACvE,YAAMC,IAAoC,EAAE,IAAID,EAAG,GAAA;AACnD,aAAIA,EAAG,SACLC,EAAU,OAAOnC,EAAUkC,EAAG,IAAI,GAC9BA,EAAG,OAAO,YAAU,OAAOC,EAAU,KAAK,gBAE5CD,EAAG,QAAKC,EAAU,MAAMD,EAAG,MACxBC;AAAA,IACT;AAEA,UAAMC,IAAmC;AAAA,MACvC,qBAFuBJ,EAAW,IAAIC,CAAkB;AAAA,IAEnC,GAEjBnD,IAAM,MAAMa,EAAS,WAAWyC,GAAW1E,CAAO;AACxD,QAAI,CAACoB,EAAK;AACV,UAAMiC,IAAUjC,EAAI,gBAAgB,IAAIyB,EAAmBzB,EAAI,gBAAgB,EAAE,IAAI,CAACyC,MAAMA,EAAE,IAAI,CAAC,IAAI,CAAA;AACvG,WAAO,EAAE,KAAAzC,GAAK,SAAAiC,EAAA;AAAA,EAChB;AAEA,SAAO;AAAA,IACL,SAAAS;AAAA,IACA,YAAAC;AAAA,IACA,aAAAC;AAAA,IACA,cAAAtB;AAAA,IACA,YAAA0B;AAAA,IACA,YAAAC;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bjornharrtell/json-api",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/lib.js",
|
|
6
6
|
"module": "./dist/lib.js",
|
|
@@ -18,17 +18,24 @@
|
|
|
18
18
|
"url": "git+https://github.com/bjornharrtell/json-api.git"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@biomejs/biome": "2.
|
|
22
|
-
"@types/node": "^25.0
|
|
23
|
-
"
|
|
21
|
+
"@biomejs/biome": "2.4.4",
|
|
22
|
+
"@types/node": "^25.3.0",
|
|
23
|
+
"@vitest/coverage-v8": "^4.0.18",
|
|
24
|
+
"coveralls": "^3.1.1",
|
|
25
|
+
"typedoc": "^0.28.17",
|
|
24
26
|
"typescript": "~5.9.3",
|
|
25
27
|
"vite": "^7.3.1",
|
|
26
28
|
"vite-plugin-dts": "^4.5.4",
|
|
27
|
-
"vitest": "^4.0.
|
|
29
|
+
"vitest": "^4.0.18"
|
|
28
30
|
},
|
|
29
31
|
"scripts": {
|
|
30
32
|
"dev": "vite",
|
|
31
33
|
"test": "vitest",
|
|
34
|
+
"test:integration": "vitest run tests/integration/jsonapi-dotnetcore.spec.ts",
|
|
35
|
+
"test:coverage": "vitest run --coverage",
|
|
36
|
+
"test:integration:coverage": "vitest run --coverage tests/integration/jsonapi-dotnetcore.spec.ts",
|
|
37
|
+
"coverage": "vitest run --coverage",
|
|
38
|
+
"coveralls": "pnpm run coverage && cat ./coverage/lcov.info | coveralls",
|
|
32
39
|
"build": "vite build",
|
|
33
40
|
"preview": "vite preview",
|
|
34
41
|
"type-check": "tsc --noEmit",
|