-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.js
More file actions
117 lines (107 loc) · 3.02 KB
/
Copy pathexamples.js
File metadata and controls
117 lines (107 loc) · 3.02 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import tino, { withMiddlewares, jsondb } from "https://deno.land/x/tino@v1.0.5/tino.js";
const app = tino.create();
// Basic and preferred
const use = (props) => ({ resp: ({ props }) });
app.get(() => ({ path: "/ping", use }));
app.get(() => ({ path: "/ping/:id/:param/:type", use }));
// Very basic (status 200 is default)
app.get(() => ({ path: "/ping-simple", resp: "pong-simple" }));
app.get(() => ({ path: "/ping-simple-func", resp: () => "pong-simple-func" }));
// Make other API endpoints
app.get(() => ({ path: "/api/v2", use: jsondb(true), root: true }));
// Use middlewares
const withAuth = (props) => {
return { isUser: true };
};
const throwAuth = () => {
throw { status: 401, resp: "Boom", type: "text/html" };
};
const isAdmin = (props) => {
return { isAdmin: false, ...props };
};
const withDB = async (props) => {
return { coll: {}, ...props };
};
const composed = withMiddlewares(
withAuth,
isAdmin,
withDB,
);
const useComposed = composed(() => {
return { resp: "ping-simple-func-composed" };
});
app.get(
() => ({
path: "/ping-simple-func-composed",
use: useComposed,
}),
);
// Or functional (these are all different ways to do the same thing)
// Depending on your function's logic, return statuses and types accordingly
app.get(() => ({
path: "/use",
use: () => ({ resp: "use", status: 200, type: "text/plain" }),
}));
app.get(() => ({
path: "/use-async",
use: async () => ({ resp: "use-async", status: 200, type: "text/plain" }),
}));
app.get(() => ({
path: "/use-func-async",
use: () => ({
resp: async () => "use-func-async",
status: 200,
type: "text/plain",
}),
}));
app.get(() => ({
path: "/use-async-func-async",
use: async () => ({
resp: async () => "use-async-func-async",
status: 200,
type: "text/plain",
}),
}));
// Define type outside of use
app.get(() => ({
path: "/use-type",
use: () => ({ resp: "use-type", status: 200 }),
type: "text/html",
}));
// But you can overwrite it if it's returned from your controller
app.get(() => ({
path: "/use-type-type",
use: () => ({ resp: "use-type-type", status: 200, type: "text/plain" }),
type: "text/html",
}));
const composed2 = withMiddlewares(
(props) => ({ isAdmin: true }),
);
app.post(() => ({
path: "/post2/:id", // or optional with :id?
use: composed2((props) => {
return { resp: { ...props } };
}),
something: "else",
}));
app.post(() => ({
path: "/post/:id",
use: ({ body, params, query, something }) => ({
resp: () => ({ body, params, query, something }),
status: 201,
}),
// resp: ({ body, params, query, something }) => ({ body, params, query, something, status: 201 }),
something: "else",
}));
// Set 404 using these (latter overwrites former)
app.not_found(() => ({ resp: "<p>Nothing here...</p>", type: "text/html" }));
app.not_found(() => ({
resp: () => "<p>Nothing here...</p>",
type: "text/html",
}));
app.not_found(() => ({
resp: async () => "<p>Nothing here...</p>",
type: "text/html",
}));
tino.listen({ app, port: 8000 });
console.log(`Server running at 8000`);