Skip to content

Repository files navigation

nklient

A modern, feature-rich HTTP request client for Node.js with support for HTTPS, HTTP/2, cookies, retry logic, interceptors, and more.

CICoveragenpm versionLicense

Features

  • Promise-based API with async/await support
  • HTTPS support with certificate validation options
  • Cookie management with automatic cookie jar
  • Retry logic with exponential backoff
  • Request/Response interceptors for middleware functionality
  • Streaming support for large files
  • Proxy support for HTTP/HTTPS proxies
  • Compression handling (gzip, deflate, brotli)
  • Redirect following with method change support
  • Query string and form data helpers
  • Timeout support with configurable delays
  • Custom agents for connection pooling
  • TypeScript definitions included
  • Zero external dependencies (only optional peer dependencies)

Installation

npm install nklient

Quick Start

constnklient=require('nklient');// Simple GET requestconstresponse=awaitnklient.get('https://api.example.com/users').exec();console.log(response.body);// POST request with JSONconstnewUser=awaitnklient.post('https://api.example.com/users').json({name: 'John Doe',email: 'john@example.com'}).exec();// Using async/awaittry{constdata=awaitnklient.get('https://api.example.com/data').headers('Authorization','Bearer token').query({page: 1,limit: 10}).exec();console.log(data.body);}catch(error){console.error('Request failed:',error.message);}

API Reference

HTTP Methods

All HTTP methods return a RequestWrapper instance that can be configured with chainable methods.

nklient.get(url)nklient.post(url)nklient.put(url)nklient.patch(url)nklient.delete(url)nklient.head(url)nklient.options(url)

Request Configuration

Headers

// Set individual headernklient.get(url).headers('Authorization','Bearer token').headers('X-Custom','value')// Set multiple headersnklient.get(url).headers({'Authorization': 'Bearer token','X-Custom': 'value'})

Request Body

// JSON body (auto-sets Content-Type)nklient.post(url).json({key: 'value'})// Form datanklient.post(url).form({username: 'john',password: 'secret'})// Raw bodynklient.post(url).body('raw string data')nklient.post(url).body(Buffer.from('binary data'))

Query Parameters

nklient.get(url).query({page: 1,limit: 10})// Results in: url?page=1&limit=10

Timeout

nklient.get(url).timeout(5000)// 5 seconds

Cookies

// Use custom cookie jarconstjar=nklient.jar();nklient.get(url).jar(jar)// Disable cookies for a requestnklient.get(url).noJar()

Retry Configuration

nklient.get(url).retry({attempts: 3,delay: 1000,maxDelay: 10000,backoff: 2,retryOn: [408,429,500,502,503,504]})

Other Options

nklient.get(url).maxRedirects(5)// Maximum number of redirects.encoding('utf8')// Response encoding (null for Buffer).stream()// Get response as stream.rejectUnauthorized(false)// Disable SSL certificate validation.proxy('http://proxy.example.com:8080')// Use proxy.agent(customAgent)// Use custom HTTP agent

Response Object

{statusCode: 200,headers: {'content-type': 'application/json',// ... other headers},body: {/* parsed JSON or string/Buffer */},request: {uri: 'https://example.com/api',method: 'GET',headers: {/* request headers */}}}

Interceptors

Add middleware to requests and responses:

// Request interceptorconstrequestId=nklient.interceptors.request.use(async(config)=>{config.headers['X-Request-ID']=generateId();returnconfig;});// Response interceptorconstresponseId=nklient.interceptors.response.use(async(response)=>{console.log(`Request took ${response.duration}ms`);returnresponse;});// Remove interceptornklient.interceptors.request.eject(requestId);nklient.interceptors.response.eject(responseId);

Custom Instances

Create instances with custom defaults:

constapi=nklient.create({headers: {'Authorization': 'Bearer token','Content-Type': 'application/json'},timeout: 10000,retry: {attempts: 5,delay: 1000}});// Use instanceconstresponse=awaitapi.get('/users').exec();

Global Configuration

// Set global defaultsnklient.defaults({timeout: 30000,headers: {'User-Agent': 'MyApp/1.0'}});

Advanced Examples

File Upload with Streaming

constfs=require('fs');constresponse=awaitnklient.post('https://api.example.com/upload').headers('Content-Type','application/octet-stream').body(fs.createReadStream('large-file.zip')).exec();

Download File with Progress

constfs=require('fs');constresponse=awaitnklient.get('https://example.com/large-file.zip').stream().exec();constfileStream=fs.createWriteStream('downloaded-file.zip');letdownloaded=0;response.body.on('data',(chunk)=>{downloaded+=chunk.length;console.log(`Downloaded: ${downloaded} bytes`);});response.body.pipe(fileStream);

Error Handling with Retry

try{constresponse=awaitnklient.get('https://flaky-api.example.com/data').retry({attempts: 3,delay: 1000,backoff: 2,retryOn: [408,429,500,502,503,504]}).timeout(5000).exec();console.log('Success:',response.body);}catch(error){if(error.code==='ETIMEDOUT'){console.error('Request timed out');}elseif(error.code==='ECONNREFUSED'){console.error('Connection refused');}else{console.error('Request failed:',error.message);}}

Using with Proxy

constresponse=awaitnklient.get('https://api.example.com/data').proxy('http://proxy.company.com:8080').exec();

Cookie Management

constjar=nklient.jar();// Login request - cookies are savedawaitnklient.post('https://api.example.com/login').jar(jar).json({username: 'user',password: 'pass'}).exec();// Subsequent requests use saved cookiesconstprofile=awaitnklient.get('https://api.example.com/profile').jar(jar).exec();

Development

# Install dependencies
npm install
# Run tests
npm test# Run tests with coverage
npm run test:coverage
# Run tests in watch mode
npm run test:watch
# Lint code
npm run lint
# Format code
npm run format

Development Status

Features Implemented

All core features are implemented and working:

  • Retry Logic - Exponential backoff with configurable retry conditions
  • Cookie Handling - Automatic cookie management with tough-cookie
  • Request Cancellation - AbortController support for request cancellation
  • Streaming Support - Request and response streaming with progress tracking
  • Plugin System - Extensible plugin architecture for custom functionality
  • Proxy Support - HTTP/HTTPS proxy support with proper agent handling
  • Browser Build - Browser-compatible version using fetch API

Test Coverage

Current Status: 33 passing tests, 25 failing tests (mostly due to nock mocking issues)

Passing Test Areas:

  • Basic HTTP methods (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
  • HTTPS support and certificate validation
  • Headers, query parameters, and request body handling
  • Cookie management and retry logic
  • Interceptors and streaming support
  • Custom agents and createClient functionality

Known Issues:

  • Timeout and redirect tests fail due to nock interference
  • Some error handling scenarios need real network testing
  • Memory leak tests require actual resource monitoring

Coverage Gaps:

  • Response stream error handling
  • Cookie error scenarios
  • Configuration loading edge cases
  • Integration tests without mocking

License

Apache License 2.0

About

Http request client in NodeJS

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages