streamfetch v0.8.1
StreamFetch
Welcome to StreamFetch – your new best friend for making HTTP requests in Node.js! If you’re looking for a lightweight, no-nonsense HTTP client with zero dependencies, you’ve come to the right place. Perfect for lightweight AI applications, StreamFetch is here to make your life easier.
Why StreamFetch?
Why choose StreamFetch over the gazillion other HTTP clients out there? Great question!
- Super Simple: StreamFetch is so easy to use, even your grandma could do it (well, maybe not, but you get the point).
- No Dependencies: I don’t believe in baggage. StreamFetch is dependency-free, which means it's lightweight and fast.
- GET and POST Requests: Need to fetch data or send it? I've got you covered with simple methods for both.
- Streaming Data: Handle streaming data like a pro with the optional
onData
callback. - Handles Redirects and Timeouts: I’ve thought of everything so you don’t have to.
Installation
Install StreamFetch with npm and get started in seconds:
npm install streamfetch
Usage
Basic Examples
Example 1: Simple GET Request
import { get } from "streamfetch";
// Perform a GET request
async function fetchData() {
try {
const response = await get("http://www.example.com");
if (response.ok) {
const data = await response.text();
console.log(data);
} else {
console.error("Network response was not ok.");
}
} catch (error) {
console.error("Fetch error:", error);
}
}
fetchData();
Example 2: Streaming Data in an Express App from the Goq API
import express from "express";
import { get } from "streamfetch";
const app = express();
app.get("/stream-groq", async (req, res) => {
try {
await get("https://api.groq.com/openai/v1/chat/completions", {
headers: {
Authorization: `Bearer YOUR_OPENAI_API_KEY`,
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [
{
role: "user",
content:
"Explain the importance of fast language models",
},
],
model: "llama3-8b-8192",
}),
onData: (chunk) => {
res.write(chunk);
},
});
res.end();
} catch (error) {
console.error("Fetch error:", error);
res.status(500).send("An error occurred while fetching data");
}
});
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});
More Usage Examples
Perform a POST Request
import { post } from "streamfetch";
async function postData() {
try {
const response = await post(
"https://jsonplaceholder.typicode.com/posts",
{
title: "foo",
body: "bar",
userId: 1,
},
{
headers: {
"Content-Type": "application/json",
},
}
);
if (response.ok) {
const data = await response.json();
console.log(data);
} else {
console.error("Network response was not ok.");
}
} catch (error) {
console.error("Fetch error:", error);
}
}
postData();
Handle Streaming Data
import { get } from "streamfetch";
async function fetchDataWithStream() {
try {
const chunks = [];
await get("http://www.example.com", {
onData: (chunk) => {
console.log("Received chunk:", chunk);
chunks.push(chunk.toString());
},
});
console.log("Complete response:", chunks.join(""));
} catch (error) {
console.error("Fetch error:", error);
}
}
fetchDataWithStream();
API
fetch(url, options)
Perform an HTTP request.
url
(string): The URL to fetch.options
(object):method
(string): The HTTP method to use (default: 'GET').headers
(object): The headers to include in the request.body
(string|object): The body of the request for POST/PUT methods.onData
(function): Callback function to handle streaming data.followRedirects
(boolean): Whether to follow redirects automatically (default: true).maxRedirects
(number): Maximum number of redirects to follow (default: 5).timeout
(number): Timeout in milliseconds for the request.signal
(AbortSignal): Abort signal to cancel the request.
Returns: A promise that resolves to the response object.
get(url, options)
Perform a GET request.
url
(string): The URL to fetch.options
(object): The options for the request.
Returns: A promise that resolves to the response object.
post(url, body, options)
Perform a POST request.
url
(string): The URL to fetch.body
(object): The body of the request.options
(object): The options for the request.
Returns: A promise that resolves to the response object.
Running Tests
I've even made testing a breeze. To run the tests, ensure you have Jest installed and use the following command:
npm test
License
StreamFetch is proudly licensed under the MIT License. See the LICENSE file for more details.
Contributing
I love contributions! Found a bug? Have an idea for an enhancement? Open an issue or submit a pull request. Let’s make StreamFetch even better together!
Acknowledgements
StreamFetch was inspired by the need for a lightweight and flexible HTTP client for Node.js, particularly suited for lightweight AI applications. Because sometimes, you just want things to work without the bloat.
Happy fetching! 🚀