Comparing version 0.0.16 to 0.0.17
@@ -166,3 +166,4 @@ declare type AbortControllerGroup = { | ||
export declare type SWRError<T> = Error & { | ||
type: null | T; | ||
result: FetchErr | FetchErrResponse<T>; | ||
errorType: null | T; | ||
status: null | number; | ||
@@ -169,0 +170,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
const C = (e) => "File" in window && e instanceof File, H = (e) => e instanceof FormData, _ = (e) => { | ||
const D = (e) => "File" in window && e instanceof File, H = (e) => e instanceof FormData, _ = (e) => { | ||
const t = new FormData(); | ||
@@ -9,5 +9,5 @@ return Object.entries(e).forEach(([n, r]) => { | ||
return t.set(n, `${r}`); | ||
if (C(r)) | ||
if (D(r)) | ||
return t.set(n, r); | ||
if (Array.isArray(r) && r.length && r.every((s) => C(s))) | ||
if (Array.isArray(r) && r.length && r.every((s) => D(s))) | ||
return r.forEach((s) => t.append(`${n}[]`, s)); | ||
@@ -60,4 +60,4 @@ if (typeof r == "object" && r !== null || Array.isArray(r)) { | ||
} | ||
}, W = (e) => e == null, q = (e) => { | ||
if (W(e)) | ||
}, q = (e) => e == null, z = (e) => { | ||
if (q(e)) | ||
throw new Error("Non-nullable assertion failed"); | ||
@@ -76,3 +76,3 @@ return e; | ||
log: () => void console.log(e), | ||
notNullable: () => q(e) | ||
notNullable: () => z(e) | ||
}), m = Object.assign(U, { | ||
@@ -100,3 +100,3 @@ Success: U, | ||
} | ||
}), z = (e) => { | ||
}), B = (e) => { | ||
try { | ||
@@ -122,3 +122,3 @@ return m.Success(e()); | ||
files: E, | ||
timeout: T, | ||
timeout: O, | ||
unwrap: K, | ||
@@ -150,3 +150,3 @@ ...l | ||
} | ||
const N = typeof T == "number" ? setTimeout(() => h.abort(J), T) : null; | ||
const N = typeof O == "number" ? setTimeout(() => h.abort(J), O) : null; | ||
if (c && (l.body = f(c), b({ | ||
@@ -163,5 +163,5 @@ "Content-Type": "application/json" | ||
try { | ||
const a = await s(G, l), R = a.ok; | ||
const a = await s(G, l), F = a.ok; | ||
N && clearTimeout(N); | ||
const F = a.headers.get("content-type"), A = F == null ? void 0 : F.includes("application/json"); | ||
const R = a.headers.get("content-type"), A = R == null ? void 0 : R.includes("application/json"); | ||
if (n && !A) | ||
@@ -173,16 +173,16 @@ return S( | ||
); | ||
const D = await a.text(), $ = A ? g(D) : D; | ||
if (R) | ||
return B(a, $); | ||
const x = $; | ||
return n && !x ? S( | ||
const $ = await a.text(), x = A ? g($) : $; | ||
if (F) | ||
return V(a, x); | ||
const C = x; | ||
return n && !C ? S( | ||
a, | ||
null, | ||
new Error("Fetch response error nullable in: fetch(..., { strict: true })") | ||
) : S(a, x, new Error("Fetch response not ok")); | ||
) : S(a, C, new Error("Fetch response not ok")); | ||
} catch (a) { | ||
const R = h.signal.aborted && h.signal.reason === J; | ||
const F = h.signal.aborted && h.signal.reason === J; | ||
return w(a, { | ||
aborted: h.signal.aborted, | ||
timeout: R | ||
timeout: F | ||
}); | ||
@@ -196,3 +196,3 @@ } | ||
} | ||
}, B = (e, t) => Object.assign(m(t), { | ||
}, V = (e, t) => Object.assign(m(t), { | ||
aborted: !1, | ||
@@ -230,3 +230,3 @@ timeout: !1, | ||
response: new Response() | ||
}), O = (e, t) => { | ||
}), T = (e, t) => { | ||
const n = "_initFetch" in e ? e._initFetch : e, r = "_composedFetch" in e ? e._composedFetch : (o) => o, s = (o) => r(t ? t(o) : o), i = async (o, g) => { | ||
@@ -243,12 +243,12 @@ const f = (c, d) => (c = p(c), n(c + o, { ...d, ...g })); | ||
_composedFetch: s, | ||
_create: (o) => O(i, o) | ||
_create: (o) => T(i, o) | ||
}); | ||
}, V = (e, t) => O(e, (n) => (r, s) => { | ||
}, W = (e, t) => T(e, (n) => (r, s) => { | ||
const i = typeof t == "function" ? t(s) : t; | ||
return n(r, j(s, i)); | ||
}), X = (e, t) => O(e, (n) => (r, s) => (r = p(r), n( | ||
}), X = (e, t) => T(e, (n) => (r, s) => (r = p(r), n( | ||
typeof t == "function" ? t(r) : r + p(t), | ||
s | ||
))), ee = (e, t) => X(e, (n) => t + n), te = (e) => { | ||
const t = (n) => V(e, { method: n }); | ||
const t = (n) => W(e, { method: n }); | ||
return { | ||
@@ -270,3 +270,3 @@ method: t, | ||
fetch: (i, u) => { | ||
const o = z( | ||
const o = B( | ||
() => typeof n == "function" ? n(i) : n | ||
@@ -294,10 +294,13 @@ ); | ||
}, se = (e) => (t, n) => e(t, n).then((r) => r.unwrap()), oe = (e) => { | ||
var t; | ||
if (e.failed) | ||
throw I(e.error, (t = e.response) == null ? void 0 : t.status); | ||
throw I(e); | ||
return e.value; | ||
}, I = (e, t) => Object.assign(new Error("ADNF: SWRError"), { | ||
type: e, | ||
status: t ?? null | ||
}); | ||
}, I = (e) => { | ||
var t; | ||
return Object.assign(e.error, { | ||
result: e, | ||
errorType: e.errorType, | ||
status: ((t = e.response) == null ? void 0 : t.status) ?? null | ||
}); | ||
}; | ||
export { | ||
@@ -318,6 +321,6 @@ m as Result, | ||
re as withDeclarations, | ||
O as withFetch, | ||
T as withFetch, | ||
te as withMethods, | ||
V as withOptions, | ||
W as withOptions, | ||
X as withResource | ||
}; |
{ | ||
"name": "adnf", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "files": [ |
@@ -5,3 +5,2 @@ # 🐕 ADNF | ||
- [🐕 ADNF](#-adnf) | ||
- [Overview](#overview) | ||
@@ -54,15 +53,10 @@ - [The FetchResult](#the-fetchresult) | ||
result.errorType // "NoFlower" | null | undefined | ||
result.type // alias for errorType | ||
result.aborted // fetch was aborted | ||
result.timeout // fetch was aborted due to a timeout | ||
result.resolved // fetch was able to resolve to a request | ||
// Unwrap your value: | ||
// Unwrap your value | ||
result.unwrap() // (throws Error) | Flower | null | ||
result.notNullable() // (throws Error) | Flower | ||
result.log() // logs value or error if present | ||
// Or handle your cases: | ||
// Response data | ||
result.data // Flower | null | 'NoFlower' | ||
// Success data | ||
if (result.success) { | ||
@@ -73,8 +67,17 @@ result // Success<Flower[]> | ||
// Error cases | ||
if (result.failed) { | ||
if (result.response) return result // ErrResponse | ||
result // Err | ||
result.error // Error | ||
result.message // string | undefined | ||
// Error response | ||
result.type // alias for errorType | ||
result.errorType // "NoFlower" | null | undefined | ||
result.error // the thrown Error object | ||
result.message // string | ||
} | ||
result.aborted // fetch was aborted | ||
result.timeout // fetch was aborted due to a timeout | ||
result.resolved // fetch was able to resolve to a request | ||
``` | ||
@@ -186,3 +189,3 @@ | ||
Declares fetches instead of running them immediately. Helps with prepared fetches, creating services and generating an identifier hash `key`. | ||
Declares fetches instead of running them immediately. Helps with prepared fetches, creating services and generating an cache identifier `key`. | ||
@@ -189,0 +192,0 @@ ```tsx |
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
42280
638
334