Back to Fresh

Api Reference

docs/latest/advanced/api-reference.md

2.3.310.7 KB
Original Source

This page lists all public exports from Fresh's entry points.

[info]: You can also explore Fresh's full API documentation on JSR: @fresh/core

fresh

The main entry point for server-side code.

ts
import { App, createDefine, HttpError, page, staticFiles } from "fresh";
ExportKindDescription
AppClassThe main application class. See App.
staticFilesFunctionMiddleware for serving static files. See Static Files.
createDefineFunctionCreate type-safe define.* helpers. See Define Helpers.
pageFunctionReturn data from a handler to a page component. See Data Fetching.
HttpErrorClassThrow HTTP errors with status codes. See Error Handling.
corsFunctionCORS middleware. See cors.
csrfFunctionCSRF protection middleware. See csrf.
cspFunctionContent Security Policy middleware. See csp.
trailingSlashesFunctionTrailing slash enforcement middleware. See trailingSlashes.

Types:

ExportKindDescription
Context / FreshContextInterfaceThe request context passed to all middlewares and handlers.
PagePropsTypeProps received by page components (data, url, params, state, etc.).
Middleware / MiddlewareFnTypeMiddleware function type.
HandlerFnTypeSingle handler function type.
HandlerByMethodTypeObject with per-method handler functions.
RouteHandlerTypeUnion of HandlerFn and HandlerByMethod.
PageResponseTypeReturn type of page().
RouteConfigInterfaceRoute configuration (routeOverride, skipInheritedLayouts, etc.).
LayoutConfigInterfaceLayout configuration (skipInheritedLayouts, skipAppWrapper).
DefineInterfaceType of the object returned by createDefine().
FreshConfig / ResolvedFreshConfigInterfaceApp configuration types.
ListenOptionsInterfaceOptions for app.listen().
IslandTypeIsland component type.
MethodTypeHTTP method union type.
RouteDataTypeData type returned by route handlers via page().
Lazy / MaybeLazyTypeUtility types for lazily-loaded routes and middleware.
CORSOptionsInterfaceOptions for cors().
CsrfOptionsInterfaceOptions for csrf().
CSPOptionsInterfaceOptions for csp().

fresh/runtime

Shared runtime utilities for both server and client code. Safe to import in islands.

ts
import {
  asset,
  assetSrcSet,
  Head,
  HttpError,
  IS_BROWSER,
  Partial,
} from "fresh/runtime";
ExportKindDescription
IS_BROWSERConstanttrue in the browser, false on the server. Use to guard browser-only code.
assetFunctionAdd cache-busting query params to asset URLs. See Static Files.
assetSrcSetFunctionApply asset() to all URLs in a srcset string.
PartialComponentMark a region for partial updates. See Partials.
HeadComponentAdd elements to the document <head>. See <head> element.
HttpErrorClassHTTP error class (re-exported from fresh).

fresh/dev

Development and build tools. Only used in dev.ts (legacy) or build scripts.

ts
import { Builder } from "fresh/dev";
ExportKindDescription
BuilderClassPre-Vite build system (legacy). See Builder.

Types:

ExportKindDescription
BuildOptionsInterfaceOptions for new Builder().
ResolvedBuildConfigInterfaceResolved build configuration.
OnTransformArgs / OnTransformOptions / TransformFnTypeBuild plugin hook types.