miniget - Small HTTP(S) GET Client

4.2.3 · active · verified Wed Apr 22

miniget is a lightweight HTTP(S) GET request library designed for both Node.js and browser environments, emphasizing minimal dependencies and a small footprint. It provides core functionalities like automatic redirects (up to 10 by default), request retries for 5xx or connection errors, and reconnects for interrupted downloads, allowing streams to resume. The current stable version is `4.2.3`, with releases typically addressing bug fixes and minor features rather than following a strict semantic versioning cadence between major versions. Key differentiators include its zero-dependency nature, support for streaming responses via a readable stream, and methods for concatenating responses into a single text body. It also exposes a configurable `defaultOptions` object for global settings and ships with TypeScript type definitions, making it well-suited for modern TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both promise-based text retrieval and streaming a response to a file using `miniget`, including error handling and event listeners for redirects and responses. It also shows how to configure global default options.

import miniget from 'miniget';
import { createWriteStream } from 'node:fs';

async function fetchAndStreamData(url: string, filePath: string) {
  try {
    // Fetch and concatenate content as text
    const textBody = await miniget(url).text();
    console.log(`Fetched text from ${url.substring(0, 30)}... : ${textBody.substring(0, 50)}...`);

    // Stream content to a file
    const stream = miniget(url, { maxRedirects: 5, maxRetries: 1 });
    stream.on('redirect', (newUrl) => console.log(`Redirected to: ${newUrl}`));
    stream.on('error', (err) => console.error(`Stream error: ${err.message}`));
    stream.on('response', (res) => console.log(`Received status: ${res.statusCode}`));

    const fileStream = createWriteStream(filePath);
    stream.pipe(fileStream);

    await new Promise<void>((resolve, reject) => {
      fileStream.on('finish', () => {
        console.log(`Streamed content to ${filePath}`);
        resolve();
      });
      fileStream.on('error', reject);
    });

  } catch (error: any) {
    console.error(`Failed to fetch or stream: ${error.message}`);
  }
}

// Example usage with a placeholder URL
// Replace with a valid URL for actual execution
const exampleUrl = 'https://jsonplaceholder.typicode.com/posts/1';
const outputFilePath = './output.json';
fetchAndStreamData(exampleUrl, outputFilePath);

// You can also modify global defaults
miniget.defaultOptions.headers = { 'User-Agent': 'miniget-example-app' };
console.log('Global User-Agent set.');

view raw JSON →