Lightweight Node.js HTTP Client
btch-http is a lightweight and type-safe HTTP client for Node.js, built directly on the Node.js `https` module, ensuring zero external dependencies. The current stable version is 2.0.3, with recent minor updates following a major version 2.0.0 release. It differentiates itself by offering full TypeScript support out-of-the-box, smart error handling that consolidates network, timeout, and HTTP status code (>=300) errors into a custom `HttpError` class, and comprehensive timeout support. It provides both a quick one-liner `HttpGet` function for simple GET requests with URL encoding and a full-featured `HttpClient` class for more complex interactions involving configurable base URLs, versions, default headers, and POST requests with JSON bodies or custom headers. It requires Node.js >=20.18.1.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax to import `btch-http` in an environment configured for ES modules.fixUse ES module import syntax: `import { SymbolName } from 'btch-http';` -
TypeError: btch_http_1.HttpClient is not a constructor
cause Incorrect import of `HttpClient` or attempting to use it in a CommonJS context when the package is ESM-only.fixEnsure you are using `import { HttpClient } from 'btch-http';` and your project is configured for ES modules, with a compatible Node.js version. -
Failed to download TikTok data: Network Error (or similar message within try/catch)
cause A network connectivity issue, DNS resolution failure, or the target server refused the connection.fixVerify your internet connection, ensure the target `baseUrl` is correct and accessible, and check firewall settings. The `HttpError` will contain details about the underlying network issue.
Warnings
- breaking Version 2.0.0 of `btch-http` switched to being an ESM-only package. CommonJS `require()` statements are no longer supported.
- breaking With version 2.0.0, the package's engine requirement was updated to Node.js `>=20.18.1`. Running on older Node.js versions will result in errors.
- gotcha The `HttpGet` function automatically appends the second URL argument as a query parameter (e.g., `?url=...`) to the endpoint. This implicit behavior might be unexpected if not explicitly known.
- gotcha btch-http's error handling is strict: it throws an `HttpError` for any HTTP status code >= 300, as well as network errors, timeouts, or JSON parsing failures. This might differ from other HTTP clients that only throw for 5xx status codes.
Install
-
npm install btch-http -
yarn add btch-http -
pnpm add btch-http
Imports
- HttpGet
const { HttpGet } = require('btch-http');import { HttpGet } from 'btch-http'; - HttpClient
const { HttpClient } = require('btch-http');import { HttpClient } from 'btch-http'; - HttpError
const { HttpError } = require('btch-http');import { HttpError } from 'btch-http'; - HttpClientConfig
import { HttpClientConfig } from 'btch-http';import type { HttpClientConfig } from 'btch-http';
Quickstart
import { HttpGet } from 'btch-http';
const tiktokVideoUrl = 'https://www.tiktok.com/@omagadsus/video/7025456384175017243';
async function downloadTikTokData() {
try {
// The `HttpGet` function automatically appends `?url=...` for the second argument.
const data = await HttpGet(
'ttdl',
tiktokVideoUrl,
'1.0.0', // Client version
30000, // Timeout in milliseconds
'https://backend1.tioo.eu.org' // Base URL for the endpoint
);
console.log('Download URL:', data.video);
console.log('Raw data:', data);
} catch (error) {
if (error instanceof Error) {
console.error('Failed to download TikTok data:', error.message);
} else {
console.error('An unknown error occurred:', error);
}
}
}
downloadTikTokData();