# react-api-utils

> A React utility library for making API calls using React Query and Axios, providing context-based configuration and custom hooks for seamless integration.

Latest version **0.0.5** (published 2025-01-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install react-api-utils
pnpm add react-api-utils
yarn add react-api-utils
bun add react-api-utils
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.5 |
| Published | 2025-01-02 |
| First published | 2024-12-29 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 8 |
| Unpacked size | 196.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Vikram Samak |
| Maintainers | vikramsamak |
| Keywords | react, react-query, axios, api-helper, api-calls, react-library, context-provider, typescript, http-client, data-fetching, async-storage, state-management |

## Links

- npm: https://www.npmjs.com/package/react-api-utils
- Repository: https://github.com/vikramsamak/react-api-utils
- Homepage: https://github.com/vikramsamak/react-api-utils#readme
- Issues: https://github.com/vikramsamak/react-api-utils/issues
- npm.io page: https://npm.io/package/react-api-utils

## Dependencies (8)

- [axios](https://npm.io/package/axios.md) ^1.7.9
- [react](https://npm.io/package/react.md) ^18.3.1
- [react-dom](https://npm.io/package/react-dom.md) ^18.3.1
- [vite-plugin-dts](https://npm.io/package/vite-plugin-dts.md) ^4.4.0
- [@tanstack/react-query](https://npm.io/package/@tanstack/react-query.md) ^5.62.10
- [@tanstack/react-query-persist-client](https://npm.io/package/@tanstack/react-query-persist-client.md) ^5.62.10
- [@tanstack/query-async-storage-persister](https://npm.io/package/@tanstack/query-async-storage-persister.md) ^5.62.9
- [@react-native-async-storage/async-storage](https://npm.io/package/@react-native-async-storage/async-storage.md) ^2.1.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 0.0.5 (latest) — 2025-01-02
- 0.0.4 — 2024-12-29

## README

# React API Utils

A utility library for simplifying API requests in React applications. Built on top of **React Query** and **Axios**, it provides a seamless way to manage API calls and caching with an intuitive context-based setup.

## Features

- 🌐 **Axios Integration**: Customize API calls with ease.
- ⚡ **React Query**: Automatic caching, refetching, and stale data management.
- 📦 **Async Storage Support**: Persisted caching for React Native or browser-based environments.
- 🛠️ **TypeScript Ready**: Fully typed for better developer experience.

## Installation

```bash
npm install react-api-utils
# or
yarn add react-api-utils
```

## Usage

### Setup Provider

Wrap your application with the `ApiHelperProvider` to set up the context and configuration.

```tsx
import React from 'react';
import { ApiHelperProvider } from 'react-api-utils';

const App = () => (
  <ApiHelperProvider baseURL="https://api.example.com">
    <YourApp />
  </ApiHelperProvider>
);

export default App;
```

### Using `useApiHelper`

Leverage the `useApiHelper` hook to perform API requests effortlessly.

```tsx
import { useApiHelper } from 'react-api-utils';

const MyComponent = () => {
  const { data, isLoading, isError } = useApiHelper({
    url: '/endpoint',
    method: 'GET',
    queryKey: ['endpoint-data'],
  });

  if (isLoading) return <p>Loading...</p>;
  if (isError) return <p>Something went wrong!</p>;

  return <pre>{JSON.stringify(data, null, 2)}</pre>;
};
```

## API

### `ApiHelperProvider`

| Prop                | Type                 | Default     | Description                       |
| ------------------- | -------------------- | ----------- | --------------------------------- |
| `baseURL`           | `string`             | `undefined` | Base URL for all Axios requests.  |
| `axiosConfig`       | `AxiosRequestConfig` | `{}`        | Custom Axios configuration.       |
| `queryClientConfig` | `QueryClientConfig`  | `{}`        | React Query client configuration. |

### `useApiHelper`

| Option     | Type                                | Description                                    |
| ---------- | ----------------------------------- | ---------------------------------------------- |
| `url`      | `string`                            | API endpoint to fetch data from.               |
| `method`   | `'GET' , 'POST' , 'PUT' , 'DELETE'` | HTTP method for the request.                   |
| `queryKey` | `Array<string>`                     | Unique key for React Query's cache management. |

## Contributing

Contributions are welcome! Feel free to submit issues or pull requests.

---
_Source: https://npm.io/package/react-api-utils · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
