Middleware
WebComPy separates port transport implementations (FetchPort) from cross-cutting HTTP and RPC policies (FetchMiddleware and RpcMiddleware). You register async interceptors in an active DI scope — from components or plugins — to inject auth headers, record timings or caches, and return validated synthetic responses without replacing FetchPort or bypassing SSR hydration caches and blocked-path guards.
When to use#
- Need auth headers, telemetry, retries, or offline mocks around
HttpClientor SSE/fetch streams — useFetchMiddlewareviaadd_fetch_middleware. - Want typed interception on HTTP JSON-RPC (
Procedure,batch,notify, HTTPStreamingProcedure) — useRpcMiddlewareviaadd_rpc_middleware(see also RPC Contracts and RPC Streaming). - Swapping out an entire port in unit tests — override the port directly inside
DIScope(see Dependency Injection Deep Dive). - Sharing middlewares cleanly across applications — bundle them in a
WebComPyPlugin(see Plugins).
Setup#
Install fetch middlewares through add_fetch_middleware (webcompy.ports) and RPC middlewares through add_rpc_middleware (webcompy.rpc) inside component setup or plugin hooks:
from webcompy.components import ComponentContext, define_component, use_async_result
from webcompy.ajax import HttpClient
from webcompy.elements import html
from webcompy.ports import FetchNext, FetchRequest, add_fetch_middleware
from webcompy import use_computed
@define_component()
def ApiConsumer(context: ComponentContext[None]):
async def attach_auth(request: FetchRequest, next: FetchNext):
request.headers = {**(request.headers or {}), "Authorization": "Bearer token"}
return await next(request)
add_fetch_middleware(attach_auth)
profile = use_async_result(lambda: HttpClient.get("/api/profile"))
return html.P(
{},
use_computed(
lambda: profile.data.value.text if profile.data.value is not None else "Loading..."
),
)
Chains compose outside-in in registration order (middlewares[0] is outermost):
Request ──► middlewares[0] ──► middlewares[1] ──► concrete FetchPort / dispatcher
Response ◄── middlewares[0] ◄── middlewares[1] ◄── concrete FetchPort / dispatcher
Recipes#
1. Inject authentication or tracing headers (FetchMiddleware & RpcMiddleware)#
Mutate request.headers (on FetchRequest) or ctx.headers (on RpcContext) before delegating to await next(...):
# FetchMiddleware for REST / HttpClient / non-GET SSE endpoints
from webcompy.ports import FetchNext, FetchRequest, add_fetch_middleware
async def trace_fetch_middleware(request: FetchRequest, next: FetchNext):
request.headers = {**(request.headers or {}), "X-Trace-Id": "req-123"}
return await next(request)
# RpcMiddleware operates on typed contract objects before JSON-RPC encoding
from webcompy.rpc import RpcContext, add_rpc_middleware
async def auth_rpc_middleware(ctx: RpcContext, next):
ctx.headers["Authorization"] = "Bearer session-token"
if ctx.method.startswith("admin."):
ctx.headers["X-Admin-Scope"] = "1"
return await next(ctx)
On RpcMiddleware, ctx.params holds the typed dataclass instance before wire serialization, and for batch(...) calls ctx.is_batch is True, ctx.method is "", and ctx.batch_entries lists each RpcBatchEntry in call order.
2. Log timings and cache responses around await next(...)#
Code before await next(...) runs on the outbound leg; code after it sees the returned Response, FetchStream, or decoded RPC result:
import time
from webcompy.ports import FetchNext, FetchRequest, Response
def make_timing_cache_middleware():
cache: dict[str, Response] = {}
async def timing_cache(request: FetchRequest, next: FetchNext):
if request.method == "GET" and request.url in cache:
return await next(request, response=cache[request.url])
started = time.monotonic()
response = await next(request)
elapsed_ms = (time.monotonic() - started) * 1000
if isinstance(response, Response) and request.method == "GET" and response.ok:
cache[request.url] = response
print(f"{request.method} {request.url} -> {response.status_code} ({elapsed_ms:.1f}ms)")
return response
return timing_cache
Passing await next(request, response=cached) short-circuits remaining inner middlewares and the concrete FetchPort while returning cached cleanly through outer layers.
3. Build offline mocks without losing schema validation#
Both middleware layers let client apps run serverless demos on static hosting (webcompy generate) while preserving normal HTTP and RPC validation — the exact pattern introduced in Step 5: Remote Data (and used in standalone middleware demos):
import json
from webcompy.ports import FetchNext, FetchRequest, Response, add_fetch_middleware
from webcompy.rpc import RpcContext, add_rpc_middleware
async def offline_rest_mock(request: FetchRequest, next: FetchNext):
if request.url == "/api/tasks" and request.method == "GET":
return Response(
text=json.dumps([{"id": 1, "title": "Ship docs", "done": True}]),
headers={"Content-Type": "application/json"},
status_code=200,
status_text="OK",
ok=True,
)
return await next(request)
async def offline_rpc_mock(ctx: RpcContext, next):
if ctx.method == "users.get":
# Routed through apply_transfer_meta + schema validation just like a live response
return await next(ctx, response={"result": {"id": 1, "name": "Ada", "role": "admin"}})
return await next(ctx)
For streaming HTTP RPC procedures (StreamingProcedure), pass stream= (await next(ctx, stream=synthetic_fetch_stream)) to substitute an SSE FetchStream whose chunks still pass through per-item typed decoding. See RPC Streaming for a complete FetchStream mock example.
Behavior notes#
- DI-scoped additive registries: Both
FETCH_MIDDLEWARE_KEY(FetchMiddlewareRegistry) andRPC_MIDDLEWARE_KEY(RpcMiddlewareRegistry) are created fresh perRenderContext, resolved through the activeDIScope, and disposed when the context ends. Becauseprovide()overwrites keys while.use()appends, always calladd_fetch_middleware()/add_rpc_middleware()(orregistry.use(...)) rather than re-providing the registry key. - Plugin aggregation order:
PluginManager.init_render_context()readsget_fetch_middlewares()andget_rpc_middlewares()acrossWebComPyAppConfig.pluginsin declaration order and appends them before component setup runs — so plugin middlewares sit outermost relative to component-installed middlewares. - Streaming header-commit timing: On
FetchPort.stream()calls,await next(request)resolves once responsestatus_code,headers, andokare committed — before any body chunks are consumed. Middleware can inspect status/headers or return a substituteFetchStreamwithout downstream consumers noticing. - SSR hydration & blocked-path delegation: WebComPy's wrapped
FetchPortdelegatespopulate_from_transfer,get_transfer_data,clear_cache,close,is_self_site_url, and the servernoopmarker to the inner concrete port. Self-site SSR recursion blocking and hydration response caching remain intact even with arbitrary fetch middlewares installed. - HTTP-only RPC interception:
RpcMiddlewareapplies to HTTP operations (RpcHttpClient.call,notify,batch,stream).RpcWsClientoperations bypassRpcMiddleware.
Pitfalls#
- Returning from
RpcMiddlewarewithout callingawait next(...): UnlikeFetchMiddleware(which allows returning aResponsedirectly withoutnext), returning a bare value fromRpcMiddlewarewithout delegating tonextraisesRuntimeErrorso schema/metadata validation can never be skipped. Always callawait next(ctx, response=...)(orstream=...). - Overwriting
Content-TypeinRpcMiddleware: WebComPy resetsContent-Type(case-insensitively) toapplication/jsonafter all RPC middlewares run so handlers cannot corrupt the JSON-RPC wire format. - Registering middlewares at module import time: Outside an active render context,
add_fetch_middleware/add_rpc_middlewarefall back to the app scope or raiseInjectionErrorif no app scope exists — causing cross-request state or test leakage. Register inside component setup,DIScope, or plugin hooks. - Replacing
FETCH_MIDDLEWARE_KEYviaprovide(): Replacing the registry key drops middlewares contributed earlier by plugins or ancestor setup. Always append withadd_*_middleware().
Next steps#
- Try stacked HTTP mocks and tracing live in Fetch Middleware and typed contract synthesis in RPC Middleware.
- Understand the scope lifecycle and per-request port isolation behind middleware registries in Dependency Injection Deep Dive.
- Package fetch and RPC middlewares into reusable extensions in Plugins.
- Apply
RpcMiddlewareto typed contracts and streams in RPC Contracts, RPC, and RPC Streaming. - Review basic
HttpClientrequests and Step 5'sFetchMiddlewareAPI mock in HTTP & Async and Step 5: Remote Data. - Full API: Server, CLI, RPC & Ports API.