2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# @push.rocks/smartrequest
  
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								A modern, cross-platform HTTP/HTTPS request library for Node.js and browsers with a unified API, supporting form data, file uploads, JSON, binary data, streams, and unix sockets.
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Install
  
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```bash
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Using npm
  
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								npm install @push .rocks/smartrequest --save
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								# Using pnpm
  
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								pnpm add @push .rocks/smartrequest
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								# Using yarn
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								yarn add @push .rocks/smartrequest
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Key Features
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  🚀 **Modern Fetch-like API**  - Familiar response methods (`.json()` , `.text()` , `.arrayBuffer()` , `.stream()` ) 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  🌐 **Cross-Platform**  - Works in both Node.js and browsers with a unified API 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  🔌 **Unix Socket Support**  - Connect to local services like Docker (Node.js only) 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  📦 **Form Data & File Uploads**  - Built-in support for multipart/form-data 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  🔁 **Pagination Support**  - Multiple strategies (offset, cursor, Link headers) 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  ⚡ **Keep-Alive Connections**  - Efficient connection pooling in Node.js 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  🛡️ **TypeScript First**  - Full type safety and IntelliSense support 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  🎯 **Zero Magic Defaults**  - Explicit configuration following fetch API principles 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  📡 **Streaming Support**  - Stream buffers, files, and custom data without loading into memory 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  🔧 **Highly Configurable**  - Timeouts, retries, headers, and more 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								## Architecture
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								SmartRequest v3.0 features a multi-layer architecture that provides consistent behavior across platforms:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  **Core Base** - Abstract classes and unified types shared across implementations 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  **Core Node** - Node.js implementation using native http/https modules 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  **Core Fetch** - Browser implementation using the Fetch API 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  **Core** - Dynamic implementation selection based on environment 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  **Client** - High-level fluent API for everyday use 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-01-12 19:32:20 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								## Usage
  
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								`@push.rocks/smartrequest`  provides a clean, type-safe API inspired by the native fetch API but with additional features needed for modern applications. 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Basic Usage
  
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Simple GET request
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchUserData(userId: number) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url(`https://jsonplaceholder.typicode.com/users/${userId}` )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Use the fetch-like response API
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const userData = await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log(userData); // The parsed JSON response
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// POST request with JSON body
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function createPost(title: string, body: string, userId: number) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url('https://jsonplaceholder.typicode.com/posts')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .json({ title, body, userId })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const createdPost = await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log(createdPost); // The created post
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Direct Core API Usage
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For advanced use cases, you can use the Core API directly:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import { CoreRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function directCoreRequest() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const request = new CoreRequest('https://api.example.com/data', {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    method: 'GET',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    headers: {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      Accept: 'application/json',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    },
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await request.fire();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const data = await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return data;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Setting Headers and Query Parameters
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function searchRepositories(query: string, perPage: number = 10) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url('https://api.github.com/search/repositories')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .header('Accept', 'application/vnd.github.v3+json')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .query({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      q: query,
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      per_page: perPage.toString(),
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const data = await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return data.items;
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Handling Timeouts and Retries
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchWithRetry(url: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url(url)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .timeout(5000) // 5 seconds timeout
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .retry(3) // Retry up to 3 times on failure
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Setting Request Options
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Use the `options()`  method to set any request options supported by the underlying implementation:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Set various options
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .url('https://api.example.com/data')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .options({
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    keepAlive: true, // Enable connection reuse (Node.js)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    timeout: 10000, // 10 second timeout
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    hardDataCuttingTimeout: 15000, // 15 second hard timeout
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Platform-specific options are also supported
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Working with Different Response Types
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The API provides a fetch-like interface for handling different response types:
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// JSON response (default)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchJson(url: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create().url(url).get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return await response.json(); // Parses JSON automatically
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Text response
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchText(url: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create().url(url).get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return await response.text(); // Returns response as string
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Binary data
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function downloadImage(url: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url(url)
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .accept('binary') // Optional: hints to server we want binary
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const buffer = await response.arrayBuffer();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return Buffer.from(buffer); // Convert ArrayBuffer to Buffer if needed
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Streaming response (Web Streams API)
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								async function streamLargeFile(url: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create().url(url).get();
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Get a web-style ReadableStream (works in both Node.js and browsers)
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const stream = response.stream();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  if (stream) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const reader = stream.getReader();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    try {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      while (true) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        const { done, value } = await reader.read();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        if (done) break;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        console.log(`Received ${value.length} bytes of data` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } finally {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      reader.releaseLock();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Node.js specific stream (only in Node.js environment)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function streamWithNodeApi(url: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create().url(url).get();
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Only available in Node.js, throws error in browser
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const nodeStream = response.streamNode();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  nodeStream.on('data', (chunk) => {
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    console.log(`Received ${chunk.length} bytes of data` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return new Promise((resolve, reject) => {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    nodeStream.on('end', resolve);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    nodeStream.on('error', reject);
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Response Object Methods
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The response object provides these methods:
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `json<T>(): Promise<T>`  - Parse response as JSON 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `text(): Promise<string>`  - Get response as text 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `arrayBuffer(): Promise<ArrayBuffer>`  - Get response as ArrayBuffer 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  `stream(): ReadableStream<Uint8Array> | null`  - Get web-style ReadableStream (cross-platform) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `streamNode(): NodeJS.ReadableStream`  - Get Node.js stream (Node.js only, throws in browser) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  `raw(): Response | http.IncomingMessage`  - Get the underlying platform response 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Each body method can only be called once per response, similar to the fetch API.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Important: Always Consume Response Bodies
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**You should always consume response bodies, even if you don't need the data.** Unconsumed response bodies can cause:
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  Memory leaks as data accumulates in buffers 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Socket hanging with keep-alive connections 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Connection pool exhaustion 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// ❌ BAD - Response body is not consumed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .url('https://api.example.com/status')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								if (response.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log('Success!');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Socket may hang here!
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// ✅ GOOD - Response body is consumed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .url('https://api.example.com/status')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								if (response.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log('Success!');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								await response.text(); // Consume the body even if not needed
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								In Node.js, SmartRequest automatically drains unconsumed responses to prevent socket hanging, but it's still best practice to explicitly consume response bodies. When auto-drain occurs, you'll see a console log: `Auto-draining unconsumed response body for [URL] (status: [STATUS])` .
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								You can disable auto-drain if needed:
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Disable auto-drain (not recommended unless you have specific requirements)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .url('https://api.example.com/data')
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  .autoDrain(false) // Disable auto-drain
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 15:44:04 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Now you MUST consume the body or the socket will hang
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								await response.text();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Advanced Features
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Form Data with File Uploads
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import * as fs from 'fs';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								async function uploadMultipleFiles(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  files: Array< { name: string; path: string }>,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const formFields = files.map((file) => ({
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    name: 'files',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    value: fs.readFileSync(file.path),
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    filename: file.name,
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    contentType: 'application/octet-stream',
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  }));
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url('https://api.example.com/upload')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .formData(formFields)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Streaming Request Bodies
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								SmartRequest provides multiple ways to stream data in requests, making it easy to upload large files or send real-time data without loading everything into memory:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import * as fs from 'fs';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import { Readable } from 'stream';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Stream a Buffer directly
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function uploadBuffer() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const buffer = Buffer.from('Hello, World!');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/upload')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .buffer(buffer, 'text/plain')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Stream a file using Node.js streams
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function uploadLargeFile(filePath: string) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const fileStream = fs.createReadStream(filePath);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/upload')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .stream(fileStream, 'application/octet-stream')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Stream data from any readable source
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function streamData(dataSource: Readable) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/stream')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .stream(dataSource)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Advanced: Full control over request streaming (Node.js only)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function customStreaming() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/stream')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .raw((request) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      // Custom streaming logic - you have full control
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      request.write('chunk1');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      request.write('chunk2');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      // Stream from another source
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      someReadableStream.pipe(request);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Send Uint8Array (works in both Node.js and browser)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function uploadBinaryData() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const data = new Uint8Array([72, 101, 108, 108, 111]); // "Hello"
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/binary')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .buffer(data, 'application/octet-stream')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .post();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								#### Streaming Methods
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  **`.buffer(data, contentType?)` ** - Stream a Buffer or Uint8Array directly 
						 
					
						
							
								
									
										
										
										
											2025-08-19 01:36:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  `data` : Buffer (Node.js) or Uint8Array (both platforms) to send
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  `contentType` : Optional content type (defaults to 'application/octet-stream')
							 
						 
					
						
							
								
									
										
										
										
											2025-08-19 01:36:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  ✅ Works in both Node.js and browsers
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-19 01:36:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  **`.stream(stream, contentType?)` ** - Stream from ReadableStream 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  `stream` : Web ReadableStream (both platforms) or Node.js stream (Node.js only)
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  `contentType` : Optional content type
							 
						 
					
						
							
								
									
										
										
										
											2025-08-19 01:36:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  ✅ Web ReadableStream works in both Node.js and browsers
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  ⚠️ Node.js streams only work in Node.js environment
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-19 01:36:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  **`.raw(streamFunc)` ** - Advanced control over request streaming 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  `streamFunc` : Function that receives the raw request object for custom streaming
							 
						 
					
						
							
								
									
										
										
										
											2025-08-19 01:36:44 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  -  ❌ **Node.js only**  - not supported in browsers
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  -  Use for advanced scenarios like chunked transfer encoding
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 22:29:24 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								These methods are particularly useful for:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Uploading large files without loading them into memory 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Streaming real-time data to servers 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Proxying data between services 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Implementing chunked transfer encoding 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Unix Socket Support (Node.js only)
  
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Connect to a service via Unix socket
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function queryViaUnixSocket() {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url('http://unix:/var/run/docker.sock:/v1.24/containers/json')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Pagination Support
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								The library includes built-in support for various pagination strategies:
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Offset-based pagination (page &  limit)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchAllUsers() {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const client = SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url('https://api.example.com/users')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .withOffsetPagination({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      pageParam: 'page',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      limitParam: 'limit',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      startPage: 1,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      pageSize: 20,
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      totalPath: 'meta.total',
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Get first page with pagination info
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const firstPage = await client.getPaginated();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log(`Found ${firstPage.items.length} users on first page` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log(`Has more pages: ${firstPage.hasNextPage}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  if (firstPage.hasNextPage) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    // Get next page
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const secondPage = await firstPage.getNextPage();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    console.log(`Found ${secondPage.items.length} more users` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  // Or get all pages at once (use with caution for large datasets)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const allUsers = await client.getAllPages();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log(`Retrieved ${allUsers.length} users in total` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Cursor-based pagination
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchAllPosts() {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const allPosts = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url('https://api.example.com/posts')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .withCursorPagination({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      cursorParam: 'cursor',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      cursorPath: 'meta.nextCursor',
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      hasMorePath: 'meta.hasMore',
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .getAllPages();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  console.log(`Retrieved ${allPosts.length} posts in total` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Link header-based pagination (GitHub API style)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchAllIssues(repo: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const paginatedResponse = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .url(`https://api.github.com/repos/${repo}/issues` )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .header('Accept', 'application/vnd.github.v3+json')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .withLinkPagination()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .getPaginated();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return paginatedResponse.getAllPages();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Keep-Alive Connections (Node.js)
  
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Enable keep-alive for better performance with multiple requests
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function performMultipleRequests() {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Note: keepAlive is NOT enabled by default
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response1 = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/endpoint1')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .options({ keepAlive: true })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  const response2 = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/endpoint2')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .options({ keepAlive: true })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  // Connections are pooled and reused when keepAlive is enabled
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return [await response1.json(), await response2.json()];
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:49:50 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Rate Limiting (429 Too Many Requests) Handling
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The library includes built-in support for handling HTTP 429 (Too Many Requests) responses with intelligent backoff:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Simple usage - handle 429 with defaults
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchWithRateLimitHandling() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/data')
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .handle429Backoff() // Automatically retry on 429
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:49:50 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Advanced usage with custom configuration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchWithCustomRateLimiting() {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .url('https://api.example.com/data')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .handle429Backoff({
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      maxRetries: 5, // Try up to 5 times (default: 3)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      respectRetryAfter: true, // Honor Retry-After header (default: true)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      maxWaitTime: 30000, // Max 30 seconds wait (default: 60000)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      fallbackDelay: 2000, // 2s initial delay if no Retry-After (default: 1000)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      backoffFactor: 2, // Exponential backoff multiplier (default: 2)
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:49:50 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      onRateLimit: (attempt, waitTime) => {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        console.log(`Rate limited. Attempt ${attempt}, waiting ${waitTime}ms` );
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      },
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:49:50 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Example: API client with rate limit handling
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class RateLimitedApiClient {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  private async request(path: string) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .url(`https://api.example.com${path}` )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .handle429Backoff({
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        maxRetries: 3,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        onRateLimit: (attempt, waitTime) => {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								          console.log(
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            `API rate limit hit. Waiting ${waitTime}ms before retry ${attempt}` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								          );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        },
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:49:50 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  async fetchData(id: string) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const response = await this.request(`/data/${id}` ).get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The rate limiting feature:
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:49:50 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								-  Automatically detects 429 responses and retries with backoff 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Respects the `Retry-After`  header when present (supports both seconds and HTTP date formats) 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Uses exponential backoff when no `Retry-After`  header is provided 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Allows custom callbacks for monitoring rate limit events 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-  Caps maximum wait time to prevent excessive delays 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Platform-Specific Features
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Browser-Specific Options
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When running in a browser, you can use browser-specific fetch options:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .url('https://api.example.com/data')
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  .options({
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    credentials: 'include', // Include cookies
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    mode: 'cors', // CORS mode
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    cache: 'no-cache', // Cache mode
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    referrerPolicy: 'no-referrer',
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Node.js-Specific Options
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								When running in Node.js, you can use Node-specific options:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import { Agent } from 'https';
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const response = await SmartRequest.create()
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .url('https://api.example.com/data')
							 
						 
					
						
							
								
									
										
										
										
											2025-07-29 13:19:43 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  .options({
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    agent: new Agent({ keepAlive: true }), // Custom agent
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    socketPath: '/var/run/api.sock', // Unix socket
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  })
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  .get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Complete Example: Building a REST API Client
  
						 
					
						
							
								
									
										
										
										
											2025-04-03 06:52:58 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Here's a complete example of building a typed API client:
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest, type CoreResponse } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								interface User {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  id: number;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  name: string;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  email: string;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								interface Post {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  id: number;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  title: string;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  body: string;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  userId: number;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								class BlogApiClient {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  private baseUrl = 'https://jsonplaceholder.typicode.com';
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  private async request(path: string) {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      .url(`${this.baseUrl}${path}` )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .header('Accept', 'application/json');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  async getUser(id: number): Promise< User >  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const response = await this.request(`/users/${id}` ).get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return response.json< User > ();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  async createPost(post: Omit< Post ,  ' id ' > ): Promise< Post >  {
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const response = await this.request('/posts').json(post).post();
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    return response.json< Post > ();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  async deletePost(id: number): Promise< void >  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const response = await this.request(`/posts/${id}` ).delete();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if (!response.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      throw new Error(`Failed to delete post: ${response.statusText}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								  async getAllPosts(userId?: number): Promise< Post [ ] >  {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const client = this.request('/posts');
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if (userId) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      client.query({ userId: userId.toString() });
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const response = await client.get();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    return response.json< Post [ ] > ();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Usage
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const api = new BlogApiClient();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const user = await api.getUser(1);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								const posts = await api.getAllPosts(user.id);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Error Handling
  
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```typescript
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								import { SmartRequest } from '@push .rocks/smartrequest';
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								async function fetchWithErrorHandling(url: string) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  try {
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    const response = await SmartRequest.create()
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								      .url(url)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .timeout(5000)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .retry(2)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      .get();
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // Check if request was successful
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (!response.ok) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      throw new Error(`HTTP ${response.status}: ${response.statusText}` );
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // Handle different content types
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    const contentType = response.headers['content-type'];
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    if (contentType?.includes('application/json')) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return await response.json();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else if (contentType?.includes('text/')) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return await response.text();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      return await response.arrayBuffer();
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  } catch (error) {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    if (error.code === 'ECONNREFUSED') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      console.error('Connection refused - is the server running?');
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    } else if (error.code === 'ETIMEDOUT') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      console.error('Request timed out');
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } else if (error.name === 'AbortError') {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      console.error('Request was aborted');
							 
						 
					
						
							
								
									
										
										
										
											2025-07-27 21:23:20 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    } else {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								      console.error('Request failed:', error.message);
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    throw error;
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								  }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								```
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## Migrating from v2.x to v3.x
  
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Version 3.0 brings significant architectural improvements and a more consistent API:
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 23:20:52 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								1.  **Legacy API Removed** : The function-based API (getJson, postJson, etc.) has been removed. Use SmartRequest instead. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								2.  **Unified Response API** : All responses now use the same fetch-like interface regardless of platform. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								3.  **Stream Changes** : The `stream()`  method now returns a web-style ReadableStream on all platforms. Use `streamNode()`  for Node.js streams. 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								4.  **Cross-Platform by Default** : The library now works in browsers out of the box with automatic platform detection. 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								## License and Legal Information
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license ](license ) file within this repository.
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								### Trademarks
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
							 
						 
					
						
							
								
									
										
										
										
											2020-01-12 19:32:20 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								### Company Information
  
						 
					
						
							
								
									
										
										
										
											2020-01-12 19:32:20 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-07-28 07:45:37 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Task Venture Capital GmbH  
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Registered at District court Bremen HRB 35230 HB, Germany
							 
						 
					
						
							
								
									
										
										
										
											2020-01-12 19:32:20 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2024-04-14 03:54:39 +02:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								For any legal inquiries or if you require further information, please contact us via email at hello@task .vc.
							 
						 
					
						
							
								
									
										
										
										
											2020-01-12 19:32:20 +00:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2025-08-18 00:21:14 +00:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.