[]previous and next page

RequestEvent

The state of one request.

The state of one request. It is available as the argument of middleware, API routes and server functions, as context.event in beforeLoad and loaders, and through getRequestEvent() where the runtime has AsyncLocalStorage.

Import#

ts
import type { RequestEvent } from "@shadow-js/dusk/server";

Signature#

ts
interface RequestEvent {
  /** The request, with its body limited to the configured size. */
  readonly request: Request;
  /**
   * The body as bytes, exactly as sent (a byte order mark and invalid UTF-8 kept). Read once: a second call returns the
   * same bytes, and so does `readJson` afterward. Bounded by the route's body limit: a larger body gives 413. Use it to
   * verify a webhook signature over the bytes that were signed.
   * @example
   * const bytes = await event.rawBody();
   * if (!verify(bytes, event.request.headers.get("x-signature"))) return json({ error: "bad" }, { status: 401 });
   * @since 1.0.0
   */
  rawBody(): Promise<Uint8Array<ArrayBuffer>>;
  /**
   * The URL the middleware is checking. For a client navigation check (`isNavigationCheck`) it is the
   * TARGET of the navigation (path and search), not the `/_dusk/nav` request, so a middleware can build
   * `redirect_url` from it.
   */
  readonly url: URL;
  readonly method: string;
  /**
   * True while the browser's navigation check runs the chain of its target (`POST /_dusk/nav`), before
   * the client loads anything. A middleware that redirects here sends the browser to sign-in with
   * `redirect_url` set to `url`. False for every document request.
   */
  readonly isNavigationCheck: boolean;
  /** Aborted when the client disconnects or the request times out. */
  readonly signal: AbortSignal;
  /** Random id of this request, for logs. Never taken from the client. */
  readonly id: string;
  /** CSP nonce of this request, when a CSP or `nonce: true` is configured. Put it on inline scripts. */
  readonly nonce: string | undefined;
  readonly cookies: CookieJar;
  /** Headers added to the response of this request (cookies are added separately). */
  readonly responseHeaders: Headers;
  /**
   * Sets whether the framework validates this response with entity tags: `false` opts out (no generated ETag, and a
   * route's own ETag answers 200 even to a matching `If-None-Match`); `true` gives an API answer a generated weak ETag
   * (the body is read to compute it). Without a call, a cacheable page gets one (see `cache.etag`).
   * @example
   * export const GET = () => { event.etag(true); return json(data); }; // a public API answer with 304 support
   * @since 1.0.0
   */
  etag(enabled: boolean): void;
  /**
   * Your own metrics for the `Server-Timing` header and the `onTimings` hook (see `serverTiming`). Without
   * `serverTiming` (the production default) its methods run your function and record nothing.
   * @example
   * const rows = await event.timing.measure("db", () => query(sql), "orders");
   * @since 1.0.0
   */
  readonly timing: EventTiming;
  /** The validated environment (see `defineEnv`). Empty when no schema is configured. */
  readonly env: Readonly<Record<string, unknown>>;
  /** The value the host passed as the second argument of `fetch(request, env)`. */
  readonly platform: unknown;
  /** Address of the client as the host reports it (after the trusted-proxy rules), if known. */
  readonly clientAddress: string | undefined;
  /** True in development mode: error details are shown. */
  readonly dev: boolean;
  /** The app's mount path (`""` at the root, `/app` under `base`). Relative metadata URLs resolve against it. */
  readonly base: string;
  /** The app's `trailingSlash` policy. Metadata routes (sitemaps) apply it to their URLs. */
  readonly trailingSlash: "never" | "always" | "preserve";
  /**
   * Where the visitor is, as the platform reported it (Cloudflare, Vercel, Netlify; see `GeoInfo`). Every field is
   * `undefined` when there is no platform source, so a client's own headers are never used.
   */
  readonly geo: GeoInfo;
  /** True while draft (preview) mode is on for this request (see `draftMode()`). */
  readonly draft: boolean;
  /** Attributes of `<html>`; set them before the page renders. */
  readonly document: DocumentAttributes;
  /** The matched route. `kind` is `"none"` until the route is resolved. */
  readonly route: RouteInfo;
  /** Stores a value under a typed key. */
  set<T>(key: ContextKey<T>, value: T): void;
  /** Reads a value; throws when the key was never set. */
  get<T>(key: ContextKey<T>): T;
  /** Reads a value, or `undefined`. */
  tryGet<T>(key: ContextKey<T>): T | undefined;
  /** Route params of an API route or page (decoded). */
  readonly params: Readonly<Record<string, string>>;
  /**
   * Request-scoped values set by middleware and read by guards, loaders and server functions. Typed
   * through the {@link DuskLocals} interface. Server only: never sent to the client by the framework.
   */
  readonly locals: DuskLocals;
}

