hook-fetch
Version:
A lightweight and modern HTTP request library developed based on the native Fetch API of the browser, providing a user-friendly interface similar to Axios and powerful extensibility.
1 lines • 832 B
JavaScript
const e=require(`../../chunk-mbd8xe7s.cjs`),t=e.__toESM(require(`react`)),n=({request:e,onError:n})=>{let r=(0,t.useRef)(null),[i,a]=(0,t.useState)(!1),o=(...t)=>(r.current=e(...t),r),s=(...t)=>(r.current=e(...t),a(!0),r.current?.catch(e=>{e instanceof Error&&!e.message.includes(`Unexpected token`)&&e.name!==`AbortError`&&n?.(e)}),r.current?.finally(()=>{a(!1)}),r),c=(...e)=>(s(...e),r.current.text()),l=(...e)=>(s(...e),r.current.stream()),u=(...e)=>(s(...e),r.current.blob()),d=(...e)=>(s(...e),r.current.arrayBuffer()),f=(...e)=>(s(...e),r.current.formData()),p=(...e)=>(s(...e),r.current.bytes()),m=()=>{if(a(!1),r)try{r.current?.abort()}catch(e){console.error(`cancel error`,e)}};return{request:o,stream:l,text:c,blob:u,arrayBufferData:d,formDataResult:f,bytesData:p,cancel:m,loading:i,setLoading:a}};exports.useHookFetch=n;