Kinetex Universal HTTP Client
raw JSON →Kinetex is an early-stage universal HTTP client designed for a wide range of JavaScript runtimes, including Node.js (v18+), browsers, Deno, Bun, and Cloudflare Workers. Currently at version `0.0.2`, it aims to provide a comprehensive solution with built-in support for advanced features such as HTTP/2 and HTTP/3, schema validation (via Zod or Valibot), a flexible middleware system, and integrated utilities for caching, request deduplication, retries, HAR recording, Server-Sent Events (SSE), and GraphQL. Its key differentiators include its broad runtime compatibility, modern HTTP protocol support, and extensive feature set that often requires multiple separate libraries in other HTTP clients. The project is in active, rapid development, and API stability cannot be guaranteed at this early stage.
Common errors
error Error: Cannot find module 'zod' or its corresponding type declarations. ↓
npm install zod. error ERR_REQUIRE_ESM: require() of ES Module .../node_modules/kinetex/dist/index.js from ... not supported. ↓
"type": "module" to your package.json, ensure your Node.js version is >=18, and use import statements. error TypeError: Cannot read properties of undefined (reading 'get') ↓
import kinetex from 'kinetex'; Warnings
breaking Kinetex is currently in a very early development stage (v0.0.2). Expect frequent and potentially breaking API changes between minor and even patch versions as the project stabilizes. Avoid using in production without thorough testing and readiness for rapid updates. ↓
gotcha Kinetex relies on specific peer dependencies for core functionality like schema validation (Zod, Valibot) and Node.js HTTP client (Undici). These must be installed manually by the user, as Kinetex does not bundle them. Failure to install them will result in runtime errors. ↓
gotcha Kinetex targets modern JavaScript environments and explicitly requires Node.js version 18 or higher. It is an ESM-only package and does not support CommonJS `require()` syntax. Attempting to use it in older Node.js versions or CommonJS modules will lead to import errors. ↓
Install
npm install kinetex yarn add kinetex pnpm add kinetex Imports
- kinetex wrong
import { kinetex } from 'kinetex'; const kinetex = require('kinetex');correctimport kinetex from 'kinetex'; - Kinetex wrong
import Kinetex from 'kinetex'; const { Kinetex } = require('kinetex');correctimport { Kinetex } from 'kinetex'; - defineMiddleware wrong
import defineMiddleware from 'kinetex'; const { defineMiddleware } = require('kinetex');correctimport { defineMiddleware } from 'kinetex';
Quickstart
import kinetex, { defineMiddleware } from 'kinetex';
import { z } from 'zod'; // Assuming Zod is installed: npm install zod
const authMiddleware = defineMiddleware(async (ctx, next) => {
console.log(`Requesting: ${ctx.url}`);
ctx.request.headers.set('Authorization', `Bearer ${process.env.API_TOKEN ?? 'your-secret-token'}`);
const response = await next(ctx);
console.log(`Response status: ${response.status}`);
return response;
});
const userSchema = z.object({
id: z.number(),
name: z.string(),
email: z.string().email(),
});
async function fetchUserData() {
try {
const response = await kinetex
.use(authMiddleware)
.get('https://jsonplaceholder.typicode.com/users/1')
.response(userSchema);
console.log('Fetched User Data:', response.data);
} catch (error) {
if (error instanceof z.ZodError) {
console.error('Validation Error:', error.errors);
} else {
console.error('Fetch Error:', error.message);
}
}
}
fetchUserData();