TezXTezX
API

Response

The TezX framework provides a rich response system via the ctx object. This system makes it easy to return JSON, HTML, text, files, redirects, and custom responses with a clean API.

Core Type Definitions

export type NextCallback = () => Promise<void>; // continue middleware chain

export type HttpBaseResponse = Response | Promise<Response>;

export type Ctx<T extends Record<string, any> = {}, Path extends string = any> =
  Context<T, Path> & T & Record<string, any>;

export type Callback<T = {}, Path extends string = any> =
  (ctx: Ctx<T, Path>) => HttpBaseResponse;

export type Middleware<T = {}, Path extends string = any> =
  (ctx: Ctx<T, Path>, next: NextCallback) =>
    HttpBaseResponse | Promise<HttpBaseResponse | void> | NextCallback;

export type ErrorHandler<T = {}> =
  (err: Error, ctx: Ctx<T>) => HttpBaseResponse;

Native Response

For full control, return a native Response:

app.get("/data", (ctx) => {
  ctx.setHeader("Content-Type", "text/plain" )
  return new Response("Hello World", {
    status: 200,
    headers: ctx.header(),
  });
});

Response Utilities on ctx

ctx.json(body, status?, headers?)

Send JSON.

app.get("/json", (ctx) => ctx.json({ success: true }, 200));
  • Content-Type: application/json

ctx.html(html, status?, headers?)

Send HTML.

app.get("/html", (ctx) => ctx.html("<h1>Welcome</h1>"));
  • Content-Type: text/html

ctx.text(text, status?, headers?)

Send plain text.

app.get("/plain", (ctx) => ctx.text("Just text"));
  • Content-Type: text/plain

ctx.redirect(url, status?, headers?)

Redirect user.

app.get("/go", (ctx) => ctx.redirect("https://example.com"));
  • Default Status: 302
  • Header: Location

ctx.sendFile(filePath, options)

Serve a file from your server. Automatically sets MIME type and streams efficiently. Supports download too.

// Serve normally
app.get("/banner", (ctx) => ctx.sendFile("/assets/banner.jpg"));

// Download with custom filename
app.get("/manual", (ctx) =>
  ctx.sendFile("/assets/manual.pdf", { filename: "guide.pdf" })
);

// Force download as binary
app.get("/raw", (ctx) =>
  ctx.sendFile("/data/archive.zip", { download: true })
);

Options

NameTypeDescription
filenamestringSet filename to force download.
downloadbooleanForce download as binary (application/octet-stream).
headersRecord<string,string> (via ResponseInit)Optional headers like status or extra headers.
  • Download mode: filename or download sets Content-Disposition and Content-Type: application/octet-stream.
  • Normal mode: Automatically detects MIME type and streams file.
  • Throws 404 if file not found.

Best Practices

Use CaseRecommended Method
JSON API responsectx.json()
HTML renderingctx.html()
Plain text outputctx.text()
External redirectsctx.redirect()
Static asset servingctx.sendFile()
Full manual controlNative Response