1.1.3 • Published 2 years ago
@fourkord/use-api v1.1.3
use-api
Installation
- Using yarn
yarn add @fourkord/use-api
- Using npm
npm i @fourkord/use-api
Usage
- Basic usage
const instance = axios.create({
baseURL: "http://localhost:4000"
});
const [fetch, {data, setData, loading, error}] = useApi({
axiosInstance: instance,
method: ApiClientMethod.GET,
url: "/endpoint",
onSuccess: (data) => {
console.log(data);
},
onFail: (error) => {
console.log(error);
},
defaultLoading: false // optional. If true, setLoading is true by default, not when api call made. defaultLoading = false by default.
});
- Using types
interface IRequest {
field1: string
}
interface IResponse {
field1: string
}
interface IErrorResponse {
field1: string
}
const [fetch] = useApi<IRequest, IResponse, IErrorResponse>({
axiosInstance: instance,
method: ApiClientMethod.GET,
url: "/endpoint",
onSuccess: (data) => { // optional
console.log(data);
},
onFail: (error) => { // optional
console.log(error);
}
});
- Fetch arguments
useEffect(async () => {
await fetch({
data: {"field1": "value1"}, // optional
urlAddition: "/1" // /endpoint + /1 => /endpoint/1, optional
});
}, [fetch]);