@bjornharrtell/json-api 2.0.3 → 3.0.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 +27 -23
- package/dist/docs/assets/main.js +2 -2
- 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/index.html +3 -3
- package/dist/docs/interfaces/BaseRecord.html +4 -0
- package/dist/docs/interfaces/FetchOptions.html +2 -2
- package/dist/docs/interfaces/FetchParams.html +1 -1
- package/dist/docs/interfaces/JsonApiDocument.html +2 -2
- package/dist/docs/interfaces/JsonApiError.html +2 -2
- package/dist/docs/interfaces/JsonApiFetcher.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/JsonApiStore.html +12 -12
- package/dist/docs/interfaces/JsonApiStoreConfig.html +4 -4
- package/dist/docs/interfaces/ModelDefinition.html +5 -6
- package/dist/docs/interfaces/PageOption.html +2 -2
- package/dist/docs/interfaces/Relationship.html +4 -2
- package/dist/docs/modules.html +1 -1
- package/dist/docs/types/JsonApiLink.html +1 -1
- package/dist/docs/types/JsonApiStoreUseFunction.html +1 -1
- package/dist/lib.d.ts +38 -40
- package/dist/lib.js +152 -140
- package/dist/lib.js.map +1 -1
- package/package.json +6 -6
- package/dist/docs/classes/Model.html +0 -4
package/dist/lib.js
CHANGED
|
@@ -1,163 +1,175 @@
|
|
|
1
|
-
function
|
|
2
|
-
return new URL(
|
|
1
|
+
function b(...o) {
|
|
2
|
+
return new URL(o.join("/")).href;
|
|
3
3
|
}
|
|
4
|
-
async function R(
|
|
5
|
-
const { headers: r, searchParams:
|
|
6
|
-
method:
|
|
4
|
+
async function R(o, e) {
|
|
5
|
+
const { headers: r, searchParams: d, method: i, signal: s, body: a } = e, y = `?${d}`, m = o.replace(/(?:\?.*?)?(?=#|$)/, y), w = await fetch(m, {
|
|
6
|
+
method: i,
|
|
7
7
|
headers: r,
|
|
8
|
-
signal:
|
|
9
|
-
body:
|
|
8
|
+
signal: s,
|
|
9
|
+
body: a
|
|
10
10
|
});
|
|
11
|
-
if (!
|
|
12
|
-
return await
|
|
11
|
+
if (!w.ok) throw new Error(`HTTP error! status: ${w.status} ${w.statusText}`);
|
|
12
|
+
return await w.json();
|
|
13
13
|
}
|
|
14
|
-
class
|
|
15
|
-
constructor(
|
|
16
|
-
this.endpoint =
|
|
17
|
-
}
|
|
18
|
-
createOptions(
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
if (
|
|
23
|
-
for (const [
|
|
24
|
-
|
|
25
|
-
for (const [
|
|
26
|
-
return
|
|
27
|
-
}
|
|
28
|
-
async fetchDocument(
|
|
29
|
-
const
|
|
30
|
-
r &&
|
|
31
|
-
const
|
|
32
|
-
return await R(
|
|
33
|
-
}
|
|
34
|
-
async fetchAll(
|
|
35
|
-
const
|
|
36
|
-
return (await R(
|
|
37
|
-
}
|
|
38
|
-
async fetchOne(
|
|
39
|
-
const
|
|
40
|
-
return (await R(
|
|
41
|
-
}
|
|
42
|
-
async fetchHasMany(
|
|
43
|
-
const
|
|
44
|
-
return await R(
|
|
45
|
-
}
|
|
46
|
-
async fetchBelongsTo(
|
|
47
|
-
const
|
|
48
|
-
return await R(
|
|
49
|
-
}
|
|
50
|
-
async post(
|
|
51
|
-
const r =
|
|
52
|
-
data:
|
|
53
|
-
}),
|
|
54
|
-
return await R(r,
|
|
14
|
+
class k {
|
|
15
|
+
constructor(e) {
|
|
16
|
+
this.endpoint = e;
|
|
17
|
+
}
|
|
18
|
+
createOptions(e = {}, r = {}, d = !1, i) {
|
|
19
|
+
const s = new URLSearchParams(), a = new Headers(e.headers);
|
|
20
|
+
a.append("Accept", "application/vnd.api+json"), d && a.append("Content-Type", "application/vnd.api+json");
|
|
21
|
+
const y = { searchParams: s, headers: a, body: i };
|
|
22
|
+
if (e.fields)
|
|
23
|
+
for (const [m, w] of Object.entries(e.fields)) s.append(`fields[${m}]`, w.join(","));
|
|
24
|
+
e.page?.size && s.append("page[size]", e.page.size.toString()), e.page?.number && s.append("page[number]", e.page.number.toString()), e.include && s.append("include", e.include.join(",")), e.filter && s.append("filter", e.filter);
|
|
25
|
+
for (const [m, w] of Object.entries(r)) s.append(m, w);
|
|
26
|
+
return y;
|
|
27
|
+
}
|
|
28
|
+
async fetchDocument(e, r, d, i) {
|
|
29
|
+
const s = [this.endpoint, e];
|
|
30
|
+
r && s.push(r);
|
|
31
|
+
const a = b(...s);
|
|
32
|
+
return await R(a, this.createOptions(d, i));
|
|
33
|
+
}
|
|
34
|
+
async fetchAll(e, r, d) {
|
|
35
|
+
const i = b(this.endpoint, e);
|
|
36
|
+
return (await R(i, this.createOptions(r, d))).data;
|
|
37
|
+
}
|
|
38
|
+
async fetchOne(e, r, d, i) {
|
|
39
|
+
const s = b(this.endpoint, e, r);
|
|
40
|
+
return (await R(s, this.createOptions(d, i))).data;
|
|
41
|
+
}
|
|
42
|
+
async fetchHasMany(e, r, d, i, s) {
|
|
43
|
+
const a = b(this.endpoint, e, r, d);
|
|
44
|
+
return await R(a, this.createOptions(i, s));
|
|
45
|
+
}
|
|
46
|
+
async fetchBelongsTo(e, r, d, i, s) {
|
|
47
|
+
const a = b(this.endpoint, e, r, d);
|
|
48
|
+
return await R(a, this.createOptions(i, s));
|
|
49
|
+
}
|
|
50
|
+
async post(e) {
|
|
51
|
+
const r = b(this.endpoint, e.type), i = JSON.stringify({
|
|
52
|
+
data: e
|
|
53
|
+
}), s = this.createOptions({}, {}, !0, i);
|
|
54
|
+
return await R(r, s);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
|
-
function
|
|
58
|
-
return
|
|
57
|
+
function H(o) {
|
|
58
|
+
return o.replace(/[-][a-z\u00E0-\u00F6\u00F8-\u00FE]/g, (e) => e.slice(1).toUpperCase());
|
|
59
59
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
}
|
|
60
|
+
const S = Symbol("jsonApiType");
|
|
61
|
+
function z(o, e) {
|
|
62
|
+
return o[S] = e, o;
|
|
64
63
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (!n) throw new Error(`Model with name ${e} not defined`);
|
|
89
|
-
return n;
|
|
90
|
-
}
|
|
91
|
-
function T(e, n, f) {
|
|
92
|
-
function w(s) {
|
|
93
|
-
return l(
|
|
94
|
-
y(s.type),
|
|
95
|
-
s.id,
|
|
96
|
-
s.attributes
|
|
97
|
-
);
|
|
64
|
+
function A(o) {
|
|
65
|
+
return o[S];
|
|
66
|
+
}
|
|
67
|
+
function j(o, e, r) {
|
|
68
|
+
o[e] = r;
|
|
69
|
+
}
|
|
70
|
+
var B = /* @__PURE__ */ ((o) => (o[o.HasMany = 0] = "HasMany", o[o.BelongsTo = 1] = "BelongsTo", o))(B || {});
|
|
71
|
+
function C(o, e) {
|
|
72
|
+
const r = e ?? new k(o.endpoint), d = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map();
|
|
73
|
+
for (const t of o.modelDefinitions)
|
|
74
|
+
d.set(t.type, t), t.relationships && i.set(t.type, t.relationships);
|
|
75
|
+
function s(t) {
|
|
76
|
+
return o.kebabCase ? H(t) : t;
|
|
77
|
+
}
|
|
78
|
+
function a(t, c) {
|
|
79
|
+
if (!d.get(t)) throw new Error(`Model type ${t} not defined`);
|
|
80
|
+
const u = c.id ?? crypto.randomUUID(), f = { id: u, ...c };
|
|
81
|
+
if (z(f, t), o.kebabCase) {
|
|
82
|
+
const l = { id: u };
|
|
83
|
+
z(l, t);
|
|
84
|
+
for (const [h, n] of Object.entries(c))
|
|
85
|
+
h !== "id" && n !== void 0 && (l[s(h)] = n);
|
|
86
|
+
return l;
|
|
98
87
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
const
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
88
|
+
return f;
|
|
89
|
+
}
|
|
90
|
+
function y(t, c, p) {
|
|
91
|
+
const u = /* @__PURE__ */ new Map();
|
|
92
|
+
if (p)
|
|
93
|
+
for (const n of p) {
|
|
94
|
+
const T = a(n.type, {
|
|
95
|
+
id: n.id,
|
|
96
|
+
...n.attributes
|
|
97
|
+
});
|
|
98
|
+
u.set(n.id, T);
|
|
99
|
+
}
|
|
100
|
+
const f = c.map(
|
|
101
|
+
(n) => a(t, {
|
|
102
|
+
id: n.id,
|
|
103
|
+
...n.attributes
|
|
104
|
+
})
|
|
105
|
+
), l = /* @__PURE__ */ new Map();
|
|
106
|
+
for (const n of f)
|
|
107
|
+
l.set(n.id, n);
|
|
108
|
+
function h(n) {
|
|
109
|
+
const T = l.get(n.id) ?? u.get(n.id);
|
|
110
|
+
if (!T) throw new Error("Unexpected not found record");
|
|
111
|
+
if (!n.relationships) return;
|
|
112
|
+
const O = i.get(n.type);
|
|
113
|
+
if (O)
|
|
114
|
+
for (const [g, E] of Object.entries(n.relationships)) {
|
|
115
|
+
const D = s(g), M = O[D];
|
|
116
|
+
if (!M) continue;
|
|
117
|
+
const P = (M.relationshipType === 0 ? E.data : [E.data]).filter((v) => v && v.type === M.type).map((v) => u.get(v.id) || l.get(v.id)).filter(Boolean);
|
|
118
|
+
j(T, D, M.relationshipType === 0 ? P : P[0]);
|
|
115
119
|
}
|
|
116
120
|
}
|
|
117
|
-
return
|
|
121
|
+
return p && (c.forEach(h), p.forEach(h)), f;
|
|
118
122
|
}
|
|
119
|
-
async function
|
|
120
|
-
const
|
|
121
|
-
return { doc:
|
|
123
|
+
async function m(t, c, p) {
|
|
124
|
+
const u = await r.fetchDocument(t, void 0, c, p), f = u.data, l = y(t, f, u.included);
|
|
125
|
+
return { doc: u, records: l };
|
|
122
126
|
}
|
|
123
|
-
async function
|
|
124
|
-
const
|
|
125
|
-
if (!
|
|
126
|
-
return
|
|
127
|
+
async function w(t, c, p, u) {
|
|
128
|
+
const f = await r.fetchDocument(t, c, p, u), l = f.data, n = y(t, [l], f.included)[0];
|
|
129
|
+
if (!n) throw new Error(`Record with id ${c} not found`);
|
|
130
|
+
return n;
|
|
127
131
|
}
|
|
128
|
-
async function
|
|
129
|
-
const
|
|
130
|
-
if (!
|
|
131
|
-
const
|
|
132
|
-
if (!
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
132
|
+
async function $(t, c, p, u) {
|
|
133
|
+
const f = A(t);
|
|
134
|
+
if (!f) throw new Error("Record type not found - ensure records are created via createRecord");
|
|
135
|
+
const l = i.get(f);
|
|
136
|
+
if (!l) throw new Error(`Model ${f} has no relationships`);
|
|
137
|
+
const h = l[c];
|
|
138
|
+
if (!h) throw new Error(`Relationship ${c} not defined`);
|
|
139
|
+
if (h.relationshipType === 1) {
|
|
140
|
+
const g = await r.fetchBelongsTo(f, t.id, c, p, u), E = g.data, D = a(h.type, {
|
|
141
|
+
id: E.id,
|
|
142
|
+
...E.attributes
|
|
143
|
+
});
|
|
144
|
+
return j(t, c, D), g;
|
|
136
145
|
}
|
|
137
|
-
const
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
146
|
+
const n = await r.fetchHasMany(f, t.id, c, p, u), O = (h.relationshipType === 0 ? n.data : [n.data]).map((g) => a(h.type, {
|
|
147
|
+
id: g.id,
|
|
148
|
+
...g.attributes
|
|
149
|
+
}));
|
|
150
|
+
return j(t, c, h.relationshipType === 0 ? O : O[0]), n;
|
|
151
|
+
}
|
|
152
|
+
async function U(t) {
|
|
153
|
+
const c = A(t);
|
|
154
|
+
if (!c) throw new Error("Record type not found - ensure records are created via createRecord");
|
|
155
|
+
const p = {
|
|
156
|
+
id: t.id,
|
|
157
|
+
type: c,
|
|
158
|
+
attributes: t
|
|
145
159
|
};
|
|
146
|
-
await r.post(
|
|
160
|
+
await r.post(p);
|
|
147
161
|
}
|
|
148
162
|
return {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
saveRecord: S
|
|
163
|
+
findAll: m,
|
|
164
|
+
findRecord: w,
|
|
165
|
+
findRelated: $,
|
|
166
|
+
createRecord: a,
|
|
167
|
+
saveRecord: U
|
|
155
168
|
};
|
|
156
169
|
}
|
|
157
170
|
export {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
I as useJsonApi
|
|
171
|
+
B as RelationshipType,
|
|
172
|
+
H as camel,
|
|
173
|
+
C as useJsonApi
|
|
162
174
|
};
|
|
163
175
|
//# 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 { JsonApiDocument, JsonApiResource } from './json-api.js'\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 signal?: AbortSignal\n}\n\nexport interface FetchParams {\n [key: string]: string\n}\n\nexport interface JsonApiFetcher {\n fetchDocument(type: string, id?: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiDocument>\n fetchOne(type: string, id: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiResource>\n fetchAll(type: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiResource[]>\n fetchHasMany(\n type: string,\n id: string,\n name: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument>\n fetchBelongsTo(\n type: string,\n id: string,\n name: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument>\n post(data: JsonApiResource): Promise<JsonApiDocument>\n}\n\ninterface Options {\n searchParams: URLSearchParams\n headers: Headers\n method?: string\n body?: BodyInit\n signal?: AbortSignal\n}\n\nasync function req(url: string, options: Options) {\n const { headers, searchParams, method, signal, body } = options\n const textSearchParams = `?${searchParams}`\n const finalUrl = url.replace(/(?:\\?.*?)?(?=#|$)/, textSearchParams)\n const response = await fetch(finalUrl, {\n method,\n headers,\n signal,\n body,\n })\n if (!response.ok) throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`)\n const data = (await response.json()) as JsonApiDocument\n return data\n}\n\nexport class JsonApiFetcherImpl implements JsonApiFetcher {\n constructor(private endpoint: string) {}\n createOptions(options: FetchOptions = {}, params: FetchParams = {}, post = false, body?: BodyInit): Options {\n const searchParams = new URLSearchParams()\n const headers = new Headers(options.headers)\n headers.append('Accept', 'application/vnd.api+json')\n if (post) headers.append('Content-Type', '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) {\n const url = resolvePath(this.endpoint, resource.type)\n const postDoc: JsonApiDocument = {\n data: resource,\n }\n const body = JSON.stringify(postDoc)\n const options = this.createOptions({}, {}, true, body)\n const doc = await req(url, options)\n return doc\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.js'\nimport { camel } from './util.js'\n\nexport interface JsonApiResourceIdentifier {\n id: string\n type: string\n}\n\nexport interface JsonApiRelationship {\n data: null | [] | JsonApiResourceIdentifier | JsonApiResourceIdentifier[]\n}\n\nexport interface JsonApiResource {\n id: 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 JsonApiError {\n id: string\n status: string\n code?: string\n title: string\n detail?: string\n meta?: JsonApiMeta\n}\n\n/**\n * Base class for models\n */\nexport class Model {\n constructor(public id: string) {\n this.id = id\n }\n [key: string]: unknown\n}\n\nexport interface ModelDefinition {\n /**\n * The JSON:API type for the model\n */\n type: string\n /**\n * The model constructor\n */\n ctor: typeof Model\n /**\n * Relationships for the model\n */\n rels?: 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 ctor: typeof Model\n type: RelationshipType\n}\n\nexport interface JsonApi {\n /**\n * Models registered with this store\n */\n modelRegistry: Map<typeof Model, string>\n /**\n * Relationships registered with this store\n */\n relRegistry: Map<typeof Model, Record<string, Relationship>>\n /**\n * @internal\n */\n createRecord<T extends typeof Model>(ctor: T, properties: Partial<InstanceType<T>> & { id?: string }): InstanceType<T>\n /**\n * Find all records of a given type\n * @returns the JSON API document that was fetched and the records that were found\n */\n findAll<T extends typeof Model>(\n ctor: T,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<{ doc: JsonApiDocument; records: InstanceType<T>[] }>\n /**\n * Find a single record by id\n * @returns the record that was found\n */\n findRecord<T extends typeof Model>(\n ctor: T,\n id: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<InstanceType<T>>\n /**\n * Find related records for a given record and relationship name\n * @returns the JSON API document that was fetched\n */\n findRelated(record: Model, name: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiDocument>\n}\n\nexport type JsonApiUseFunction = () => JsonApi\n\nexport function useJsonApi(config: JsonApiConfig, fetcher?: JsonApiFetcher) {\n const _fetcher = fetcher ?? new JsonApiFetcherImpl(config.endpoint)\n\n const modelRegistry = new Map<typeof Model, string>()\n const modelsByType = new Map<string, typeof Model>()\n const relsRegistry = new Map<typeof Model, Record<string, Relationship>>()\n\n for (const modelDef of config.modelDefinitions) {\n const ctor = modelDef.ctor\n modelRegistry.set(ctor, modelDef.type)\n modelsByType.set(modelDef.type, ctor)\n if (modelDef.rels) relsRegistry.set(ctor, modelDef.rels)\n }\n\n function normalize(str: string) {\n return config.kebabCase ? camel(str) : str\n }\n\n function internalCreateRecord<T extends typeof Model>(ctor: T, id: string, properties?: Partial<InstanceType<T>>) {\n const record = new ctor(id)\n if (properties)\n for (const [key, value] of Object.entries(properties)) if (value !== undefined) record[normalize(key)] = value\n return record as InstanceType<T>\n }\n\n function getModelType(ctor: typeof Model) {\n const type = modelRegistry.get(ctor)\n if (!type) throw new Error(`Model ${ctor.name} not defined`)\n return type\n }\n\n function getModel(type: string) {\n const ctor = modelsByType.get(type)\n if (!ctor) throw new Error(`Model with name ${type} not defined`)\n return ctor\n }\n\n function resourcesToRecords<T extends typeof Model>(\n ctor: T,\n resources: JsonApiResource[],\n included?: JsonApiResource[],\n ) {\n function createRecord<T extends typeof Model>(resource: JsonApiResource) {\n return internalCreateRecord<T>(\n getModel(resource.type) as T,\n resource.id,\n resource.attributes as Partial<InstanceType<T>>,\n )\n }\n // create records for included resources\n const includedMap = new Map<string, InstanceType<typeof Model>>()\n if (included) for (const resource of included) includedMap.set(resource.id, createRecord(resource))\n // create records for main resources\n const records = resources.map((r) => internalCreateRecord<T>(ctor, r.id, r.attributes as Partial<InstanceType<T>>))\n const recordsMap = new Map<string, InstanceType<typeof Model>>()\n for (const r of records) recordsMap.set(r.id, r)\n // populate relationships\n function populateRelationships(resource: JsonApiResource) {\n const record = recordsMap.get(resource.id) ?? includedMap.get(resource.id)\n if (!record) throw new Error('Unexpected not found record')\n const recordCtor = getModel(resource.type)\n if (!resource.relationships) return\n for (const [name, reldoc] of Object.entries(resource.relationships)) {\n const rels = relsRegistry.get(recordCtor)\n // NOTE: if relationship is not defined but exists in data, it is ignored\n if (!rels) continue\n const normalizedName = normalize(name)\n const rel = rels[normalizedName]\n if (!rel) throw new Error(`Relationship ${normalizedName} not defined`)\n const relType = getModelType(rel.ctor)\n const rids =\n rel.type === RelationshipType.HasMany\n ? (reldoc.data as JsonApiResourceIdentifier[])\n : [reldoc.data as JsonApiResourceIdentifier]\n const relIncludedRecords = rids\n .filter((d) => d && includedMap.has(d.id) && d.type === relType)\n .map((d) => includedMap.get(d.id))\n const relRecords = rids\n .filter((d) => d && recordsMap.has(d.id) && d.type === relType)\n .map((d) => recordsMap.get(d.id))\n relRecords.push(...relIncludedRecords)\n record[normalizedName] = rel.type === RelationshipType.HasMany ? relRecords : relRecords[0]\n }\n }\n if (included) {\n resources.map(populateRelationships)\n included.map(populateRelationships)\n }\n return records as InstanceType<T>[]\n }\n\n async function findAll<T extends typeof Model>(ctor: T, options?: FetchOptions, params?: FetchParams) {\n const type = getModelType(ctor)\n const doc = await _fetcher.fetchDocument(type, undefined, options, params)\n const resources = doc.data as JsonApiResource[]\n const records = resourcesToRecords(ctor, resources, doc.included)\n return { doc, records }\n }\n\n async function findRecord<T extends typeof Model>(ctor: T, id: string, options?: FetchOptions, params?: FetchParams) {\n const type = getModelType(ctor)\n const doc = await _fetcher.fetchDocument(type, id, options, params)\n const resource = doc.data as JsonApiResource\n const records = resourcesToRecords(ctor, [resource], doc.included)\n const record = records[0]\n if (!record) throw new Error(`Record with id ${id} not found`)\n return record as InstanceType<T>\n }\n\n async function findRelated(record: Model, name: string, options?: FetchOptions, params?: FetchParams) {\n const ctor = record.constructor as typeof Model\n const type = getModelType(ctor)\n const rels = relsRegistry.get(ctor)\n if (!rels) throw new Error(`Model ${ctor.name} has no relationships`)\n const rel = rels[name]\n if (!rel) throw new Error(`Has many relationship ${name} not defined`)\n if (rel.type === RelationshipType.BelongsTo) {\n const doc = await _fetcher.fetchBelongsTo(type, record.id, name, options, params)\n const related = doc.data as JsonApiResource\n const relatedRecord = internalCreateRecord(rel.ctor, related.id, related.attributes)\n record[name] = relatedRecord\n return doc\n }\n const doc = await _fetcher.fetchHasMany(type, record.id, name, options, params)\n const related =\n rel.type === RelationshipType.HasMany ? (doc.data as JsonApiResource[]) : [doc.data as JsonApiResource]\n const relatedRecords = related.map((r) => internalCreateRecord(rel.ctor, r.id, r.attributes))\n record[name] = rel.type === RelationshipType.HasMany ? relatedRecords : relatedRecords[0]\n return doc\n }\n\n async function saveRecord(record: Model) {\n const type = getModelType(record.constructor as typeof Model)\n const resource: JsonApiResource = {\n id: record.id,\n type,\n attributes: record,\n }\n await _fetcher.post(resource)\n }\n\n return {\n modelRegistry,\n relsRegistry,\n findAll,\n findRecord,\n findRelated,\n saveRecord,\n }\n}\n"],"names":["resolvePath","segments","req","url","options","headers","searchParams","method","signal","body","textSearchParams","finalUrl","response","JsonApiFetcherImpl","endpoint","params","post","requestOptions","key","value","type","id","name","resource","camel","str","match","Model","RelationshipType","useJsonApi","config","fetcher","_fetcher","modelRegistry","modelsByType","relsRegistry","modelDef","ctor","normalize","internalCreateRecord","properties","record","getModelType","getModel","resourcesToRecords","resources","included","createRecord","includedMap","records","r","recordsMap","populateRelationships","recordCtor","reldoc","rels","normalizedName","rel","relType","rids","relIncludedRecords","d","relRecords","findAll","doc","findRecord","findRelated","related","relatedRecord","relatedRecords","saveRecord"],"mappings":"AAEA,SAASA,KAAeC,GAA4B;AAClD,SAAO,IAAI,IAAIA,EAAS,KAAK,GAAG,CAAC,EAAE;AACrC;AAiDA,eAAeC,EAAIC,GAAaC,GAAkB;AAChD,QAAM,EAAE,SAAAC,GAAS,cAAAC,GAAc,QAAAC,GAAQ,QAAAC,GAAQ,MAAAC,MAASL,GAClDM,IAAmB,IAAIJ,CAAY,IACnCK,IAAWR,EAAI,QAAQ,qBAAqBO,CAAgB,GAC5DE,IAAW,MAAM,MAAMD,GAAU;AAAA,IACrC,QAAAJ;AAAA,IACA,SAAAF;AAAA,IACA,QAAAG;AAAA,IACA,MAAAC;AAAA,EAAA,CACD;AACD,MAAI,CAACG,EAAS,GAAI,OAAM,IAAI,MAAM,uBAAuBA,EAAS,MAAM,IAAIA,EAAS,UAAU,EAAE;AAEjG,SADc,MAAMA,EAAS,KAAA;AAE/B;AAEO,MAAMC,EAA6C;AAAA,EACxD,YAAoBC,GAAkB;AAAlB,SAAA,WAAAA;AAAA,EAAA;AAAA,EACpB,cAAcV,IAAwB,IAAIW,IAAsB,CAAA,GAAIC,IAAO,IAAOP,GAA0B;AAC1G,UAAMH,IAAe,IAAI,gBAAA,GACnBD,IAAU,IAAI,QAAQD,EAAQ,OAAO;AAC3C,IAAAC,EAAQ,OAAO,UAAU,0BAA0B,GAC/CW,KAAMX,EAAQ,OAAO,gBAAgB,0BAA0B;AACnE,UAAMY,IAAiB,EAAE,cAAAX,GAAc,SAAAD,GAAS,MAAAI,EAAA;AAChD,QAAIL,EAAQ;AACV,iBAAW,CAACc,GAAKC,CAAK,KAAK,OAAO,QAAQf,EAAQ,MAAM,EAAG,CAAAE,EAAa,OAAO,UAAUY,CAAG,KAAKC,E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AAE,IAAIA,EAAE,UAAU,CAAC;AAC5F,WAAAT,EAAOnB,CAAI,IAAImC,EAAI,SAAS,IAA2BY,IAAiBA,EAAe,CAAC,GACjFL;AAAA,EAAA;AAGT,iBAAeM,EAAW7B,GAAe;AACvC,UAAMrB,IAAOsB,EAAaD,EAAO,WAA2B,GACtDlB,IAA4B;AAAA,MAChC,IAAIkB,EAAO;AAAA,MACX,MAAArB;AAAA,MACA,YAAYqB;AAAA,IAAA;AAEd,UAAMT,EAAS,KAAKT,CAAQ;AAAA,EAAA;AAG9B,SAAO;AAAA,IACL,eAAAU;AAAA,IACA,cAAAE;AAAA,IACA,SAAA4B;AAAA,IACA,YAAAE;AAAA,IACA,aAAAC;AAAA,IACA,YAAAI;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 { 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 signal?: AbortSignal\n}\n\nexport interface FetchParams {\n [key: string]: string\n}\n\nexport interface JsonApiFetcher {\n fetchDocument(type: string, id?: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiDocument>\n fetchOne(type: string, id: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiResource>\n fetchAll(type: string, options?: FetchOptions, params?: FetchParams): Promise<JsonApiResource[]>\n fetchHasMany(\n type: string,\n id: string,\n name: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument>\n fetchBelongsTo(\n type: string,\n id: string,\n name: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument>\n post(data: JsonApiResource): Promise<JsonApiDocument>\n}\n\ninterface Options {\n searchParams: URLSearchParams\n headers: Headers\n method?: string\n body?: BodyInit\n signal?: AbortSignal\n}\n\nasync function req(url: string, options: Options) {\n const { headers, searchParams, method, signal, body } = options\n const textSearchParams = `?${searchParams}`\n const finalUrl = url.replace(/(?:\\?.*?)?(?=#|$)/, textSearchParams)\n const response = await fetch(finalUrl, {\n method,\n headers,\n signal,\n body,\n })\n if (!response.ok) throw new Error(`HTTP error! status: ${response.status} ${response.statusText}`)\n const data = (await response.json()) as JsonApiDocument\n return data\n}\n\nexport class JsonApiFetcherImpl implements JsonApiFetcher {\n constructor(private endpoint: string) {}\n createOptions(options: FetchOptions = {}, params: FetchParams = {}, post = false, body?: BodyInit): Options {\n const searchParams = new URLSearchParams()\n const headers = new Headers(options.headers)\n headers.append('Accept', 'application/vnd.api+json')\n if (post) headers.append('Content-Type', '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) {\n const url = resolvePath(this.endpoint, resource.type)\n const postDoc: JsonApiDocument = {\n data: resource,\n }\n const body = JSON.stringify(postDoc)\n const options = this.createOptions({}, {}, true, body)\n const doc = await req(url, options)\n return doc\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 type: string\n}\n\nexport interface JsonApiRelationship {\n data: null | [] | JsonApiResourceIdentifier | JsonApiResourceIdentifier[]\n}\n\nexport interface JsonApiResource {\n id: 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 JsonApiError {\n id: string\n status: string\n code?: string\n title: string\n detail?: string\n meta?: JsonApiMeta\n}\n\n/**\n * Symbol for storing the JSON:API type on records\n * @public\n */\nexport const JSON_API_TYPE = Symbol('jsonApiType')\n\n/**\n * Helper type for records with the JSON:API type symbol\n */\ntype RecordWithType = BaseRecord & {\n [JSON_API_TYPE]: string\n}\n\n/**\n * Type-safe helper to set the JSON:API type on a record\n */\nfunction setRecordType<T extends BaseRecord>(record: T, type: string): T {\n (record as RecordWithType)[JSON_API_TYPE] = type\n return record\n}\n\n/**\n * Type-safe helper to get the JSON:API type from a record\n */\nfunction getRecordType(record: BaseRecord): string | undefined {\n return (record as RecordWithType)[JSON_API_TYPE]\n}\n\n/**\n * Type-safe helper to set a relationship on a record\n */\nfunction setRelationship(record: BaseRecord, name: string, value: unknown): void {\n (record as Record<string, unknown>)[name] = value\n}\n\n/**\n * Base interface for records\n */\nexport interface BaseRecord {\n id: string\n [JSON_API_TYPE]?: string\n [key: string]: unknown\n}\n\n/**\n * Model definition\n */\nexport interface ModelDefinition<T extends BaseRecord = BaseRecord> {\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\nexport interface JsonApi {\n /**\n * Find all records of a given type\n * @returns the JSON API document that was fetched and the records that were found\n */\n findAll<T extends BaseRecord>(\n type: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<{ doc: JsonApiDocument; records: T[] }>\n\n /**\n * Find a single record by id\n * @returns the record that was found\n */\n findRecord<T extends BaseRecord>(\n type: string,\n id: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<T>\n\n /**\n * Find related records for a given record and relationship name\n * @returns the JSON API document that was fetched\n */\n findRelated<T extends BaseRecord>(\n record: T, \n relationshipName: string, \n options?: FetchOptions, \n params?: FetchParams\n ): Promise<JsonApiDocument>\n\n /**\n * Create a new record instance\n */\n createRecord<T extends BaseRecord>(type: string, properties: Partial<T> & { id?: string }): T\n\n /**\n * Save a record\n */\n saveRecord<T extends BaseRecord>(record: T): Promise<void>\n}\n\nexport type JsonApiUseFunction = () => JsonApi\n\nexport function useJsonApi(config: JsonApiConfig, fetcher?: JsonApiFetcher): JsonApi {\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) {\n relationshipDefinitions.set(modelDef.type, modelDef.relationships)\n }\n }\n\n function normalize(str: string) {\n return config.kebabCase ? camel(str) : str\n }\n\n function createRecord<T extends BaseRecord>(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, ...properties } as T\n setRecordType(record, type)\n \n // Normalize property keys if needed\n if (config.kebabCase) {\n const normalizedRecord = { id } as Record<string, unknown>\n setRecordType(normalizedRecord as BaseRecord, type)\n for (const [key, value] of Object.entries(properties)) {\n if (key !== 'id' && value !== undefined) {\n normalizedRecord[normalize(key)] = value\n }\n }\n return normalizedRecord as T\n }\n \n return record\n }\n\n function resourcesToRecords<T extends BaseRecord>(\n type: string,\n resources: JsonApiResource[],\n included?: JsonApiResource[],\n ): T[] {\n // Create records for included resources\n const includedMap = new Map<string, BaseRecord>()\n if (included) {\n for (const resource of included) {\n const record = createRecord<BaseRecord>(resource.type, {\n id: resource.id,\n ...(resource.attributes as Record<string, unknown>),\n })\n includedMap.set(resource.id, record)\n }\n }\n\n // Create records for main resources\n const records = resources.map(resource => \n createRecord<T>(type, {\n id: resource.id,\n ...(resource.attributes as Partial<T>),\n })\n )\n\n const recordsMap = new Map<string, BaseRecord>()\n for (const record of records) {\n recordsMap.set(record.id, record)\n }\n\n // Populate relationships\n function populateRelationships(resource: JsonApiResource) {\n const record = recordsMap.get(resource.id) ?? includedMap.get(resource.id)\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 // Ignore undefined relationships\n\n const rids = rel.relationshipType === RelationshipType.HasMany\n ? (reldoc.data as JsonApiResourceIdentifier[])\n : [reldoc.data as JsonApiResourceIdentifier]\n\n const relatedRecords = rids\n .filter(d => d && d.type === rel.type)\n .map(d => includedMap.get(d.id) || recordsMap.get(d.id))\n .filter(Boolean)\n\n setRelationship(record, normalizedName, rel.relationshipType === RelationshipType.HasMany \n ? relatedRecords \n : relatedRecords[0])\n }\n }\n\n if (included) {\n resources.forEach(populateRelationships)\n included.forEach(populateRelationships)\n }\n\n return records\n }\n\n async function findAll<T extends BaseRecord>(\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<T>(type, resources, doc.included)\n return { doc, records }\n }\n\n async function findRecord<T extends BaseRecord>(\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<T>(type, [resource], doc.included)\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<T extends BaseRecord>(\n record: T,\n relationshipName: string,\n options?: FetchOptions,\n params?: FetchParams,\n ): Promise<JsonApiDocument> {\n // Get the type from the symbol\n const recordType = getRecordType(record)\n if (!recordType) throw new Error('Record type not found - ensure records are created via createRecord')\n \n const rels = relationshipDefinitions.get(recordType)\n if (!rels) throw new Error(`Model ${recordType} 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(recordType, 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 as Record<string, unknown>),\n })\n setRelationship(record, relationshipName, relatedRecord)\n return doc\n }\n\n const doc = await _fetcher.fetchHasMany(recordType, record.id, relationshipName, options, params)\n const related = rel.relationshipType === RelationshipType.HasMany\n ? (doc.data as JsonApiResource[])\n : [doc.data as JsonApiResource]\n \n const relatedRecords = related.map(r => createRecord(rel.type, {\n id: r.id,\n ...(r.attributes as Record<string, unknown>),\n }))\n \n setRelationship(record, relationshipName, rel.relationshipType === RelationshipType.HasMany \n ? relatedRecords \n : relatedRecords[0])\n \n return doc\n }\n\n async function saveRecord<T extends BaseRecord>(record: T): Promise<void> {\n // Get the type from the symbol\n const recordType = getRecordType(record)\n if (!recordType) throw new Error('Record type not found - ensure records are created via createRecord')\n \n const resource: JsonApiResource = {\n id: record.id,\n type: recordType,\n attributes: record,\n }\n await _fetcher.post(resource)\n }\n\n return {\n findAll,\n findRecord,\n findRelated,\n createRecord,\n saveRecord,\n }\n}\n"],"names":["resolvePath","segments","req","url","options","headers","searchParams","method","signal","body","textSearchParams","finalUrl","response","JsonApiFetcherImpl","endpoint","params","post","requestOptions","key","value","type","id","name","resource","camel","str","match","JSON_API_TYPE","setRecordType","record","getRecordType","setRelationship","RelationshipType","useJsonApi","config","fetcher","_fetcher","modelDefinitions","relationshipDefinitions","modelDef","normalize","createRecord","properties","normalizedRecord","resourcesToRecords","resources","included","includedMap","records","recordsMap","populateRelationships","rels","reldoc","normalizedName","rel","relatedRecords","d","findAll","doc","findRecord","findRelated","relationshipName","recordType","related","relatedRecord","r","saveRecord"],"mappings":"AAEA,SAASA,KAAeC,GAA4B;AAClD,SAAO,IAAI,IAAIA,EAAS,KAAK,GAAG,CAAC,EAAE;AACrC;AAiDA,eAAeC,EAAIC,GAAaC,GAAkB;AAChD,QAAM,EAAE,SAAAC,GAAS,cAAAC,GAAc,QAAAC,GAAQ,QAAAC,GAAQ,MAAAC,MAASL,GAClDM,IAAmB,IAAIJ,CAAY,IACnCK,IAAWR,EAAI,QAAQ,qBAAqBO,CAAgB,GAC5DE,IAAW,MAAM,MAAMD,GAAU;AAAA,IACrC,QAAAJ;AAAA,IACA,SAAAF;AAAA,IACA,QAAAG;AAAA,IACA,MAAAC;AAAA,EAAA,CACD;AACD,MAAI,CAACG,EAAS,GAAI,OAAM,IAAI,MAAM,uBAAuBA,EAAS,MAAM,IAAIA,EAAS,UAAU,EAAE;AAEjG,SADc,MAAMA,EAAS,KAAA;AAE/B;AAEO,MAAMC,EAA6C;AAAA,EACxD,YAAoBC,GAAkB;AAAlB,SAAA,WAAAA;AAAA,EAAmB;AAAA,EACvC,cAAcV,IAAwB,IAAIW,IAAsB,CAAA,GAAIC,IAAO,IAAOP,GAA0B;AAC1G,UAAMH,IAAe,IAAI,gBAAA,GACnBD,IAAU,IAAI,QAAQD,EAAQ,OAAO;AAC3C,IAAAC,EAAQ,OAAO,UAAU,0BAA0B,GAC/CW,KAAMX,EAAQ,OAAO,gBAAgB,0BAA0B;AACnE,UAAMY,IAAiB,EAAE,cAAAX,GAAc,SAAAD,GAAS,MAAAI,EAAA;AAChD,QAAIL,EA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EAAgBF,GAAQgC,GAAkBP,EAAI,qBAAqB,IAC/DC,IACAA,EAAe,CAAC,CAAC,GAEdG;AAAA,EACT;AAEA,iBAAeQ,EAAiCrC,GAA0B;AAExE,UAAMiC,IAAahC,EAAcD,CAAM;AACvC,QAAI,CAACiC,EAAY,OAAM,IAAI,MAAM,qEAAqE;AAEtG,UAAMvC,IAA4B;AAAA,MAChC,IAAIM,EAAO;AAAA,MACX,MAAMiC;AAAA,MACN,YAAYjC;AAAA,IAAA;AAEd,UAAMO,EAAS,KAAKb,CAAQ;AAAA,EAC9B;AAEA,SAAO;AAAA,IACL,SAAAkC;AAAA,IACA,YAAAE;AAAA,IACA,aAAAC;AAAA,IACA,cAAAnB;AAAA,IACA,YAAAyB;AAAA,EAAA;AAEJ;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bjornharrtell/json-api",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/lib.js",
|
|
6
6
|
"module": "./dist/lib.js",
|
|
@@ -18,11 +18,11 @@
|
|
|
18
18
|
"url": "git+https://github.com/bjornharrtell/json-api.git"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@biomejs/biome": "2.2.
|
|
22
|
-
"@types/node": "^24.
|
|
23
|
-
"typedoc": "^0.28.
|
|
24
|
-
"typescript": "~5.9.
|
|
25
|
-
"vite": "^7.1.
|
|
21
|
+
"@biomejs/biome": "2.2.5",
|
|
22
|
+
"@types/node": "^24.7.1",
|
|
23
|
+
"typedoc": "^0.28.13",
|
|
24
|
+
"typescript": "~5.9.3",
|
|
25
|
+
"vite": "^7.1.9",
|
|
26
26
|
"vite-plugin-dts": "^4.5.4",
|
|
27
27
|
"vitest": "^3.2.4"
|
|
28
28
|
},
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html><html class="default" lang="en" data-base="../"><head><meta charset="utf-8"/><meta http-equiv="x-ua-compatible" content="IE=edge"/><title>Model | @bjornharrtell/json-api</title><meta name="description" content="Documentation for @bjornharrtell/json-api"/><meta name="viewport" content="width=device-width, initial-scale=1"/><link rel="stylesheet" href="../assets/style.css"/><link rel="stylesheet" href="../assets/highlight.css"/><script defer src="../assets/main.js"></script><script async src="../assets/icons.js" id="tsd-icons-script"></script><script async src="../assets/search.js" id="tsd-search-script"></script><script async src="../assets/navigation.js" id="tsd-nav-script"></script></head><body><script>document.documentElement.dataset.theme = localStorage.getItem("tsd-theme") || "os";document.body.style.display="none";setTimeout(() => window.app?app.showPage():document.body.style.removeProperty("display"),500)</script><header class="tsd-page-toolbar"><div class="tsd-toolbar-contents container"><a href="../index.html" class="title">@bjornharrtell/json-api</a><div id="tsd-toolbar-links"></div><button id="tsd-search-trigger" class="tsd-widget" aria-label="Search"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-search"></use></svg></button><dialog id="tsd-search" aria-label="Search"><input role="combobox" id="tsd-search-input" aria-controls="tsd-search-results" aria-autocomplete="list" aria-expanded="true" autocapitalize="off" autocomplete="off" placeholder="Search the docs" maxLength="100"/><ul role="listbox" id="tsd-search-results"></ul><div id="tsd-search-status" aria-live="polite" aria-atomic="true"><div>Preparing search index...</div></div></dialog><a href="#" class="tsd-widget menu" id="tsd-toolbar-menu-trigger" data-toggle="menu" aria-label="Menu"><svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-menu"></use></svg></a></div></header><div class="container container-main"><div class="col-content"><div class="tsd-page-title"><ul class="tsd-breadcrumb" aria-label="Breadcrumb"><li><a href="" aria-current="page">Model</a></li></ul><h1>Class Model</h1></div><section class="tsd-panel tsd-comment"><div class="tsd-comment tsd-typography"><p>Base class for models</p>
|
|
2
|
-
</div><div class="tsd-comment tsd-typography"></div></section><section class="tsd-panel"><h4 class="tsd-before-signature">Indexable</h4><ul class="tsd-signatures"><li class="tsd-index-signature"><div class="tsd-signature"><span class="tsd-signature-symbol">[</span><span class="tsd-kind-parameter">key</span>: <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">]:</span> <span class="tsd-signature-type">unknown</span></div></li></ul></section><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/bjornharrtell/json-api/blob/a8cccf432f5c08c4590e9c4bc7c9c8dc3f2dd055/src/json-api.ts#L78">json-api.ts:78</a></li></ul></aside><section class="tsd-panel-group tsd-index-group"><section class="tsd-panel tsd-index-panel"><details class="tsd-index-content tsd-accordion" open><summary class="tsd-accordion-summary tsd-index-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h5 class="tsd-index-heading uppercase">Index</h5></summary><div class="tsd-accordion-details"><section class="tsd-index-section"><h3 class="tsd-index-heading">Constructors</h3><div class="tsd-index-list"><a href="#constructor" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Constructor"><use href="../assets/icons.svg#icon-512"></use></svg><span>constructor</span></a>
|
|
3
|
-
</div></section><section class="tsd-index-section"><h3 class="tsd-index-heading">Properties</h3><div class="tsd-index-list"><a href="#id" class="tsd-index-link"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Property"><use href="../assets/icons.svg#icon-1024"></use></svg><span>id</span></a>
|
|
4
|
-
</div></section></div></details></section></section><details class="tsd-panel-group tsd-member-group tsd-accordion" open><summary class="tsd-accordion-summary" data-key="section-Constructors"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h2>Constructors</h2></summary><section><section class="tsd-panel tsd-member"><h3 class="tsd-anchor-link" id="constructor"><span>constructor</span><a href="#constructor" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><ul class="tsd-signatures"><li class=""><div class="tsd-signature tsd-anchor-link" id="constructormodel"><span class="tsd-signature-keyword">new</span> <span class="tsd-kind-constructor-signature">Model</span><span class="tsd-signature-symbol">(</span><span class="tsd-kind-parameter">id</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span><span class="tsd-signature-symbol">)</span><span class="tsd-signature-symbol">:</span> <a href="" class="tsd-signature-type tsd-kind-class">Model</a><a href="#constructormodel" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></div><div class="tsd-description"><div class="tsd-parameters"><h4 class="tsd-parameters-title">Parameters</h4><ul class="tsd-parameter-list"><li><span><span class="tsd-kind-parameter">id</span>: <span class="tsd-signature-type">string</span></span></li></ul></div><h4 class="tsd-returns-title">Returns <a href="" class="tsd-signature-type tsd-kind-class">Model</a></h4><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/bjornharrtell/json-api/blob/a8cccf432f5c08c4590e9c4bc7c9c8dc3f2dd055/src/json-api.ts#L79">json-api.ts:79</a></li></ul></aside></div></li></ul></section></section></details><details class="tsd-panel-group tsd-member-group tsd-accordion" open><summary class="tsd-accordion-summary" data-key="section-Properties"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h2>Properties</h2></summary><section><section class="tsd-panel tsd-member"><h3 class="tsd-anchor-link" id="id"><span>id</span><a href="#id" aria-label="Permalink" class="tsd-anchor-icon"><svg viewBox="0 0 24 24" aria-hidden="true"><use href="../assets/icons.svg#icon-anchor"></use></svg></a></h3><div class="tsd-signature"><span class="tsd-kind-property">id</span><span class="tsd-signature-symbol">:</span> <span class="tsd-signature-type">string</span></div><aside class="tsd-sources"><ul><li>Defined in <a href="https://github.com/bjornharrtell/json-api/blob/a8cccf432f5c08c4590e9c4bc7c9c8dc3f2dd055/src/json-api.ts#L79">json-api.ts:79</a></li></ul></aside></section></section></details></div><div class="col-sidebar"><div class="page-menu"><div class="tsd-navigation settings"><details class="tsd-accordion"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>Settings</h3></summary><div class="tsd-accordion-details"><div class="tsd-filter-visibility"><span class="settings-label">Member Visibility</span><ul id="tsd-filter-options"><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-protected" name="protected"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Protected</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-inherited" name="inherited" checked/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>Inherited</span></label></li><li class="tsd-filter-item"><label class="tsd-filter-input"><input type="checkbox" id="tsd-filter-external" name="external"/><svg width="32" height="32" viewBox="0 0 32 32" aria-hidden="true"><rect class="tsd-checkbox-background" width="30" height="30" x="1" y="1" rx="6" fill="none"></rect><path class="tsd-checkbox-checkmark" d="M8.35422 16.8214L13.2143 21.75L24.6458 10.25" stroke="none" stroke-width="3.5" stroke-linejoin="round" fill="none"></path></svg><span>External</span></label></li></ul></div><div class="tsd-theme-toggle"><label class="settings-label" for="tsd-theme">Theme</label><select id="tsd-theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></div></div></details></div><details open class="tsd-accordion tsd-page-navigation"><summary class="tsd-accordion-summary"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg><h3>On This Page</h3></summary><div class="tsd-accordion-details"><details open class="tsd-accordion tsd-page-navigation-section"><summary class="tsd-accordion-summary" data-key="section-Constructors"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Constructors</summary><div><a href="#constructor"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Constructor"><use href="../assets/icons.svg#icon-512"></use></svg><span>constructor</span></a></div></details><details open class="tsd-accordion tsd-page-navigation-section"><summary class="tsd-accordion-summary" data-key="section-Properties"><svg width="20" height="20" viewBox="0 0 24 24" fill="none" aria-hidden="true"><use href="../assets/icons.svg#icon-chevronDown"></use></svg>Properties</summary><div><a href="#id"><svg class="tsd-kind-icon" viewBox="0 0 24 24" aria-label="Property"><use href="../assets/icons.svg#icon-1024"></use></svg><span>id</span></a></div></details></div></details></div><div class="site-menu"><nav class="tsd-navigation"><a href="../modules.html">@bjornharrtell/json-api</a><ul class="tsd-small-nested-navigation" id="tsd-nav-container"><li>Loading...</li></ul></nav></div></div></div><footer><p class="tsd-generator">Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></footer><div class="overlay"></div></body></html>
|