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.

1 lines 804 B
import{useRef as e,useState as t}from"react";const n=({request:n,onError:r})=>{let i=e(null),[a,o]=t(!1),s=(...e)=>(i.current=n(...e),i),c=(...e)=>{var t;return i.current=n(...e),o(!0),(t=i.current)?.catch(e=>{e instanceof Error&&!e.message.includes(`Unexpected token`)&&e.name!==`AbortError`&&r?.(e)}).finally(()=>{o(!1)}),i},l=(...e)=>(c(...e),i.current.text()),u=(...e)=>(c(...e),i.current.stream()),d=(...e)=>(c(...e),i.current.blob()),f=(...e)=>(c(...e),i.current.arrayBuffer()),p=(...e)=>(c(...e),i.current.formData()),m=(...e)=>(c(...e),i.current.bytes()),h=()=>{if(o(!1),i)try{var e;(e=i.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:a,setLoading:o}};export{n as useHookFetch};