1.1.1 • Published 4 months ago

axios-handler v1.1.1

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

axios-handler

axios-handler is a React/React Native custom hook that simplifies and enhances the use of the Axios library for executing API requests efficiently.

Installation

Install using npm:

npm install axios-handler axios

Or using yarn:

yarn add axios-handler axios

Features

  • Simplifies API request management in React and React Native applications.
  • Built-in support for loading and error states.
  • Supports dynamic parameters for API queries.
  • Provides refresh and reset functionality for easy data manipulation.

Quick Start

Example Code

Here's an example of how to use axios-handler in a React Native project:

import React, { useEffect } from 'react'
import { ActivityIndicator, Text, Button } from 'react-native'
import { useFetching } from 'axios-handler'
import axios from 'axios'

const Component = () => {
  const { fetch, refresh, data, loading, refreshing, error, reset } = useFetching({
    query: async (props) => {
      const { id, filters } = props
      return await axios.get(`api/v2/example/${id}`, { params: filters })
    },
    onComplete: (response) => {
      console.log('Response:', response)
    },
    onError: (error) => {
      console.log('Error:', error)
    },
    initialLoading: true,
  })

  useEffect(() => {
    fetch({ id: 123, filters: { test: 'test' } })
  }, [])

  return (
    <>
      {loading && <ActivityIndicator />}
      {refreshing && <ActivityIndicator />}
      {error && <Text>Error: {error.message}</Text>}
      {data && <Text>Data: {JSON.stringify(data.data)}</Text>}
      <Button title="Refresh" onPress={() => refresh({ id: 123, filters: { test: 'test' } })} />
      <Button title="Reset" onPress={reset} />
    </>
  )
}

export default Component

API Documentation

Hook Parameters

The useFetching hook accepts a configuration object with the following parameters:

ParameterTypeDescriptionRequiredDefault
query(props?: any) => Promise<AxiosResponse<T, D>>Function to perform the HTTP request using Axios.YesN/A
onComplete(response: AxiosResponse<T, D>) => voidCallback on successful request completion. response.NoN/A
onError(error: AxiosError<E>) => voidCallback on request failure. object.NoN/A
initialLoadingbooleanWhether the request starts in a loading state.Nofalse
initialRefreshingbooleanWhether the request starts in a refreshing state.Nofalse

Return Values

The useFetching hook returns an object with the following properties:

PropertyTypeDescription
fetch(props?: any) => Promise<void>Starts the HTTP request.
refresh(props?: any) => Promise<void>Re-triggers the request in a refreshing state.
dataAxiosResponse<T, D> \| nullThe response data or null if no request has been made.
loadingbooleanWhether the request is loading.
refreshingbooleanWhether the request is refreshing.
errorAxiosError<E> \| nullThe error object if the request fails.
reset() => voidResets the state to its initial values.

Type Definitions

Here are the type definitions used for the hook:

Parameters

export type FetchingParams<T, D, E> = {
  query: (props?: any) => Promise<AxiosResponse<T, D>>
  onComplete?: (response: AxiosResponse<T, D>) => void
  onError?: (error: AxiosError<E>) => void
  initialLoading?: boolean
  initialRefreshing?: boolean
}

Return Values

export type FetchingReturn<T, D, E> = {
  fetch: (props?: any) => Promise<void>
  refresh: (props?: any) => Promise<void>
  data: AxiosResponse<T, D> | null
  loading: boolean
  refreshing: boolean
  error: AxiosError<E> | null
  reset: () => void
}

Contribution Guidelines

We welcome contributions! Here’s how you can help:

  • Report issues or request features by opening an issue.
  • Submit pull requests to suggest fixes or enhancements.

Before contributing, please ensure your code follows the project's style and passes all tests.


License

This project is licensed under the ISC License.

1.1.1

4 months ago

1.1.0

7 months ago

1.0.9

7 months ago

1.0.8

7 months ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago