π New version available! This package was formerly known as
@neylorxt/react-api. The name has been updated to@neylorxt/react-requestfor better clarity and this new version brings many improvements!
@neylorxt/react-request is a lightweight package that simplifies sending HTTP requests with Axios in your React projects. Designed to be simple and accessible for beginners while offering the flexibility needed by experienced developers.
- New universal function:
sendRequest()can handle all types of requests (GET, POST, PUT, DELETE) - Simplified interface: One function for all your requests
- Maximum flexibility: Combines simplicity with the power of Axios
- New signature:
sendData(),updateData(),deleteData()now use anoptionsobject for better clarity - URL parameters support: All functions now support
paramsin the config - Enhanced error handling: Better error detection and categorization
- Included types: No need to install separate types
- Improved IntelliSense: Autocompletion and type checking
React Request was created to simplify your API interactions:
- π― Simple to use: Clear functions for every need
- π‘οΈ Simplified error handling: No more complex
try...catchblocks - π Standardized responses: Consistent format for all responses
- π Flexible: From basic usage to advanced configurations
# Installation with npm
npm install axios @neylorxt/react-request
# Installation with Yarn
yarn add axios @neylorxt/react-request
# Migration from old version
npm uninstall @neylorxt/react-api
npm install @neylorxt/react-request@latestThis is the new star of the package! This function can do everything and greatly simplifies your code.
import { sendRequest } from '@neylorxt/react-request';
// Simple GET
const users = await sendRequest('/api/users');
// POST with data
const newUser = await sendRequest('/api/users', {
method: 'post',
data: { name: 'John', email: 'john@example.com' }
});
// PUT with authentication
const updatedUser = await sendRequest('/api/users/1', {
method: 'put',
data: { name: 'John Updated' },
config: {
headers: { Authorization: `Bearer ${token}` }
}
});
// DELETE with parameters
const result = await sendRequest('/api/users/1', {
method: 'delete',
params: { force: true },
config: {
headers: { Authorization: `Bearer ${token}` }
}
});import { getData } from '@neylorxt/react-request';
// Simple GET
const response = await getData('/api/posts');
// GET with parameters and authentication
const response = await getData('/api/posts', {
params: { page: 2, limit: 10 },
headers: { Authorization: `Bearer ${token}` }
});
if (response.success) {
console.log('Posts:', response.data);
}options object for better clarity.
import { sendData } from '@neylorxt/react-request';
// Old version (still supported)
// const response = await sendData(url, data, config);
// β
New recommended version
const response = await sendData('/api/posts', {
data: { title: 'My post', content: 'Content...' },
config: {
headers: { Authorization: `Bearer ${token}` },
params: { draft: false }
}
});import { updateData } from '@neylorxt/react-request';
const response = await updateData('/api/posts/1', {
data: { title: 'Updated title' },
config: {
headers: { Authorization: `Bearer ${token}` }
}
});import { deleteData } from '@neylorxt/react-request';
const response = await deleteData('/api/posts/1', {
config: {
headers: { Authorization: `Bearer ${token}` },
params: { force: true }
}
});| Property | Type | Description |
|---|---|---|
headers |
object |
Custom headers (Authentication, Content-Type, etc.) |
params |
object |
URL parameters (?page=1&limit=10) |
timeout |
number |
Timeout in milliseconds |
withCredentials |
boolean |
Send cross-domain cookies |
responseType |
string |
Response format ('json', 'blob', 'text') |
import { sendRequest } from '@neylorxt/react-request';
const token = localStorage.getItem('authToken');
const config = {
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
},
params: {
include: 'comments,author',
page: 1
},
timeout: 10000
};
// Fetch posts with comments
const response = await sendRequest('/api/posts', {
method: 'get',
config
});
// Create a new post
const newPost = await sendRequest('/api/posts', {
method: 'post',
data: { title: 'My title', content: 'My content' },
config
});All functions return the same response format:
// On success
{
success: true,
status: 200,
data: { /* server data */ },
headers: { /* response headers */ }
}
// On error
{
success: false,
status: 404,
data: { /* server error data */ },
headers: { /* response headers */ },
errorMessage: "Not Found",
errorType: "HTTP_ERROR" // or "NETWORK_ERROR", "CONFIG_ERROR"
}import { sendRequest } from '@neylorxt/react-request';
// Login
const login = async (credentials) => {
const response = await sendRequest('/api/auth/login', {
method: 'post',
data: credentials
});
if (response.success) {
const token = response.data.token;
localStorage.setItem('authToken', token);
return token;
}
throw new Error(response.errorMessage);
};
// Authenticated request
const fetchUserData = async () => {
const token = localStorage.getItem('authToken');
return await sendRequest('/api/user/profile', {
config: {
headers: { Authorization: `Bearer ${token}` }
}
});
};import { getData } from '@neylorxt/react-request';
const fetchPosts = async (page = 1, filters = {}) => {
return await getData('/api/posts', {
params: {
page,
limit: 20,
...filters
}
});
};
// Usage
const posts = await fetchPosts(1, { category: 'tech', status: 'published' });// Before
import { ... } from '@neylorxt/react-api';
// Now
import { ... } from '@neylorxt/react-request';// Before
const response = await sendData(url, data, config);
const response = await updateData(url, data, config);
const response = await deleteData(url, config);
// Now (recommended)
const response = await sendData(url, { data, config });
const response = await updateData(url, { data, config });
const response = await deleteData(url, { config });
// The old API still works for backward compatibilityThis project is open-source! Your contributions are welcome:
- π Report bugs
- π‘ Suggest improvements
- π Improve documentation
- π§ Submit pull requests
MIT License - Use freely in your projects!