Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rwsbillyang/usecache

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rwsbillyang/usecache - npm Package Compare versions

Comparing version 0.9.7 to 0.9.8

2

dist/esm/index.js

@@ -1,1 +0,1 @@

var Q=Object.defineProperty,X=Object.defineProperties;var Y=Object.getOwnPropertyDescriptors;var R=Object.getOwnPropertySymbols;var Z=Object.prototype.hasOwnProperty,ee=Object.prototype.propertyIsEnumerable;var _=(t,e,n)=>e in t?Q(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,$=(t,e)=>{for(var n in e||(e={}))Z.call(e,n)&&_(t,n,e[n]);if(R)for(var n of R(e))ee.call(e,n)&&_(t,n,e[n]);return t},q=(t,e)=>X(t,Y(e));var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},te=()=>window.location.protocol+"//"+window.location.host;var f={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0;if(e===l.NONE)return n;let a,s=o.cacheKeyPrefix()+t;return e===l.OnlySessionStorage?a=sessionStorage.getItem(s):e===l.OnlyLocalStorage?a=localStorage.getItem(s):e===l.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),o.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${n}`),a||n},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0,a=f.getItem(t,e);return a?JSON.parse(a):n},saveItem:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;if(n===l.NONE)return;let a=o.cacheKeyPrefix()+t;n===l.OnlySessionStorage?sessionStorage.setItem(a,e):n===l.OnlyLocalStorage?localStorage.setItem(a,e):n===l.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),o.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}, v=${e}`)},saveObject:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;return f.saveItem(t,JSON.stringify(e),n)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage;if(e===l.NONE)return;let n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))}};var F={findOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return r[i]}}},findMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let u=0;u<e.length;u++)d[s]===e[u]&&r.push(d)}return r}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultStorageType;if(n===l.NONE)return;let a=f.getItem(t,n);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),f.saveObject(t,s)):f.saveObject(t,[e])}else f.saveObject(t,[e]);console.log("onAddOne done")},onEditOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e[s]){o.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),r[i]=e,f.saveObject(t,r);return}o.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else o.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let u=0;u<r.length;u++)r[u][s]===d[s]&&(r[u]=d)}f.saveItem(t,JSON.stringify(r));return}else{f.saveItem(t,JSON.stringify(e));return}}else o.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return o.EnableLog&&console.log(`del one: ${s}=${e}`),r.splice(i,1),f.saveItem(t,JSON.stringify(r)),r}}},onDelOne:function(t,e){var n;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(s===l.NONE)return;let g=a||o.defaultIdentiyKey,r=(n=e[g])===null||n===void 0?void 0:n.toString();r?F.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+g+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let u=e[d];r[i][s]===u&&(o.EnableLog&&console.log(`del one: ${s}=${u}`),r.splice(i,1))}return f.saveItem(t,JSON.stringify(r)),r}}},onDelMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);g&&g.length>0?F.onDelManyByIds(t,g,n,a):console.log("onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:o.defaultStorageType,n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:o.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var p;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(p||(p={}));function C(t){if(t){if(t.code===p.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var B=t=>encodeURIComponent(JSON.stringify(t));import{useEffect as ne,useState as j}from"react";function oe(t,e,n){var a,s,g,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,u=arguments.length>5?arguments[5]:void 0,c=arguments.length>6?arguments[6]:void 0,y=arguments.length>7?arguments[7]:void 0;D(t,e,n,i,d||((a=o.request)===null||a===void 0?void 0:a.showToast),u||((s=o.request)===null||s===void 0?void 0:s.showToast),c||((g=o.request)===null||g===void 0?void 0:g.showLoading),y||((r=o.request)===null||r===void 0?void 0:r.hideLoading))}function D(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(n){let d=f.getObject(n,a);if(d)return o.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+n),e(d),!0;o.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+n)}return r&&r(),o.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{i&&i();let u=d.data;if(u.code===p.OK){let c=C(u);return c===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&f.saveItem(n,JSON.stringify(c),a),e(c)),!1}else return g&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+u.code+",msg="+u.msg),g(u.msg||u.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function re(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[g,r]=j(!1),[i,d]=j(),[u,c]=j();return ne(()=>{var y,L;r(!0);let v=n?(y=o.request)===null||y===void 0?void 0:y.getWithoutAuth:(L=o.request)===null||L===void 0?void 0:L.get;if(!v)console.warn("not config request promise, please set ConfigRequest firstly");else{var E,S,m,N;a&&(!((E=o.request)!==null&&E!==void 0&&E.showLoading)||!((S=o.request)!==null&&S!==void 0&&S.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),D(()=>v(t),O=>{r(!1),d(O)},e,s,()=>{r(!1),c("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},O=>{r(!1),c(O)},(m=o.request)===null||m===void 0?void 0:m.showLoading,(N=o.request)===null||N===void 0?void 0:N.hideLoading)}},[t]),{loading:g,entity:i,errMsg:u}}import{useEffect as ae,useRef as se,useState as b}from"react";function V(t){if(!t)return"";let e=t!=null&&t.pagination?q($({},t),{umi:B(t.pagination),sort:void 0,pagination:void 0}):q($({},t),{sort:void 0}),n=[];for(let a in e)if(a){let s=t[a];s==null||s===""?console.log("no value for item, ignore"):n.push(`${a}=${s}`)}return n.length>0?"?"+n.sort().join("&"):""}function ie(t,e,n){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,g=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[r,i]=b(),[d,u]=b(!0),[c,y]=b(!1),[L,v]=b(),[E,S]=b(e?W(e):!0),{current:m}=se({queryParamsStr:V(n),pageSize:(n==null||(a=n.pagination)===null||a===void 0?void 0:a.pageSize)||o.PageSize,useCache:!!e,isLoadMore:!1,refresh:0}),N=h=>m.queryParamsStr=V(h),O=h=>m.useCache=h,k=h=>m.isLoadMore=h,H=()=>m.refresh++,J=(h,ge,G,x)=>{var P;let K=t+m.queryParamsStr;o.EnableLog&&console.log("fetch from remote... url="+K);let U=(P=o.request)===null||P===void 0?void 0:P.get;if(!U){console.warn("please inject get request firstly");return}U(K).then(w=>{u(!1),O(!0);let I=w.data,T=C(I);if(I.code===p.OK)if(T){y(!1);let z=h&&r&&r.length>0?r.concat(T):T;if(i(z),e&&f.saveObject(e,z,g),s){let A=T.length>=G;S(A),e&&M(e,A)}}else y(!0),s&&(v("no data"),e&&M(e,!1));else y(!0),s&&(S(!1),e&&M(e,!1)),v(I.msg||I.code),o.EnableLog&&console.log("useCacheList: fail from remote server: code="+I.code+",msg="+I.msg);x&&x(T),k(!1)}).catch(w=>{O(!0),x&&x(),u(!1),y(!0),v(w.message),s&&(e&&M(e,!1),S(!1)),k(!1),o.EnableLog&&console.log("useCacheList exception from remote server:",w)})};return ae(()=>{if(o.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote"),u(!0),m.useCache&&e){let h=f.getObject(e,g);h?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(h),u(!1),S(W(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),J(m.isLoadMore,m.queryParamsStr,m.pageSize))}else o.EnableLog&&console.log("useCache=false, try from remote..."),J(m.isLoadMore,m.queryParamsStr,m.pageSize)},[t,m.queryParamsStr,m.refresh]),{isLoading:d,isError:c,errMsg:L,loadMoreState:E,list:r,setQuery:N,setList:i,fetchDataFromRemote:J,setRefresh:H,setUseCache:O,setIsLoadMore:k}}function W(t){let e=o.cacheKeyPrefix()+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function M(t,e){let n=o.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(n,e?"1":"0")}export{p as CODE,F as Cache,f as CacheStorage,l as StorageType,o as UseCacheConfig,te as currentHost,B as encodeUmi,oe as fetchWithLoading,D as genericFetch,C as getDataFromBox,re as useCache,ie as useCacheList};
var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var n={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},Q=()=>window.location.protocol+"//"+window.location.host;var u={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0;if(e===l.NONE)return o;let a,s=n.cacheKeyPrefix()+t;return e===l.OnlySessionStorage?a=sessionStorage.getItem(s):e===l.OnlyLocalStorage?a=localStorage.getItem(s):e===l.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),n.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${o}`),a||o},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,o=arguments.length>2?arguments[2]:void 0,a=u.getItem(t,e);return a?JSON.parse(a):o},saveItem:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;if(o===l.NONE)return;let a=n.cacheKeyPrefix()+t;o===l.OnlySessionStorage?sessionStorage.setItem(a,e):o===l.OnlyLocalStorage?localStorage.setItem(a,e):o===l.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),n.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}, v=${e}`)},saveObject:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;return u.saveItem(t,JSON.stringify(e),o)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage;if(e===l.NONE)return;let o=n.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(o):e===l.OnlyLocalStorage?localStorage.removeItem(o):e===l.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))}};var U={findOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return r[i]}}},findMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let f=0;f<e.length;f++)d[s]===e[f]&&r.push(d)}return r}}},onAddOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultStorageType;if(o===l.NONE)return;let a=u.getItem(t,o);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),u.saveObject(t,s)):u.saveObject(t,[e])}else u.saveObject(t,[e]);console.log("onAddOne done")},onEditOne:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e[s]){n.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),r[i]=e,u.saveObject(t,r);return}n.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else n.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let f=0;f<r.length;f++)r[f][s]===d[s]&&(r[f]=d)}u.saveItem(t,JSON.stringify(r));return}else{u.saveItem(t,JSON.stringify(e));return}}else n.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return n.EnableLog&&console.log(`del one: ${s}=${e}`),r.splice(i,1),u.saveItem(t,JSON.stringify(r)),r}}},onDelOne:function(t,e){var o;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(s===l.NONE)return;let g=a||n.defaultIdentiyKey,r=(o=e[g])===null||o===void 0?void 0:o.toString();r?U.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+g+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType)===l.NONE)return;let s=o||n.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let f=e[d];r[i][s]===f&&(n.EnableLog&&console.log(`del one: ${s}=${f}`),r.splice(i,1))}return u.saveItem(t,JSON.stringify(r)),r}}},onDelMany:function(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:n.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType;if(a===l.NONE)return;let s=o||n.defaultIdentiyKey,g=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);g&&g.length>0?U.onDelManyByIds(t,g,o,a):console.log("onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:n.defaultStorageType,o=n.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(o):e===l.OnlyLocalStorage?localStorage.removeItem(o):e===l.BothStorage&&(sessionStorage.removeItem(o),localStorage.removeItem(o))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:n.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var O;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(O||(O={}));function x(t){if(t){if(t.code===O.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var j=t=>encodeURIComponent(JSON.stringify(t));import{useEffect as X,useState as D}from"react";function Y(t,e,o){var a,s,g,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,f=arguments.length>5?arguments[5]:void 0,c=arguments.length>6?arguments[6]:void 0,y=arguments.length>7?arguments[7]:void 0;K(t,e,o,i,d||((a=n.request)===null||a===void 0?void 0:a.showToast),f||((s=n.request)===null||s===void 0?void 0:s.showToast),c||((g=n.request)===null||g===void 0?void 0:g.showLoading),y||((r=n.request)===null||r===void 0?void 0:r.hideLoading))}function K(t,e,o){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:n.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(o){let d=u.getObject(o,a);if(d)return n.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+o),e(d),!0;n.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+o)}return r&&r(),n.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{i&&i();let f=d.data;if(f.code===O.OK){let c=x(f);return c===void 0?s&&(n.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(o&&u.saveItem(o,JSON.stringify(c),a),e(c)),!1}else return g&&(n.EnableLog&&console.log("genericFetch: fail from remote server: code="+f.code+",msg="+f.msg),g(f.msg||f.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),n.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function Z(t,e){let o=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[g,r]=D(!1),[i,d]=D(),[f,c]=D();return X(()=>{var y,N;r(!0);let I=o?(y=n.request)===null||y===void 0?void 0:y.getWithoutAuth:(N=n.request)===null||N===void 0?void 0:N.get;if(!I)console.warn("not config request promise, please set ConfigRequest firstly");else{var T,h,S,C;a&&(!((T=n.request)!==null&&T!==void 0&&T.showLoading)||!((h=n.request)!==null&&h!==void 0&&h.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),K(()=>I(t),p=>{r(!1),d(p)},e,s,()=>{r(!1),c("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},p=>{r(!1),c(p)},(S=n.request)===null||S===void 0?void 0:S.showLoading,(C=n.request)===null||C===void 0?void 0:C.hideLoading)}},[t]),{loading:g,entity:i,errMsg:f}}import{useEffect as ee,useRef as te,useState as L}from"react";function z(t){if(!t)return"";t.pagination&&(t.umi=j(t.pagination)),t.pagination=void 0,n.EnableLog&&console.log("query2Params: newQuery="+JSON.stringify(t));let e=[];for(let o in t)if(o){let a=t[o];a==null||a===""||e.push(`${o}=${a}`)}return e.length>0?"?"+e.sort().join("&"):""}function ne(t,e,o){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,g=arguments.length>4&&arguments[4]!==void 0?arguments[4]:n.defaultStorageType,[r,i]=L(),[d,f]=L(!0),[c,y]=L(!1),[N,I]=L(),[T,h]=L(e?A(e):!0),[S,C]=L(t+z(o)),[p,W]=L(0),{current:v}=te({pageSize:(o==null||(a=o.pagination)===null||a===void 0?void 0:a.pageSize)||n.PageSize,useCache:!!e,isLoadMore:!1}),J=m=>{v.useCache=m},$=m=>{v.isLoadMore=m},_=()=>{W(p+1)},V=m=>{n.EnableLog&&console.log("update query for wholeUrl...query="+JSON.stringify(m)),C(t+z(m))},F=(m,H,G,w)=>{var B;n.EnableLog&&console.log("fetch from remote... url="+m);let q=(B=n.request)===null||B===void 0?void 0:B.get;if(!q){console.warn("please inject get request firstly");return}q(m).then(M=>{f(!1),J(!0);let E=M.data,b=x(E);if(E.code===O.OK)if(b){y(!1);let P=H&&r&&r.length>0?r.concat(b):b;if(i(P),e&&u.saveObject(e,P,g),s){let R=b.length>=G;h(R),e&&k(e,R)}}else y(!0),s&&(I("no data"),e&&k(e,!1));else y(!0),s&&(h(!1),e&&k(e,!1)),I(E.msg||E.code),n.EnableLog&&console.log("useCacheList: fail from remote server: code="+E.code+",msg="+E.msg);w&&w(b),$(!1)}).catch(M=>{J(!0),w&&w(),f(!1),y(!0),I(M.message),s&&(e&&k(e,!1),h(!1)),$(!1),n.EnableLog&&console.log("useCacheList exception from remote server:",M)})};return ee(()=>{if(n.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote, refreshCount="+p+", wholeUrl="+S),f(!0),v.useCache&&e){let m=u.getObject(e,g);m?(n.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(m),f(!1),h(A(e))):(n.EnableLog&&console.log("no local cache, try from remote..."),F(S,v.isLoadMore,v.pageSize))}else n.EnableLog&&console.log("useCache=false, try from remote..."),F(S,v.isLoadMore,v.pageSize)},[S,p]),{isLoading:d,isError:c,errMsg:N,loadMoreState:T,list:r,refreshCount:p,setList:i,fetchDataFromRemote:F,setQuery:V,setRefresh:_,setUseCache:J,setIsLoadMore:$}}function A(t){let e=n.cacheKeyPrefix()+t+"/loadMore",o=sessionStorage.getItem(e);return!(o&&o==="0")}function k(t,e){let o=n.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(o,e?"1":"0")}export{O as CODE,U as Cache,u as CacheStorage,l as StorageType,n as UseCacheConfig,Q as currentHost,j as encodeUmi,Y as fetchWithLoading,K as genericFetch,x as getDataFromBox,Z as useCache,ne as useCacheList};

@@ -312,10 +312,11 @@ import * as react from 'react';

list: T[] | undefined;
setQuery: (query?: Q) => string;
refreshCount: number;
setList: react.Dispatch<react.SetStateAction<T[] | undefined>>;
fetchDataFromRemote: (isLoadMore: boolean, queryParamsStr: string, pageSize: number, onDone?: ((data?: T[]) => void) | undefined) => void;
setRefresh: () => number;
setUseCache: (useLocalCache: boolean) => boolean;
setIsLoadMore: (toLoadMore: boolean) => boolean;
fetchDataFromRemote: (wholeUrl: string, isLoadMore: boolean, pageSize: number, onDone?: ((data?: T[]) => void) | undefined) => void;
setQuery: (query?: Q) => void;
setRefresh: () => void;
setUseCache: (useLocalCache: boolean) => void;
setIsLoadMore: (toLoadMore: boolean) => void;
};
export { CODE, Cache, CacheStorage, DataBox, DataBoxBase, DataBoxTableList, IRequest, PaginationQueryBase, StorageType, UmiListPagination, UseCacheConfig, currentHost, encodeUmi, fetchWithLoading, genericFetch, getDataFromBox, useCache, useCacheList };

@@ -1,1 +0,1 @@

"use strict";var k=Object.defineProperty,ne=Object.defineProperties,oe=Object.getOwnPropertyDescriptor,re=Object.getOwnPropertyDescriptors,ae=Object.getOwnPropertyNames,_=Object.getOwnPropertySymbols;var W=Object.prototype.hasOwnProperty,se=Object.prototype.propertyIsEnumerable;var V=(t,e,n)=>e in t?k(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,D=(t,e)=>{for(var n in e||(e={}))W.call(e,n)&&V(t,n,e[n]);if(_)for(var n of _(e))se.call(e,n)&&V(t,n,e[n]);return t},K=(t,e)=>ne(t,re(e));var ie=(t,e)=>{for(var n in e)k(t,n,{get:e[n],enumerable:!0})},ge=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ae(e))!W.call(t,s)&&s!==n&&k(t,s,{get:()=>e[s],enumerable:!(a=oe(e,s))||a.enumerable});return t};var le=t=>ge(k({},"__esModule",{value:!0}),t);var ue={};ie(ue,{CODE:()=>p,Cache:()=>J,CacheStorage:()=>f,StorageType:()=>l,UseCacheConfig:()=>o,currentHost:()=>H,encodeUmi:()=>P,fetchWithLoading:()=>G,genericFetch:()=>$,getDataFromBox:()=>E,useCache:()=>Q,useCacheList:()=>Z});module.exports=le(ue);var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},H=()=>window.location.protocol+"//"+window.location.host;var f={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0;if(e===l.NONE)return n;let a,s=o.cacheKeyPrefix()+t;return e===l.OnlySessionStorage?a=sessionStorage.getItem(s):e===l.OnlyLocalStorage?a=localStorage.getItem(s):e===l.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),o.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${n}`),a||n},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0,a=f.getItem(t,e);return a?JSON.parse(a):n},saveItem:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;if(n===l.NONE)return;let a=o.cacheKeyPrefix()+t;n===l.OnlySessionStorage?sessionStorage.setItem(a,e):n===l.OnlyLocalStorage?localStorage.setItem(a,e):n===l.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),o.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}, v=${e}`)},saveObject:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;return f.saveItem(t,JSON.stringify(e),n)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage;if(e===l.NONE)return;let n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))}};var J={findOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return r[i]}}},findMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let u=0;u<e.length;u++)d[s]===e[u]&&r.push(d)}return r}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultStorageType;if(n===l.NONE)return;let a=f.getItem(t,n);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),f.saveObject(t,s)):f.saveObject(t,[e])}else f.saveObject(t,[e]);console.log("onAddOne done")},onEditOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e[s]){o.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),r[i]=e,f.saveObject(t,r);return}o.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else o.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let u=0;u<r.length;u++)r[u][s]===d[s]&&(r[u]=d)}f.saveItem(t,JSON.stringify(r));return}else{f.saveItem(t,JSON.stringify(e));return}}else o.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return o.EnableLog&&console.log(`del one: ${s}=${e}`),r.splice(i,1),f.saveItem(t,JSON.stringify(r)),r}}},onDelOne:function(t,e){var n;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(s===l.NONE)return;let g=a||o.defaultIdentiyKey,r=(n=e[g])===null||n===void 0?void 0:n.toString();r?J.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+g+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=f.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let u=e[d];r[i][s]===u&&(o.EnableLog&&console.log(`del one: ${s}=${u}`),r.splice(i,1))}return f.saveItem(t,JSON.stringify(r)),r}}},onDelMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);g&&g.length>0?J.onDelManyByIds(t,g,n,a):console.log("onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:o.defaultStorageType,n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:o.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var p;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(p||(p={}));function E(t){if(t){if(t.code===p.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var P=t=>encodeURIComponent(JSON.stringify(t));var N=require("react");function G(t,e,n){var a,s,g,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,u=arguments.length>5?arguments[5]:void 0,c=arguments.length>6?arguments[6]:void 0,y=arguments.length>7?arguments[7]:void 0;$(t,e,n,i,d||((a=o.request)===null||a===void 0?void 0:a.showToast),u||((s=o.request)===null||s===void 0?void 0:s.showToast),c||((g=o.request)===null||g===void 0?void 0:g.showLoading),y||((r=o.request)===null||r===void 0?void 0:r.hideLoading))}function $(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(n){let d=f.getObject(n,a);if(d)return o.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+n),e(d),!0;o.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+n)}return r&&r(),o.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{i&&i();let u=d.data;if(u.code===p.OK){let c=E(u);return c===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&f.saveItem(n,JSON.stringify(c),a),e(c)),!1}else return g&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+u.code+",msg="+u.msg),g(u.msg||u.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function Q(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[g,r]=(0,N.useState)(!1),[i,d]=(0,N.useState)(),[u,c]=(0,N.useState)();return(0,N.useEffect)(()=>{var y,T;r(!0);let I=n?(y=o.request)===null||y===void 0?void 0:y.getWithoutAuth:(T=o.request)===null||T===void 0?void 0:T.get;if(!I)console.warn("not config request promise, please set ConfigRequest firstly");else{var C,O,m,b;a&&(!((C=o.request)!==null&&C!==void 0&&C.showLoading)||!((O=o.request)!==null&&O!==void 0&&O.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),$(()=>I(t),v=>{r(!1),d(v)},e,s,()=>{r(!1),c("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},v=>{r(!1),c(v)},(m=o.request)===null||m===void 0?void 0:m.showLoading,(b=o.request)===null||b===void 0?void 0:b.hideLoading)}},[t]),{loading:g,entity:i,errMsg:u}}var S=require("react");function X(t){if(!t)return"";let e=t!=null&&t.pagination?K(D({},t),{umi:P(t.pagination),sort:void 0,pagination:void 0}):K(D({},t),{sort:void 0}),n=[];for(let a in e)if(a){let s=t[a];s==null||s===""?console.log("no value for item, ignore"):n.push(`${a}=${s}`)}return n.length>0?"?"+n.sort().join("&"):""}function Z(t,e,n){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,g=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[r,i]=(0,S.useState)(),[d,u]=(0,S.useState)(!0),[c,y]=(0,S.useState)(!1),[T,I]=(0,S.useState)(),[C,O]=(0,S.useState)(e?Y(e):!0),{current:m}=(0,S.useRef)({queryParamsStr:X(n),pageSize:(n==null||(a=n.pagination)===null||a===void 0?void 0:a.pageSize)||o.PageSize,useCache:!!e,isLoadMore:!1,refresh:0}),b=h=>m.queryParamsStr=X(h),v=h=>m.useCache=h,F=h=>m.isLoadMore=h,ee=()=>m.refresh++,B=(h,fe,te,w)=>{var j;let U=t+m.queryParamsStr;o.EnableLog&&console.log("fetch from remote... url="+U);let z=(j=o.request)===null||j===void 0?void 0:j.get;if(!z){console.warn("please inject get request firstly");return}z(U).then(M=>{u(!1),v(!0);let L=M.data,x=E(L);if(L.code===p.OK)if(x){y(!1);let A=h&&r&&r.length>0?r.concat(x):x;if(i(A),e&&f.saveObject(e,A,g),s){let R=x.length>=te;O(R),e&&q(e,R)}}else y(!0),s&&(I("no data"),e&&q(e,!1));else y(!0),s&&(O(!1),e&&q(e,!1)),I(L.msg||L.code),o.EnableLog&&console.log("useCacheList: fail from remote server: code="+L.code+",msg="+L.msg);w&&w(x),F(!1)}).catch(M=>{v(!0),w&&w(),u(!1),y(!0),I(M.message),s&&(e&&q(e,!1),O(!1)),F(!1),o.EnableLog&&console.log("useCacheList exception from remote server:",M)})};return(0,S.useEffect)(()=>{if(o.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote"),u(!0),m.useCache&&e){let h=f.getObject(e,g);h?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(h),u(!1),O(Y(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),B(m.isLoadMore,m.queryParamsStr,m.pageSize))}else o.EnableLog&&console.log("useCache=false, try from remote..."),B(m.isLoadMore,m.queryParamsStr,m.pageSize)},[t,m.queryParamsStr,m.refresh]),{isLoading:d,isError:c,errMsg:T,loadMoreState:C,list:r,setQuery:b,setList:i,fetchDataFromRemote:B,setRefresh:ee,setUseCache:v,setIsLoadMore:F}}function Y(t){let e=o.cacheKeyPrefix()+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function q(t,e){let n=o.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(n,e?"1":"0")}0&&(module.exports={CODE,Cache,CacheStorage,StorageType,UseCacheConfig,currentHost,encodeUmi,fetchWithLoading,genericFetch,getDataFromBox,useCache,useCacheList});
"use strict";var q=Object.defineProperty;var te=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var re=(t,e)=>{for(var n in e)q(t,n,{get:e[n],enumerable:!0})},ae=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ne(e))!oe.call(t,s)&&s!==n&&q(t,s,{get:()=>e[s],enumerable:!(a=te(e,s))||a.enumerable});return t};var se=t=>ae(q({},"__esModule",{value:!0}),t);var ie={};re(ie,{CODE:()=>S,Cache:()=>J,CacheStorage:()=>u,StorageType:()=>l,UseCacheConfig:()=>o,currentHost:()=>A,encodeUmi:()=>$,fetchWithLoading:()=>W,genericFetch:()=>F,getDataFromBox:()=>N,useCache:()=>_,useCacheList:()=>G});module.exports=se(ie);var l={OnlySessionStorage:1,OnlyLocalStorage:2,BothStorage:3,NONE:0};var o={EnableLog:!1,cacheKeyPrefix:()=>"",defaultIdentiyKey:"_id",defaultStorageType:l.OnlySessionStorage,PageSize:10},A=()=>window.location.protocol+"//"+window.location.host;var u={getItem:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0;if(e===l.NONE)return n;let a,s=o.cacheKeyPrefix()+t;return e===l.OnlySessionStorage?a=sessionStorage.getItem(s):e===l.OnlyLocalStorage?a=localStorage.getItem(s):e===l.BothStorage&&(a=sessionStorage.getItem(s),a||(a=localStorage.getItem(s),a&&sessionStorage.setItem(s,a))),o.EnableLog&&console.log(`CacheStorage.getItem: key=${s}, defaultValue=${n}`),a||n},getObject:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage,n=arguments.length>2?arguments[2]:void 0,a=u.getItem(t,e);return a?JSON.parse(a):n},saveItem:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;if(n===l.NONE)return;let a=o.cacheKeyPrefix()+t;n===l.OnlySessionStorage?sessionStorage.setItem(a,e):n===l.OnlyLocalStorage?localStorage.setItem(a,e):n===l.BothStorage&&(sessionStorage.setItem(a,e),localStorage.setItem(a,e)),o.EnableLog&&console.log(`CacheStorage.saveItem: key=${a}, v=${e}`)},saveObject:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:l.OnlySessionStorage;return u.saveItem(t,JSON.stringify(e),n)},remove:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:l.OnlySessionStorage;if(e===l.NONE)return;let n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))}};var J={findOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return r[i]}}},findMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++){let d=r[i];for(let f=0;f<e.length;f++)d[s]===e[f]&&r.push(d)}return r}}},onAddOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultStorageType;if(n===l.NONE)return;let a=u.getItem(t,n);if(a){let s=JSON.parse(a);s&&s.length>0?(s.unshift(e),u.saveObject(t,s)):u.saveObject(t,[e])}else u.saveObject(t,[e]);console.log("onAddOne done")},onEditOne:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e[s]){o.EnableLog&&console.log(`onEditOne, e[${s}]=${e[s]}, shortKey=${t}`),r[i]=e,u.saveObject(t,r);return}o.EnableLog&&console.log(`onEditOne\uFF1Anot found in list, key=${s}, shortKey=${t}`);return}}else o.EnableLog&&console.log("onEditOne\uFF1Anot found list: shortKey="+t)},onEditMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t,a);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<e.length;i++){let d=e[i];for(let f=0;f<r.length;f++)r[f][s]===d[s]&&(r[f]=d)}u.saveItem(t,JSON.stringify(r));return}else{u.saveItem(t,JSON.stringify(e));return}}else o.EnableLog&&console.log("onEditMany: not found list, shortKey="+t)},onDelOneById:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)if(r[i][s]===e)return o.EnableLog&&console.log(`del one: ${s}=${e}`),r.splice(i,1),u.saveItem(t,JSON.stringify(r)),r}}},onDelOne:function(t,e){var n;let a=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(s===l.NONE)return;let g=a||o.defaultIdentiyKey,r=(n=e[g])===null||n===void 0?void 0:n.toString();r?J.onDelOneById(t,r,a,s):console.log("onDelOne: not found id by key="+g+"in entity="+JSON.stringify(e))},onDelManyByIds:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey;if((arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType)===l.NONE)return;let s=n||o.defaultIdentiyKey,g=u.getItem(t);if(g){let r=JSON.parse(g);if(r&&r.length>0){for(let i=0;i<r.length;i++)for(let d=0;d<e.length;d++){let f=e[d];r[i][s]===f&&(o.EnableLog&&console.log(`del one: ${s}=${f}`),r.splice(i,1))}return u.saveItem(t,JSON.stringify(r)),r}}},onDelMany:function(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:o.defaultIdentiyKey,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType;if(a===l.NONE)return;let s=n||o.defaultIdentiyKey,g=e.map(r=>{var i;return(i=r[s])===null||i===void 0?void 0:i.toString()}).filter(r=>!!r);g&&g.length>0?J.onDelManyByIds(t,g,n,a):console.log("onDelOne: not found id by key="+s+"in entity list="+JSON.stringify(e))},evictCache:function(t){let e=arguments.length>1&&arguments[1]!==void 0?arguments[1]:o.defaultStorageType,n=o.cacheKeyPrefix()+t;e===l.OnlySessionStorage?sessionStorage.removeItem(n):e===l.OnlyLocalStorage?localStorage.removeItem(n):e===l.BothStorage&&(sessionStorage.removeItem(n),localStorage.removeItem(n))},evictAllCaches:function(){let t=arguments.length>0&&arguments[0]!==void 0?arguments[0]:o.defaultStorageType;t===l.OnlySessionStorage?sessionStorage.clear():t===l.OnlyLocalStorage?localStorage.clear():t===l.BothStorage&&(sessionStorage.clear(),localStorage.clear())}};var S;(function(t){t.OK="OK",t.KO="KO",t.NewUser="NewUser",t.TokenExpired="TokenExpired"})(S||(S={}));function N(t){if(t){if(t.code===S.OK)return t.data;console.warn("getDataFromBox:"+JSON.stringify(t));return}else{console.warn("\u51FA\u9519\u4E86\uFF0C\u8BF7\u6C42\u7ED3\u679C\u6CA1\u6709\u6570\u636E");return}}var $=t=>encodeURIComponent(JSON.stringify(t));var T=require("react");function W(t,e,n){var a,s,g,r;let i=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,d=arguments.length>4?arguments[4]:void 0,f=arguments.length>5?arguments[5]:void 0,y=arguments.length>6?arguments[6]:void 0,h=arguments.length>7?arguments[7]:void 0;F(t,e,n,i,d||((a=o.request)===null||a===void 0?void 0:a.showToast),f||((s=o.request)===null||s===void 0?void 0:s.showToast),y||((g=o.request)===null||g===void 0?void 0:g.showLoading),h||((r=o.request)===null||r===void 0?void 0:r.hideLoading))}function F(t,e,n){let a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:o.defaultStorageType,s=arguments.length>4?arguments[4]:void 0,g=arguments.length>5?arguments[5]:void 0,r=arguments.length>6?arguments[6]:void 0,i=arguments.length>7?arguments[7]:void 0;if(n){let d=u.getObject(n,a);if(d)return o.EnableLog&&console.log("genericFetch: got value from cache, shortKey="+n),e(d),!0;o.EnableLog&&console.log("genericFetch: not found value from cache, shortKey="+n)}return r&&r(),o.EnableLog&&console.log("genericFetch: from remote server..."),t().then(d=>{i&&i();let f=d.data;if(f.code===S.OK){let y=N(f);return y===void 0?s&&(o.EnableLog&&console.log("genericFetch: no data from remote server"),s()):(n&&u.saveItem(n,JSON.stringify(y),a),e(y)),!1}else return g&&(o.EnableLog&&console.log("genericFetch: fail from remote server: code="+f.code+",msg="+f.msg),g(f.msg||f.code)),!1}).catch(d=>(i&&i(),g&&g(d.message),o.EnableLog&&console.log("genericFetch exception from remote server:",d),!1)),!1}function _(t,e){let n=arguments.length>2&&arguments[2]!==void 0?arguments[2]:!1,a=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!1,s=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[g,r]=(0,T.useState)(!1),[i,d]=(0,T.useState)(),[f,y]=(0,T.useState)();return(0,T.useEffect)(()=>{var h,C;r(!0);let I=n?(h=o.request)===null||h===void 0?void 0:h.getWithoutAuth:(C=o.request)===null||C===void 0?void 0:C.get;if(!I)console.warn("not config request promise, please set ConfigRequest firstly");else{var b,p,O,x;a&&(!((b=o.request)!==null&&b!==void 0&&b.showLoading)||!((p=o.request)!==null&&p!==void 0&&p.hideLoading))&&console.warn("not config request showLoading/hideLoading, please set ConfigRequest firstly"),F(()=>I(t),v=>{r(!1),d(v)},e,s,()=>{r(!1),y("\u6570\u636E\u79FB\u6C11\u706B\u661F\u4E86^_^")},v=>{r(!1),y(v)},(O=o.request)===null||O===void 0?void 0:O.showLoading,(x=o.request)===null||x===void 0?void 0:x.hideLoading)}},[t]),{loading:g,entity:i,errMsg:f}}var c=require("react");function V(t){if(!t)return"";t.pagination&&(t.umi=$(t.pagination)),t.pagination=void 0,o.EnableLog&&console.log("query2Params: newQuery="+JSON.stringify(t));let e=[];for(let n in t)if(n){let a=t[n];a==null||a===""||e.push(`${n}=${a}`)}return e.length>0?"?"+e.sort().join("&"):""}function G(t,e,n){var a;let s=arguments.length>3&&arguments[3]!==void 0?arguments[3]:!0,g=arguments.length>4&&arguments[4]!==void 0?arguments[4]:o.defaultStorageType,[r,i]=(0,c.useState)(),[d,f]=(0,c.useState)(!0),[y,h]=(0,c.useState)(!1),[C,I]=(0,c.useState)(),[b,p]=(0,c.useState)(e?H(e):!0),[O,x]=(0,c.useState)(t+V(n)),[v,Q]=(0,c.useState)(0),{current:L}=(0,c.useRef)({pageSize:(n==null||(a=n.pagination)===null||a===void 0?void 0:a.pageSize)||o.PageSize,useCache:!!e,isLoadMore:!1}),U=m=>{L.useCache=m},j=m=>{L.isLoadMore=m},X=()=>{Q(v+1)},Y=m=>{o.EnableLog&&console.log("update query for wholeUrl...query="+JSON.stringify(m)),x(t+V(m))},D=(m,Z,ee,M)=>{var K;o.EnableLog&&console.log("fetch from remote... url="+m);let P=(K=o.request)===null||K===void 0?void 0:K.get;if(!P){console.warn("please inject get request firstly");return}P(m).then(k=>{f(!1),U(!0);let E=k.data,w=N(E);if(E.code===S.OK)if(w){h(!1);let R=Z&&r&&r.length>0?r.concat(w):w;if(i(R),e&&u.saveObject(e,R,g),s){let z=w.length>=ee;p(z),e&&B(e,z)}}else h(!0),s&&(I("no data"),e&&B(e,!1));else h(!0),s&&(p(!1),e&&B(e,!1)),I(E.msg||E.code),o.EnableLog&&console.log("useCacheList: fail from remote server: code="+E.code+",msg="+E.msg);M&&M(w),j(!1)}).catch(k=>{U(!0),M&&M(),f(!1),h(!0),I(k.message),s&&(e&&B(e,!1),p(!1)),j(!1),o.EnableLog&&console.log("useCacheList exception from remote server:",k)})};return(0,c.useEffect)(()=>{if(o.EnableLog&&console.log("in useCacheList useEffect, try load from local or remote, refreshCount="+v+", wholeUrl="+O),f(!0),L.useCache&&e){let m=u.getObject(e,g);m?(o.EnableLog&&console.log("fetch from local cache... shortKey="+e),i(m),f(!1),p(H(e))):(o.EnableLog&&console.log("no local cache, try from remote..."),D(O,L.isLoadMore,L.pageSize))}else o.EnableLog&&console.log("useCache=false, try from remote..."),D(O,L.isLoadMore,L.pageSize)},[O,v]),{isLoading:d,isError:y,errMsg:C,loadMoreState:b,list:r,refreshCount:v,setList:i,fetchDataFromRemote:D,setQuery:Y,setRefresh:X,setUseCache:U,setIsLoadMore:j}}function H(t){let e=o.cacheKeyPrefix()+t+"/loadMore",n=sessionStorage.getItem(e);return!(n&&n==="0")}function B(t,e){let n=o.cacheKeyPrefix()+t+"/loadMore";sessionStorage.setItem(n,e?"1":"0")}0&&(module.exports={CODE,Cache,CacheStorage,StorageType,UseCacheConfig,currentHost,encodeUmi,fetchWithLoading,genericFetch,getDataFromBox,useCache,useCacheList});
{
"name": "@rwsbillyang/usecache",
"version": "0.9.7",
"version": "0.9.8",
"description": "react hooks use cache before client fetch data from remote server, likes cache in server side before database I/O",

@@ -38,4 +38,4 @@ "repository": {

],
"exports":{
".":{
"exports": {
".": {
"import": "./dist/esm/index.js",

@@ -94,2 +94,3 @@ "require": "./dist/index.js"

"esbuild-plugin-babel": "^0.2.3",
"esbuild-wasm": "latest",
"eslint": "^8.23.0",

@@ -116,8 +117,7 @@ "eslint-config-airbnb-base": "^15.0.0",

"vite-plugin-dts": "^1.4.1",
"vitest": "^0.23.1",
"esbuild-wasm":"latest"
"vitest": "^0.23.1"
},
"overrides": {
"esbuild":"npm:esbuild-wasm@latest"
"esbuild": "npm:esbuild-wasm@latest"
}
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc