fluxion-http
v1.0.2
Published
A minimal, zero-dependency HTTP client, status checker, and response formatter for Node.js and TypeScript
Maintainers
Readme
Fluxion HTTP
A minimal, zero-dependency HTTP client, status checker, and response formatter for Node.js and TypeScript.
Features
- Lightweight - No external dependencies, only uses Node.js built-in modules
- TypeScript Support - Fully typed with excellent IDE support
- Status Checking - Check HTTP status codes for multiple URLs
- Dual Format - CommonJS and ES Modules support
- Fast - Minimal overhead, built on Node.js http/https modules
- MIT Licensed - Open source and free to use
Installation
npm install fluxion-http
or
yarn add fluxion-httpUsage
Basic HTTP Request
import { request } from 'fluxion-http';
const data = await request({
url: 'https://jsonplaceholder.typicode.com/users/1'
});
console.log(data);POST Request with Body
import { request } from 'fluxion-http';
const response = await request({
url: 'https://jsonplaceholder.typicode.com/posts',
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {
title: 'Hello World',
body: 'This is a test post',
userId: 1
}
});
console.log(response);Check Multiple URLs
import { statusCheck } from 'fluxion-http';
const results = await statusCheck([
'https://google.com',
'https://example.com',
'https://github.com'
]);
console.log(results);
// [
// { url: 'https://google.com', status: 200, ok: true, responseTimeMs: 150 },
// { url: 'https://example.com', status: 200, ok: true, responseTimeMs: 200 },
// { url: 'https://github.com', status: 200, ok: true, responseTimeMs: 180 }
// ]Parse Response Data
import { parseResponse } from 'fluxion-http';
const data = parseResponse('{"name":"John"}', 'application/json');
console.log(data); // { name: 'John' }API Reference
request(options)
Make an HTTP request and return parsed JSON response.
Options:
url(string, required) - The URL to requestmethod(string, optional) - HTTP method (GET, POST, PUT, DELETE, etc.). Defaults to 'GET'headers(object, optional) - Custom HTTP headersbody(any, optional) - Request body (will be stringified as JSON)timeout(number, optional) - Request timeout in milliseconds
Returns: Promise resolving to parsed response data (or raw string if not JSON)
statusCheck(urls)
Check HTTP status codes for multiple URLs.
Parameters:
urls(string[]) - Array of URLs to check
Returns: Promise resolving to array of StatusResult objects
StatusResult:
{
url: string;
status: number;
ok: boolean;
responseTimeMs: number;
}parseResponse(data, contentType?)
Parse response data based on content type.
Parameters:
data(unknown) - The data to parsecontentType(string, optional) - MIME type to determine parsing strategy
Returns: Parsed data or null if parsing fails
Examples
Health Check Service
import { statusCheck } from 'fluxion-http';
async function checkServices() {
const urls = [
'https://api.example.com',
'https://db.example.com',
'https://cache.example.com'
];
const results = await statusCheck(urls);
const allHealthy = results.every(r => r.ok);
console.log(allHealthy ? 'All services OK' : 'Some services down');
return results;
}API Data Fetching
import { request } from 'fluxion-http';
async function fetchUserData(userId: number) {
try {
const user = await request({
url: `https://api.example.com/users/${userId}`,
timeout: 5000
});
return user;
} catch (error) {
console.error('Failed to fetch user:', error);
throw error;
}
}TypeScript Support
All types are included and fully typed:
import { request, statusCheck, parseResponse } from 'fluxion-http';
import type { RequestOptions, StatusResult } from 'fluxion-http';
const options: RequestOptions = {
url: 'https://api.example.com/data',
method: 'POST',
headers: { 'Authorization': 'Bearer token' },
body: { key: 'value' }
};
const results: StatusResult[] = await statusCheck(['https://example.com']);Requirements
Node.js 14.0.0 or higher