Comparing version 0.0.10 to 0.0.11
@@ -102,3 +102,3 @@ declare type AbortControllerGroup = { | ||
declare type PreparedFetch<V = unknown, E = unknown, A = unknown> = (args: A) => PromiseWithError<V, E>; | ||
declare type PreparedFetch<V = unknown, E = unknown, A = unknown> = (args: A, options?: FetchOptions) => PromiseWithError<V, E>; | ||
@@ -112,2 +112,16 @@ /** | ||
/** | ||
* Append search params to resource or complete URL. | ||
* | ||
* ```ts | ||
* params('/user', { id: 'a' }) | ||
* // /user?id=a | ||
* params('https://github.com/user', { id: 'a' }) | ||
* // https://github.com/user?id=a | ||
* params('https://github.com/user?id=a&for=b', { id: 'b' }) | ||
* // https://github.com/user?id=b&for=b | ||
* ``` | ||
*/ | ||
export declare const respectParams: (path: string, params: Record<string, any>, replace?: boolean) => string; | ||
export declare const Result: (<V>(value: V) => Success<V>) & { | ||
@@ -114,0 +128,0 @@ Success: <V>(value: V) => Success<V>; |
302
dist/adnf.js
@@ -1,17 +0,17 @@ | ||
const x = (e) => "File" in window && e instanceof File, $ = (e) => e instanceof FormData, C = (e) => { | ||
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(([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)) { | ||
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)) { | ||
try { | ||
const o = JSON.stringify(n); | ||
t.set(r, o); | ||
const o = JSON.stringify(r); | ||
t.set(n, o); | ||
} catch { | ||
@@ -23,8 +23,8 @@ } | ||
}), t; | ||
}, _ = (e, t) => { | ||
}, L = (e, t) => { | ||
e.addEventListener( | ||
"abort", | ||
(r) => { | ||
const n = r; | ||
t.abort(n.currentTarget.reason); | ||
(n) => { | ||
const r = n; | ||
t.abort(r.currentTarget.reason); | ||
}, | ||
@@ -36,22 +36,33 @@ { | ||
); | ||
}, M = (e = {}, t = {}, r = !0) => { | ||
r && (e.group && t.group || e.form && t.form || e.files && t.files) && console.warn( | ||
}, R = (e = {}, t = {}, n = !0) => { | ||
n && (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 n = { ...e, ...t }; | ||
return (e.headers || t.headers) && (n.headers = { | ||
const r = { ...e, ...t }; | ||
return (e.headers || t.headers) && (r.headers = { | ||
...e.headers, | ||
...t.headers | ||
}), (e.data || t.data) && (n.data = { | ||
}), (e.data || t.data) && (r.data = { | ||
...e.data, | ||
...t.data | ||
}), (e.params || t.params) && (n.params = { | ||
}), (e.params || t.params) && (r.params = { | ||
...e.params, | ||
...t.params | ||
}), n; | ||
}, G = (e) => ["post", "put", "delete", "patch"].includes(e.toLowerCase()), H = (e) => e == null, q = (e) => { | ||
if (H(e)) | ||
}), r; | ||
}, W = (e, t, n = !1) => { | ||
const r = new URL(e, "https://developer.mozilla.org"), o = Object.fromEntries(r.searchParams), s = Object.fromEntries(new URLSearchParams(t)), a = new URLSearchParams( | ||
n ? s : { ...o, ...s } | ||
).toString(), c = a ? `?${a}` : ""; | ||
return G(e) ? `${r.origin}${r.pathname}${c}` : `${r.pathname}${c}`; | ||
}, G = (e) => { | ||
try { | ||
return !!new URL(e); | ||
} catch { | ||
return !1; | ||
} | ||
}, H = (e) => ["post", "put", "delete", "patch"].includes(e.toLowerCase()), q = (e) => e == null, z = (e) => { | ||
if (q(e)) | ||
throw new Error("Non-nullable assertion failed"); | ||
return e; | ||
}, k = (e) => ({ | ||
}, _ = (e) => ({ | ||
value: e, | ||
@@ -63,5 +74,5 @@ failed: !1, | ||
log: () => void console.log(e), | ||
notNullable: () => q(e) | ||
}), h = Object.assign(k, { | ||
Success: k, | ||
notNullable: () => z(e) | ||
}), m = Object.assign(_, { | ||
Success: _, | ||
Err: (e, t) => ({ | ||
@@ -80,33 +91,33 @@ value: void 0, | ||
}) | ||
}), Y = async (e, t) => { | ||
}), Z = async (e, t) => { | ||
const { | ||
strict: r = !0, | ||
fetchSymbol: n = Symbol(), | ||
strict: n = !0, | ||
fetchSymbol: r = Symbol(), | ||
fetch: o = window.fetch, | ||
abortPrevious: a = !1, | ||
abortPrevious: s = !1, | ||
group: u, | ||
label: s, | ||
parse: g = JSON.parse, | ||
stringify: S = JSON.stringify, | ||
label: a, | ||
parse: c = JSON.parse, | ||
stringify: g = JSON.stringify, | ||
// data | ||
data: b, | ||
form: l, | ||
params: j, | ||
form: f, | ||
params: U, | ||
files: w, | ||
timeout: O, | ||
unwrap: V, | ||
...i | ||
unwrap: Y, | ||
...l | ||
} = t ?? {}; | ||
try { | ||
const d = (c) => { | ||
i.headers = { | ||
...i.headers, | ||
...c | ||
const h = (i) => { | ||
l.headers = { | ||
...l.headers, | ||
...i | ||
}; | ||
}; | ||
s && d({ | ||
"X-Request-Label": s | ||
a && h({ | ||
"X-Request-Label": a | ||
}); | ||
const f = new AbortController(); | ||
if (a) { | ||
const d = new AbortController(); | ||
if (s) { | ||
if (!u) | ||
@@ -118,48 +129,45 @@ throw new Error( | ||
} | ||
if (i.signal && _(i.signal, f), i.signal = f.signal, u) { | ||
const c = u.add(); | ||
_(c.signal, f); | ||
if (l.signal && L(l.signal, d), l.signal = d.signal, u) { | ||
const i = u.add(); | ||
L(i.signal, d); | ||
} | ||
const N = typeof O == "number" ? setTimeout(() => f.abort(L), O) : null; | ||
if (b && (i.body = S(b), d({ | ||
const N = typeof O == "number" ? setTimeout(() => d.abort(k), O) : null; | ||
if (b && (l.body = g(b), h({ | ||
"Content-Type": "application/json" | ||
})), l || w) { | ||
if (i.body) | ||
})), f || w) { | ||
if (l.body) | ||
throw new Error("Fetch provided multiple values for body. Pick data, files or form."); | ||
l && (i.body = $(l) ? l : C(l)), w && (i.body = C(w)), d({ | ||
f && (l.body = M(f) ? f : C(f)), w && (l.body = C(w)), h({ | ||
"Content-Type": "multipart/form-data" | ||
}); | ||
} | ||
const U = new URLSearchParams({ | ||
...Object.fromEntries(new URL(e).searchParams), | ||
...Object.fromEntries(new URLSearchParams(j ?? {})) | ||
}), W = j ? `${e}?${U.toString()}` : e; | ||
const J = W(e, U ?? {}); | ||
try { | ||
const c = await o(W, i), p = c.ok; | ||
const i = await o(J, l), y = i.ok; | ||
N && clearTimeout(N); | ||
const y = c.headers.get("content-type"), T = y == null ? void 0 : y.includes("application/json"); | ||
if (r && !T) | ||
return E( | ||
c, | ||
const E = i.headers.get("content-type"), P = E == null ? void 0 : E.includes("application/json"); | ||
if (n && !P) | ||
return F( | ||
i, | ||
null, | ||
"Fetch response not json in: fetch(..., { strict: true })" | ||
); | ||
const A = await c.text(), D = T ? g(A) : A; | ||
if (p) | ||
return B(c, D); | ||
const P = D; | ||
return r && !P ? E( | ||
c, | ||
const T = await i.text(), A = P ? c(T) : T; | ||
if (y) | ||
return B(i, A); | ||
const D = A; | ||
return n && !D ? F( | ||
i, | ||
null, | ||
"Fetch response error nullable in: fetch(..., { strict: true })" | ||
) : E(c, P); | ||
} catch (c) { | ||
const p = f.signal.aborted && f.signal.reason === L; | ||
return J(c, { | ||
aborted: f.signal.aborted, | ||
timeout: p | ||
) : F(i, D); | ||
} catch (i) { | ||
const y = d.signal.aborted && d.signal.reason === k; | ||
return $(i, { | ||
aborted: d.signal.aborted, | ||
timeout: y | ||
}); | ||
} | ||
} catch (d) { | ||
return J(d, { | ||
} catch (h) { | ||
return $(h, { | ||
aborted: !1, | ||
@@ -169,3 +177,3 @@ timeout: !1 | ||
} | ||
}, B = (e, t) => Object.assign(h(t), { | ||
}, B = (e, t) => Object.assign(m(t), { | ||
aborted: !1, | ||
@@ -175,9 +183,9 @@ timeout: !1, | ||
response: e | ||
}), E = (e, t, r, n = {}) => Object.assign(h.Err(t, r), { | ||
}), F = (e, t, n, r = {}) => Object.assign(m.Err(t, n), { | ||
aborted: !1, | ||
timeout: !1, | ||
resolved: !0, | ||
...n, | ||
...r, | ||
response: e | ||
}), J = (e, t = {}) => Object.assign(h.Err(null, e), { | ||
}), $ = (e, t = {}) => Object.assign(m.Err(null, e), { | ||
aborted: !1, | ||
@@ -188,3 +196,3 @@ timeout: !1, | ||
response: void 0 | ||
}), L = Symbol("Timeout"), Z = async (e, t) => (console.log( | ||
}), k = Symbol("Timeout"), v = async (e, t) => (console.log( | ||
"%c🐕 " + ((t == null ? void 0 : t.method) || "GET").toUpperCase(), | ||
@@ -194,22 +202,22 @@ "background: #85ce47; border-radius: 999px; font-weight: 600; padding: 1.5px 5.5px; color: black", | ||
t | ||
), Object.assign(h({ resource: e, options: t }), { | ||
), Object.assign(m({ resource: e, options: t }), { | ||
aborted: !1, | ||
timeout: !1, | ||
response: new Response() | ||
})), v = async (e, t) => Object.assign(h({}), { | ||
})), ee = async (e, t) => Object.assign(m({}), { | ||
aborted: !1, | ||
timeout: !1, | ||
response: new Response() | ||
}), R = (e, t) => { | ||
const r = "_initFetch" in e ? e._initFetch : e, n = "_composedFetch" in e ? e._composedFetch : (s) => s, o = (s) => n(t ? t(s) : s), a = (s, g) => o((b, l) => r(b + s, { ...l, ...g }))("", {}); | ||
return Object.assign(a, { | ||
_initFetch: r, | ||
}), j = (e, t) => { | ||
const n = "_initFetch" in e ? e._initFetch : e, r = "_composedFetch" in e ? e._composedFetch : (a) => a, o = (a) => r(t ? t(a) : a), s = (a, c) => o((b, f) => n(b + a, { ...f, ...c }))("", {}); | ||
return Object.assign(s, { | ||
_initFetch: n, | ||
_composedFetch: o, | ||
_create: (s) => R(a, s) | ||
_create: (a) => j(s, a) | ||
}); | ||
}, X = (e, t) => R(e, (r) => (n, o) => { | ||
const a = typeof t == "function" ? t(o) : t; | ||
return r(n, M(o, a)); | ||
}), z = (e, t) => R(e, (r) => (n, o) => r(typeof t == "function" ? t(n) : n + t, o)), ee = (e, t) => z(e, (r) => t + r), te = (e) => { | ||
const t = (r) => X(e, { method: r }); | ||
}, V = (e, t) => j(e, (n) => (r, o) => { | ||
const s = typeof t == "function" ? t(o) : t; | ||
return n(r, R(o, s)); | ||
}), X = (e, t) => j(e, (n) => (r, o) => n(typeof t == "function" ? t(r) : r + t, o)), te = (e, t) => X(e, (n) => t + n), re = (e) => { | ||
const t = (n) => V(e, { method: n }); | ||
return { | ||
@@ -225,40 +233,37 @@ method: t, | ||
}; | ||
}, m = /* @__PURE__ */ new WeakMap(); | ||
}, p = /* @__PURE__ */ new WeakMap(); | ||
let I = 0; | ||
function F(e) { | ||
const t = typeof e, r = e && e.constructor, n = r == Date; | ||
if (Object(e) === e && !n && r != RegExp) { | ||
let o = m.get(e); | ||
function S(e) { | ||
const t = typeof e, n = e && e.constructor, r = n == Date; | ||
if (Object(e) === e && !r && n != RegExp) { | ||
let o = p.get(e); | ||
if (o) | ||
return o; | ||
o = ++I + "~", m.set(e, o); | ||
let a; | ||
if (r == Array) { | ||
for (o = "@", a = 0; a < e.length; a++) | ||
o += F(e[a]) + ","; | ||
m.set(e, o); | ||
} else if (r == Object) { | ||
o = ++I + "~", p.set(e, o); | ||
let s; | ||
if (n == Array) { | ||
for (o = "@", s = 0; s < e.length; s++) | ||
o += S(e[s]) + ","; | ||
p.set(e, o); | ||
} else if (n == Object) { | ||
o = "#"; | ||
const u = Object.keys(e).sort(); | ||
for (; (a = u.pop()) !== void 0; ) | ||
e[a] !== void 0 && (o += a + ":" + F(e[a]) + ","); | ||
m.set(e, o); | ||
for (; (s = u.pop()) !== void 0; ) | ||
e[s] !== void 0 && (o += s + ":" + S(e[s]) + ","); | ||
p.set(e, o); | ||
} | ||
return o; | ||
} | ||
return n ? e.toJSON() : t == "symbol" ? e.toString() : t == "string" ? JSON.stringify(e) : "" + e; | ||
return r ? e.toJSON() : t == "symbol" ? e.toString() : t == "string" ? JSON.stringify(e) : "" + e; | ||
} | ||
const re = (e) => (t, r) => { | ||
const n = typeof r == "function", o = K(t, typeof r == "function" ? {} : r); | ||
const ne = (e) => (t, n) => { | ||
const r = typeof n == "function", o = K(t, typeof n == "function" ? {} : n); | ||
return { | ||
key: o, | ||
fetch: (u) => { | ||
const s = typeof r == "function" ? r(u) : r; | ||
return e( | ||
t, | ||
M(s, { | ||
key: o, | ||
method: n ? s != null && s.method && G(s.method) ? s.method : "post" : r == null ? void 0 : r.method | ||
}) | ||
); | ||
fetch: (u, a) => { | ||
const c = typeof n == "function" ? n(u) : n, g = R(c, { | ||
key: o, | ||
method: r ? c != null && c.method && H(c.method) ? c.method : "post" : n == null ? void 0 : n.method | ||
}); | ||
return e(t, R(g, a)); | ||
}, | ||
@@ -268,5 +273,5 @@ resource: t | ||
}, K = (e, t) => { | ||
const r = [e, { params: (t == null ? void 0 : t.params) ?? {} }]; | ||
return F(r); | ||
}, ne = () => { | ||
const n = [e, { params: (t == null ? void 0 : t.params) ?? {} }]; | ||
return S(n); | ||
}, oe = () => { | ||
const e = /* @__PURE__ */ new Set(); | ||
@@ -283,7 +288,7 @@ return { | ||
}; | ||
}, oe = (e) => (t, r) => e(t, r).then((n) => n.unwrap()), se = (e) => (t, r) => e(t, r).then((n) => { | ||
}, se = (e) => (t, n) => e(t, n).then((r) => r.unwrap()), ae = (e) => (t, n) => e(t, n).then((r) => { | ||
var o; | ||
if (n.failed) | ||
throw Q(n.error, (o = n.response) == null ? void 0 : o.status); | ||
return n.value; | ||
if (r.failed) | ||
throw Q(r.error, (o = r.response) == null ? void 0 : o.status); | ||
return r.value; | ||
}), Q = (e, t) => Object.assign(new Error("ADNF: SWRError"), { | ||
@@ -294,18 +299,19 @@ type: e, | ||
export { | ||
h as Result, | ||
J as ResultErr, | ||
ne as createAbortGroup, | ||
Z as debugFetch, | ||
Y as fetch, | ||
M as mergeOptions, | ||
Y as resultFetch, | ||
se as swr, | ||
oe as unwrap, | ||
v as voidFetch, | ||
ee as withBase, | ||
re as withDeclarations, | ||
R as withFetch, | ||
te as withMethods, | ||
X as withOptions, | ||
z as withResource | ||
m as Result, | ||
$ as ResultErr, | ||
oe as createAbortGroup, | ||
v as debugFetch, | ||
Z as fetch, | ||
R as mergeOptions, | ||
W as respectParams, | ||
Z as resultFetch, | ||
ae as swr, | ||
se as unwrap, | ||
ee as voidFetch, | ||
te as withBase, | ||
ne as withDeclarations, | ||
j as withFetch, | ||
re as withMethods, | ||
V as withOptions, | ||
X as withResource | ||
}; |
{ | ||
"name": "adnf", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"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
43178
618