Comparing version 0.0.15 to 0.0.16
@@ -23,3 +23,3 @@ declare type AbortControllerGroup = { | ||
declare type Declare<F extends Fetch> = <V = unknown, E = unknown, A = void>(resource: string, options?: FetchOptions | ((args: A) => FetchOptions)) => InferResult<F> extends FetchResult ? Declaration<FetchResult<V, E>, never, A> : Declaration<V, null | E, A>; | ||
declare type Declare<F extends Fetch> = <V = unknown, E = unknown, A = void>(resource: Resource, options?: FetchOptions | ((args: A) => FetchOptions)) => InferResult<F> extends FetchResult ? Declaration<FetchResult<V, E>, never, A> : Declaration<V, null | E, A>; | ||
@@ -29,16 +29,20 @@ /** | ||
*/ | ||
declare type DeclareFetch = <F extends Fetch>(fetch: F) => Declare<F>; | ||
declare type DeclareFetch = <F extends ResultFetch>(fetch: F) => Declare<F>; | ||
declare type Dependent<F extends Fetch> = F & { | ||
_create: (create: DependFetch) => Dependent<F>; | ||
_composedFetch: DependFetch; | ||
_initFetch: Fetch; | ||
declare type Dependent<F extends ResultFetch> = F & { | ||
_create: (create: DependResultFetch) => Dependent<F>; | ||
_composedFetch: DependResultFetch; | ||
_initFetch: ResultFetch; | ||
}; | ||
declare type DependFetch = (fetch: Fetch) => Fetch; | ||
declare type DependResultFetch = (fetch: ResultFetch) => ResultFetch; | ||
declare type Err<E> = { | ||
declare type Err<T> = { | ||
value: undefined; | ||
error: E; | ||
data: T; | ||
type: T; | ||
failed: true; | ||
errorType: T; | ||
error: Error; | ||
message: string; | ||
success: false; | ||
@@ -50,3 +54,3 @@ unwrap: () => never; | ||
export declare type Fetch<V = unknown, E = unknown> = (resource: string, options?: FetchOptions) => PromiseWithError<V, E>; | ||
export declare type Fetch<V = unknown, E = unknown> = (resource: Resource, options?: FetchOptions) => PromiseWithError<V, E>; | ||
@@ -105,28 +109,32 @@ export declare type FetchDeclaration<V, E, A = void> = Declaration<V, E, A>; | ||
declare type PreparedFetch<V = unknown, E = unknown, A = unknown> = (args: A, options?: FetchOptions) => PromiseWithError<V, E>; | ||
/** | ||
* Types | ||
*/ | ||
declare type PromiseWithError<V = unknown, E = never> = Promise<V> & { | ||
__error?: E; | ||
}; | ||
/** | ||
* Append search params to resource or complete URL. | ||
* | ||
* ```ts | ||
* respectParams('/user', { id: 'a' }) | ||
* params('/user', { id: 'a' }) | ||
* // /user?id=a | ||
* respectParams('https://github.com/user', { id: 'a' }) | ||
* params('https://github.com/user', { id: 'a' }) | ||
* // https://github.com/user?id=a | ||
* respectParams('https://github.com/user?id=a&for=b', { id: 'b' }) | ||
* 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; | ||
declare const params: (path: string, params: Record<string, any>, replace?: boolean) => string; | ||
export { params } | ||
export { params as respectParams } | ||
declare type PreparedFetch<V = unknown, E = unknown, A = unknown> = (args: A, options?: FetchOptions) => PromiseWithError<V, E>; | ||
/** | ||
* Types | ||
*/ | ||
declare type PromiseWithError<V = unknown, E = never> = Promise<V> & { | ||
__error?: E; | ||
}; | ||
declare type Resource = string | string[]; | ||
export declare const Result: (<V>(value: V) => Success<V>) & { | ||
Success: <V>(value: V) => Success<V>; | ||
Err: <ET>(error: ET, message?: unknown) => Err<ET>; | ||
Err: <ET>(type: ET, error?: Error) => Err<ET>; | ||
}; | ||
@@ -136,5 +144,5 @@ | ||
export declare const ResultErr: (message: string, props?: Partial<FetchErr>) => FetchErr; | ||
export declare const ResultErr: (error?: unknown, props?: Partial<FetchErr>) => FetchErr; | ||
declare type ResultFetch = <V = unknown, E = unknown>(resource: string, options?: FetchOptions) => Promise<FetchResult<V, E>>; | ||
declare type ResultFetch = <V = unknown, E = unknown>(resource: Resource, options?: FetchOptions) => Promise<FetchResult<V, E>>; | ||
@@ -150,3 +158,7 @@ /** | ||
value: V; | ||
data: V; | ||
type: undefined; | ||
errorType: undefined; | ||
error: undefined; | ||
message: undefined; | ||
failed: false; | ||
@@ -181,9 +193,9 @@ success: true; | ||
*/ | ||
export declare const withBase: <F extends Fetch>(fetch: F, base: string) => F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
export declare const withBase: <F extends ResultFetch>(fetch: F, base: string) => F & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
@@ -200,9 +212,9 @@ }; | ||
*/ | ||
export declare const withFetch: <F extends Fetch>(prevFetch: F | Dependent<F>, depend?: DependFetch) => F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
export declare const withFetch: <F extends ResultFetch>(prevFetch: F | Dependent<F>, depend?: DependResultFetch) => F & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
@@ -214,73 +226,73 @@ }; | ||
*/ | ||
export declare const withMethods: <F extends Fetch>(fetch: F) => { | ||
export declare const withMethods: <F extends ResultFetch>(fetch: F) => { | ||
method: (method: string) => F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
get: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
head: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
post: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
put: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
delete: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
del: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
}; | ||
patch: F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
@@ -290,9 +302,9 @@ }; | ||
export declare const withOptions: <F extends Fetch>(fetch: F, options: FetchOptions | ((options?: FetchOptions) => FetchOptions)) => F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
export declare const withOptions: <F extends ResultFetch>(fetch: F, options: FetchOptions | ((options?: FetchOptions) => FetchOptions)) => F & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
@@ -304,9 +316,9 @@ }; | ||
*/ | ||
export declare const withResource: <F extends Fetch>(fetch: F, resource: string | ((resource: string) => string)) => F & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & { | ||
_initFetch: Fetch<unknown, unknown>; | ||
_composedFetch: (fetch: Fetch) => Fetch<unknown, unknown>; | ||
_create: (create: DependFetch) => Fetch & any; | ||
export declare const withResource: <F extends ResultFetch>(fetch: F, resource: Resource | ((resource: Resource) => string)) => F & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & { | ||
_initFetch: ResultFetch; | ||
_composedFetch: (fetch: ResultFetch) => ResultFetch; | ||
_create: (create: DependResultFetch) => ResultFetch & any; | ||
}; | ||
@@ -313,0 +325,0 @@ }; |
351
dist/adnf.js
@@ -1,17 +0,17 @@ | ||
const x = (e) => "File" in window && e instanceof File, M = (e) => e instanceof FormData, C = (e) => { | ||
const C = (e) => "File" in window && e instanceof File, H = (e) => e instanceof FormData, _ = (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 (C(r)) | ||
return t.set(n, r); | ||
if (Array.isArray(r) && r.length && r.every((s) => C(s))) | ||
return r.forEach((s) => t.append(`${n}[]`, s)); | ||
if (typeof r == "object" && r !== null || Array.isArray(r)) { | ||
try { | ||
const o = JSON.stringify(n); | ||
t.set(r, o); | ||
const s = JSON.stringify(r); | ||
t.set(n, s); | ||
} catch { | ||
@@ -26,5 +26,5 @@ } | ||
"abort", | ||
(r) => { | ||
const n = r; | ||
t.abort(n.currentTarget.reason); | ||
(n) => { | ||
const r = n; | ||
t.abort(r.currentTarget.reason); | ||
}, | ||
@@ -36,23 +36,21 @@ { | ||
); | ||
}, R = (e = {}, t = {}, r = !0) => { | ||
r && (e.group && t.group || e.form && t.form || e.files && t.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 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; | ||
}, W = (e, t, r = !1) => { | ||
const n = new URL(e, "https://developer.mozilla.org"), o = Object.fromEntries(n.searchParams), s = Object.fromEntries(new URLSearchParams(t)), a = new URLSearchParams( | ||
r ? s : { ...o, ...s } | ||
).toString(), c = a ? `?${a}` : ""; | ||
return G(e) ? `${n.origin}${n.pathname}${c}` : `${n.pathname}${c}`; | ||
}, G = (e) => { | ||
}), r; | ||
}, k = (e, t, n = !1) => { | ||
const r = new URL(e, "https://developer.mozilla.org"), s = Object.fromEntries(r.searchParams), i = Object.fromEntries(new URLSearchParams(t)), 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(e) ? `${r.origin}${r.pathname}${c}` : `${r.pathname}${c}`; | ||
}, p = (e) => (Array.isArray(e) ? e : [e]).join("/"), M = (e) => { | ||
try { | ||
@@ -63,60 +61,78 @@ return !!new URL(e); | ||
} | ||
}, H = (e) => ["post", "put", "delete", "patch"].includes(e.toLowerCase()), q = (e) => e == null, z = (e) => { | ||
if (q(e)) | ||
}, W = (e) => e == null, q = (e) => { | ||
if (W(e)) | ||
throw new Error("Non-nullable assertion failed"); | ||
return e; | ||
}, _ = (e) => ({ | ||
}, U = (e) => ({ | ||
value: e, | ||
data: e, | ||
failed: !1, | ||
success: !0, | ||
type: void 0, | ||
errorType: void 0, | ||
error: void 0, | ||
message: void 0, | ||
unwrap: () => e, | ||
log: () => void console.log(e), | ||
notNullable: () => z(e) | ||
}), m = Object.assign(_, { | ||
Success: _, | ||
Err: (e, t) => ({ | ||
value: void 0, | ||
error: e, | ||
failed: !0, | ||
success: !1, | ||
log: () => void console.error({ type: e, message: t }), | ||
unwrap: () => { | ||
throw typeof e == "string" ? new Error(e) : typeof t == "string" ? new Error(t) : (console.error("Result.error: ", { error: e, message: t }), new Error("Result.error: Unknown error")); | ||
}, | ||
notNullable: () => { | ||
throw new Error("Result.distinct: Nullable value or error"); | ||
} | ||
}) | ||
}), Z = async (e, t) => { | ||
notNullable: () => q(e) | ||
}), m = Object.assign(U, { | ||
Success: U, | ||
Err: (e, t = new Error("Result.Err: No error provided")) => { | ||
const n = () => void console.error({ type: e, error: t, message: t == null ? void 0 : t.message }); | ||
return { | ||
value: void 0, | ||
data: e, | ||
type: e, | ||
errorType: e, | ||
error: t, | ||
message: t.message, | ||
failed: !0, | ||
success: !1, | ||
log: n, | ||
unwrap: () => { | ||
throw n(), t; | ||
}, | ||
notNullable: () => { | ||
throw new Error("Result.distinct: Nullable value or error"); | ||
} | ||
}; | ||
} | ||
}), z = (e) => { | ||
try { | ||
return m.Success(e()); | ||
} catch (t) { | ||
return m.Err(null, P(t) ? t : void 0); | ||
} | ||
}, P = (e) => e instanceof Error, Q = async (e, t) => { | ||
const { | ||
strict: r = !0, | ||
fetchSymbol: n = Symbol(), | ||
fetch: o = window.fetch, | ||
abortPrevious: s = !1, | ||
strict: n = !0, | ||
fetchSymbol: r = Symbol(), | ||
fetch: s = window.fetch, | ||
abortPrevious: i = !1, | ||
group: u, | ||
label: a, | ||
parse: c = JSON.parse, | ||
stringify: g = JSON.stringify, | ||
label: o, | ||
parse: g = JSON.parse, | ||
stringify: f = JSON.stringify, | ||
// data | ||
data: b, | ||
form: f, | ||
params: U, | ||
files: w, | ||
timeout: O, | ||
unwrap: Y, | ||
data: c, | ||
form: d, | ||
params: y, | ||
files: E, | ||
timeout: T, | ||
unwrap: K, | ||
...l | ||
} = t ?? {}; | ||
e = p(e); | ||
try { | ||
const h = (i) => { | ||
const b = (a) => { | ||
l.headers = { | ||
...l.headers, | ||
...i | ||
...a | ||
}; | ||
}; | ||
a && h({ | ||
"X-Request-Label": a | ||
o && b({ | ||
"X-Request-Label": o | ||
}); | ||
const d = new AbortController(); | ||
if (s) { | ||
const h = new AbortController(); | ||
if (i) { | ||
if (!u) | ||
@@ -128,45 +144,45 @@ throw new Error( | ||
} | ||
if (l.signal && L(l.signal, d), l.signal = d.signal, u) { | ||
const i = u.add(); | ||
L(i.signal, d); | ||
if (l.signal && L(l.signal, h), l.signal = h.signal, u) { | ||
const a = u.add(); | ||
L(a.signal, h); | ||
} | ||
const N = typeof O == "number" ? setTimeout(() => d.abort(k), O) : null; | ||
if (b && (l.body = g(b), h({ | ||
const N = typeof T == "number" ? setTimeout(() => h.abort(J), T) : null; | ||
if (c && (l.body = f(c), b({ | ||
"Content-Type": "application/json" | ||
})), f || w) { | ||
})), d || E) { | ||
if (l.body) | ||
throw new Error("Fetch provided multiple values for body. Pick data, files or form."); | ||
f && (l.body = M(f) ? f : C(f)), w && (l.body = C(w)), h({ | ||
d && (l.body = H(d) ? d : _(d)), E && (l.body = _(E)), b({ | ||
"Content-Type": "multipart/form-data" | ||
}); | ||
} | ||
const J = W(e, U ?? {}); | ||
const G = k(e, y ?? {}); | ||
try { | ||
const i = await o(J, l), y = i.ok; | ||
const a = await s(G, l), R = a.ok; | ||
N && clearTimeout(N); | ||
const E = i.headers.get("content-type"), P = E == null ? void 0 : E.includes("application/json"); | ||
if (r && !P) | ||
return F( | ||
i, | ||
const F = a.headers.get("content-type"), A = F == null ? void 0 : F.includes("application/json"); | ||
if (n && !A) | ||
return S( | ||
a, | ||
null, | ||
"Fetch response not json in: fetch(..., { strict: true })" | ||
new Error("Fetch response not json in: fetch(..., { strict: true })") | ||
); | ||
const T = await i.text(), A = P ? c(T) : T; | ||
if (y) | ||
return B(i, A); | ||
const D = A; | ||
return r && !D ? F( | ||
i, | ||
const D = await a.text(), $ = A ? g(D) : D; | ||
if (R) | ||
return B(a, $); | ||
const x = $; | ||
return n && !x ? S( | ||
a, | ||
null, | ||
"Fetch response error nullable in: fetch(..., { strict: true })" | ||
) : F(i, D); | ||
} catch (i) { | ||
const y = d.signal.aborted && d.signal.reason === k; | ||
return $(i, { | ||
aborted: d.signal.aborted, | ||
timeout: y | ||
new Error("Fetch response error nullable in: fetch(..., { strict: true })") | ||
) : S(a, x, new Error("Fetch response not ok")); | ||
} catch (a) { | ||
const R = h.signal.aborted && h.signal.reason === J; | ||
return w(a, { | ||
aborted: h.signal.aborted, | ||
timeout: R | ||
}); | ||
} | ||
} catch (h) { | ||
return $(h, { | ||
} catch (b) { | ||
return w(b, { | ||
aborted: !1, | ||
@@ -181,15 +197,18 @@ timeout: !1 | ||
response: e | ||
}), F = (e, t, r, n = {}) => Object.assign(m.Err(t, r), { | ||
}), S = (e, t, n, r = {}) => Object.assign(m.Err(t, n), { | ||
aborted: !1, | ||
timeout: !1, | ||
resolved: !0, | ||
...n, | ||
...r, | ||
response: e | ||
}), $ = (e, t = {}) => Object.assign(m.Err(null, e), { | ||
aborted: !1, | ||
timeout: !1, | ||
resolved: !1, | ||
...t, | ||
response: void 0 | ||
}), k = Symbol("Timeout"), v = async (e, t) => (console.log( | ||
}), w = (e, t = {}) => { | ||
const n = P(e) ? e : new Error("ResultErr: Provided error is on instance of Error()"); | ||
return Object.assign(m.Err(null, n), { | ||
aborted: !1, | ||
timeout: !1, | ||
resolved: !1, | ||
...t, | ||
response: void 0 | ||
}); | ||
}, J = Symbol("Timeout"), Y = async (e, t) => (console.log( | ||
"%c🐕 " + ((t == null ? void 0 : t.method) || "GET").toUpperCase(), | ||
@@ -203,18 +222,28 @@ "background: #85ce47; border-radius: 999px; font-weight: 600; padding: 1.5px 5.5px; color: black", | ||
response: new Response() | ||
})), ee = async (e, t) => Object.assign(m({}), { | ||
})), Z = async (e, t) => Object.assign(m({}), { | ||
aborted: !1, | ||
timeout: !1, | ||
response: new Response() | ||
}), j = (e, t) => { | ||
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, c) => o((b, f) => r(b + a, { ...f, ...c }))("", {}); | ||
return Object.assign(s, { | ||
_initFetch: r, | ||
_composedFetch: o, | ||
_create: (a) => j(s, a) | ||
}), O = (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) => { | ||
const f = (c, d) => (c = p(c), n(c + o, { ...d, ...g })); | ||
try { | ||
return s(f)("", {}); | ||
} catch (c) { | ||
return w(P(c) ? c : void 0); | ||
} | ||
}; | ||
return Object.assign(i, { | ||
_initFetch: n, | ||
_composedFetch: s, | ||
_create: (o) => O(i, o) | ||
}); | ||
}, V = (e, t) => j(e, (r) => (n, o) => { | ||
const s = typeof t == "function" ? t(o) : t; | ||
return r(n, R(o, s)); | ||
}), X = (e, t) => j(e, (r) => (n, o) => r(typeof t == "function" ? t(n) : n + t, o)), te = (e, t) => X(e, (r) => t + r), re = (e) => { | ||
const t = (r) => V(e, { method: r }); | ||
}, V = (e, t) => O(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( | ||
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 }); | ||
return { | ||
@@ -230,44 +259,19 @@ method: t, | ||
}; | ||
}, p = /* @__PURE__ */ new WeakMap(); | ||
let I = 0; | ||
function S(e) { | ||
const t = typeof e, r = e && e.constructor, n = r == Date; | ||
if (Object(e) === e && !n && r != RegExp) { | ||
let o = p.get(e); | ||
if (o) | ||
return o; | ||
o = ++I + "~", p.set(e, o); | ||
let s; | ||
if (r == Array) { | ||
for (o = "@", s = 0; s < e.length; s++) | ||
o += S(e[s]) + ","; | ||
p.set(e, o); | ||
} else if (r == Object) { | ||
o = "#"; | ||
const u = Object.keys(e).sort(); | ||
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; | ||
} | ||
const ne = (e) => (t, r) => { | ||
const n = typeof r == "function", o = K(t, typeof r == "function" ? {} : r); | ||
}, re = (e) => (t, n) => { | ||
t = p(t); | ||
const r = v(t, typeof n == "function" ? {} : n); | ||
return { | ||
key: o, | ||
fetch: (u, a) => { | ||
const c = typeof r == "function" ? r(u) : r, g = R(c, { | ||
key: o, | ||
method: n ? c != null && c.method && H(c.method) ? c.method : "post" : r == null ? void 0 : r.method | ||
}); | ||
return e(t, R(g, a)); | ||
key: r, | ||
fetch: (i, u) => { | ||
const o = z( | ||
() => typeof n == "function" ? n(i) : n | ||
); | ||
if (o.failed) | ||
return w(o.error); | ||
const g = o.value, f = j(g, { key: r }); | ||
return e(t, j(f, u)); | ||
}, | ||
resource: t | ||
}; | ||
}, K = (e, t) => { | ||
const r = [e, { params: (t == null ? void 0 : t.params) ?? {} }]; | ||
return S(r); | ||
}, oe = () => { | ||
}, v = (e, t) => k(e, (t == null ? void 0 : t.params) ?? {}), ne = () => { | ||
const e = /* @__PURE__ */ new Set(); | ||
@@ -284,8 +288,8 @@ return { | ||
}; | ||
}, se = (e) => (t, r) => e(t, r).then((n) => n.unwrap()), ae = (e) => { | ||
}, se = (e) => (t, n) => e(t, n).then((r) => r.unwrap()), oe = (e) => { | ||
var t; | ||
if (e.failed) | ||
throw Q(e.error, (t = e.response) == null ? void 0 : t.status); | ||
throw I(e.error, (t = e.response) == null ? void 0 : t.status); | ||
return e.value; | ||
}, Q = (e, t) => Object.assign(new Error("ADNF: SWRError"), { | ||
}, I = (e, t) => Object.assign(new Error("ADNF: SWRError"), { | ||
type: e, | ||
@@ -296,18 +300,19 @@ status: t ?? null | ||
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 swrResult, | ||
w as ResultErr, | ||
ne as createAbortGroup, | ||
Y as debugFetch, | ||
Q as fetch, | ||
j as mergeOptions, | ||
k as params, | ||
k as respectParams, | ||
Q as resultFetch, | ||
oe as swrResult, | ||
se as unwrap, | ||
ee as voidFetch, | ||
te as withBase, | ||
ne as withDeclarations, | ||
j as withFetch, | ||
re as withMethods, | ||
Z as voidFetch, | ||
ee as withBase, | ||
re as withDeclarations, | ||
O as withFetch, | ||
te as withMethods, | ||
V as withOptions, | ||
X as withResource | ||
}; |
{ | ||
"name": "adnf", | ||
"version": "0.0.15", | ||
"version": "0.0.16", | ||
"type": "module", | ||
@@ -38,3 +38,3 @@ "files": [ | ||
"swr": "^2.2.5", | ||
"typescript": "^5.2.2", | ||
"typescript": "^5.5.3", | ||
"vite": "^4.4.5", | ||
@@ -41,0 +41,0 @@ "vite-plugin-dts": "^3.5.2", |
@@ -19,5 +19,4 @@ # 🐕 ADNF | ||
- [Helpers](#helpers) | ||
- [`unwrap` and `swr`](#unwrap-and-swr) | ||
- [`createAbortGroup`](#createabortgroup) | ||
- [`respectParams`](#respectparams) | ||
- [`params`](#params) | ||
- [Recipes](#recipes) | ||
@@ -55,3 +54,5 @@ - [Resources](#resources) | ||
result.error // "NoFlower" | null | undefined | ||
result.errorType // "NoFlower" | null | undefined | ||
result.type // alias for errorType | ||
result.aborted // fetch was aborted | ||
@@ -75,2 +76,4 @@ result.timeout // fetch was aborted due to a timeout | ||
result // Err | ||
result.error // Error | ||
result.message // string | undefined | ||
} | ||
@@ -186,10 +189,34 @@ ``` | ||
```tsx | ||
import { fetch, withDeclarations, params } from 'adnf' | ||
const declare = withDeclarations(fetch) | ||
const fetchUser = id => declare('/user', { params: { id } }) | ||
// Declare GET fetch | ||
const declaration = fetchUser('a') | ||
const getUser = (id: string) => declare<{}, 'Unauthorized'>('/user', { params: { id } }) | ||
declaration.key // "@"/user",#params:#id:"a",,," | ||
const declaration = getUser('a') | ||
declaration.key // /user?id=a | ||
declaration.fetch() // run fetch as usual | ||
declare('/user', { params: { id } }).key // /user?id=a | ||
declare('/user', () => ({ params: { id } })).key // /user | ||
declare(params('/user', { id })).key // /user?id=a | ||
declare(`/user/${id}`).key // /user/a | ||
declare(['/user', id]).key // /user/a | ||
// Declare mutative fetch | ||
import { params } from 'adnf' | ||
const editFlower = (id: string) => | ||
declare<{}, 'Unauthorized', Partial<Flower>>(params('/flower', { id }), flower => ({ | ||
method: 'put', | ||
data: flower, | ||
})) | ||
const declaration = editFlower('tulip') | ||
declaration.key // /flower?id=tulip | ||
``` | ||
@@ -219,15 +246,2 @@ | ||
#### `unwrap` | ||
If you’re working with a library that expects errors to be thrown, use unwrap. Unwrapping fetch does not restore the Fetch API 1:1, primarily differences in behavior due to ResultFetch’s strict option. | ||
```tsx | ||
import { fetch as resultFetch, unwrap } from "adnf" | ||
const fetch = unwrap(resultFetch) | ||
const result = await = resultFetch<User>("/me") // FetchResult<User, unkown> | ||
const user = await = fetch<User>("/me") // User | ||
``` | ||
#### `createAbortGroup` | ||
@@ -251,14 +265,14 @@ | ||
#### `respectParams` | ||
#### `params` | ||
Merge/replace search params to resource or complete URL. Will respect provided format. | ||
`respectParams(path: string, params, replace: boolean)` | ||
`params(path: string, params, replace: boolean)` | ||
```ts | ||
respectParams('/user', { id: 'a' }) | ||
params('/user', { id: 'a' }) | ||
// /user?id=a | ||
respectParams('https://github.com/user?id=a&for=b', { id: 'b' }) | ||
params('https://github.com/user?id=a&for=b', { id: 'b' }) | ||
// https://github.com/user?id=b&for=b | ||
respectParams('https://github.com/user?id=a&for=b', { id: 'b' }, true) | ||
params('https://github.com/user?id=a&for=b', { id: 'b' }, true) | ||
// https://github.com/user?id=b | ||
@@ -269,25 +283,2 @@ ``` | ||
#### Last wins and First wins | ||
```tsx | ||
import { fetch, createAbortGroup, ResultErr } from 'adnf' | ||
// LWW | ||
const group = createAbortGroup() | ||
fetch('/a', { group, abortPrevious: true }) // Err | ||
fetch('/b', { group, abortPrevious: true }) // Success | ErrResponse | ||
// FWW | ||
const group = createAbortGroup() | ||
const patientFetch = resource => { | ||
if (group.controllers.length) return ResultErr({ aborted: true }) | ||
group.fetch(resource, { group }) | ||
} | ||
patientFetch('/a') // will resolve | ||
patientFetch('/b') // will not resolve if "/a" fetch did not finish | ||
``` | ||
#### Fetch logger | ||
@@ -294,0 +285,0 @@ |
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
634
42192
7
331