8000 docs: Add Vitepress app · aklinker1/zeta@77129d0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77129d0

Browse files
committed
docs: Add Vitepress app
1 parent f89b725 commit 77129d0

21 files changed

Lines changed: 1866 additions & 132 deletions

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
dist
33
.DS_Store
4+
docs/.vitepress/dist
5+
docs/.vitepress/cache

‎README.md‎

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -154,45 +154,7 @@ const app = createApp()
154154

155155
Internally, Zeta uses [`rou3`](https://www.npmjs.com/package/rou3) to match routes. To add a path parameter, you can use `:name`, `**`, or `**:name`. For type safety, you can use a validation framework to define an object schema for the path parameters.
156156

157-
```ts
158-
import { z } from "zod";
159157

160-
const app = createApp()
161-
.get(
162-
"/path/:name",
163-
{
164-
// Optional: Add type-safety and validation to the route
165-
path: z.object({
166-
name: z.coerce.number(),
167-
}),
168-
},
169-
({ path }) => {
170-
console.log(path.name);
171-
},
172-
)
173-
.get(
174-
"/path/**",
175-
{
176-
path: z.object({
177-
"**": z.string(),
178-
}),
179-
},
180-
async ({ path }) => {
181-
console.log(path["**"]);
182-
},
183-
)
184-
.get(
185-
"/path/**:name",
186-
{
187-
path: z.object({
188-
name: z.string(),
189-
}),
190-
},
191-
({ path }) => {
192-
console.log(path.name);
193-
},
194-
);
195-
```
196158

197159
> Note that path parameters are strings. If your validation framework supports converting strings to other types, like with Zod's [`z.coerce`](https://zod.dev/api?id=coercion) or [`z.stringbool`](https://zod.dev/api?id=stringbool), you can use it to convert the string values to the desired type.
198160
@@ -342,98 +304,6 @@ app.get(
342304
343305
## Life Cycle Hooks
344306
345-
You can use any of the following APIs to hook into the request lifecycle:
346-
347-
```mermaid
348-
---
349-
title: Request Life Cycle
350-
---
351-
graph LR
352-
A((Client)) -.-> B
353-
B[onGlobalRequest] -- "Route Matched" --> D
354-
B -- "Not Found" --> C
355-
C[onGlobalError] ---> J
356-
D[onTransform] -- "Validate Inputs" --> E
357-
D -- "Error thrown" --> C
358-
E[onBeforeHandle] ---> F
359-
E -- "Error thrown" --> C
360-
F((Handler)) --> G
361-
F -- "Error thrown" --> C
362-
G[onAfterHandle] -- "Validate Response" --> H
363-
G -- "Error thrown" --> C
364-
H[onMapResponse] ---> J
365-
H -- "Error thrown" --> C
366-
J@{ shape: sm-circ } -.-> K
367-
J ---> L
368-
K((Client))
369-
L["onGlobalAfterResponse (deferred)"]
370-
```
371-
372-
- `onGlobalRequest`: Called as soon as a request is made to the app.
373-
- Return value is shallow merged into the `ctx` object for future callbacks.
374-
- `onTransform`: Called before input parameters have been validated, can be used to transform input values before validation occurs.
375-
- Return value is shallow merged into the `ctx` object for future callbacks.
376-
- `onBeforeHandle`: Called after inputs have been validated, right before the route handler is executed.
377-
- Return value is shallow merged into the `ctx` object for future callbacks.
378-
- `onAfterHandle`: Called after the route handler is executed.
379-
- Return value replaces the value returned from the handler.
380-
- `onMapResponse`: Convert the return value into a [`Response` object](https://developer.mozilla.org/en-US/docs/Web/API/Response).
381-
- If a `Response` value is returned, send it to the client.
382-
- If a `Response` value is not returned, Zeta infers the response content type based on the handler's response value.
383-
- `onGlobalAfterResponse`: Called after the response is sent to the client.
384-
- Return value is ignored.
385-
- `onGlobalError`: If an error is thrown at any point in the lifecycle, this hook will be called giving you the opportunity to report the error.
386-
387-
There are two types of hooks: global and isolated.
388-
389-
- **Global**: These hooks are registered on the final, top-level app, regardless of where they are defined in your app's composition tree. They always run for every request that hits the server. Use these for cross-cutting concerns like logging or authentication.
390-
- **Isolated**: These hooks only apply to routes defined on the _same app instance_, _after_ the hook is declared. They are perfect for setting up context or running middleware specific to a group of routes (e.g., a plugin).
391-
392-
Here's an example combining several different hooks:
393-
394-
```ts
395-
import { createApp } from "@aklinker1/zeta";
396-
import { NotFoundHttpError } from "@aklinker1/zeta/errors";
397-
398-
const usersApp = createApp({ prefix: "/api/users" })
399-
.get("/", {}, () => {
400-
return [];
401-
})
402-
.onTransform(({ path }) => {
403-
return {
404-
path: {
405-
...path,
406-
userId: Number(path.userId),
407-
},
408-
};
409-
})
410-
.onBeforeHandle(async ({ path }) => {
411-
const user = await getUser(path.userId);
412-
if (user == null) throw new NotFoundHttpError("User not found");
413-
414-
return { user };
415-
})
416-
.get("/:userId", {}, async ({ user }) => user)
417-
.put("/:userId", {}, async ({ user }) => {
418-
// ...
419-
})
420-
.delete("/:userId", {}, async ({ user }) => {
421-
// ...
422-
});
423-
424-
const app = createApp()
425-
.onGlobalRequest(() => {
426-
return {
427-
startTime: performance.now(),
428-
};
429-
})
430-
.onGlobalAfterResponse(({ startTime }) => {
431-
const endTime = performance.now();
432-
console.log(`Request duration: ${endTime - startTime} ms`);
433-
})
434-
.use(apiApp);
435-
```
436-
437307
## `App#decorate`
438308
439309
Shorthand for `.onTransform(() => decorators)`, just adding values to the request context.

0 commit comments

Comments
 (0)
0