GitHub 0
Skip to content

API reference

Every export of whoasks, whoasks/core and whoasks/h3, with signatures, the seven types, the built-in forms, response headers and errors.
import { whoasks, mdream } from "whoasks";
import { whoasks as core } from "whoasks/core";
import { middleware } from "whoasks/h3";
ts
ImportHolds
whoaskswhoasks with mdream as its converter, mdream, and the seven types
whoasks/corewhoasks with no converter, and the seven types. It has no imports and stays under 10 kB
whoasks/h3middleware
function whoasks(...layers: Layer[]): WhoAsks;
ts
  • From whoasks: { convert: mdream() } is the first layer. From whoasks/core: no converter until a layer sets one.
  • Merge order: later forms replace earlier ones, renderers and hooks run in layer order, and the last convert and the last strict win. See How layers merge.
function mdream(options?: Partial<MdreamOptions>): Convert;
ts
  • Exported from whoasks only.
  • Calls mdream's htmlToMarkdown(html, { minimal: true, origin: url.origin, ...options }). Your options override the two defaults. MdreamOptions is mdream's own type.
function middleware(site: WhoAsks): Middleware;
ts
  • From whoasks/h3. Middleware is h3's type. Needs h3 2.0.8 or later.
  • Use: new H3().use(middleware(site)).
interface WhoAsks {
  readonly forms: Readonly<Record<string, Form>>;
  ask(request: Request, offers?: readonly string[]): Ask;
  respond(
    request: Request,
    renders: Record<string, unknown>,
    init?: ResponseInit,
  ): Promise<Response>;
  handle(
    request: Request,
    next: (request: Request) => Response | Promise<Response>,
  ): Promise<Response>;
}
ts

The merged forms: html, markdown, json, plus what layers added.

  • Synchronous. Reads the URL and the Accept header, then runs the hooks.
  • offers lists form names, default first. Without it: html, then each form that has a renderer, then markdown when a converter is set.
  • Throws whoasks: no form named "<name>" for an offer that is not a form.
  • See Negotiation.
  • The keys of renders are the offers, and the first is the default.
  • A value is a string, a Response, anything JSON.stringify takes, or a function returning one of those.
  • With html and no markdown, and a converter set, it also offers markdown made from the HTML.
  • With strict, answers 406 when nothing offered is acceptable.
  • Throws whoasks: no form named "<name>" for a key that is not a form.
  • See API routes.
  • next is the app. It gets the request it should answer: the original one, or the canonical URL for a twin or ?format=.
  • Passes any method but GET and HEAD to next untouched.
  • Never answers 406.
  • See Pages.
HeaderSet by whoasks
Content-TypeThe picked type. ; charset=utf-8 is added to text/* types. Not changed on a Response you return
VaryEach entry of ask.vary, once. Existing values are kept
Content-Length, Content-Encoding, ETagRemoved from a page that was converted
StatusKept from the app's page, from init, or from a Response you return. 406 only from a strict respond
interface Ask {
  form: string;
  type: string;
  reason: "twin" | "query" | "accept" | "default" | "layer";
  acceptable: boolean;
  url: URL;
  vary: string[];
}
ts
FieldMeaning
formName of the picked form
typeMedia type to send, without parameters
reasonWhat decided. See Read the pick
acceptableFalse when the request allowed none of the offered types
urlThe request URL without the twin suffix and without ?format=
varyRequest headers the pick depended on. Starts as ["Accept"]
interface Form {
  types: readonly string[];
  ext?: string;
}
ts
FieldMeaning
typesMedia types that name this form in Accept. A wildcard or a URL override gets the first
extSuffix of the twin URL, such as .md. Without the dot it is also a ?format= value

Built-in forms:

FormMedia typesext
htmltext/htmlnone
markdowntext/markdown, text/x-markdown, text/plain.md
jsonapplication/jsonnone
interface Layer {
  forms?: Record<string, Form>;
  render?: Record<string, Render>;
  ask?(ask: Ask, request: Request): Partial<Pick<Ask, "form" | "type" | "vary">> | void;
  convert?: Convert | false;
  strict?: boolean;
}
ts
FieldMeaningDefault
formsAdd a form, or replace a built-in one by nameThe three built-in forms
renderProduce a form for a URL in handle. Keyed by form nameNone
askChange the pick. A change the request did not allow is droppedNone
convertThe HTML to Markdown converter, or false for nonemdream() from whoasks, none from whoasks/core
strictIn respond, answer 406 when the request allowed nothing offeredfalse

See Layers.

type Render = (
  ctx: RenderContext,
) => string | Response | null | undefined | Promise<string | Response | null | undefined>;

interface RenderContext {
  request: Request;
  ask: Ask;
  next(): Promise<Response>;
}
ts
  • Returns the body for a form, or nothing to let the next renderer try.
  • next() is the app's own response for ask.url. The app runs once, and each call returns a fresh copy.
type Convert = (html: string, ctx: { request: Request; url: URL }) => string | Promise<string>;
ts
  • url is the canonical URL, request the request as it arrived.

Ask, Convert, Form, Layer, Render, RenderContext and WhoAsks are exported from whoasks and from whoasks/core.