@bjornharrtell/json-api 6.1.0 → 6.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/dist/docs/assets/navigation.js +1 -1
- package/dist/docs/assets/search.js +1 -1
- 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/interfaces/AtomicOperation.html +2 -2
- package/dist/docs/interfaces/BaseEntity.html +3 -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 +3 -2
- package/dist/docs/interfaces/JsonApiRelationship.html +2 -2
- package/dist/docs/interfaces/JsonApiResource.html +3 -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/interfaces/SerializeOptions.html +3 -3
- package/dist/docs/modules.html +1 -1
- 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/docs/variables/META.html +3 -0
- package/dist/lib.d.ts +11 -2
- package/dist/lib.js +62 -58
- package/dist/lib.js.map +1 -1
- package/package.json +17 -14
package/dist/lib.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const P = "application/vnd.api+json", J = 'application/vnd.api+json; ext="https://jsonapi.org/ext/atomic"',
|
|
1
|
+
const P = "application/vnd.api+json", J = 'application/vnd.api+json; ext="https://jsonapi.org/ext/atomic"', C = "HTTP error! status: ", S = "Accept", j = "Content-Type", N = "POST";
|
|
2
2
|
function R(...o) {
|
|
3
3
|
return new URL(o.join("/")).href;
|
|
4
4
|
}
|
|
@@ -21,12 +21,12 @@ async function g(o, t) {
|
|
|
21
21
|
body: r
|
|
22
22
|
}), b = await U(E);
|
|
23
23
|
if (!E.ok)
|
|
24
|
-
throw new k(`${
|
|
24
|
+
throw new k(`${C}${E.status} ${E.statusText}`, E.status, b);
|
|
25
25
|
return b;
|
|
26
26
|
}
|
|
27
27
|
async function L(o, t) {
|
|
28
28
|
const { signal: n, body: f } = t, a = N, p = new Headers(t.headers ?? {});
|
|
29
|
-
p.append(
|
|
29
|
+
p.append(S, J), p.append(j, J);
|
|
30
30
|
const r = await fetch(o, {
|
|
31
31
|
method: a,
|
|
32
32
|
headers: p,
|
|
@@ -34,7 +34,7 @@ async function L(o, t) {
|
|
|
34
34
|
body: f
|
|
35
35
|
}), l = await U(r);
|
|
36
36
|
if (!r.ok)
|
|
37
|
-
throw new k(`${
|
|
37
|
+
throw new k(`${C}${r.status} ${r.statusText}`, r.status, l);
|
|
38
38
|
return r.status === 204 ? void 0 : l;
|
|
39
39
|
}
|
|
40
40
|
class q {
|
|
@@ -43,7 +43,7 @@ class q {
|
|
|
43
43
|
}
|
|
44
44
|
createOptions(t = {}, n = {}, f) {
|
|
45
45
|
const a = new URLSearchParams(), p = new Headers(t.headers ?? {});
|
|
46
|
-
p.append(
|
|
46
|
+
p.append(S, P);
|
|
47
47
|
const r = { searchParams: a, headers: p, body: f };
|
|
48
48
|
if (t.fields)
|
|
49
49
|
for (const [l, O] of Object.entries(t.fields)) a.append(`fields[${l}]`, O.join(","));
|
|
@@ -77,14 +77,14 @@ class q {
|
|
|
77
77
|
const f = R(this.endpoint, t.type), p = JSON.stringify({
|
|
78
78
|
data: t
|
|
79
79
|
}), r = this.createOptions(n, {}, p);
|
|
80
|
-
return r.method = N, r.headers.set(
|
|
80
|
+
return r.method = N, r.headers.set(j, P), await g(f, r);
|
|
81
81
|
}
|
|
82
82
|
async patch(t, n) {
|
|
83
83
|
if (!t.id) throw new Error("Resource must have an id to be patched");
|
|
84
84
|
const f = R(this.endpoint, t.type, t.id), p = JSON.stringify({
|
|
85
85
|
data: t
|
|
86
86
|
}), r = new Headers(n?.headers ?? {});
|
|
87
|
-
r.set(
|
|
87
|
+
r.set(j, P), r.append(S, P);
|
|
88
88
|
const l = await fetch(f, {
|
|
89
89
|
method: "PATCH",
|
|
90
90
|
headers: r,
|
|
@@ -93,7 +93,7 @@ class q {
|
|
|
93
93
|
}), O = await U(l);
|
|
94
94
|
if (!l.ok)
|
|
95
95
|
throw new k(
|
|
96
|
-
`${
|
|
96
|
+
`${C}${l.status} ${l.statusText}`,
|
|
97
97
|
l.status,
|
|
98
98
|
O
|
|
99
99
|
);
|
|
@@ -107,8 +107,8 @@ class q {
|
|
|
107
107
|
function K(o) {
|
|
108
108
|
return o.replace(/[-][a-z\u00E0-\u00F6\u00F8-\u00FE]/g, (t) => t.slice(1).toUpperCase());
|
|
109
109
|
}
|
|
110
|
-
const X = "atomic:operations", B = "atomic:results";
|
|
111
|
-
var
|
|
110
|
+
const X = "atomic:operations", B = "atomic:results", G = /* @__PURE__ */ Symbol("meta");
|
|
111
|
+
var Q = /* @__PURE__ */ ((o) => (o[o.HasMany = 0] = "HasMany", o[o.BelongsTo = 1] = "BelongsTo", o))(Q || {});
|
|
112
112
|
function $(o, t, n) {
|
|
113
113
|
o[t] = n;
|
|
114
114
|
}
|
|
@@ -116,7 +116,7 @@ function H(o) {
|
|
|
116
116
|
const t = { type: o.type };
|
|
117
117
|
return o.lid ? t.lid = o.lid : o.id && (t.id = o.id), t;
|
|
118
118
|
}
|
|
119
|
-
function
|
|
119
|
+
function W(o, t) {
|
|
120
120
|
const n = t ?? new q(o.endpoint), f = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Map();
|
|
121
121
|
for (const e of o.modelDefinitions)
|
|
122
122
|
f.set(e.type, e), e.relationships && a.set(e.type, e.relationships);
|
|
@@ -124,25 +124,27 @@ function V(o, t) {
|
|
|
124
124
|
return o.kebabCase ? K(e) : e;
|
|
125
125
|
}
|
|
126
126
|
function r(e, i) {
|
|
127
|
-
const T = i?.
|
|
128
|
-
d.attributes = {},
|
|
129
|
-
for (const [u,
|
|
130
|
-
if (!(u === "id" || u === "lid" || u === "type" ||
|
|
131
|
-
if (
|
|
127
|
+
const T = i?.includeToManyRelationships !== !1, c = a.get(e.type), d = H(e);
|
|
128
|
+
d.attributes = {}, c && (d.relationships = {});
|
|
129
|
+
for (const [u, y] of Object.entries(e))
|
|
130
|
+
if (!(u === "id" || u === "lid" || u === "type" || y === void 0))
|
|
131
|
+
if (c && u in c && d.relationships) {
|
|
132
132
|
const m = c[u];
|
|
133
133
|
if (m.relationshipType === 0) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
134
|
+
if (T) {
|
|
135
|
+
const w = y;
|
|
136
|
+
d.relationships[u] = {
|
|
137
|
+
data: w.map(H)
|
|
138
|
+
};
|
|
139
|
+
}
|
|
138
140
|
} else if (m.relationshipType === 1) {
|
|
139
|
-
const w =
|
|
141
|
+
const w = y;
|
|
140
142
|
d.relationships[u] = {
|
|
141
143
|
data: H(w)
|
|
142
144
|
};
|
|
143
145
|
} else
|
|
144
146
|
throw new Error(`Unknown relationship type for ${u}`);
|
|
145
|
-
} else c && u in c || (d.attributes[u] =
|
|
147
|
+
} else c && u in c || (d.attributes[u] = y);
|
|
146
148
|
return d;
|
|
147
149
|
}
|
|
148
150
|
function l(e, i) {
|
|
@@ -150,44 +152,45 @@ function V(o, t) {
|
|
|
150
152
|
const c = i.id ?? crypto.randomUUID(), d = { id: c, type: e, ...i };
|
|
151
153
|
if (o.kebabCase) {
|
|
152
154
|
const u = { id: c, type: e };
|
|
153
|
-
for (const [
|
|
154
|
-
|
|
155
|
+
for (const [y, m] of Object.entries(i))
|
|
156
|
+
y !== "id" && m !== void 0 && (u[p(y)] = m);
|
|
155
157
|
return u;
|
|
156
158
|
}
|
|
157
159
|
return d;
|
|
158
160
|
}
|
|
159
161
|
function O(e, i) {
|
|
160
162
|
function T(s) {
|
|
161
|
-
|
|
163
|
+
const h = l(s.type, {
|
|
162
164
|
id: s.id,
|
|
163
165
|
...s.attributes
|
|
164
166
|
});
|
|
167
|
+
return s.meta && (h[G] = s.meta), h;
|
|
165
168
|
}
|
|
166
|
-
function c(s,
|
|
167
|
-
s.has(
|
|
169
|
+
function c(s, h) {
|
|
170
|
+
s.has(h.type) || s.set(h.type, /* @__PURE__ */ new Map()), s.get(h.type)?.set(h.id, h);
|
|
168
171
|
}
|
|
169
|
-
function d(s,
|
|
170
|
-
if (
|
|
171
|
-
return s.has(
|
|
172
|
+
function d(s, h) {
|
|
173
|
+
if (h.id === void 0) throw new Error("Resource identifier must have an id");
|
|
174
|
+
return s.has(h.type) || s.set(h.type, /* @__PURE__ */ new Map()), s.get(h.type)?.get(h.id);
|
|
172
175
|
}
|
|
173
176
|
const u = /* @__PURE__ */ new Map();
|
|
174
177
|
if (i) for (const s of i) c(u, T(s));
|
|
175
|
-
const
|
|
176
|
-
for (const s of
|
|
178
|
+
const y = e.map(T), m = /* @__PURE__ */ new Map();
|
|
179
|
+
for (const s of y) c(m, s);
|
|
177
180
|
function w(s) {
|
|
178
|
-
const
|
|
179
|
-
if (!
|
|
181
|
+
const h = d(m, s) ?? d(u, s);
|
|
182
|
+
if (!h) throw new Error("Unexpected not found record");
|
|
180
183
|
if (!s.relationships) return;
|
|
181
184
|
const A = a.get(s.type);
|
|
182
185
|
if (A)
|
|
183
186
|
for (const [_, v] of Object.entries(s.relationships)) {
|
|
184
|
-
const z = p(_),
|
|
185
|
-
if (!
|
|
186
|
-
const I = (
|
|
187
|
+
const z = p(_), M = A[z];
|
|
188
|
+
if (!M || !v.data) continue;
|
|
189
|
+
const I = (M.relationshipType === 0 ? v.data : [v.data]).filter((D) => D?.type === M.type).map((D) => d(u, D) || d(m, D)).filter(Boolean);
|
|
187
190
|
$(
|
|
188
|
-
|
|
191
|
+
h,
|
|
189
192
|
z,
|
|
190
|
-
|
|
193
|
+
M.relationshipType === 0 ? I : I[0]
|
|
191
194
|
);
|
|
192
195
|
}
|
|
193
196
|
}
|
|
@@ -195,7 +198,7 @@ function V(o, t) {
|
|
|
195
198
|
for (const s of e) w(s);
|
|
196
199
|
for (const s of i) w(s);
|
|
197
200
|
}
|
|
198
|
-
return
|
|
201
|
+
return y;
|
|
199
202
|
}
|
|
200
203
|
async function E(e, i, T) {
|
|
201
204
|
const c = await n.fetchDocument(e, void 0, i, T), d = c.data, u = O(d, c.included);
|
|
@@ -209,25 +212,25 @@ function V(o, t) {
|
|
|
209
212
|
async function x(e, i, T, c) {
|
|
210
213
|
const d = e.type, u = a.get(d);
|
|
211
214
|
if (!u) throw new Error(`Model ${d} has no relationships`);
|
|
212
|
-
const
|
|
213
|
-
if (!
|
|
214
|
-
if (
|
|
215
|
-
const
|
|
215
|
+
const y = u[i];
|
|
216
|
+
if (!y) throw new Error(`Relationship ${i} not defined`);
|
|
217
|
+
if (y.relationshipType === 1) {
|
|
218
|
+
const h = await n.fetchBelongsTo(d, e.id, i, T, c), A = h.data, _ = l(y.type, {
|
|
216
219
|
id: A.id,
|
|
217
220
|
...A.attributes
|
|
218
221
|
});
|
|
219
|
-
return $(e, i, _),
|
|
222
|
+
return $(e, i, _), h;
|
|
220
223
|
}
|
|
221
|
-
const m = await n.fetchHasMany(d, e.id, i, T, c), s = (
|
|
222
|
-
(
|
|
223
|
-
id:
|
|
224
|
-
...
|
|
224
|
+
const m = await n.fetchHasMany(d, e.id, i, T, c), s = (y.relationshipType === 0 ? m.data : [m.data]).map(
|
|
225
|
+
(h) => l(y.type, {
|
|
226
|
+
id: h.id,
|
|
227
|
+
...h.attributes
|
|
225
228
|
})
|
|
226
229
|
);
|
|
227
230
|
return $(
|
|
228
231
|
e,
|
|
229
232
|
i,
|
|
230
|
-
|
|
233
|
+
y.relationshipType === 0 ? s : s[0]
|
|
231
234
|
), m;
|
|
232
235
|
}
|
|
233
236
|
async function F(e, i) {
|
|
@@ -250,17 +253,17 @@ function V(o, t) {
|
|
|
250
253
|
else if (w.data === null || w.ref?.relationship)
|
|
251
254
|
s.data = w.data;
|
|
252
255
|
else {
|
|
253
|
-
const
|
|
254
|
-
s.data =
|
|
256
|
+
const h = r(w.data, T);
|
|
257
|
+
s.data = h;
|
|
255
258
|
}
|
|
256
259
|
return w.ref && (s.ref = w.ref), s;
|
|
257
260
|
}
|
|
258
261
|
const d = e.map(c), u = {
|
|
259
262
|
[X]: d
|
|
260
|
-
},
|
|
261
|
-
if (!
|
|
262
|
-
const m =
|
|
263
|
-
return { doc:
|
|
263
|
+
}, y = await n.postAtomic(u, i);
|
|
264
|
+
if (!y) return;
|
|
265
|
+
const m = y[B] ? O(y[B].map((w) => w.data)) : [];
|
|
266
|
+
return { doc: y, records: m };
|
|
264
267
|
}
|
|
265
268
|
return {
|
|
266
269
|
findAll: E,
|
|
@@ -272,8 +275,9 @@ function V(o, t) {
|
|
|
272
275
|
};
|
|
273
276
|
}
|
|
274
277
|
export {
|
|
275
|
-
G as
|
|
278
|
+
G as META,
|
|
279
|
+
Q as RelationshipType,
|
|
276
280
|
K as camel,
|
|
277
|
-
|
|
281
|
+
W as useJsonApi
|
|
278
282
|
};
|
|
279
283
|
//# sourceMappingURL=lib.js.map
|
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\nconst CONTENT_TYPE_JSON_API = 'application/vnd.api+json'\nconst CONTENT_TYPE_JSON_API_ATOMIC = 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"'\nconst HTTP_ERROR_PREFIX = 'HTTP error! status: '\nconst HEADER_ACCEPT = 'Accept'\nconst HEADER_CONTENT_TYPE = 'Content-Type'\nconst METHOD_POST = 'POST'\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_PREFIX}${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 = METHOD_POST\n const headers = new Headers(options.headers ?? {})\n headers.append(HEADER_ACCEPT, CONTENT_TYPE_JSON_API_ATOMIC)\n headers.append(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON_API_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_PREFIX}${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(HEADER_ACCEPT, CONTENT_TYPE_JSON_API)\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 = METHOD_POST\n newOptions.headers.set(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON_API)\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(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON_API)\n headers.append(HEADER_ACCEPT, CONTENT_TYPE_JSON_API)\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_PREFIX}${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\nconst ATOMIC_OPERATIONS_KEY = 'atomic:operations' as const\nconst ATOMIC_RESULTS_KEY = 'atomic:results' as const\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 | JsonApiResourceIdentifier[] | null\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 | JsonApiResourceIdentifier | JsonApiResourceIdentifier[] | null\n ref?: JsonApiReference\n}\n\nexport interface SerializeOptions {\n /** Whether to include relationships when serializing a resource. Defaults to true. */\n includeRelationships?: boolean\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, serializeOptions?: SerializeOptions): JsonApiResource {\n const includeRelationships = serializeOptions?.includeRelationships !== false\n const relationships = relationshipDefinitions.get(record.type)\n const resource: JsonApiResource = serializeRid(record) as JsonApiResource\n resource.attributes = {}\n if (includeRelationships && 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 (includeRelationships && 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 if (!(relationships && key in relationships)) {\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<{ doc: JsonApiDocument; record: 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 { doc, 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)).record\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 serializeOptions?: SerializeOptions,\n ): Promise<{ doc: JsonApiAtomicDocument; records: BaseEntity[] } | undefined> {\n function toJsonApiOperation(op: AtomicOperation): JsonApiAtomicOperation {\n const jsonApiOp: JsonApiAtomicOperation = { op: op.op }\n if (op.data !== undefined) {\n if (Array.isArray(op.data)) {\n // To-many relationship operation: data is already an array of resource identifiers\n jsonApiOp.data = op.data\n } else if (op.data === null || op.ref?.relationship) {\n // To-one relationship operation: null clears, or a single resource identifier\n jsonApiOp.data = op.data as JsonApiResourceIdentifier | null\n } else {\n // Resource operation\n const resource = serialize(op.data as BaseEntity, serializeOptions)\n jsonApiOp.data = resource\n }\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_KEY]: atomicOperations,\n }\n const doc = await _fetcher.postAtomic(atomicDoc, options)\n if (!doc) return\n const records = doc[ATOMIC_RESULTS_KEY] ? resourcesToRecords(doc[ATOMIC_RESULTS_KEY].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":["CONTENT_TYPE_JSON_API","CONTENT_TYPE_JSON_API_ATOMIC","HTTP_ERROR_PREFIX","HEADER_ACCEPT","HEADER_CONTENT_TYPE","METHOD_POST","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","ATOMIC_OPERATIONS_KEY","ATOMIC_RESULTS_KEY","RelationshipType","setRelationship","record","serializeRid","entity","rid","useJsonApi","config","fetcher","_fetcher","modelDefinitions","relationshipDefinitions","modelDef","normalize","serialize","serializeOptions","includeRelationships","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","atomicOperations","atomicDoc"],"mappings":"AAEA,MAAMA,IAAwB,4BACxBC,IAA+B,k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IAAID,EAAG,GAAA;AACnD,UAAIA,EAAG,SAAS;AACd,YAAI,MAAM,QAAQA,EAAG,IAAI;AAEvB,UAAAC,EAAU,OAAOD,EAAG;AAAA,iBACXA,EAAG,SAAS,QAAQA,EAAG,KAAK;AAErC,UAAAC,EAAU,OAAOD,EAAG;AAAA,aACf;AAEL,gBAAM1D,IAAWsB,EAAUoC,EAAG,MAAoBnC,CAAgB;AAClE,UAAAoC,EAAU,OAAO3D;AAAA,QACnB;AAEF,aAAI0D,EAAG,QAAKC,EAAU,MAAMD,EAAG,MACxBC;AAAA,IACT;AACA,UAAMC,IAAmBJ,EAAW,IAAIC,CAAkB,GACpDI,IAAmC;AAAA,MACvC,CAACvD,CAAqB,GAAGsD;AAAA,IAAA,GAErB1D,IAAM,MAAMe,EAAS,WAAW4C,GAAW/E,CAAO;AACxD,QAAI,CAACoB,EAAK;AACV,UAAMqC,IAAUrC,EAAIK,CAAkB,IAAIwB,EAAmB7B,EAAIK,CAAkB,EAAE,IAAI,CAACwC,MAAMA,EAAE,IAAI,CAAC,IAAI,CAAA;AAC3G,WAAO,EAAE,KAAA7C,GAAK,SAAAqC,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\nconst CONTENT_TYPE_JSON_API = 'application/vnd.api+json'\nconst CONTENT_TYPE_JSON_API_ATOMIC = 'application/vnd.api+json; ext=\"https://jsonapi.org/ext/atomic\"'\nconst HTTP_ERROR_PREFIX = 'HTTP error! status: '\nconst HEADER_ACCEPT = 'Accept'\nconst HEADER_CONTENT_TYPE = 'Content-Type'\nconst METHOD_POST = 'POST'\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_PREFIX}${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 = METHOD_POST\n const headers = new Headers(options.headers ?? {})\n headers.append(HEADER_ACCEPT, CONTENT_TYPE_JSON_API_ATOMIC)\n headers.append(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON_API_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_PREFIX}${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(HEADER_ACCEPT, CONTENT_TYPE_JSON_API)\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 = METHOD_POST\n newOptions.headers.set(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON_API)\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(HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON_API)\n headers.append(HEADER_ACCEPT, CONTENT_TYPE_JSON_API)\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_PREFIX}${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\nconst ATOMIC_OPERATIONS_KEY = 'atomic:operations' as const\nconst ATOMIC_RESULTS_KEY = 'atomic:results' as const\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 meta?: JsonApiMeta\n}\n\nexport interface JsonApiMeta {\n // Pagination\n totalPages?: number\n total?: 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 | JsonApiResourceIdentifier[] | null\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 | JsonApiResourceIdentifier | JsonApiResourceIdentifier[] | null\n ref?: JsonApiReference\n}\n\nexport interface SerializeOptions {\n /** Whether to include to-many (HasMany) relationships when serializing a resource. Defaults to true. */\n includeToManyRelationships?: boolean\n}\n\n/**\n * Symbol key used to store JSON:API resource-level meta on a record, avoiding\n * collisions with a literal \"meta\" attribute field.\n */\nexport const META: unique symbol = Symbol('meta')\n\nexport interface BaseEntity {\n id: string\n lid?: string\n type: string\n [META]?: JsonApiMeta\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, serializeOptions?: SerializeOptions): JsonApiResource {\n const includeToManyRelationships = serializeOptions?.includeToManyRelationships !== false\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 if (includeToManyRelationships) {\n const entities = value as unknown as BaseEntity[]\n resource.relationships[key] = {\n data: entities.map(serializeRid),\n }\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 if (!(relationships && key in relationships)) {\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 const record = createRecord(resource.type, {\n id: resource.id,\n ...resource.attributes,\n })\n if (resource.meta) record[META] = resource.meta\n return record\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<{ doc: JsonApiDocument; record: 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 { doc, 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)).record\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 serializeOptions?: SerializeOptions,\n ): Promise<{ doc: JsonApiAtomicDocument; records: BaseEntity[] } | undefined> {\n function toJsonApiOperation(op: AtomicOperation): JsonApiAtomicOperation {\n const jsonApiOp: JsonApiAtomicOperation = { op: op.op }\n if (op.data !== undefined) {\n if (Array.isArray(op.data)) {\n // To-many relationship operation: data is already an array of resource identifiers\n jsonApiOp.data = op.data\n } else if (op.data === null || op.ref?.relationship) {\n // To-one relationship operation: null clears, or a single resource identifier\n jsonApiOp.data = op.data as JsonApiResourceIdentifier | null\n } else {\n // Resource operation\n const resource = serialize(op.data as BaseEntity, serializeOptions)\n jsonApiOp.data = resource\n }\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_KEY]: atomicOperations,\n }\n const doc = await _fetcher.postAtomic(atomicDoc, options)\n if (!doc) return\n const records = doc[ATOMIC_RESULTS_KEY] ? resourcesToRecords(doc[ATOMIC_RESULTS_KEY].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":["CONTENT_TYPE_JSON_API","CONTENT_TYPE_JSON_API_ATOMIC","HTTP_ERROR_PREFIX","HEADER_ACCEPT","HEADER_CONTENT_TYPE","METHOD_POST","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","ATOMIC_OPERATIONS_KEY","ATOMIC_RESULTS_KEY","META","RelationshipType","setRelationship","record","serializeRid","entity","rid","useJsonApi","config","fetcher","_fetcher","modelDefinitions","relationshipDefinitions","modelDef","normalize","serialize","serializeOptions","includeToManyRelationships","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","atomicOperations","atomicDoc"],"mappings":"AAEA,MA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,EACnD,CAAC;AAAA,EAClB;AAEA,iBAAesD,EACbC,GACA3E,GACA0C,GAC4E;AAC5E,aAASkC,EAAmBC,GAA6C;AACvE,YAAMC,IAAoC,EAAE,IAAID,EAAG,GAAA;AACnD,UAAIA,EAAG,SAAS;AACd,YAAI,MAAM,QAAQA,EAAG,IAAI;AAEvB,UAAAC,EAAU,OAAOD,EAAG;AAAA,iBACXA,EAAG,SAAS,QAAQA,EAAG,KAAK;AAErC,UAAAC,EAAU,OAAOD,EAAG;AAAA,aACf;AAEL,gBAAM3D,IAAWuB,EAAUoC,EAAG,MAAoBnC,CAAgB;AAClE,UAAAoC,EAAU,OAAO5D;AAAA,QACnB;AAEF,aAAI2D,EAAG,QAAKC,EAAU,MAAMD,EAAG,MACxBC;AAAA,IACT;AACA,UAAMC,IAAmBJ,EAAW,IAAIC,CAAkB,GACpDI,IAAmC;AAAA,MACvC,CAACxD,CAAqB,GAAGuD;AAAA,IAAA,GAErB3D,IAAM,MAAMgB,EAAS,WAAW4C,GAAWhF,CAAO;AACxD,QAAI,CAACoB,EAAK;AACV,UAAMsC,IAAUtC,EAAIK,CAAkB,IAAIyB,EAAmB9B,EAAIK,CAAkB,EAAE,IAAI,CAACyC,MAAMA,EAAE,IAAI,CAAC,IAAI,CAAA;AAC3G,WAAO,EAAE,KAAA9C,GAAK,SAAAsC,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": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/lib.js",
|
|
6
6
|
"module": "./dist/lib.js",
|
|
@@ -9,11 +9,25 @@
|
|
|
9
9
|
".": "./dist/lib.js",
|
|
10
10
|
"./*": "./src/*"
|
|
11
11
|
},
|
|
12
|
-
"files": [
|
|
12
|
+
"files": [
|
|
13
|
+
"README.md",
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
13
16
|
"repository": {
|
|
14
17
|
"type": "git",
|
|
15
18
|
"url": "git+https://github.com/bjornharrtell/json-api.git"
|
|
16
19
|
},
|
|
20
|
+
"devDependencies": {
|
|
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",
|
|
26
|
+
"typescript": "~5.9.3",
|
|
27
|
+
"vite": "^7.3.1",
|
|
28
|
+
"vite-plugin-dts": "^4.5.4",
|
|
29
|
+
"vitest": "^4.0.18"
|
|
30
|
+
},
|
|
17
31
|
"scripts": {
|
|
18
32
|
"dev": "vite",
|
|
19
33
|
"test": "vitest",
|
|
@@ -26,16 +40,5 @@
|
|
|
26
40
|
"preview": "vite preview",
|
|
27
41
|
"type-check": "tsc --noEmit",
|
|
28
42
|
"typedoc": "typedoc"
|
|
29
|
-
},
|
|
30
|
-
"devDependencies": {
|
|
31
|
-
"@biomejs/biome": "2.4.4",
|
|
32
|
-
"@types/node": "^25.3.0",
|
|
33
|
-
"@vitest/coverage-v8": "^4.0.18",
|
|
34
|
-
"coveralls": "^3.1.1",
|
|
35
|
-
"typedoc": "^0.28.17",
|
|
36
|
-
"typescript": "~5.9.3",
|
|
37
|
-
"vite": "^7.3.1",
|
|
38
|
-
"vite-plugin-dts": "^4.5.4",
|
|
39
|
-
"vitest": "^4.0.18"
|
|
40
43
|
}
|
|
41
|
-
}
|
|
44
|
+
}
|