deno/lib/req
2025-09-26 19:38:24 +02:00
..
_ prepare for jsr 2024-06-10 01:58:09 +01:00
deno.jsonc update -/data version 2024-06-10 02:34:55 +01:00
mod.ts prepare for jsr 2024-06-10 01:58:09 +01:00
readme.md lowercase readme 2025-09-26 19:38:24 +02:00

-/req

tiny fetch wrapper for deno, node, and the browser

features

  • lightweight:
    • ~500 loc
    • 2 lightweight dependencies
  • composable
  • extendable
  • augments types of native fetch (e.g. allows type parameter .json<T>())
  • consistent, strongly typed api

usage

import req, { status } from "-/req";

await req("https://api.example")
  .path("/api/v1")
  .path("/users", id)
  .timeout(5_000)
  .post() // or .method("POST")
  .data({
    id,
    name,
    tags
  }, "json"); // tries to guess if no type provided, also supports: "form", "buffer", "multipart"

const attachment = await req("https://api.example")
  .path("/api/v1", "/messages", id, "/attachments/0")
  .redirect(false) // false | number
  .blob();

const succeeded = attachment[status] === 200;

// quick and clean api wrappers
const api = req("https://api.example")
  .path("/api/v1")
  .auth(token, "bot") // assumes bearer if no name provided
  .agent("example-client/1.0"); // sets req/:version (+:link_to_repo) by default

// GET /api/v1/users/:id
const user = await api("users", id);
// GET /api/v1/users?tags=test
const test_users = await api("users", { tags: "test" });
// POST /api/v1/users/:id with json body
const { status } = await api("POST", "users", id, { id, name, tags });

// custom request finalizer
const api = req("https://api.example")
  .header("accept", "application/vnd.msgpack")
  .finalizer(to_msgpack);

dependencies

  • path (unjs/pathe)
    as a lighter drop-in for node:path
  • @std/encoding
    for basic auth

history

  • originally inspired by centra (node:http, node:https)
  • forked as @aero/centra to add bonus features, types, and cleanup some code
  • rewritten as @aero/http with undici because it's faster and has a nicer API
  • rewritten now as -/req in deno/ts using native fetch