Fetch Middleware

This example stacks two FetchMiddleware functions inside component setup: an outer tracing middleware that injects X-Demo-Trace headers and logs every status code, and an inner mock responder that answers GET, POST, and DELETE requests to /api/notes completely in memory.

Because requests never touch the network, the exact same code runs cleanly under webcompy start and on the statically generated webcompy generate site with zero failed network calls.

Fetch Middleware

Demo
Code
import json
from dataclasses import dataclass
from typing import Any

from webcompy.aio import AsyncWrapper
from webcompy.ajax import HttpClient
from webcompy.app import WebComPyApp
from webcompy.components import ComponentContext, define_component, use_async_result
from webcompy.elements import html, repeat, switch
from webcompy.ports import FetchMiddleware, FetchNext, FetchRequest, FetchStream, Response, add_fetch_middleware
from webcompy.signal import ReactiveList, use_reactive_list, use_state


@dataclass
class Note:
    id: int
    title: str
    tag: str


def _json_response(payload: Any, status_code: int = 200, status_text: str = "OK") -> Response:
    return Response(
        text=json.dumps(payload),
        headers={"Content-Type": "application/json"},
        status_code=status_code,
        status_text=status_text,
        ok=status_code < 400,
    )


def create_notes_mock() -> FetchMiddleware:
    store: list[dict[str, Any]] = [
        {"id": 1, "title": "Intercept GET /api/notes", "tag": "mock"},
        {"id": 2, "title": "Validate response with dataclass", "tag": "schema"},
    ]

    async def notes_mock(request: FetchRequest, next: FetchNext) -> Response | FetchStream:
        path = request.url.split("?", 1)[0]
        raw_body = request.body.decode("utf-8") if isinstance(request.body, bytes) else (request.body or "{}")
        if path == "/api/notes" and request.method == "GET":
            return _json_response(list(store))
        if path == "/api/notes" and request.method == "POST":
            payload = json.loads(raw_body)
            title = str(payload.get("title", "")).strip()
            if not title:
                return _json_response({"error": "title required"}, 400, "Bad Request")
            created = {
                "id": max((item["id"] for item in store), default=0) + 1,
                "title": title,
                "tag": str(payload.get("tag", "user")),
            }
            store.append(created)
            return _json_response(created, 201, "Created")
        if path.startswith("/api/notes/") and request.method == "DELETE":
            note_id = int(path.rsplit("/", 1)[-1])
            for idx, item in enumerate(store):
                if item["id"] == note_id:
                    store.pop(idx)
                    return _json_response({"deleted": note_id})
            return _json_response({"error": "not found"}, 404, "Not Found")
        return await next(request)

    return notes_mock


def create_trace_middleware(trace_log: ReactiveList[str]) -> FetchMiddleware:
    counter = 0

    async def trace_middleware(request: FetchRequest, next: FetchNext) -> Response | FetchStream:
        nonlocal counter
        counter += 1
        trace_id = f"trace-{counter}"
        request.headers = {**(request.headers or {}), "X-Demo-Trace": trace_id}
        response = await next(request)
        trace_log.append(f"{request.method} {request.url} -> {response.status_code} ({trace_id})")
        return response

    return trace_middleware


@define_component()
def FetchMiddlewareDemo(_: ComponentContext[None]):
    trace_log: ReactiveList[str] = use_reactive_list(lambda: [])
    notes: ReactiveList[Note] = use_reactive_list(lambda: [])
    title_input = use_state(lambda: "")
    status = use_state(lambda: "")

    add_fetch_middleware(create_trace_middleware(trace_log))
    add_fetch_middleware(create_notes_mock())

    async def _load_notes() -> list[Note]:
        loaded = await HttpClient.get("/api/notes", response_type=list[Note])
        notes.value = loaded
        return loaded

    notes_result = use_async_result(_load_notes)

    @AsyncWrapper()
    async def _add_note(_ev: Any) -> None:
        title = title_input.value.strip()
        if not title:
            status.value = "Enter a note title."
            return
        created = await HttpClient.post("/api/notes", json={"title": title, "tag": "custom"}, response_type=Note)
        notes.append(created)
        title_input.value = ""
        status.value = f"Saved note #{created.id}"

    def _delete_note(note_id: int):
        @AsyncWrapper()
        async def _on_click(_ev: Any) -> None:
            await HttpClient.delete(f"/api/notes/{note_id}")
            notes.value = [n for n in notes.value if n.id != note_id]
            status.value = f"Deleted note #{note_id}"

        return _on_click

    return html.DIV(
        {"class": "fetch-mw-demo"},
        html.H1({}, "Fetch Middleware Mock"),
        html.DIV(
            {"class": "note-form"},
            html.INPUT({"type": "text", "placeholder": "New note title...", ":bind": title_input}),
            html.BUTTON({"type": "button", "@click": _add_note}, "Add Note"),
        ),
        html.P({"class": "status-line"}, status),
        switch(
            {
                "case": notes_result.is_success,
                "generator": lambda: html.UL(
                    {"class": "notes-list"},
                    repeat(
                        notes,
                        lambda note, _key: html.LI(
                            {"class": "note-item"},
                            html.SPAN({"class": "note-title"}, f"{note.title} [{note.tag}]"),
                            html.BUTTON({"type": "button", "@click": _delete_note(note.id)}, "Delete"),
                        ),
                        key=lambda note: note.id,
                    ),
                ),
            },
            default=lambda: html.P({}, "Loading notes..."),
        ),
        html.H2({}, "Middleware Trace"),
        html.UL(
            {"class": "trace-list"},
            repeat(trace_log, lambda entry: html.LI({}, entry)),
        ),
    )


