https://github.com/stephenhebert/axios-plugins
A collection of Axios plugins for enhanced functionality
https://github.com/stephenhebert/axios-plugins
Last synced: 5 months ago
JSON representation
A collection of Axios plugins for enhanced functionality
- Host: GitHub
- URL: https://github.com/stephenhebert/axios-plugins
- Owner: stephenhebert
- License: mit
- Created: 2025-06-04T04:22:58.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-04T05:54:40.000Z (about 1 year ago)
- Last Synced: 2025-10-05T07:19:09.412Z (9 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@stephenhebert/axios-plugins
- Size: 56.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @stephenhebert/axios-plugins
A collection of Axios plugins for enhanced functionality.
## Features
- **Allow 404 Responses**: Handle 404 responses gracefully without throwing errors.
- **Inflight Cache**: Prevent duplicate requests by caching inflight requests.
- **Get All Pages**: Automatically fetch paginated API results.
## Installation
Install the package via npm:
```bash
npm install @stephenhebert/axios-plugins
```
## Usage
### Allow 404 Responses
Enable the `allow404` plugin to handle 404 responses gracefully:
```ts
import axios from 'axios'
import { allow404 } from '@stephenhebert/axios-plugins'
const client = axios.create({
plugins: {
allow404: true,
},
})
allow404.install(client)
client.get('http://example.com/api/data')
.then(response => console.log(response.status)) // 404
.catch(error => console.error(error))
```
### Inflight Cache
Prevent duplicate requests by enabling the `inflightCache` plugin:
```ts
import axios from 'axios'
import { inflightCache } from '@stephenhebert/axios-plugins'
const client = axios.create({
plugins: {
inflightCache: true,
},
})
inflightCache.install(client)
client.get('http://example.com/api/data')
.then(response => console.log(response.data))
```
### Get All Pages
Automatically fetch all paginated results:
```ts
import axios from 'axios'
import { getAllPages } from '@stephenhebert/axios-plugins'
const client = axios.create()
getAllPages.install(client)
client.get('http://example.com/api/data', {
plugins: {
getAllPages: true,
},
})
.then(response => console.log(response.data))
```
### With Abort Controller
Wrap request callback in factory decorator to handle aborting previous requests when subsequent requests are made:
```ts
import axios from 'axios'
import { withAbortController } from '@stephenhebert/axios-plugins'
const client = axios.create()
const fetchData = withAbortController( async (signal) => {
try {
await client.get('http://example.com/api/data', { signal } )
}
catch (error) {}
})
fetchData() // starts first request
fetchData() // cancels first request and starts second request
```
## Development
### Build
Run the following command to build the project:
```bash
npm run build
```
### Test
Run the tests using Vitest:
```bash
npm run test
```
### Lint
Lint the codebase using ESLint:
```bash
npm run lint
```
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
## Contributing
Contributions are welcome! Please open an issue or submit a pull request on [GitHub](https://github.com/stephenhebert/axios-plugins).
## Keywords
- axios
- plugins
- middleware
- interceptors
- http
- typescript
- javascript
- api