turtlefetch
Version:
Eaisily fetch from api with turtlefetch!
139 lines (80 loc) • 3.88 kB
Markdown
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.
- 🌐 **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`, and `delete` methods for common HTTP operations.
- 🧰 **Flexible Configuration:** Easily configure headers, authentication tokens, and other options.
Install TurtleFetch via NPM:
npm install turtlefetch
const TurtleFetch = require('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',
},
});
apiClient.get('/data')
.then((data) => console.log('GET Response:', data))
.catch((error) => console.error('GET Error:', error));
apiClient.post('/data', { key: 'value' })
.then((response) => console.log('POST Response:', response))
.catch((error) => console.error('POST Error:', error));
apiClient.put('/data/1', { key: 'updatedValue' })
.then((response) => console.log('PUT Response:', response))
.catch((error) => console.error('PUT Error:', error));
apiClient.delete('/data/1')
.then((response) => console.log('DELETE Response:', response))
.catch((error) => console.error('DELETE Error:', error));
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));
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));
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);
});
- 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.
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
TurtleFetch