Skip to content

fetchQuery wiki Page

Abdul Daim edited this page Jan 11, 2025 · 1 revision

FetchQuery Wiki

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.

Table of Contents

Installation

# Using npm
npm install fetchquery

# Using yarn
yarn add fetchquery

# Using pnpm
pnpm add fetchquery

Basic Usage

Simple GET Request

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);
  }
}

POST Request with Data

const response = await fetchQuery.post(
  'https://api.example.com/users',
  {
    name: 'John Doe',
    email: 'john@example.com'
  }
);

Using Custom Headers

const headers = {
  'Authorization': 'Bearer your-token',
  'Custom-Header': 'custom-value'
};

const response = await fetchQuery.get('https://api.example.com/protected', headers);

Advanced Features

Caching Configuration

// 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);

Response Object Structure

{
  status: 200,
  statusText: 'OK',
  headers: {
    'content-type': 'application/json',
    // ... other headers
  },
  config: {
    // Request configuration
  },
  data: {
    // Response data
  }
}

TypeScript Support

Basic TypeScript Usage

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'
  }
);

API Reference

Available Methods

get(url, headers?, skipCache?)

Makes a GET request.

  • url: string (required)
  • headers: object (optional)
  • skipCache: boolean (optional)

post(url, body, headers?, skipCache?)

Makes a POST request.

  • url: string (required)
  • body: any (required)
  • headers: object (optional)
  • skipCache: boolean (optional)

put(url, body, headers?, skipCache?)

Makes a PUT request.

patch(url, body, headers?, skipCache?)

Makes a PATCH request.

delete(url, body?, headers?, skipCache?)

Makes a DELETE request.

Caching System

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

Cache Configuration

// Default cache expiration is 1 hour
// Change cache expiration time
fetchQuery.setCacheExpirationTime(30 * 60 * 1000); // 30 minutes

Error Handling

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);
  }
}

Best Practices

  1. Cache Management

    • Set appropriate cache expiration times based on data volatility
    • Use skipCache for time-sensitive data
  2. Error Handling

    • Always implement try-catch blocks
    • Handle different types of errors appropriately
  3. TypeScript Usage

    • Define interfaces for your data structures
    • Use generic types for better type safety
  4. Headers Management

    • Set default headers for common use cases
    • Override headers when needed

FAQ

Q: How does caching work?

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.

Q: Can I disable caching completely?

A: Yes, you can either set a very short cache expiration time or pass skipCache: true to individual requests.

Q: Does it support request cancellation?

A: Not in the current version, but it's planned for future releases.

Q: How does it handle compression?

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.