Comparing version 0.0.22 to 0.0.23
@@ -62,2 +62,4 @@ declare type AbortControllerGroup = { | ||
export declare const FetchNoResponse: (error?: unknown, props?: Partial<NoResponse>) => NoResponse; | ||
export declare type FetchOptions = RequestInit & { | ||
@@ -170,4 +172,2 @@ strict?: boolean; | ||
export declare const ResultErr: (error?: unknown, props?: Partial<NoResponse>) => NoResponse; | ||
declare type ResultFetch = <V = unknown, E = unknown>(resource: Resource, options?: FetchOptions) => Promise<FetchResult<V, E>>; | ||
@@ -174,0 +174,0 @@ |
274
dist/adnf.js
@@ -1,17 +0,17 @@ | ||
const C = (t) => "File" in window && t instanceof File, H = (t) => t instanceof FormData, D = (t) => { | ||
const e = new FormData(); | ||
return Object.entries(t).forEach(([n, r]) => { | ||
const C = (e) => "File" in window && e instanceof File, H = (e) => e instanceof FormData, D = (e) => { | ||
const t = new FormData(); | ||
return Object.entries(e).forEach(([n, r]) => { | ||
if (r !== void 0) { | ||
if (r === null) | ||
return e.set(n, ""); | ||
return t.set(n, ""); | ||
if (typeof r == "string" || typeof r == "number") | ||
return e.set(n, `${r}`); | ||
return t.set(n, `${r}`); | ||
if (C(r)) | ||
return e.set(n, r); | ||
if (Array.isArray(r) && r.length && r.every((s) => C(s))) | ||
return r.forEach((s) => e.append(`${n}[]`, s)); | ||
return t.set(n, r); | ||
if (Array.isArray(r) && r.length && r.every((o) => C(o))) | ||
return r.forEach((o) => t.append(`${n}[]`, o)); | ||
if (typeof r == "object" && r !== null || Array.isArray(r)) { | ||
try { | ||
const s = JSON.stringify(r); | ||
e.set(n, s); | ||
const o = JSON.stringify(r); | ||
t.set(n, o); | ||
} catch { | ||
@@ -22,47 +22,47 @@ } | ||
} | ||
}), e; | ||
}, _ = (t, e) => { | ||
t.addEventListener( | ||
}), t; | ||
}, _ = (e, t) => { | ||
e.addEventListener( | ||
"abort", | ||
(n) => { | ||
const r = n; | ||
e.abort(r.currentTarget.reason); | ||
t.abort(r.currentTarget.reason); | ||
}, | ||
{ | ||
once: !0, | ||
signal: e.signal | ||
signal: t.signal | ||
} | ||
); | ||
}, j = (t = {}, e = {}, n = !0) => { | ||
n && (t.group && e.group || t.form && e.form || t.files && e.files) && console.warn( | ||
}, j = (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 r = { ...t, ...e }; | ||
return (t.headers || e.headers) && (r.headers = { | ||
...t.headers, | ||
...e.headers | ||
}), (t.data || e.data) && (r.data = { | ||
...t.data, | ||
...e.data | ||
}), (t.params || e.params) && (r.params = { | ||
...t.params, | ||
...e.params | ||
const r = { ...e, ...t }; | ||
return (e.headers || t.headers) && (r.headers = { | ||
...e.headers, | ||
...t.headers | ||
}), (e.data || t.data) && (r.data = { | ||
...e.data, | ||
...t.data | ||
}), (e.params || t.params) && (r.params = { | ||
...e.params, | ||
...t.params | ||
}), r; | ||
}, J = (t, e, n = !1) => { | ||
const r = new URL(t, "https://developer.mozilla.org"), s = Object.fromEntries(r.searchParams), i = Object.fromEntries(new URLSearchParams(e)), u = n ? i : { ...s, ...i }, o = Object.entries(u).toSorted(([d], [y]) => d.localeCompare(y)), f = new URLSearchParams(o).toString(), c = f ? `?${f}` : ""; | ||
return M(t) ? `${r.origin}${r.pathname}${c}` : `${r.pathname}${c}`; | ||
}, b = (t) => (Array.isArray(t) ? t : [t]).join("/"), M = (t) => { | ||
}, J = (e, t, n = !1) => { | ||
const r = new URL(e, "https://developer.mozilla.org"), o = Object.fromEntries(r.searchParams), i = Object.fromEntries(new URLSearchParams(t)), u = n ? i : { ...o, ...i }, s = Object.entries(u).toSorted(([d], [y]) => d.localeCompare(y)), f = new URLSearchParams(s).toString(), c = f ? `?${f}` : ""; | ||
return M(e) ? `${r.origin}${r.pathname}${c}` : `${r.pathname}${c}`; | ||
}, b = (e) => (Array.isArray(e) ? e : [e]).join("/"), M = (e) => { | ||
try { | ||
return !!new URL(t); | ||
return !!new URL(e); | ||
} catch { | ||
return !1; | ||
} | ||
}, q = (t) => t == null, z = (t) => { | ||
if (q(t)) | ||
}, q = (e) => e == null, z = (e) => { | ||
if (q(e)) | ||
throw new Error("Non-nullable assertion failed"); | ||
return t; | ||
}, L = (t) => ({ | ||
return e; | ||
}, L = (e) => ({ | ||
ok: !0, | ||
value: t, | ||
data: t, | ||
value: e, | ||
data: e, | ||
except: void 0, | ||
@@ -72,15 +72,15 @@ failed: !1, | ||
message: void 0, | ||
unwrap: () => t, | ||
log: () => void console.log(t), | ||
notNullable: () => z(t) | ||
unwrap: () => e, | ||
log: () => void console.log(e), | ||
notNullable: () => z(e) | ||
}), m = Object.assign(L, { | ||
Ok: L, | ||
Except: (t, e = new Error("Result.Except: No error provided")) => { | ||
const n = () => void console.error({ type: t, error: e, message: e == null ? void 0 : e.message }); | ||
Except: (e, t = new Error("Result.Except: No error provided")) => { | ||
const n = () => void console.error({ type: e, error: t, message: t == null ? void 0 : t.message }); | ||
return { | ||
value: void 0, | ||
except: t, | ||
data: t, | ||
error: e, | ||
message: e.message, | ||
except: e, | ||
data: e, | ||
error: t, | ||
message: t.message, | ||
failed: !0, | ||
@@ -90,3 +90,3 @@ ok: !1, | ||
unwrap: () => { | ||
throw n(), e; | ||
throw n(), t; | ||
}, | ||
@@ -98,17 +98,17 @@ notNullable: () => { | ||
} | ||
}), B = (t) => { | ||
}), B = (e) => { | ||
try { | ||
return m.Ok(t()); | ||
} catch (e) { | ||
return m.Except(null, S(e) ? e : void 0); | ||
return m.Ok(e()); | ||
} catch (t) { | ||
return m.Except(null, S(t) ? t : void 0); | ||
} | ||
}, S = (t) => t instanceof Error, Y = async (t, e) => { | ||
}, S = (e) => e instanceof Error, Y = async (e, t) => { | ||
const { | ||
strict: n = !0, | ||
fetchSymbol: r = Symbol(), | ||
fetch: s = window.fetch, | ||
fetch: o = window.fetch, | ||
abortPrevious: i = !1, | ||
group: u, | ||
label: o, | ||
parse: g = JSON.parse, | ||
label: s, | ||
parse: p = JSON.parse, | ||
stringify: f = JSON.stringify, | ||
@@ -123,6 +123,6 @@ // data | ||
...l | ||
} = e ?? {}; | ||
t = b(t); | ||
} = t ?? {}; | ||
e = b(e); | ||
try { | ||
const p = (a) => { | ||
const g = (a) => { | ||
l.headers = { | ||
@@ -133,4 +133,4 @@ ...l.headers, | ||
}; | ||
o && p({ | ||
"X-Request-Label": o | ||
s && g({ | ||
"X-Request-Label": s | ||
}); | ||
@@ -150,3 +150,3 @@ const h = new AbortController(); | ||
const N = typeof x == "number" ? setTimeout(() => h.abort(U), x) : null; | ||
if (c && (l.body = f(c), p({ | ||
if (c && (l.body = f(c), g({ | ||
"Content-Type": "application/json" | ||
@@ -156,9 +156,9 @@ })), d || E) { | ||
throw new Error("Fetch provided multiple values for body. Pick data, files or form."); | ||
d && (l.body = H(d) ? d : D(d)), E && (l.body = D(E)), p({ | ||
d && (l.body = H(d) ? d : D(d)), E && (l.body = D(E)), g({ | ||
"Content-Type": "multipart/form-data" | ||
}); | ||
} | ||
const G = J(t, y ?? {}); | ||
const G = J(e, y ?? {}); | ||
try { | ||
const a = await s(G, l), F = a.ok; | ||
const a = await o(G, l), F = a.ok; | ||
N && clearTimeout(N); | ||
@@ -172,3 +172,3 @@ const R = a.headers.get("content-type"), T = R == null ? void 0 : R.includes("application/json"); | ||
); | ||
const k = await a.text(), A = T ? g(k) : k; | ||
const k = await a.text(), A = T ? p(k) : k; | ||
if (F) | ||
@@ -189,4 +189,4 @@ return V(a, A); | ||
} | ||
} catch (p) { | ||
return w(p, { | ||
} catch (g) { | ||
return w(g, { | ||
aborted: !1, | ||
@@ -196,8 +196,8 @@ timeout: !1 | ||
} | ||
}, V = (t, e) => Object.assign(m.Ok(e), { | ||
}, V = (e, t) => Object.assign(m.Ok(t), { | ||
aborted: !1, | ||
timeout: !1, | ||
resolved: !0, | ||
response: t | ||
}), O = (t, e, n, r = {}) => Object.assign(m.Except(e, n), { | ||
response: e | ||
}), O = (e, t, n, r = {}) => Object.assign(m.Except(t, n), { | ||
aborted: !1, | ||
@@ -207,5 +207,5 @@ timeout: !1, | ||
...r, | ||
response: t | ||
}), w = (t, e = {}) => { | ||
const n = S(t) ? t : new Error("ResultErr: Provided error is on instance of Error()"); | ||
response: e | ||
}), w = (e, t = {}) => { | ||
const n = S(e) ? e : new Error("FetchNoResponse: Provided error is on instance of Error()"); | ||
return Object.assign(m.Except(null, n), { | ||
@@ -215,23 +215,23 @@ aborted: !1, | ||
resolved: !1, | ||
...e, | ||
...t, | ||
response: void 0 | ||
}); | ||
}, U = Symbol("Timeout"), Z = async (t, e) => (console.log( | ||
"%c🐕 " + ((e == null ? void 0 : e.method) || "GET").toUpperCase(), | ||
}, U = Symbol("Timeout"), Z = async (e, t) => (console.log( | ||
"%c🐕 " + ((t == null ? void 0 : t.method) || "GET").toUpperCase(), | ||
"background: #85ce47; border-radius: 999px; font-weight: 600; padding: 1.5px 5.5px; color: black", | ||
t, | ||
e | ||
), Object.assign(m.Ok({ resource: t, options: e }), { | ||
e, | ||
t | ||
), Object.assign(m.Ok({ resource: e, options: t }), { | ||
aborted: !1, | ||
timeout: !1, | ||
response: new Response() | ||
})), v = async (t, e) => Object.assign(m.Ok({}), { | ||
})), v = async (e, t) => Object.assign(m.Ok({}), { | ||
aborted: !1, | ||
timeout: !1, | ||
response: new Response() | ||
}), P = (t, e) => { | ||
const n = "_initFetch" in t ? t._initFetch : t, r = "_composedFetch" in t ? t._composedFetch : (o) => o, s = (o) => r(e ? e(o) : o), i = async (o, g) => { | ||
const f = (c, d) => (c = b(c), n(c + o, { ...d, ...g })); | ||
}), P = (e, t) => { | ||
const n = "_initFetch" in e ? e._initFetch : e, r = "_composedFetch" in e ? e._composedFetch : (s) => s, o = (s) => r(t ? t(s) : s), i = async (s, p) => { | ||
const f = (c, d) => (c = b(c), n(c + s, { ...d, ...p })); | ||
try { | ||
return s(f)("", {}); | ||
return o(f)("", {}); | ||
} catch (c) { | ||
@@ -243,67 +243,67 @@ return w(S(c) ? c : void 0); | ||
_initFetch: n, | ||
_composedFetch: s, | ||
_create: (o) => P(i, o) | ||
_composedFetch: o, | ||
_create: (s) => P(i, s) | ||
}); | ||
}, W = (t, e) => P(t, (n) => (r, s) => { | ||
const i = typeof e == "function" ? e(s) : e; | ||
return n(r, j(s, i)); | ||
}), X = (t, e) => P(t, (n) => (r, s) => (r = b(r), n( | ||
typeof e == "function" ? e(r) : r + b(e), | ||
s | ||
))), tt = (t, e) => X(t, (n) => e + n), et = (t) => { | ||
const e = (n) => W(t, { method: n }); | ||
}, W = (e, t) => P(e, (n) => (r, o) => { | ||
const i = typeof t == "function" ? t(o) : t; | ||
return n(r, j(o, i)); | ||
}), X = (e, t) => P(e, (n) => (r, o) => (r = b(r), n( | ||
typeof t == "function" ? t(r) : r + b(t), | ||
o | ||
))), ee = (e, t) => X(e, (n) => t + n), te = (e) => { | ||
const t = (n) => W(e, { method: n }); | ||
return { | ||
method: e, | ||
get: e("get"), | ||
head: e("head"), | ||
post: e("post"), | ||
put: e("put"), | ||
delete: e("delete"), | ||
del: e("delete"), | ||
patch: e("patch") | ||
method: t, | ||
get: t("get"), | ||
head: t("head"), | ||
post: t("post"), | ||
put: t("put"), | ||
delete: t("delete"), | ||
del: t("delete"), | ||
patch: t("patch") | ||
}; | ||
}, rt = (t) => (e, n) => { | ||
e = b(e); | ||
const r = I(e, typeof n == "function" ? {} : n); | ||
}, re = (e) => (t, n) => { | ||
t = b(t); | ||
const r = I(t, typeof n == "function" ? {} : n); | ||
return { | ||
key: r, | ||
fetch: (i, u) => { | ||
const o = B( | ||
const s = B( | ||
() => typeof n == "function" ? n(i) : n | ||
); | ||
if (o.failed) | ||
return w(o.error); | ||
const g = o.value, f = j(g, { key: r }); | ||
return t(e, j(f, u)); | ||
if (s.failed) | ||
return w(s.error); | ||
const p = s.value, f = j(p, { key: r }); | ||
return e(t, j(f, u)); | ||
}, | ||
resource: e | ||
resource: t | ||
}; | ||
}, I = (t, e) => J(t, (e == null ? void 0 : e.params) ?? {}), nt = () => { | ||
const t = /* @__PURE__ */ new Set(); | ||
}, I = (e, t) => J(e, (t == null ? void 0 : t.params) ?? {}), ne = () => { | ||
const e = /* @__PURE__ */ new Set(); | ||
return { | ||
controllers: t, | ||
cancel: () => (t.forEach((e) => { | ||
e.abort("Aborted controller group"), t.delete(e); | ||
}), t), | ||
controllers: e, | ||
cancel: () => (e.forEach((t) => { | ||
t.abort("Aborted controller group"), e.delete(t); | ||
}), e), | ||
add: () => { | ||
const e = new AbortController(); | ||
return t.add(e), e; | ||
const t = new AbortController(); | ||
return e.add(t), t; | ||
} | ||
}; | ||
}, st = (t) => (e, n) => t(e, n).then((r) => r.unwrap()), ot = (t) => { | ||
if (t.failed) | ||
throw K(t); | ||
return t.value; | ||
}, K = (t) => { | ||
var e; | ||
return Object.assign(t.error, { | ||
result: t, | ||
except: t.except, | ||
status: ((e = t.response) == null ? void 0 : e.status) ?? null | ||
}, oe = (e) => (t, n) => e(t, n).then((r) => r.unwrap()), se = (e) => { | ||
if (e.failed) | ||
throw K(e); | ||
return e.value; | ||
}, K = (e) => { | ||
var t; | ||
return Object.assign(e.error, { | ||
result: e, | ||
except: e.except, | ||
status: ((t = e.response) == null ? void 0 : t.status) ?? null | ||
}); | ||
}; | ||
export { | ||
w as FetchNoResponse, | ||
m as Result, | ||
w as ResultErr, | ||
nt as createAbortGroup, | ||
ne as createAbortGroup, | ||
Z as debugFetch, | ||
@@ -315,11 +315,11 @@ Y as fetch, | ||
Y as resultFetch, | ||
ot as swrResult, | ||
st as unwrap, | ||
se as swrResult, | ||
oe as unwrap, | ||
v as voidFetch, | ||
tt as withBase, | ||
rt as withDeclarations, | ||
ee as withBase, | ||
re as withDeclarations, | ||
P as withFetch, | ||
et as withMethods, | ||
te as withMethods, | ||
W as withOptions, | ||
X as withResource | ||
}; |
{ | ||
"name": "adnf", | ||
"version": "0.0.22", | ||
"version": "0.0.23", | ||
"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
42792