@bjornharrtell/pinia-json-api
Advanced tools
Comparing version 0.8.0 to 0.8.1
@@ -8,6 +8,6 @@ import { defineStore as x } from "pinia"; | ||
class N { | ||
constructor(n, a) { | ||
this.endpoint = n, this.state = a; | ||
constructor(n, i) { | ||
this.endpoint = n, this.state = i; | ||
} | ||
createOptions(n = {}, a = {}) { | ||
createOptions(n = {}, i = {}) { | ||
const r = new URLSearchParams(), c = new Headers(); | ||
@@ -19,25 +19,25 @@ c.append("Accept", "application/vnd.api+json"), this.state && c.append("Authorization", `Bearer ${this.state.value.token}`); | ||
n.page?.size && r.append("page[size]", n.page.size.toString()), n.page?.number && r.append("page[number]", n.page.number.toString()), n.include && r.append("include", n.include.join(",")), n.filter && r.append("filter", n.filter); | ||
for (const [d, h] of Object.entries(a)) r.append(d, h); | ||
for (const [d, h] of Object.entries(i)) r.append(d, h); | ||
return u; | ||
} | ||
async fetchDocument(n, a, r, c) { | ||
async fetchDocument(n, i, r, c) { | ||
const u = [this.endpoint, n]; | ||
a && u.push(a); | ||
i && u.push(i); | ||
const d = $(...u); | ||
return await E.get(d, this.createOptions(r, c)).json(); | ||
} | ||
async fetchAll(n, a, r) { | ||
async fetchAll(n, i, r) { | ||
const c = $(this.endpoint, n); | ||
return (await E.get(c, this.createOptions(a, r)).json()).data; | ||
return (await E.get(c, this.createOptions(i, r)).json()).data; | ||
} | ||
async fetchOne(n, a, r, c) { | ||
const u = $(this.endpoint, n, a); | ||
async fetchOne(n, i, r, c) { | ||
const u = $(this.endpoint, n, i); | ||
return (await E.get(u, this.createOptions(r, c)).json()).data; | ||
} | ||
async fetchHasMany(n, a, r, c, u) { | ||
const d = $(this.endpoint, n, a, r); | ||
async fetchHasMany(n, i, r, c, u) { | ||
const d = $(this.endpoint, n, i, r); | ||
return await E.get(d, this.createOptions(c, u)).json(); | ||
} | ||
async fetchBelongsTo(n, a, r, c, u) { | ||
const d = $(this.endpoint, n, a, r); | ||
async fetchBelongsTo(n, i, r, c, u) { | ||
const d = $(this.endpoint, n, i, r); | ||
return await E.get(d, this.createOptions(c, u)).json(); | ||
@@ -55,4 +55,4 @@ } | ||
var G = /* @__PURE__ */ ((p) => (p[p.HasMany = 0] = "HasMany", p[p.BelongsTo = 1] = "BelongsTo", p))(G || {}); | ||
function Y(p, n, a) { | ||
const r = a ?? new N(n.endpoint, n.state), c = O(/* @__PURE__ */ new Map()), u = O(/* @__PURE__ */ new Map()), d = O(/* @__PURE__ */ new Map()), h = O(/* @__PURE__ */ new Map()); | ||
function Y(p, n, i) { | ||
const r = i ?? new N(n.endpoint, n.state), c = O(/* @__PURE__ */ new Map()), u = O(/* @__PURE__ */ new Map()), d = O(/* @__PURE__ */ new Map()), h = O(/* @__PURE__ */ new Map()); | ||
for (const e of n.modelDefinitions) { | ||
@@ -70,10 +70,10 @@ const t = e.ctor; | ||
function T(e) { | ||
return n.kebabCase && _(e), e; | ||
return n.kebabCase ? _(e) : e; | ||
} | ||
function b(e, t, s) { | ||
const w = j(e), f = v(w); | ||
let i = f.get(t); | ||
if (i || (i = O(new e(t))), s) | ||
for (const [o, l] of Object.entries(s)) l !== void 0 && (i[T(o)] = l); | ||
return f.set(t, i), i; | ||
let a = f.get(t); | ||
if (a || (a = O(new e(t))), s) | ||
for (const [o, l] of Object.entries(s)) l !== void 0 && (a[T(o)] = l); | ||
return f.set(t, a), a; | ||
} | ||
@@ -114,3 +114,3 @@ function j(e) { | ||
const f = t.map((o) => b(e, o.id, o.attributes)); | ||
function i(o) { | ||
function a(o) { | ||
const l = P(o), g = k(o.type); | ||
@@ -129,15 +129,15 @@ if (o.relationships) | ||
} | ||
return s && (t.map(i), s.map(i)), f; | ||
return s && (t.map(a), s.map(a)), f; | ||
} | ||
async function U(e, t, s) { | ||
const w = j(e), f = await r.fetchDocument(w, void 0, t, s), i = f.data, o = S(e, i, f.included); | ||
const w = j(e), f = await r.fetchDocument(w, void 0, t, s), a = f.data, o = S(e, a, f.included); | ||
return { doc: f, records: o }; | ||
} | ||
async function I(e, t, s, w) { | ||
const f = j(e), i = v(f); | ||
if (!i.has(t)) { | ||
const f = j(e), a = v(f); | ||
if (!a.has(t)) { | ||
const l = await r.fetchDocument(f, t, s, w), g = l.data, m = S(e, [g], l.included)[0]; | ||
i.set(t, m); | ||
a.set(t, m); | ||
} | ||
const o = i.get(t); | ||
const o = a.get(t); | ||
if (!o) throw new Error(`Record with id ${t} not found`); | ||
@@ -147,3 +147,3 @@ return o; | ||
async function J(e, t, s, w) { | ||
const f = e.constructor, i = j(f), o = d.get(f); | ||
const f = e.constructor, a = j(f), o = d.get(f); | ||
if (!o) throw new Error(`Model ${f.name} has no relationships`); | ||
@@ -153,6 +153,6 @@ const l = o[t]; | ||
if (l.type === 1) { | ||
const y = await r.fetchBelongsTo(i, e.id, t, s, w), M = y.data, R = b(l.ctor, M.id, M.attributes); | ||
const y = await r.fetchBelongsTo(a, e.id, t, s, w), M = y.data, R = b(l.ctor, M.id, M.attributes); | ||
return e[t] = R, y; | ||
} | ||
const g = await r.fetchHasMany(i, e.id, t, s, w), m = (l.type === 0 ? g.data : [g.data]).map((y) => b(l.ctor, y.id, y.attributes)); | ||
const g = await r.fetchHasMany(a, e.id, t, s, w), m = (l.type === 0 ? g.data : [g.data]).map((y) => b(l.ctor, y.id, y.attributes)); | ||
return e[t] = l.type === 0 ? m : m[0], g; | ||
@@ -159,0 +159,0 @@ } |
{ | ||
"name": "@bjornharrtell/pinia-json-api", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"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
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
469957