cache-memo-fetch
Version:
useFetch react hook
25 lines (23 loc) • 3.51 kB
JavaScript
/*
* MIT License
*
* Copyright (c) 2025 theMackabu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var w=Object.defineProperty,p=Object.defineProperties;var y=Object.getOwnPropertyDescriptors;var m=Object.getOwnPropertySymbols;var C=Object.prototype.hasOwnProperty,x=Object.prototype.propertyIsEnumerable;var T=(r,e,t)=>e in r?w(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,i=(r,e)=>{for(var t in e||(e={}))C.call(e,t)&&T(r,t,e[t]);if(m)for(var t of m(e))x.call(e,t)&&T(r,t,e[t]);return r},u=(r,e)=>p(r,y(e));import{use as F,useMemo as k}from"react";var h=new Map,f=new Map,o=new Map,P=5*60*1e3,d=new Map;function R(){let r=Date.now(),e=[];d.forEach((t,n)=>{r-t>P&&e.push(n)}),e.forEach(t=>{h.delete(t),f.delete(t),o.delete(t),d.delete(t)})}setInterval(R,5*60*1e3);function E(r){let e=5381,t=r.length;for(;t--;)e=(e<<5)+e^r.charCodeAt(t);return(e>>>0).toString(36)}function b(r,e){if(!e)return r;let t=e.method||"GET";if(t==="GET"&&!e.headers&&!e.body&&!e.mode&&!e.credentials)return r;let n=[r,t];if(e.headers){let s=e.headers,c=Object.keys(s);if(c.length===1){let l=c[0];n.push("".concat(l,":").concat(s[l]))}else c.length>1&&n.push(c.sort().map(l=>"".concat(l,":").concat(s[l])).join(","))}e.body&&n.push(String(e.body)),e.mode&&n.push(e.mode),e.credentials&&n.push(e.credentials);let a=n.join("|");return a.length>100?E(a):a}function I(r="GET"){let e=r.charCodeAt(0);return e===71?h:e===80?f:o}async function O(r,e){let t=await fetch(r,e);if(!t.ok)throw new Error("HTTP ".concat(t.status,": ").concat(t.statusText));return e!=null&&e.parser?e.parser(t):t.json()}function g(r,e){let t=k(()=>{let n=b(r,e),a=I(e==null?void 0:e.method),s=a.get(n);return s||(s=O(r,e).catch(c=>{if((e==null?void 0:e.fallback)!==void 0)return e.fallback;throw c}),a.set(n,s),d.set(n,Date.now())),s},[r,e]);return F(t)}function A(r,e){return g(r,u(i({},e),{parser:t=>t.text()}))}function B(r,e){return g(r,u(i({},e),{parser:t=>t.blob()}))}function $(r,e){return g(r,u(i({},e),{parser:t=>t.arrayBuffer()}))}function v(r,e){return g(r,u(i({},e),{parser:t=>t.formData()}))}function D(r,e){return g(r,u(i({},e),{parser:t=>t.bytes()}))}var M={clear:()=>{h.clear(),f.clear(),o.clear(),d.clear()},clearUrl:r=>{let e=b(r);h.delete(e),f.delete(e),o.delete(e),d.delete(e)},get stats(){return{get:h.size,post:f.size,other:o.size,total:h.size+f.size+o.size}}};export{M as cache,g as useFetch,B as useFetchBlob,$ as useFetchBuffer,D as useFetchBytes,v as useFetchForm,A as useFetchText};