Comparing version 0.0.5 to 0.0.6
@@ -18,8 +18,8 @@ declare type AbortControllerGroup = { | ||
declare type Declaration<Result, E = never> = { | ||
fetch: PreparedFetch<Result, E>; | ||
declare type Declaration<Result, E = never, A = unknown> = { | ||
fetch: PreparedFetch<Result, E, A>; | ||
key: string; | ||
}; | ||
declare type Declare<F extends Fetch> = <V = unknown, E = unknown>(resource: string, options?: FetchOptions) => InferResult<F> extends FetchResult ? Declaration<FetchResult<V, E>> : Declaration<V, null | E>; | ||
declare type Declare<F extends Fetch> = <V = unknown, E = unknown, A = unknown>(resource: string, options?: FetchOptions | ((args: A) => FetchOptions)) => InferResult<F> extends FetchResult ? Declaration<FetchResult<V, E>, never, A> : Declaration<V, null | E, A>; | ||
@@ -101,3 +101,3 @@ /** | ||
declare type PreparedFetch<V = unknown, E = unknown> = () => PromiseWithError<V, E>; | ||
declare type PreparedFetch<V = unknown, E = unknown, A = unknown> = (args: A) => PromiseWithError<V, E>; | ||
@@ -104,0 +104,0 @@ /** |
169
dist/adnf.js
const x = (e) => "File" in window && e instanceof File, M = (e) => e instanceof FormData, C = (e) => { | ||
const t = new FormData(); | ||
return Object.entries(e).forEach(([n, r]) => { | ||
if (r !== void 0) { | ||
if (r === null) | ||
return t.set(n, ""); | ||
if (typeof r == "string" || typeof r == "number") | ||
return t.set(n, `${r}`); | ||
if (x(r)) | ||
return t.set(n, r); | ||
if (Array.isArray(r) && r.length && r.every((o) => x(o))) | ||
return r.forEach((o) => t.append(`${n}[]`, o)); | ||
if (typeof r == "object" && r !== null || Array.isArray(r)) { | ||
return Object.entries(e).forEach(([r, n]) => { | ||
if (n !== void 0) { | ||
if (n === null) | ||
return t.set(r, ""); | ||
if (typeof n == "string" || typeof n == "number") | ||
return t.set(r, `${n}`); | ||
if (x(n)) | ||
return t.set(r, n); | ||
if (Array.isArray(n) && n.length && n.every((o) => x(o))) | ||
return n.forEach((o) => t.append(`${r}[]`, o)); | ||
if (typeof n == "object" && n !== null || Array.isArray(n)) { | ||
try { | ||
const o = JSON.stringify(r); | ||
t.set(n, o); | ||
const o = JSON.stringify(n); | ||
t.set(r, o); | ||
} catch { | ||
@@ -26,5 +26,5 @@ } | ||
"abort", | ||
(n) => { | ||
const r = n; | ||
t.abort(r.currentTarget.reason); | ||
(r) => { | ||
const n = r; | ||
t.abort(n.currentTarget.reason); | ||
}, | ||
@@ -36,17 +36,17 @@ { | ||
); | ||
}, U = (e = {}, t = {}, n = !0) => { | ||
n && (e.group && t.group || e.form && t.form || e.files && t.files) && console.warn( | ||
}, U = (e = {}, t = {}, r = !0) => { | ||
r && (e.group && t.group || e.form && t.form || e.files && t.files) && console.warn( | ||
"ADNF: Merging two options with group, form or files. Note these options cannot be merged and will be replaced. Hide this and similar warnings with fetch(..., { warn: false }) option." | ||
); | ||
const r = structuredClone({ ...e, ...t }); | ||
return (e.headers || t.headers) && (r.headers = { | ||
const n = structuredClone({ ...e, ...t }); | ||
return (e.headers || t.headers) && (n.headers = { | ||
...e.headers, | ||
...t.headers | ||
}), (e.data || t.data) && (r.data = { | ||
}), (e.data || t.data) && (n.data = { | ||
...e.data, | ||
...t.data | ||
}), (e.params || t.params) && (r.params = { | ||
}), (e.params || t.params) && (n.params = { | ||
...e.params, | ||
...t.params | ||
}), r; | ||
}), n; | ||
}, G = (e) => e == null, H = (e) => { | ||
@@ -81,8 +81,8 @@ if (G(e)) | ||
const { | ||
strict: n = !0, | ||
fetchSymbol: r = Symbol(), | ||
strict: r = !0, | ||
fetchSymbol: n = Symbol(), | ||
fetch: o = window.fetch, | ||
abortPrevious: s = !1, | ||
group: l, | ||
label: c, | ||
group: u, | ||
label: a, | ||
parse: m = JSON.parse, | ||
@@ -92,5 +92,5 @@ stringify: S = JSON.stringify, | ||
data: b, | ||
form: u, | ||
form: l, | ||
params: j, | ||
files: p, | ||
files: w, | ||
timeout: O, | ||
@@ -101,22 +101,22 @@ unwrap: Q, | ||
try { | ||
const d = (a) => { | ||
const d = (c) => { | ||
i.headers = { | ||
...i.headers, | ||
...a | ||
...c | ||
}; | ||
}; | ||
c && d({ | ||
"X-Request-Label": c | ||
a && d({ | ||
"X-Request-Label": a | ||
}); | ||
const f = new AbortController(); | ||
if (s) { | ||
if (!l) | ||
if (!u) | ||
throw new Error( | ||
"Fetch configured with abortPrevious but has no abort group: fetch(..., { abortPrevious: true, group? })" | ||
); | ||
l.cancel(); | ||
u.cancel(); | ||
} | ||
if (i.signal && _(i.signal, f), i.signal = f.signal, l) { | ||
const a = l.add(); | ||
_(a.signal, f); | ||
if (i.signal && _(i.signal, f), i.signal = f.signal, u) { | ||
const c = u.add(); | ||
_(c.signal, f); | ||
} | ||
@@ -126,6 +126,6 @@ const N = typeof O == "number" ? setTimeout(() => f.abort(L), O) : null; | ||
"Content-Type": "application/json" | ||
})), u || p) { | ||
})), l || w) { | ||
if (i.body) | ||
throw new Error("Fetch provided multiple values for body. Pick data, files or form."); | ||
u && (i.body = M(u) ? u : C(u)), p && (i.body = C(p)), d({ | ||
l && (i.body = M(l) ? l : C(l)), w && (i.body = C(w)), d({ | ||
"Content-Type": "multipart/form-data" | ||
@@ -139,25 +139,25 @@ }); | ||
try { | ||
const a = await o($, i), w = a.ok; | ||
const c = await o($, i), p = c.ok; | ||
N && clearTimeout(N); | ||
const y = a.headers.get("content-type"), T = y == null ? void 0 : y.includes("application/json"); | ||
if (n && !T) | ||
const y = c.headers.get("content-type"), T = y == null ? void 0 : y.includes("application/json"); | ||
if (r && !T) | ||
return E( | ||
a, | ||
c, | ||
null, | ||
"Fetch response not json in: fetch(..., { strict: true })" | ||
); | ||
const A = await a.text(), D = T ? m(A) : A; | ||
if (w) | ||
return q(a, D); | ||
const A = await c.text(), D = T ? m(A) : A; | ||
if (p) | ||
return q(c, D); | ||
const P = D; | ||
return n && !P ? E( | ||
a, | ||
return r && !P ? E( | ||
c, | ||
null, | ||
"Fetch response error nullable in: fetch(..., { strict: true })" | ||
) : E(a, P); | ||
} catch (a) { | ||
const w = f.signal.aborted && f.signal.reason === L; | ||
return J(a, { | ||
) : E(c, P); | ||
} catch (c) { | ||
const p = f.signal.aborted && f.signal.reason === L; | ||
return J(c, { | ||
aborted: f.signal.aborted, | ||
timeout: w | ||
timeout: p | ||
}); | ||
@@ -176,7 +176,7 @@ } | ||
response: e | ||
}), E = (e, t, n, r = {}) => Object.assign(h.Err(t, n), { | ||
}), E = (e, t, r, n = {}) => Object.assign(h.Err(t, r), { | ||
aborted: !1, | ||
timeout: !1, | ||
resolved: !0, | ||
...r, | ||
...n, | ||
response: e | ||
@@ -203,13 +203,13 @@ }), J = (e, t = {}) => Object.assign(h.Err(null, e), { | ||
}), R = (e, t) => { | ||
const n = "_initFetch" in e ? e._initFetch : e, r = "_composedFetch" in e ? e._composedFetch : (c) => c, o = (c) => r(t ? t(c) : c), s = (c, m) => o((b, u) => n(b + c, { ...u, ...m }))("", {}); | ||
const r = "_initFetch" in e ? e._initFetch : e, n = "_composedFetch" in e ? e._composedFetch : (a) => a, o = (a) => n(t ? t(a) : a), s = (a, m) => o((b, l) => r(b + a, { ...l, ...m }))("", {}); | ||
return Object.assign(s, { | ||
_initFetch: n, | ||
_initFetch: r, | ||
_composedFetch: o, | ||
_create: (c) => R(s, c) | ||
_create: (a) => R(s, a) | ||
}); | ||
}, B = (e, t) => R(e, (n) => (r, o) => { | ||
}, B = (e, t) => R(e, (r) => (n, o) => { | ||
const s = typeof t == "function" ? t(o) : t; | ||
return n(r, U(o, s)); | ||
}), X = (e, t) => R(e, (n) => (r, o) => n(typeof t == "function" ? t(r) : r + t, o)), v = (e, t) => X(e, (n) => t + n), ee = (e) => { | ||
const t = (n) => B(e, { method: n }); | ||
return r(n, U(o, s)); | ||
}), X = (e, t) => R(e, (r) => (n, o) => r(typeof t == "function" ? t(n) : n + t, o)), v = (e, t) => X(e, (r) => t + r), ee = (e) => { | ||
const t = (r) => B(e, { method: r }); | ||
return { | ||
@@ -228,4 +228,4 @@ method: t, | ||
function F(e) { | ||
const t = typeof e, n = e && e.constructor, r = n == Date; | ||
if (Object(e) === e && !r && n != RegExp) { | ||
const t = typeof e, r = e && e.constructor, n = r == Date; | ||
if (Object(e) === e && !n && r != RegExp) { | ||
let o = g.get(e); | ||
@@ -236,10 +236,10 @@ if (o) | ||
let s; | ||
if (n == Array) { | ||
if (r == Array) { | ||
for (o = "@", s = 0; s < e.length; s++) | ||
o += F(e[s]) + ","; | ||
g.set(e, o); | ||
} else if (n == Object) { | ||
} else if (r == Object) { | ||
o = "#"; | ||
const l = Object.keys(e).sort(); | ||
for (; (s = l.pop()) !== void 0; ) | ||
const u = Object.keys(e).sort(); | ||
for (; (s = u.pop()) !== void 0; ) | ||
e[s] !== void 0 && (o += s + ":" + F(e[s]) + ","); | ||
@@ -250,14 +250,23 @@ g.set(e, o); | ||
} | ||
return r ? e.toJSON() : t == "symbol" ? e.toString() : t == "string" ? JSON.stringify(e) : "" + e; | ||
return n ? e.toJSON() : t == "symbol" ? e.toString() : t == "string" ? JSON.stringify(e) : "" + e; | ||
} | ||
const te = (e) => (t, n) => { | ||
const r = I(t, n); | ||
const te = (e) => (t, r) => { | ||
const n = typeof r == "function", o = I(t, typeof r == "function" ? {} : r); | ||
return { | ||
key: r, | ||
fetch: (s) => e(t, U(n, { ...s, key: r })), | ||
key: o, | ||
fetch: (u) => { | ||
const a = typeof r == "function" ? r(u) : r; | ||
return e( | ||
t, | ||
U(a, { | ||
key: o, | ||
method: n ? "post" : r == null ? void 0 : r.method | ||
}) | ||
); | ||
}, | ||
resource: t | ||
}; | ||
}, I = (e, t) => { | ||
const n = [e, { params: (t == null ? void 0 : t.params) ?? {} }]; | ||
return F(n); | ||
const r = [e, { params: (t == null ? void 0 : t.params) ?? {} }]; | ||
return F(r); | ||
}, re = () => { | ||
@@ -275,7 +284,7 @@ const e = /* @__PURE__ */ new Set(); | ||
}; | ||
}, ne = (e) => (t, n) => e(t, n).then((r) => r.unwrap()), oe = (e) => (t, n) => e(t, n).then((r) => { | ||
}, ne = (e) => (t, r) => e(t, r).then((n) => n.unwrap()), oe = (e) => (t, r) => e(t, r).then((n) => { | ||
var o; | ||
if (r.failed) | ||
throw K(r.error, (o = r.response) == null ? void 0 : o.status); | ||
return r.value; | ||
if (n.failed) | ||
throw K(n.error, (o = n.response) == null ? void 0 : o.status); | ||
return n.value; | ||
}), K = (e, t) => Object.assign(new Error("ADNF: SWRError"), { | ||
@@ -282,0 +291,0 @@ type: e, |
{ | ||
"name": "adnf", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41526
594