Request
Harpia Core provides an enhanced Request
class that extends Bun’s native Request
, giving you everything you’re used to—plus a few extra tools that make backend development smoother.
To use it in your routes:
import { type Request } from "harpiats";
req.params
Section titled “req.params”Auto-parsed route parameters based on your route definition:
router.get("/users/:id", (req, res) => { const userId = req.params.id; res.send(`User ID is ${userId}`);});
req.query
Section titled “req.query”Auto-parsed query strings as a key-value object:
// For a URL like /search?q=harpia&page=2router.get("/search", (req, res) => { const { q, page } = req.query; res.send(`Searching for "${q}", page ${page}`);});
req.method
Section titled “req.method”Always returns the HTTP method in uppercase (e.g., "GET"
, "POST"
):
router.get("/inspect", (req, res) => { res.send(`Request method is ${req.method}`);});
req.cookies
Section titled “req.cookies”Simplified cookie management:
// Single cookieconst token = req.cookies.get("auth_token");
// All cookiesconst allCookies = req.cookies.getAll();
More information about Cookies is available in the Cookies section.
Native Request Methods
Section titled “Native Request Methods”Since Harpia’s Request
class extends Bun’s native one, you still have access to all original methods:
await req.json()
Section titled “await req.json()”Parses the request body as JSON.
const body = await req.json();
await req.formData()
Section titled “await req.formData()”Parses the body as form data.
const form = await req.formData();const file = form.get("avatar");
req.url
Section titled “req.url”Access the full request URL:
const fullUrl = req.url;
req.text()
Section titled “req.text()”Reads the body as plain text.
const text = await req.text();
req.redirect(url, status?)
Section titled “req.redirect(url, status?)”Redirects to another URL.
return req.redirect("/login", 302);
Note: In most cases, you’ll want to use
res.redirect()
instead, which is part of Harpia’s response utilities.
TypeScript Tip
Section titled “TypeScript Tip”To get full IntelliSense and type support, always annotate the request:
import { type Request, type Response } from "harpiats";
router.get("/profile", (req: Request, res: Response) => { const cookies = req.cookies.getAll(); res.json({ cookies });});