@kakadu/components 2.4.1 → 2.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
type PromiseOrValue<Type> = Type | Promise<Type>;
|
|
2
|
+
export declare function useFetcher<Type = Response>(handler?: (response: Response) => PromiseOrValue<Type | undefined>): {
|
|
2
3
|
isLoading: boolean;
|
|
3
|
-
fetch: (url: string, options?: RequestInit) => Promise<Type>;
|
|
4
|
+
fetch: (url: string, options?: RequestInit) => Promise<Type | undefined>;
|
|
4
5
|
response: Type | undefined;
|
|
5
6
|
error: Error | undefined;
|
|
6
7
|
};
|
|
7
|
-
export declare function useJsonFetcher<Type>(handler?: (json: unknown) => Type |
|
|
8
|
+
export declare function useJsonFetcher<Type = unknown>(handler?: (json: unknown) => PromiseOrValue<Type | undefined>): {
|
|
8
9
|
isLoading: boolean;
|
|
9
|
-
fetch: (url: string, options?: RequestInit) => Promise<Type>;
|
|
10
|
+
fetch: (url: string, options?: RequestInit) => Promise<Type | undefined>;
|
|
10
11
|
response: Type | undefined;
|
|
11
12
|
error: Error | undefined;
|
|
12
13
|
};
|
|
@@ -15,3 +16,4 @@ export default function useFetch<Type>(url?: string, options?: RequestInit): {
|
|
|
15
16
|
response: Type | undefined;
|
|
16
17
|
error: Error | undefined;
|
|
17
18
|
};
|
|
19
|
+
export {};
|
package/build/hooks/use-fetch.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const r=require("react"),w=new Map,S=new Map,m=new Map;function M(e,s){let t=m.get(e);return t||(t=new Set,m.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&m.delete(e)}}function C(e,s){S.set(e,{timestamp:Date.now(),response:s});const t=m.get(e);if(t)for(const a of t)a(s)}function E(e){const[s,t]=r.useState(!1),[a,o]=r.useState(),[i,l]=r.useState(),u=r.useCallback(async(g,f)=>{try{l(void 0),t(!0);const d=await fetch(g,f),n=e?await e(d):d;return o(n),n}catch(d){const n=d instanceof Error?d:new Error("Unknown error");throw l(n),n}finally{t(!1)}},[e]);return r.useMemo(()=>({isLoading:s,fetch:u,response:a,error:i}),[s,u,a,i])}function b(e){return E(async s=>{const t=await s.json();return e?
|
|
1
|
+
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const r=require("react"),w=new Map,S=new Map,m=new Map;function M(e,s){let t=m.get(e);return t||(t=new Set,m.set(e,t)),t.add(s),()=>{t==null||t.delete(s),t&&t.size===0&&m.delete(e)}}function C(e,s){S.set(e,{timestamp:Date.now(),response:s});const t=m.get(e);if(t)for(const a of t)a(s)}function E(e){const[s,t]=r.useState(!1),[a,o]=r.useState(),[i,l]=r.useState(),u=r.useCallback(async(g,f)=>{try{l(void 0),t(!0);const d=await fetch(g,f),n=e?await e(d):d;return o(n),n}catch(d){const n=d instanceof Error?d:new Error("Unknown error");throw l(n),n}finally{t(!1)}},[e]);return r.useMemo(()=>({isLoading:s,fetch:u,response:a,error:i}),[s,u,a,i])}function b(e){return E(async s=>{const t=await s.json();return e?e(t):t})}function y(e,s){const[t,a]=r.useState(()=>!!e),o=r.useMemo(()=>e?JSON.stringify({url:e,options:s}):"",[e,s]),i=r.useCallback(()=>{var n;return o?(n=S.get(o))==null?void 0:n.response:void 0},[o]),[l,u]=r.useState(()=>i()),[g,f]=r.useState(),d=r.useMemo(()=>{var n;return((n=s==null?void 0:s.method)==null?void 0:n.toUpperCase())??"GET"},[s]);return r.useEffect(()=>{if(!e||!o){u(void 0);return}const n=i();n===void 0?(u(void 0),f(void 0)):(u(n),f(void 0));const h=M(o,v=>{u(v),f(void 0)});return()=>{h()}},[e,o,i]),r.useEffect(()=>{if(!e||!o){a(!1);return}const n=d==="GET";if(n&&i()!==void 0){a(!1);return}let h=!0;f(void 0),a(!0);let v=n&&w.has(o)?w.get(o):void 0;if(!v){const c=fetch(e,s);w.set(o,c),c.then(()=>{w.delete(o)}),v=c}return v.then(async c=>c.json()).then(c=>{h&&(n&&C(o,c),u(c),f(void 0))}).catch(c=>{h&&f(c)}).finally(()=>{h&&a(!1)}),()=>{h=!1}},[e,s,o,d,i]),r.useMemo(()=>({isLoading:t,response:l,error:g}),[t,l,g])}exports.default=y;exports.useFetcher=E;exports.useJsonFetcher=b;
|