Quick Start
This guide covers how to combine the various packages of Webroute to produce a fully-functioning server-side app.
The resulting app will have strong type-safety, client-side types, automatic OpenAPI spec definitions and validation.
Preface
While this guide combines many Webroute packages, bear in mind they can all be used independently. As such, most steps are entirely optional.
Routes
First, we will define a "route". In webroute terms, a route is a REST endpoint that also defines things like validation and even middleware.
Defining a Route
To begin, we'll create a single route, designed to create a blog post.
Our createPostRoute
is merely a (request: Request) => Response
handler, and can be used nearly anywhere.
Adding Validation
We want to validate the input to ensure the necessary data has been provided. We can provide a schema or validator to do this.
Collect Routes
It's useful to have a single reference point for all of our routes, especially for the following steps.
Middleware
Adding Auth Middleware
We will want to ensure only logged in people can create posts. Instead of running a check within each route handler, we can define a middleware function.
Now we can register this with a route.
Routing
Our createPostRoute
can be run anywhere web-standards are supported. In some instances we may not need any routing, for example when using nextjs
.
In our case, we will use a regular router to match incoming requests.
This router allows us to match handler based on an incoming request. Therefore, our router can also be used anywhere web-standard requests are supported.
Our handleRequest
handler can now be used with many frameworks and runtimes. For example with bun
we would use it like so.
Client
We can interface with our app with type-safety on the client side.
On the server-side, we can export our AppDef
for future consumption on the client side.
We can either package our types into an npm
module, or import directly if using a monorepo.
Alternatively, we can generate a client using OpenAPI types.
OpenAPI
This step is very much optional
Generate an OpenAPI Spec
Using the @webroute/oas
package, we can create OpenAPI specs for our routes.
However, by default createSpec
doesn't know how to convert the schema library (i.e. zod
) into the required JSON Schema format. To resolve this, we can set the formatter
option.
This formatter is flexible, so we can use any library we like.
OpenAPI Customisations
We may also want to add some additional metadata which will be added to our OpenAPI spec, which we'll generate later.
We can do this for both the schema and the route.
Client from OpenAPI
For several reasons, we may not want to emit a type definition for our app: perhaps monorepos and npm
packages are too much hassle.
Instead, we can derive a typed client from our OpenAPI spec (or any for that matter), without requiring any code generation.
We need to place our spec in .d.ts
file to ensure types are correctly setup for inference.
We can then infer our AppDef
from this.
Summary
We've created a fully-fledged server-side app which runs in nearly every JS runtime and framework. For a complete implementation, please view the full example.