OmniRequest is a simple, easy-to-use HTTP client for making API requests in JavaScript and TypeScript applications. It works in browsers, Node.js, and other JavaScript environments.
- π Simple API - Similar to Axios with a clean, intuitive interface
- π Works Everywhere - Browser, Node.js, and other JavaScript environments
- π¦ Lightweight - Small footprint with minimal dependencies
- π TypeScript Support - Full type definitions for better development experience
- βοΈ Configurable - Easy to customize for your specific needs
# Using npm
npm install omnirequest
# Using yarn
yarn add omnirequest
# Using pnpm
pnpm add omnirequest
// Using ES modules (recommended)
import omnirequest from 'omnirequest';
// Using CommonJS
const omnirequest = require('omnirequest');
// Simple GET request
omnirequest.get('https://api.example.com/users')
.then(response => {
console.log(response.data); // The response data
})
.catch(error => {
console.error('Error:', error);
});
async function getUsers() {
try {
const response = await omnirequest.get('https://api.example.com/users');
console.log(response.data);
} catch (error) {
console.error('Error:', error);
}
}
getUsers();
omnirequest.post('https://api.example.com/users', {
name: 'John Doe',
email: 'john@example.com'
})
.then(response => {
console.log('User created:', response.data);
})
.catch(error => {
console.error('Error creating user:', error);
});
omnirequest.get('https://api.example.com/users', {
params: {
page: 1,
limit: 10,
sort: 'name'
}
})
.then(response => {
console.log('Users:', response.data);
});
// Create a custom instance with specific configuration
const api = omnirequest.create({
baseURL: 'https://api.example.com',
headers: {
'Authorization': 'Bearer YOUR_TOKEN',
'Content-Type': 'application/json'
},
timeout: 5000 // 5 seconds
});
// Now use this instance for all requests
api.get('/users'); // This will request https://api.example.com/users
api.post('/posts', { title: 'New Post' });
// GET request
omnirequest.get(url, config);
// POST request
omnirequest.post(url, data, config);
// PUT request
omnirequest.put(url, data, config);
// PATCH request
omnirequest.patch(url, data, config);
// DELETE request
omnirequest.delete(url, config);
// HEAD request
omnirequest.head(url, config);
// OPTIONS request
omnirequest.options(url, config);
You can customize your requests with various options:
omnirequest.get('https://api.example.com/data', {
// URL parameters
params: {
id: 123,
filter: 'active'
},
// Request headers
headers: {
'Authorization': 'Bearer token',
'Accept': 'application/json'
},
// Request timeout in milliseconds
timeout: 3000,
// Whether to include credentials for cross-site requests
withCredentials: true,
// Response type
responseType: 'json' // 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
});
// Import the library
import omnirequest from 'omnirequest';
// Create a custom API client
const api = omnirequest.create({
baseURL: 'https://api.example.com',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_TOKEN'
}
});
// Function to get users
async function getUsers() {
try {
// Get users with pagination
const response = await api.get('/users', {
params: { page: 1, limit: 10 }
});
console.log('Users:', response.data);
return response.data;
} catch (error) {
console.error('Error fetching users:', error);
throw error;
}
}
// Function to create a user
async function createUser(userData) {
try {
const response = await api.post('/users', userData);
console.log('User created:', response.data);
return response.data;
} catch (error) {
console.error('Error creating user:', error);
throw error;
}
}
// Use the functions
getUsers();
createUser({
name: 'John Doe',
email: 'john@example.com',
role: 'user'
});
Every request returns a response object with these properties:
{
// The response data (parsed JSON or string)
data: {},
// HTTP status code
status: 200,
// HTTP status message
statusText: 'OK',
// Response headers
headers: {},
// The original request configuration
config: {}
}
OmniRequest provides detailed error information:
try {
const response = await omnirequest.get('https://api.example.com/users');
console.log(response.data);
} catch (error) {
if (error.response) {
// The server responded with an error status code (4xx, 5xx)
console.error('Server error:', error.response.status);
console.error('Error data:', error.response.data);
} else if (error.request) {
// The request was made but no response was received
console.error('No response received');
} else {
// Something else went wrong
console.error('Error:', error.message);
}
}
// GET request
omnirequest.get(url, config);
// POST request
omnirequest.post(url, data, config);
// PUT request
omnirequest.put(url, data, config);
// PATCH request
omnirequest.patch(url, data, config);
// DELETE request
omnirequest.delete(url, config);
// HEAD request
omnirequest.head(url, config);
// OPTIONS request
omnirequest.options(url, config);
// Generic request method
omnirequest.request(config);
{
// URL for the request
url: 'https://api.example.com/users',
// HTTP method
method: 'GET',
// Base URL that will be prepended to the URL
baseURL: 'https://api.example.com',
// URL parameters (added to the query string)
params: {
page: 1,
limit: 10
},
// Request body for POST, PUT, PATCH
data: {
name: 'John Doe'
},
// Request headers
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer token'
},
// Request timeout in milliseconds
timeout: 5000,
// Whether to include credentials for cross-site requests
withCredentials: true,
// Response type
responseType: 'json' // 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
}
OmniRequest works in all modern browsers and Node.js:
- Chrome
- Firefox
- Safari
- Edge
- Node.js 14+
- GitHub Issues - Bug reports and feature requests
- GitHub Discussions - Questions and discussions
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Please read our Contributing Guide for more information.
If you discover a security vulnerability within OmniRequest, please follow our Security Policy.