Server
Server for Clover routes
Server
Example
Props
| Name | Type | Description |
|---|---|---|
input | z.ZodObject<any, any> | A Zod schema describing the shape of your input. Clover will look for the input inside query params, path params or JSON formatted request body. |
output | z.ZodObject<any, any> | A Zod schema describing the shape of your output. You can use the sendOutput helper inside your run function to ensure that you conform to the output. |
run | ({ request, input, authContext, sendOutput, sendError }) => Promise<Response> | The logic you want to run. You can use the validated input, or use the raw request. The authContext contains any context returned from your authenticate function. Use sendOutput or sendError helpers to send responses. |
path | string | The relative path where your handler can be reached e.g. /api/hello-world |
method | HTTPMethod | GET, POST, PUT, PATCH or DELETE. This helps Clover generate appropriate documentation, and also helps it figure out where to look for the input. For example, input will be parsed from query and path parameters for GET requests. |
description? | string | Useful for generating OpenAPI documentation for this route. |
authenticate? | (request: Request) => Promise<{ authenticated: true; context: TAuthContext } | { authenticated: false; reason: string }> | If supplied, marks the route as protected with Bearer auth in the documentation. Return authenticated: true with a context object to pass data to your handler, or authenticated: false with a reason for rejection (logged only, not sent to client). |
Authentication
Clover supports route-level authentication with typed context. When you provide an authenticate function, it will be called before your run handler. If authentication fails, a 401 response is returned automatically.
Basic Example
Authentication Result
The authenticate function must return one of two results:
Success:
The context can be any object containing user info, permissions, session data, etc. It will be passed to your run handler as authContext.
Failure:
The reason is logged server-side for debugging but is not exposed to the client. The client receives a generic 401 Unauthorized response.
Type Safety
The authContext in your run handler is automatically typed based on what your authenticate function returns:
Error Handling
If your authenticate function throws an error, Clover will:
- Log the error at the
errorlevel - Return a 401 response (fail closed for security)
This ensures that authentication errors don't accidentally grant access.
Return values
| Name | Type | Description |
|---|---|---|
handler | (request: Request) => Response | An augmented server route handler. |
clientConfig | IClientConfig | A dummy variable used to extract types and pass them to the client. You can read more in the Client docs. |
openAPIPathsObject | oas31.PathsObject | A generated OpenAPI schema for this route. You can read more about how to use this in the OpenAPI docs. |
OpenAPI
Clover uses openapi3-ts and zod-openapi to generate OpenAPI schemas for your routes. Each route returns an oas31.PathsObject. You can stitch together all the schemas into a combined document like below:
OpenAPI Examples and Descriptions
Clover supports OpenAPI examples and descriptions using Zod's .meta() method. You can add examples and descriptions to any field in your input or output schemas, including query parameters, path parameters, request bodies, and responses.
Basic Usage
Query Parameters with Examples
For GET requests, query parameters can include examples and descriptions:
Path Parameters with Examples
Path parameters also support examples and descriptions:
Nested Objects and Arrays
Examples work with nested objects and arrays:
Enums with Examples
Enum fields can have examples and descriptions:
Type Coercion
Clover handles type coercion properly in query parameters:
Additional Metadata Options
The .meta() method supports other zod-openapi properties as well:
For more details on zod-openapi metadata options, see the zod-openapi documentation.
Usage with frameworks
Next.js
Clover works with standard Web Request and Response APIs, which are only available in the new app directory in Next.js 13.4.
Swagger UI
Setting up Swagger would vary from framework to framework, but here is an illustrative example for Next.js:
Input parsing
There are three supported input types: query parameters, path parameters and JSON request bodies. Depending on the HTTP method used, Clover will parse the input from the appropriate source.
| Method | Input source |
|---|---|
GET | Path + query |
DELETE | Path + query |
POST | Path + body |
PUT | Path + body |
PATCH | Path + body |
Path parameters
Clover uses path-to-regexp to parse path parameters e.g. if you have an input schema z.object({ id: z.string() }) and path /api/users/:id, then Clover will parse the request URL to find the ID and use it to populate the input inside the run() function.