+57
README.md
+57
README.md
···
206
206
207
207
Note: Migrations must be defined in both `index.ts` and `cache.ts` to avoid circular dependencies in the import structure.
208
208
209
+
### Adding New Routes
210
+
211
+
The app uses a type-safe route system that automatically generates Swagger documentation from route definitions. This ensures the API docs always stay in sync with the actual implementation.
212
+
213
+
To add a new route, you need to:
214
+
215
+
1. Create the handler function in `src/handlers/index.ts`:
216
+
217
+
```typescript
218
+
export const handleMyNewEndpoint: RouteHandlerWithAnalytics = async (
219
+
request,
220
+
recordAnalytics,
221
+
) => {
222
+
// Your handler logic here
223
+
const data = { message: "Hello from new endpoint" };
224
+
225
+
await recordAnalytics(200);
226
+
return Response.json(data);
227
+
};
228
+
```
229
+
230
+
2. Add the route definition in `src/routes/api-routes.ts`:
231
+
232
+
```typescript
233
+
"/my-new-endpoint": {
234
+
GET: createRoute(
235
+
withAnalytics("/my-new-endpoint", "GET", handlers.handleMyNewEndpoint),
236
+
{
237
+
summary: "My new endpoint",
238
+
description: "Does something useful",
239
+
tags: ["MyFeature"],
240
+
parameters: {
241
+
query: [
242
+
queryParam("limit", "number", "Number of items to return", false, 10)
243
+
]
244
+
},
245
+
responses: Object.fromEntries([
246
+
apiResponse(200, "Success", {
247
+
type: "object",
248
+
properties: {
249
+
message: { type: "string", example: "Hello from new endpoint" }
250
+
}
251
+
})
252
+
])
253
+
}
254
+
)
255
+
}
256
+
```
257
+
258
+
The route will automatically:
259
+
- Handle analytics recording (request timing, status codes, user agents)
260
+
- Generate Swagger documentation with the provided metadata
261
+
- Include proper TypeScript types for parameters and responses
262
+
- Validate the route definition at compile time
263
+
264
+
No need to manually update Swagger docs or add boilerplate analytics code. The system handles all of that automatically based on your route definitions.
265
+
209
266
<p align="center">
210
267
<img src="https://raw.githubusercontent.com/taciturnaxolotl/carriage/master/.github/images/line-break.svg" />
211
268
</p>