Nyre-Fetch is a simple fetch wrapper with a few extra features โก๏ธ.
npm install nyre-fetch
- Supports everything from Node.js v18 fetch API
- Simplified API for HTTP methods (
get
,post
,put
,delete
, &head
) - Supports
stream.pipeTo
andstream.pipeThrough
- Allows setting base URL for all requests
import nyreFetch from "nyre-fetch";
// 1. basic get request
nyreFetch
.get("https://example.com")
.then((res) => res.json())
.then((json) => console.log(json));
// 2. download a file using streams
import fs from "node:fs";
const url = "https://example.com/file.pdf";
const readableStream = await nyreFetch.stream(url);
const writeStream = fs.createWriteStream("./file.pdf");
await readableStream.pipeTo(writeStream);
// 3. set base URL for all requests
import { createClient } from "nyre-fetch";
const client = createClient("https://example.com");
client
.get("/api/users")
.then((res) => res.json())
.then((json) => console.log(json));
An object that exposes HTTP methods (GET, POST, PUT, DELETE, HEAD, and stream) as property references.
Same global fetch API from node v18, exposed for convenience.
A class that allows setting a base URL for all requests.
import { Client } from "nyre-fetch";
const client = new Client("https://example.com");
A utility function to create a new Client instance.
const client = createClient("https://example.com");
client
.get("/api/users")
.then((res) => res.json())
.then((json) => console.log(json));
The "Node.js way" is to use streams when possible, piping response.body
to other streams. It's built on the node:stream module and exposes pipeTo
and pipeThrough
methods.
const BASE_URL = "https://jsonplaceholder.typicode.com";
const client = createClient(BASE_URL);
const stream = await client.stream("/posts/1");
const data = [];
const writeStream = new Writable({
write(chunk, _, done) {
data.push(chunk);
done();
},
});
await stream.pipeTo(writeStream);
const post = JSON.parse(Buffer.concat(data).toString());
assert(post.id, 1); // => true;
const BASE_URL = "https://jsonplaceholder.typicode.com";
const client = createClient(BASE_URL);
const stream = await client.stream("/posts/1");
const transform = new Transform({
transform(chunk, _, cb) {
this.push(chunk);
cb();
},
});
const data = [];
transform.on("data", (chunk) => {
data.push(chunk);
});
await stream.pipeThrough(transform);
const post = JSON.parse(Buffer.concat(data).toString());
assert(post.id, 1); // => true;