createMiddleware
Creates middleware that can intercept and modify server function calls or HTTP requests.Basic Usage
API Reference
'request' | 'function'
default:"'request'"
The type of middleware:
'request': Runs for all HTTP requests (pages and server functions)'function': Runs only for server function calls
Middleware Types
Request Middleware
Runs for all HTTP requests including page loads and server function calls:Function Middleware
Runs only for server function calls and can execute on both client and server:Builder Methods
.middleware()
Composes middleware by nesting other middleware:
Array<Middleware>
required
Array of middleware to run before this middleware executes.
.inputValidator()
Validates input data for function middleware:
Validator
required
Validation schema for the input data.
.client()
Defines client-side middleware for function middleware:
ClientMiddlewareFn
required
Function that runs on the client before sending the request to the server.
any
The input data being sent to the server.
object
Client-side context accumulated from previous middleware.
object
Context to send to the server (must be serializable).
'GET' | 'POST'
The HTTP method being used.
AbortSignal
AbortSignal for the request.
ClientFnMeta
Metadata about the server function (id).
string
The filename where the server function is defined.
typeof fetch
The fetch function to use for the request.
function
required
Call to proceed to the next middleware or make the server request.
.server()
Defines server-side middleware logic:
ServerMiddlewareFn
required
Function that runs on the server.
Request
The HTTP Request object.
string
The request pathname.
object
Context accumulated from previous middleware.
ServerFnMeta | undefined
Metadata about the server function if this is a server function request, undefined for page requests.
function
required
Proceeds to the next middleware. Can pass context:
next({ context: {...} })any
The validated input data.
object
Server-side context from previous middleware and sent from client.
'GET' | 'POST'
The HTTP method.
ServerFnMeta
Server function metadata (id, name, filename).
AbortSignal
AbortSignal for request cancellation.
function
required
Proceeds to next middleware. Can pass context and sendContext.