Members#

Name Type Description
request readonly request: Request The request, with its body limited to the configured size.
rawBody rawBody(): Promise<Uint8Array<ArrayBuffer>> The body as bytes, exactly as sent (a byte order mark and invalid UTF-8 kept). Read once: a second call returns the same bytes, and so does readJson afterward. Bounded by the route's body limit: a larger body gives 413. Use it to verify a webhook signature over the bytes that were signed.
url readonly url: URL The URL the middleware is checking. For a client navigation check (isNavigationCheck) it is the TARGET of the navigation (path and search), not the /_dusk/nav request, so a middleware can build redirect_url from it.
method readonly method: string
isNavigationCheck readonly isNavigationCheck: boolean True while the browser's navigation check runs the chain of its target (POST /_dusk/nav), before the client loads anything. A middleware that redirects here sends the browser to sign-in with redirect_url set to url. False for every document request.
signal readonly signal: AbortSignal Aborted when the client disconnects or the request times out.
id readonly id: string Random id of this request, for logs. Never taken from the client.
nonce readonly nonce: string | undefined CSP nonce of this request, when a CSP or nonce: true is configured. Put it on inline scripts.
cookies readonly cookies: CookieJar
responseHeaders readonly responseHeaders: Headers Headers added to the response of this request (cookies are added separately).
etag etag(enabled: boolean): void Sets whether the framework validates this response with entity tags: false opts out (no generated ETag, and a route's own ETag answers 200 even to a matching If-None-Match); true gives an API answer a generated weak ETag (the body is read to compute it). Without a call, a cacheable page gets one (see cache.etag).
timing readonly timing: EventTiming Your own metrics for the Server-Timing header and the onTimings hook (see serverTiming). Without serverTiming (the production default) its methods run your function and record nothing.
env readonly env: Readonly<Record<string, unknown>> The validated environment (see defineEnv). Empty when no schema is configured.
platform readonly platform: unknown The value the host passed as the second argument of fetch(request, env).
clientAddress readonly clientAddress: string | undefined Address of the client as the host reports it (after the trusted-proxy rules), if known.
dev readonly dev: boolean True in development mode: error details are shown.
base readonly base: string The app's mount path ("" at the root, /app under base). Relative metadata URLs resolve against it.
trailingSlash readonly trailingSlash: "never" | "always" | "preserve" The app's trailingSlash policy. Metadata routes (sitemaps) apply it to their URLs.
geo readonly geo: GeoInfo Where the visitor is, as the platform reported it (Cloudflare, Vercel, Netlify; see GeoInfo). Every field is undefined when there is no platform source, so a client's own headers are never used.
draft readonly draft: boolean True while draft (preview) mode is on for this request (see draftMode()).
document readonly document: DocumentAttributes Attributes of <html>; set them before the page renders.
route readonly route: RouteInfo The matched route. kind is "none" until the route is resolved.
set set<T>(key: ContextKey<T>, value: T): void Stores a value under a typed key.
get get<T>(key: ContextKey<T>): T Reads a value; throws when the key was never set.
tryGet tryGet<T>(key: ContextKey<T>): T | undefined Reads a value, or undefined.
params readonly params: Readonly<Record<string, string>> Route params of an API route or page (decoded).
locals readonly locals: DuskLocals Request-scoped values set by middleware and read by guards, loaders and server functions. Typed through the DuskLocals interface. Server only: never sent to the client by the framework.