API
TezXRequest
TezXRequest is a thin wrapper around the native Request object.
It makes working with query params, route params, headers, body parsing, and file uploads straightforward.
Explanation:
- Request comes in โ wrapped by
TezXRequest - You can directly access URL, method, pathname, params, query, headers
- For the body โ you can parse as text, JSON, or FormData (with file support)
๐น Properties
url: string
Full request URL.
ctx.req.url;
// "https://example.com/api/user?id=5"method: HTTPMethod
HTTP method (GET, POST, PUT, etc).
ctx.req.method;
// "POST"pathname: string
URL path without query string.
ctx.req.pathname;
// "/api/user"params: Record<string, string>
Route parameters extracted from the router.
// Route: /user/:id
ctx.req.params.id;
// "123"query: Record<string, string>
Query string parsed into a plain object.
// URL: /search?q=js&page=2
ctx.req.query.q; // "js"
ctx.req.query.page; // "2"๐ธ Methods
header(): Record<string, string>
Get all request headers.
const headers = ctx.req.header();
console.log(headers["content-type"]);
// "application/json"header(name: string): string | undefined
Get a specific header (case-insensitive).
const type = ctx.req.header("content-type");
// "application/json"text(): Promise<string>
Read raw body as a string.
const body = await ctx.req.text();
console.log(body);
// '{"username":"rakib"}'json<T = any>(): Promise<T>
Parse body as JSON. Returns {} if content-type isnโt JSON or parsing fails.
const data = await ctx.req.json<{ username: string }>();
console.log(data.username);
// "rakib"formData(): Promise<FormData>
Parse body as FormData. Supports:
application/x-www-form-urlencodedmultipart/form-data(with file uploads)
const form = await ctx.req.formData();
console.log(form.get("name"));
// "Rakib"
console.log(form.getAll("skills"));
// ["js", "ts"]useFormData<T>()
Type-safe helper for form parsing.
import { useFormData } from "tezx/helper";
const data = await useFormData<{ name: string; file: File }>(ctx);
console.log(data.name); // "Rakib"
console.log(data.file); // File objectDeveloper Tips
- Use
paramsfor dynamic routes - Use
queryfor URL query strings - Use
header("content-type")before parsing body - Use
useFormDataif you need typed + file-safe handling - Always
awaitparsing methods (text(),json(),formData())