@bjornharrtell/pinia-json-api
Advanced tools
Comparing version 0.5.2 to 0.5.3
201
dist/lib.js
@@ -1,45 +0,45 @@ | ||
import { defineStore as q } from "pinia"; | ||
import { shallowReactive as b } from "vue"; | ||
import { defineStore as U } from "pinia"; | ||
import { shallowReactive as M } from "vue"; | ||
import $ from "ky"; | ||
function v(...T) { | ||
return new URL(T.join("/")).href; | ||
function v(...b) { | ||
return new URL(b.join("/")).href; | ||
} | ||
class x { | ||
constructor(n, a) { | ||
this.endpoint = n, this.state = a; | ||
class q { | ||
constructor(n, d) { | ||
this.endpoint = n, this.state = d; | ||
} | ||
createOptions(n = {}, a = {}) { | ||
createOptions(n = {}, d = {}) { | ||
const o = new URLSearchParams(), c = new Headers(); | ||
c.append("Accept", "application/vnd.api+json"), this.state && c.append("Authorization", `Bearer ${this.state.value.token}`); | ||
const l = { searchParams: o, headers: c }; | ||
const u = { searchParams: o, headers: c }; | ||
if (n.fields) | ||
for (const [i, p] of Object.entries(n.fields)) o.append(`fields[${i}]`, p.join(",")); | ||
n.page?.size && o.append("page[size]", n.page.size.toString()), n.page?.number && o.append("page[number]", n.page.number.toString()), n.include && o.append("include", n.include.join(",")), n.filter && o.append("filter", n.filter); | ||
for (const [i, p] of Object.entries(a)) o.append(i, p); | ||
return l; | ||
for (const [i, p] of Object.entries(d)) o.append(i, p); | ||
return u; | ||
} | ||
async fetchDocument(n, a, o, c) { | ||
const l = [this.endpoint, n]; | ||
a && l.push(a); | ||
const i = v(...l); | ||
async fetchDocument(n, d, o, c) { | ||
const u = [this.endpoint, n]; | ||
d && u.push(d); | ||
const i = v(...u); | ||
return await $.get(i, this.createOptions(o, c)).json(); | ||
} | ||
async fetchAll(n, a, o) { | ||
async fetchAll(n, d, o) { | ||
const c = v(this.endpoint, n); | ||
return (await $.get(c, this.createOptions(a, o)).json()).data; | ||
return (await $.get(c, this.createOptions(d, o)).json()).data; | ||
} | ||
async fetchOne(n, a, o, c) { | ||
const l = v(this.endpoint, n, a); | ||
return (await $.get(l, this.createOptions(o, c)).json()).data; | ||
async fetchOne(n, d, o, c) { | ||
const u = v(this.endpoint, n, d); | ||
return (await $.get(u, this.createOptions(o, c)).json()).data; | ||
} | ||
async fetchHasMany(n, a, o, c, l) { | ||
const i = v(this.endpoint, n, a, o); | ||
return await $.get(i, this.createOptions(c, l)).json(); | ||
async fetchHasMany(n, d, o, c, u) { | ||
const i = v(this.endpoint, n, d, o); | ||
return await $.get(i, this.createOptions(c, u)).json(); | ||
} | ||
async fetchBelongsTo(n, a, o, c, l) { | ||
const i = v(this.endpoint, n, a, o); | ||
return await $.get(i, this.createOptions(c, l)).json(); | ||
async fetchBelongsTo(n, d, o, c, u) { | ||
const i = v(this.endpoint, n, d, o); | ||
return await $.get(i, this.createOptions(c, u)).json(); | ||
} | ||
} | ||
class K { | ||
class G { | ||
constructor(n) { | ||
@@ -49,22 +49,22 @@ this.id = n, this.id = n; | ||
} | ||
function N(T, n, a) { | ||
const o = a ?? new x(n.endpoint, n.state), c = b(/* @__PURE__ */ new Map()), l = b(/* @__PURE__ */ new Map()), i = b(/* @__PURE__ */ new Map()), p = b(/* @__PURE__ */ new Map()), E = b(/* @__PURE__ */ new Map()); | ||
function K(b, n, d) { | ||
const o = d ?? new q(n.endpoint, n.state), c = M(/* @__PURE__ */ new Map()), u = M(/* @__PURE__ */ new Map()), i = M(/* @__PURE__ */ new Map()), p = M(/* @__PURE__ */ new Map()), E = M(/* @__PURE__ */ new Map()); | ||
for (const e of n.modelDefinitions) { | ||
const t = e.ctor; | ||
c.set(t, e.type), l.set(e.type, t), e.hasMany && i.set(t, e.hasMany), e.belongsTo && p.set(t, e.belongsTo), E.set(e.type, /* @__PURE__ */ new Map()); | ||
c.set(t, e.type), u.set(e.type, t), e.hasMany && i.set(t, e.hasMany), e.belongsTo && p.set(t, e.belongsTo), E.set(e.type, /* @__PURE__ */ new Map()); | ||
} | ||
function D() { | ||
function k() { | ||
return Math.random().toString(36).substr(2, 9); | ||
} | ||
function H(e, t) { | ||
function C(e, t) { | ||
R(e); | ||
const s = t.id || D(); | ||
return j(e, s, t); | ||
const s = t.id || k(); | ||
return T(e, s, t); | ||
} | ||
function j(e, t, s) { | ||
const w = R(e), f = O(w); | ||
let d = f.get(t); | ||
if (d || (d = b(new e(t))), s) | ||
for (const [h, y] of Object.entries(s)) y !== void 0 && (d[h] = y); | ||
return f.set(t, d), d; | ||
function T(e, t, s) { | ||
const w = R(e), l = j(w); | ||
let a = l.get(t); | ||
if (a || (a = M(new e(t))), s) | ||
for (const [r, h] of Object.entries(s)) h !== void 0 && (a[r] = h); | ||
return l.set(t, a), a; | ||
} | ||
@@ -76,8 +76,8 @@ function R(e) { | ||
} | ||
function z(e) { | ||
const t = l.get(e); | ||
function D(e) { | ||
const t = u.get(e); | ||
if (!t) throw new Error(`Model with name ${e} not defined`); | ||
return t; | ||
} | ||
function O(e) { | ||
function j(e) { | ||
const t = E.get(e); | ||
@@ -87,3 +87,3 @@ if (!t) throw new Error(`Model with name ${e} not defined`); | ||
} | ||
function S(e, t) { | ||
function B(e, t) { | ||
const s = e.get(t); | ||
@@ -93,6 +93,10 @@ if (!s) throw new Error(`Record with id ${t} not found`); | ||
} | ||
function k(e, t, s) { | ||
function H(e) { | ||
const t = j(e.type); | ||
return B(t, e.id); | ||
} | ||
function S(e, t, s) { | ||
function w(r) { | ||
return j( | ||
z(r.type), | ||
return T( | ||
D(r.type), | ||
r.id, | ||
@@ -103,58 +107,57 @@ r.attributes | ||
if (s) for (const r of s) w(r); | ||
t.map((r) => j(e, r.id, r.attributes)); | ||
const f = R(e), d = O(f); | ||
function h(r) { | ||
const g = S(d, r.id); | ||
if (!s || !r.relationships) return g; | ||
for (const [u, m] of Object.entries(r.relationships)) | ||
if (i.get(e)?.has(u)) { | ||
const M = i.get(e)?.get(u); | ||
if (!M) throw new Error(`Has many relationship ${u} not defined`); | ||
const A = R(M), C = O(A), B = m.data.map((U) => S(C, U.id)); | ||
g[u] = B; | ||
} else if (p.get(e)?.has(u)) { | ||
const M = p.get(e)?.get(u); | ||
if (!M) throw new Error(`Belongs to relationship ${u} not defined`); | ||
const A = R(M), B = O(A)?.get(m.data.id); | ||
g[u] = B; | ||
} | ||
return g; | ||
const l = t.map((r) => T(e, r.id, r.attributes)); | ||
function a(r) { | ||
const h = H(r); | ||
if (r.relationships) { | ||
for (const [f, y] of Object.entries(r.relationships)) | ||
if (i.get(e)?.has(f)) { | ||
const g = i.get(e)?.get(f); | ||
if (!g) throw new Error(`Has many relationship ${f} not defined`); | ||
const m = R(g), O = j(m), A = y.data.map((L) => B(O, L.id)); | ||
h[f] = A; | ||
} else if (p.get(e)?.has(f)) { | ||
const g = p.get(e)?.get(f); | ||
if (!g) throw new Error(`Belongs to relationship ${f} not defined`); | ||
const m = R(g), A = j(m)?.get(y.data.id); | ||
h[f] = A; | ||
} | ||
} | ||
} | ||
return t.map(h); | ||
return s && (t.map(a), s.map(a)), l; | ||
} | ||
async function P(e, t, s) { | ||
const w = R(e), f = await o.fetchDocument(w, void 0, t, s), d = f.data, h = k(e, d, f.included); | ||
return { doc: f, records: h }; | ||
async function z(e, t, s) { | ||
const w = R(e), l = await o.fetchDocument(w, void 0, t, s), a = l.data, r = S(e, a, l.included); | ||
return { doc: l, records: r }; | ||
} | ||
async function P(e, t, s, w) { | ||
const l = R(e), a = j(l); | ||
if (!a.has(t)) { | ||
const h = await o.fetchDocument(l, t, s, w), f = h.data, g = S(e, [f], h.included)[0]; | ||
a.set(t, g); | ||
} | ||
const r = a.get(t); | ||
if (!r) throw new Error(`Record with id ${t} not found`); | ||
return r; | ||
} | ||
async function I(e, t, s, w) { | ||
const f = R(e), d = O(f); | ||
if (!d.has(t)) { | ||
const y = await o.fetchDocument(f, t, s, w), r = y.data, u = k(e, [r], y.included)[0]; | ||
d.set(t, u); | ||
const l = e.constructor, a = R(l), r = i.get(l); | ||
if (r?.has(t)) { | ||
const f = r.get(t); | ||
if (!f) throw new Error(`Has many relationship ${t} not defined`); | ||
const y = await o.fetchHasMany(a, e.id, t, s, w), m = y.data.map((O) => T(f, O.id, O.attributes)); | ||
return e[t] = m, y; | ||
} | ||
const h = d.get(t); | ||
if (!h) throw new Error(`Record with id ${t} not found`); | ||
return h; | ||
} | ||
async function J(e, t, s, w) { | ||
const f = e.constructor, d = R(f), h = i.get(f); | ||
const h = p.get(l); | ||
if (h?.has(t)) { | ||
const r = h.get(t); | ||
if (!r) throw new Error(`Has many relationship ${t} not defined`); | ||
const g = await o.fetchHasMany(d, e.id, t, s, w), m = g.data.map((M) => j(r, M.id, M.attributes)); | ||
return e[t] = m, g; | ||
const f = h.get(t); | ||
if (!f) throw new Error(`Belongs to relationship ${t} not defined`); | ||
const y = await o.fetchBelongsTo(a, e.id, t, s, w), g = y.data, m = T(f, g.id, g.attributes); | ||
return e[t] = m, y; | ||
} | ||
const y = p.get(f); | ||
if (y?.has(t)) { | ||
const r = y.get(t); | ||
if (!r) throw new Error(`Belongs to relationship ${t} not defined`); | ||
const g = await o.fetchBelongsTo(d, e.id, t, s, w), u = g.data, m = j(r, u.id, u.attributes); | ||
return e[t] = m, g; | ||
} | ||
throw new Error(`Model ${e.constructor.name} has no relations`); | ||
} | ||
function L() { | ||
function J() { | ||
for (const e of E.values()) e.clear(); | ||
} | ||
return q(T, () => ({ | ||
return U(b, () => ({ | ||
recordsByType: E, | ||
@@ -164,13 +167,13 @@ modelRegistry: c, | ||
belongsToRegistry: p, | ||
createRecord: H, | ||
findAll: P, | ||
findRecord: I, | ||
findRelated: J, | ||
unloadAll: L | ||
createRecord: C, | ||
findAll: z, | ||
findRecord: P, | ||
findRelated: I, | ||
unloadAll: J | ||
})); | ||
} | ||
export { | ||
K as Model, | ||
N as definePiniaJsonApiStore | ||
G as Model, | ||
K as definePiniaJsonApiStore | ||
}; | ||
//# sourceMappingURL=lib.js.map |
{ | ||
"name": "@bjornharrtell/pinia-json-api", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/lib.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
443974
2162