Skip to main content

HttpClient

TypeScript

Fetch based modern http client, supports web, nodejs, deno

How to use

import { createClient, timeout } from "httpclient"; // https://deno.land/x/httpclient/mod.ts

const client = createClient(timeout(10000)) // this is middleware
  .baseURL("https://jsonplaceholder.typicode.com")
  .headers({ authorization: "awesome" });

const json = await client.get<TodoResponse>(`todos/1`); // .json() can be skipped
const json2 = await client.get<TodoResponse>(`todos/1`).json(); // do same thing

const text = await client.get(`hello.txt`).text(); // string
const binary = await client.get(`images.png`).binary(); // Uint8array
// any typedarray
await client.get('binarymath.file').binary(Float32Array) 

const data = await client.post<FooResponse>("foo/baz", {
  json: { foo: `bar` },
  query: { query: "welcome" },
  headers: { "x-requested-by": "httpclient" },
});

// POST foo/baz?query=welcome
// Content-Type: application/json
// X-Requested-By: httpclient
// Accept: application/json
//
// {"foo":"bar"}

try {
  await client.get(`https://404notfound.com`);
} catch (e) {
  if (e instanceof HttpError) {
    console.log(e.status); // 404
  }
}

built with typescript-lib-starter

License

MIT