-
-
Notifications
You must be signed in to change notification settings - Fork 0
fetchQuery wiki Page
Welcome to the FetchQuery wiki! This library provides a simple and efficient way to make HTTP requests with built-in caching support in Node.js applications.
- Installation
- Basic Usage
- Advanced Features
- TypeScript Support
- API Reference
- Caching System
- Error Handling
- Best Practices
- FAQ
# Using npm
npm install fetchquery
# Using yarn
yarn add fetchquery
# Using pnpm
pnpm add fetchquery
const fetchQuery = require('fetchquery');
async function getData() {
try {
const response = await fetchQuery.get('https://api.example.com/data');
console.log(response.data);
} catch (error) {
console.error('Error:', error);
}
}
const response = await fetchQuery.post(
'https://api.example.com/users',
{
name: 'John Doe',
email: 'john@example.com'
}
);
const headers = {
'Authorization': 'Bearer your-token',
'Custom-Header': 'custom-value'
};
const response = await fetchQuery.get('https://api.example.com/protected', headers);
// Set cache expiration time (in milliseconds)
fetchQuery.setCacheExpirationTime(60 * 60 * 1000); // 1 hour
// Skip cache for specific request
const freshData = await fetchQuery.get('https://api.example.com/data', {}, true);
{
status: 200,
statusText: 'OK',
headers: {
'content-type': 'application/json',
// ... other headers
},
config: {
// Request configuration
},
data: {
// Response data
}
}
import fetchQuery from 'fetchquery';
interface User {
id: number;
name: string;
email: string;
}
// GET request with type
const response = await fetchQuery.get<User>('https://api.example.com/user/1');
console.log(response.data.name); // TypeScript aware!
// POST request with type
const newUser = await fetchQuery.post<User>(
'https://api.example.com/users',
{
name: 'John Doe',
email: 'john@example.com'
}
);
Makes a GET request.
-
url
: string (required) -
headers
: object (optional) -
skipCache
: boolean (optional)
Makes a POST request.
-
url
: string (required) -
body
: any (required) -
headers
: object (optional) -
skipCache
: boolean (optional)
Makes a PUT request.
Makes a PATCH request.
Makes a DELETE request.
The library implements an in-memory caching system with the following features:
- Automatic cache invalidation based on expiration time
- Cache key generation based on method, URL, and request body
- Manual cache skipping option
- Automatic cleanup of expired cache entries
// Default cache expiration is 1 hour
// Change cache expiration time
fetchQuery.setCacheExpirationTime(30 * 60 * 1000); // 30 minutes
The library provides comprehensive error handling:
try {
const response = await fetchQuery.get('https://api.example.com/data');
} catch (error) {
if (error.response) {
// Server responded with non-2xx status
console.error('Response Error:', error.response.status);
} else if (error.request) {
// Request was made but no response received
console.error('Request Error:', error.request);
} else {
// Error in setting up the request
console.error('Error:', error.message);
}
}
-
Cache Management
- Set appropriate cache expiration times based on data volatility
- Use skipCache for time-sensitive data
-
Error Handling
- Always implement try-catch blocks
- Handle different types of errors appropriately
-
TypeScript Usage
- Define interfaces for your data structures
- Use generic types for better type safety
-
Headers Management
- Set default headers for common use cases
- Override headers when needed
A: FetchQuery implements an in-memory cache system that stores responses based on the request method, URL, and body. Cache entries expire based on the configured expiration time.
A: Yes, you can either set a very short cache expiration time or pass skipCache: true
to individual requests.
A: Not in the current version, but it's planned for future releases.
A: The library automatically handles gzip, deflate, and brotli compression using Node.js's zlib module.
For more detailed information, check out our API Documentation or Examples.