Skip to content

Response API

The response function are mostly just helpers to ensure things like response JSON objects with a bad status aren't caught by a shell's error catcher.

text

For returning plain text as a response

import { text } from "htmx-router/response";
const r: Response = text( 
  "bad request",
  { statusText: "bad request", status: 400 }
);

json

For returning a json object as a response with type information

import { json } from "htmx-router/response";
const r: Response = json(
  { error: "bad request" },
  { statusText: "bad request", status: 400 }
);
const d = await r.json(); // will resolve as { error: string };

You can use TypedResponse to help unwrap the json type from a response object generated by json()

redirect

This will create a redirect response with both the http redirect headers as well as htmx headers. If you include the option clientOnly == true then it will omit the http redirect header, and instead will cause htmx to just navigate to the new page using boost (if present)

import { redirect } from "htmx-router/response";
const r: Response = redirect("/place", { clientOnly: true });

revalidate

Short hand for redirect("", { clientOnly: true })

import { revalidate } from "htmx-router/response";
const r: Response = revalidate();

refresh

Similar to redirect except it doesn't navigate the page, and instead relies on http-refresh, and hx-refresh instead.

import { refresh } from "htmx-router/response";
const r: Response = refresh();