npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@yj01jung/httpclient

v1.0.4

Published

Fetch based light http client.

Downloads

4

Readme

HttpClient GitHub stars npm (scoped)

Fetch based modern http client for web, nodejs, deno

// index.ts
import http from "@yj01jung/httpclient";

http.baseURL("https://api.example.com/");

const data = await http.post<FooResponse>("foo/baz", {
  json: { foo: [1, 2, 3, 4, 5] },
  query: { query: "welcome" },
  headers: { "Foo-Bar": "httpclient" },
});

Features

Light

  • Only 2kb(gzipped, all middlewares included) for modern environment
  • Tree shaking enabled (core is 1kb)

TypeScript Support

  • All core library, middlwares is statically typed

Middlwares

  • You can add your powerful middlewares
  • We already built timeout, auth middlwares

Automatic Mocking

  • in non-browser environment, user-agent is automatically set to latest Chrome

TypeScript Support

  • All core library, middlwares is typed

Usage

Default Client

We already built global HttpClient singleton like axios

import http from "@yj01jung/httpclient";
// default import

const json = await http.get<TodoResponse>(`todos/1`); // json

const text = await http.get(`hello.txt`).text(); // string
const binary = await http.get(`images.png`).binary(); // Uint8array

Sending Data

json

await http.post<RegisterResponse>(`/register`, {
  json: { foo: 1, bar: 2 },
});

form-data

// single file
const [myFile] = event.target.files;
await http.post<UploadResult>(`/upload-single-file`, {
  form: { foo: 1, myFile },
});

// multiple file
await http.post<UploadResult>(`/upload-multiple-file`, {
  form: { files: event.target.files },
});

Setting Headers and URL

import http from "@yj01jung/httpclient";

// request specific
http.get("/endpoint", {
  baseURL: "https://api.example.com/"
  headers: {
    "x-custom-headers": foo,
  },
});

// applied to all requests
http.baseURL("https://api.example.com/")

http.headers("x-custom-header", foo);
http.headers({
  referer: "https://example.com",
  "user-agent": randomUserAgent(),
});

Middlwares

Recommend Usage (creating new client instance)

import { createClient, timeout } from "@yj01jung/httpclient";

const client = createClient(timeout(3000), myMiddlware({ foo: "bar" }));

Discouraged (express like usage, not typed)

import http, { timeout } from "@yj01jung/httpclient";

http.use(timeout(3000));
http.use(myMiddlware({ foo: "bar" })

Timeout Middleware

Global timeout

// 1000 ms global timeout
const http = createClient(timeout(1000));

// request specitific timeout

http.get("/slow", { timeout: ms("1min") });

Auth middleware

Simplifies JWT (token based auth)

const http = createClient(auth());

// token is automatically parsed and saved
await http.post<LoginResponse>("/auth/login", { json: { id: "user", pw: "pass" } });

console.log(http.storage.token); // Bearer eyJhbGciOiJIUzUx...

await http.get("/auth/refresh");
await http.get<MyPrivateData>("/my/private");

Error Handling

Unlike default Fetch, non-successful(400x 500x) status will throw HttpError

try {
  await http.get("https://www.google.co.kr/404").text()
} catch (e) {
  console.log(e) // HttpError: 404 Not Found
  console.log(e instanceof HttpError) // true
  console.log(e.status) // 404
  console.log(e.statusText) // Not Found
}

Deno support

import http from "https://esm.sh/@yj01jung/[email protected]";
console.log(await http.get("https://dl.deno.land/release-latest.txt").text());

V2 Roadmap

  • Electron support
  • GraphQL support

Changelog

v1.0.3

  • fix HttpError

v1.0.2

  • fix deno typing

1.0.0

  • First release

License

MIT