| .. | ||
| _ | ||
| deno.jsonc | ||
| mod.ts | ||
| readme.md | ||
-/req
tiny fetch wrapper for deno, node, and the browser
features
- lightweight:
~500loc2lightweight 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 fornode: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
undicibecause it's faster and has a nicer API - rewritten now as
-/reqin deno/ts using nativefetch