Package Exports
- turtlefetch
- turtlefetch/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (turtlefetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
TurtleFetch
TurtleFetch is a lightweight and easy-to-use HTTP client that simplifies API fetching with built-in features like automatic retries, timeout handling, and flexible configuration. It works seamlessly in both Node.js and browser environments.
Features
- 🌐 Base URL Support: Set a base URL for all API requests to avoid repetition.
- 🔄 Automatic Retries: Automatically retries failed requests up to a configurable limit.
- ⏳ Timeout Handling: Aborts requests that exceed a specified timeout duration.
- 🛠️ Convenience Methods: Includes
get
,post
,put
, anddelete
methods for common HTTP operations. - 🧰 Flexible Configuration: Easily configure headers, authentication tokens, and other options.
Installation
Install TurtleFetch via NPM:
npm install turtlefetch
Usage
Importing the Package
const TurtleFetch = require('turtlefetch');
Initialize TurtleFetch
Create an instance of TurtleFetch with a base URL and default options:
const apiClient = new TurtleFetch('https://api.example.com', { headers: { Authorization: 'Bearer YOUR_TOKEN', 'Content-Type': 'application/json', }, });
Make API Requests
GET Request
apiClient.get('/data') .then((data) => console.log('GET Response:', data)) .catch((error) => console.error('GET Error:', error));
POST Request
apiClient.post('/data', { key: 'value' }) .then((response) => console.log('POST Response:', response)) .catch((error) => console.error('POST Error:', error));
PUT Request
apiClient.put('/data/1', { key: 'updatedValue' }) .then((response) => console.log('PUT Response:', response)) .catch((error) => console.error('PUT Error:', error));
DELETE Request
apiClient.delete('/data/1') .then((response) => console.log('DELETE Response:', response)) .catch((error) => console.error('DELETE Error:', error));
Configuration Options
When initializing TurtleFetch, you can pass the following options:
Option | Type | Description |
---|---|---|
baseURL |
string |
The base URL for all API requests. |
defaultOptions |
object |
Default options for all requests (e.g., headers). |
Advanced Features
Automatic Retries
If a request fails (e.g., due to network issues or server errors), TurtleFetch will automatically retry the request up to the specified number of retries.
Example:
apiClient.get('/unstable-endpoint', {}, 5) // Retry up to 5 times .then((data) => console.log(data)) .catch((error) => console.error(error));
Timeout Handling
You can specify a timeout duration (in milliseconds) for each request. If the request takes longer than this duration, it will be aborted.
Example:
apiClient.get('/slow-endpoint', {}, 3, 3000) // Timeout after 3 seconds .then((data) => console.log(data)) .catch((error) => console.error(error));
Error Handling
TurtleFetch throws an error if:
- The request fails after all retries.
- The server responds with a non-2xx status code.
- The request times out.
Example:
apiClient.get('/nonexistent-endpoint') .catch((error) => { console.error('Error Message:', error.message); console.error('Stack Trace:', error.stack); });
Why Use TurtleFetch?
- Simplifies repetitive tasks like setting headers and handling retries.
- Provides robust timeout and error-handling mechanisms.
- Lightweight and easy to integrate into any JavaScript or Node.js project.
Log
1.0.0 - released 1.0.1 - added log 1.0.2 - fixed readme.md punctuation 1.0.3 - fixed readme.md punctuation 1.0.4 - fixed readme.md punctuation 1.0.5 - fixed readme.md punctuation