Modeling an HTML server
We define ourselves what a server is, in Vanilla JS – and TS. This will allow us to write adapters for any kind of backend framework.
with a fully working example
Building a Grocery List using a multi-page app approach.
I will not try to model exactly what a server is. A lot of people – very likely waaay smarter than I – already did an extraordinarily good job at this: check out the Open API collective if you are interested. But beware you will still get cruel migrations 😅 (OpenAPI is v3 at the time of writing).
I will focus solely on what matters to our situation: modeling a server containing only HTML route handlers.
Why not aiming at universality?
But, but… it is not future-proof: you can’t build a JSON API for instance!
Yes it is, yes you can. By defining another concept: the JSON API one, and I suggest you circle back to OpenAPI, cf the intro. We use both differently, so let’s define JSON APIs and an HTML servers differently.
Sounds too simple, too good to be true? It is that simple, it is that good: the price to pay is to define your 💩, your API surface. Exhaustiveness is a trap: you have to be exhaustive for your project, not for the world. My concern right now is HTML route handlers, nothing else.
Differentiating concepts gives me flexibility: I only need to write one adapter per definition. I may have 1 to 3 definitions of big stuff like an HTML server, that’s a decent amount of adapters to write in my opinion.
And in the end it will empower me to:
Ready to get started? Let’s define an HTML server.
We will move forward step-by-step and refine our definition as needs arise (GET, POST, cookie-based authentication, …).