UNPKG

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.

2 lines 1.19 kB
const e=require(`../chunk.cjs`),t=e.__toESM(require(`react`));function n({request:e,onError:n}){let r=(0,t.useRef)(null),[i,a]=(0,t.useState)(!1),o={name:`__react-hook__`,onFinally(){a(!1)}},s=(...t)=>{if(r.current)return r.current;if(r.current=e(...t),!r.current||!(`__injectPlugins__`in r.current))throw Error(`Instance is not a HookFetchRequest`);return r.current?.__injectPlugins__([o]),r},c=(...t)=>{if(r.current=e(...t),!r.current||!(`__injectPlugins__`in r.current))throw Error(`Instance is not a HookFetchRequest`);return r.current?.__injectPlugins__([o]),a(!0),r.current?.catch(e=>{e instanceof Error&&!e.message.includes(`Unexpected token`)&&e.name!==`AbortError`&&n?.(e),a(!1)}),r},l=(...e)=>(c(...e),r.current.text()),u=(...e)=>(c(...e),r.current.stream()),d=(...e)=>(c(...e),r.current.blob()),f=(...e)=>(c(...e),r.current.arrayBuffer()),p=(...e)=>(c(...e),r.current.formData()),m=(...e)=>(c(...e),r.current.bytes()),h=()=>{if(a(!1),r)try{r.current?.abort()}catch(e){console.error(`cancel error`,e)}};return{request:s,stream:u,text:l,blob:d,arrayBufferData:f,formDataResult:p,bytesData:m,cancel:h,loading:i,setLoading:a}}exports.useHookFetch=n; //# sourceMappingURL=index.cjs.map