Giter Club home page Giter Club logo

nyre-fetch's Introduction

Nyre-Fetch

Nyre-Fetch is a simple fetch wrapper with a few extra features โšก๏ธ.

๐Ÿ“ฆ Installation

npm install nyre-fetch

๐ŸŒŸ Features

  • Supports everything from Node.js v18 fetch API
  • Simplified API for HTTP methods (get, post, put, delete, & head)
  • Supports stream.pipeTo and stream.pipeThrough
  • Allows setting base URL for all requests

๐Ÿ› ๏ธ Usage

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));

๐Ÿ“š API

nyreFetch

An object that exposes HTTP methods (GET, POST, PUT, DELETE, HEAD, and stream) as property references.

fetch

Same global fetch API from node v18, exposed for convenience.

Client

A class that allows setting a base URL for all requests.

import { Client } from "nyre-fetch";

const client = new Client("https://example.com");

createClient

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));

๐Ÿ“ก Stream API

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.

๐Ÿšฐ pipeTo(writableStream: NodeJS.WritableStream, options?: PipelineOptions)

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;

๐Ÿ”€ pipeThrough(transformStream: Transform, options?: PipelineOptions)

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;

nyre-fetch's People

Contributors

nwaughachukwuma avatar

Stargazers

Ankit Prakash avatar Jefferson Martines avatar Roman avatar Jarrian Gojar avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.