-
Notifications
You must be signed in to change notification settings - Fork 7
/
usage_playground.ts
53 lines (43 loc) · 1.11 KB
/
usage_playground.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// @todo delete this file
import { Aqua, Method } from "./mod.ts";
const app = new Aqua({
listen: {
port: 80,
},
});
app.route("/").respond(Method.GET, (_event) => {
return new Response("Hello, World!");
});
// /v1
const getUserByRequest = (_req: Request) => Promise.resolve({ name: "test" });
const v1 = app.route("/v1").step(async (event) => {
if (!event.request.headers.has("X-Api-Key")) {
event.response = Response.json(
{ error: "MISSING_API_KEY" },
{
status: 400,
}
);
return event.end();
}
const user = await getUserByRequest(event.request);
// ^ type User
return {
...event,
user,
};
});
v1.route("/user").respond(Method.GET, (event) => {
return Response.json({ data: { user: event.user } });
// ^ type User
});
// /test
const test = app.route("/test").step((event) => {
console.log("/test global step");
return event;
});
test
.route("/1")
.step((e) => (console.log("/test/1 step"), e))
.respond(Method.GET, () => new Response("GET"))
.respond(Method.POST, () => new Response("POST"));