FetchMiddlewareDemo.scoped_style = {
    ".fetch-mw-demo": {
        "font-family": "var(--font-sans, sans-serif)",
        "padding": "var(--space-3)",
        "color": "var(--color-fg)",
        "max-width": "38rem",
    },
    ".fetch-mw-demo h1": {
        "margin": "0 0 var(--space-3) 0",
        "font-size": "var(--font-size-xl, 1.25rem)",
    },
    ".fetch-mw-demo h2": {
        "margin": "var(--space-3) 0 var(--space-2) 0",
        "font-size": "var(--font-size-base, 1rem)",
    },
    ".note-form": {
        "display": "flex",
        "gap": "var(--space-2)",
        "margin-bottom": "var(--space-2)",
    },
    ".note-form input": {
        "flex": "1",
        "padding": "0.35rem 0.5rem",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg)",
        "color": "var(--color-fg)",
    },
    ".status-line": {
        "font-size": "var(--font-size-sm, 0.875rem)",
        "color": "var(--color-link)",
        "min-height": "1.2rem",
    },
    ".notes-list, .trace-list": {
        "list-style": "none",
        "padding": "0",
        "margin": "0",
    },
    ".note-item": {
        "display": "flex",
        "justify-content": "space-between",
        "align-items": "center",
        "padding": "var(--space-2) var(--space-3)",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "margin-bottom": "var(--space-2)",
        "background": "var(--color-bg-card)",
    },
    ".trace-list li": {
        "font-family": "var(--font-mono, monospace)",
        "font-size": "0.85rem",
        "padding": "0.25rem 0.5rem",
        "border-bottom": "1px solid var(--color-border)",
    },
    ".fetch-mw-demo button": {
        "padding": "0.35rem 0.75rem",
        "border": "1px solid var(--color-border)",
        "border-radius": "var(--radius-sm)",
        "background": "var(--color-bg-elevated)",
        "color": "var(--color-fg)",
        "cursor": "pointer",
    },
}

app = WebComPyApp(root_component=FetchMiddlewareDemo)
app.run()

What this demonstrates#

  • Outside-in middleware ordering: Registering create_trace_middleware before create_notes_mock wraps /api/notes so the tracing middleware records every synthetic 200/201 response produced by the mock responder.
  • Full HttpClient type decoding on mock responses: Even though create_notes_mock returns an in-memory Response, HttpClient.get("/api/notes", response_type=list[Note]) and HttpClient.post(..., response_type=Note) still deserialize and validate Note dataclasses normally.
  • Selective delegation via await next(request): Requests outside /api/notes pass downstream untouched so other page assets load normally.

Key mechanisms and how to adapt#

  1. Swap mocks per environment: Move create_notes_mock behind a development/preview condition or register tracing/auth middlewares globally in a WebComPyPlugin.get_fetch_middlewares().
  2. Short-circuit caching cleanly: Return await next(request, response=cached_response) to satisfy inner cache hits while allowing outer logging middlewares to run completion hooks.
  3. Never register middlewares at module import time: Always call add_fetch_middleware() inside component setup or plugin initialization so DI scopes isolate middleware chains per render context.

Real server wiring#

In a production deployment, remove create_notes_mock() from the browser component (keeping your outer auth/tracing middleware) and serve /api/notes from a real Starlette or FastAPI host integrated with create_asgi_app:

from dataclasses import asdict, dataclass
from fastapi import FastAPI
from pydantic import BaseModel
from webcompy_cli.config import WebComPyBuildConfig
from webcompy_server import configure_server_context, create_asgi_app


@dataclass
class Note:
    id: int
    title: str
    tag: str


class CreateNoteBody(BaseModel):
    title: str
    tag: str = "custom"


NOTES: list[Note] = [Note(id=1, title="Real server note", tag="api")]
host = FastAPI()


@host.get("/api/notes")
def list_notes() -> list[dict[str, object]]:
    return [asdict(note) for note in NOTES]


@host.post("/api/notes", status_code=201)
def create_note(body: CreateNoteBody) -> dict[str, object]:
    created = Note(id=max((n.id for n in NOTES), default=0) + 1, title=body.title, tag=body.tag)
    NOTES.append(created)
    return asdict(created)

Keep add_fetch_middleware(create_trace_middleware(...)) if desired — once create_notes_mock is removed, HttpClient routes /api/notes directly over browser fetch() (and in-process through root_app during SSR once configure_server_context(app, root_app=host) is wired).

Next steps#

  • Learn streaming interception, header-commit timing, and plugin middleware aggregation in Middleware.
  • Compare ASGI host mounting and configure_server_context wiring in ASGI Integration.
  • See how Step 5 of the guided tutorial introduces a FetchMiddleware API mock in Step 5: Remote Data and HTTP & Async.
  • Next, see contract-first typed RPC synthesis in RPC Middleware.