@tsonic/express 10.0.26 → 10.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +65 -16
- package/docs/snippets/10/cookies.ts +8 -0
- package/docs/snippets/10/cors.ts +2 -0
- package/docs/snippets/10/create-app-router.ts +1 -1
- package/docs/snippets/10/error-middleware.ts +1 -1
- package/docs/snippets/10/hello-world-app.ts +1 -2
- package/docs/snippets/10/middleware.ts +2 -3
- package/docs/snippets/10/multipart.ts +8 -0
- package/docs/snippets/10/quick-start-app.ts +1 -2
- package/docs/snippets/10/routing.ts +18 -6
- package/index/bindings.json +2407 -6409
- package/index/internal/index.d.ts +132 -233
- package/index.d.ts +8 -7
- package/package.json +1 -1
- package/tsonic.bindings.json +9 -2
package/README.md
CHANGED
|
@@ -27,13 +27,12 @@ import { express } from "@tsonic/express/index.js";
|
|
|
27
27
|
export function main(): void {
|
|
28
28
|
const app = express.create();
|
|
29
29
|
|
|
30
|
-
app.get("/", (_req, res) => {
|
|
30
|
+
app.get("/", async (_req, res, _next) => {
|
|
31
31
|
res.json({ ok: true });
|
|
32
32
|
});
|
|
33
33
|
|
|
34
34
|
app.listen(3000);
|
|
35
35
|
}
|
|
36
|
-
|
|
37
36
|
EOF
|
|
38
37
|
|
|
39
38
|
npm run dev
|
|
@@ -50,17 +49,24 @@ import { express } from "@tsonic/express/index.js";
|
|
|
50
49
|
export function main(): void {
|
|
51
50
|
const app = express.create();
|
|
52
51
|
|
|
53
|
-
app.get("/", (_req, res) => {
|
|
52
|
+
app.get("/", async (_req, res, _next) => {
|
|
54
53
|
res.send("hello");
|
|
55
54
|
});
|
|
56
55
|
|
|
57
56
|
app.listen(3000);
|
|
58
57
|
}
|
|
59
|
-
|
|
60
58
|
```
|
|
61
59
|
|
|
62
60
|
## Basic API Surface
|
|
63
61
|
|
|
62
|
+
## Handler model (important)
|
|
63
|
+
|
|
64
|
+
This package is **Task-first** (like ASP.NET): route handlers and middleware should be written as **`async`** functions (even if you don't `await` anything).
|
|
65
|
+
|
|
66
|
+
This avoids “async-void” behavior and keeps execution/exception semantics deterministic.
|
|
67
|
+
|
|
68
|
+
Also, handlers use the **3-argument** signature: `(req, res, next)` (even for routes). If you don't need `next`, name it `_next`.
|
|
69
|
+
|
|
64
70
|
### Create an app / router
|
|
65
71
|
|
|
66
72
|
```ts
|
|
@@ -69,7 +75,7 @@ import { express } from "@tsonic/express/index.js";
|
|
|
69
75
|
const app = express.create();
|
|
70
76
|
const router = express.Router();
|
|
71
77
|
|
|
72
|
-
router.get("/ping", (_req, res) => {
|
|
78
|
+
router.get("/ping", async (_req, res, _next) => {
|
|
73
79
|
res.send("pong");
|
|
74
80
|
});
|
|
75
81
|
|
|
@@ -81,28 +87,59 @@ app.use("/api", router);
|
|
|
81
87
|
Common verbs:
|
|
82
88
|
|
|
83
89
|
```ts
|
|
84
|
-
app.get("/health", (_req, res) =>
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
app.
|
|
88
|
-
|
|
89
|
-
|
|
90
|
+
app.get("/health", async (_req, res, _next) => {
|
|
91
|
+
res.send("ok");
|
|
92
|
+
});
|
|
93
|
+
app.post("/items", async (req, res, _next) => {
|
|
94
|
+
res.json(req.body);
|
|
95
|
+
});
|
|
96
|
+
app.put("/items/:id", async (req, res, _next) => {
|
|
97
|
+
res.send(req.params["id"] ?? "");
|
|
98
|
+
});
|
|
99
|
+
app.delete("/items/:id", async (_req, res, _next) => {
|
|
100
|
+
res.sendStatus(204);
|
|
101
|
+
});
|
|
102
|
+
app.patch("/items/:id", async (_req, res, _next) => {
|
|
103
|
+
res.sendStatus(204);
|
|
104
|
+
});
|
|
105
|
+
app.all("/anything", async (_req, res, _next) => {
|
|
106
|
+
res.send("matched");
|
|
107
|
+
});
|
|
90
108
|
```
|
|
91
109
|
|
|
92
110
|
### Middleware
|
|
93
111
|
|
|
94
112
|
```ts
|
|
95
|
-
app.use((req, _res, next) => {
|
|
113
|
+
app.use(async (req, _res, next) => {
|
|
96
114
|
// Do something with req
|
|
97
|
-
next();
|
|
115
|
+
await next();
|
|
98
116
|
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### CORS
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
app.use(express.cors());
|
|
99
123
|
|
|
100
124
|
```
|
|
101
125
|
|
|
126
|
+
### Cookies
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
app.get("/set-cookie", async (_req, res, _next) => {
|
|
130
|
+
res.cookie("sid", "abc");
|
|
131
|
+
res.send("ok");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
app.get("/read-cookie", async (req, res, _next) => {
|
|
135
|
+
res.json({ sid: req.cookies["sid"] });
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
102
139
|
Error middleware:
|
|
103
140
|
|
|
104
141
|
```ts
|
|
105
|
-
app.useError((err, _req, res, _next) => {
|
|
142
|
+
app.useError(async (err, _req, res, _next) => {
|
|
106
143
|
res.status(500).json({ error: `${err}` });
|
|
107
144
|
});
|
|
108
145
|
```
|
|
@@ -112,7 +149,7 @@ app.useError((err, _req, res, _next) => {
|
|
|
112
149
|
`Request` highlights:
|
|
113
150
|
|
|
114
151
|
- `req.method`, `req.path`, `req.originalUrl`
|
|
115
|
-
- `req.query`, `req.params`
|
|
152
|
+
- `req.query`, `req.params`, `req.cookies`, `req.signedCookies`
|
|
116
153
|
- `req.body` (when using body parsers)
|
|
117
154
|
- `req.get(name)` / `req.header(name)`
|
|
118
155
|
|
|
@@ -134,6 +171,19 @@ app.use(express.raw());
|
|
|
134
171
|
|
|
135
172
|
```
|
|
136
173
|
|
|
174
|
+
### Multipart / file uploads
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
const upload = express.multipart();
|
|
178
|
+
|
|
179
|
+
app.post("/upload", upload.single("avatar"), async (req, res, _next) => {
|
|
180
|
+
res.json({
|
|
181
|
+
filename: req.file?.originalname,
|
|
182
|
+
fields: req.body,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
137
187
|
### Static files
|
|
138
188
|
|
|
139
189
|
```ts
|
|
@@ -165,4 +215,3 @@ This repo is versioned by runtime major:
|
|
|
165
215
|
|
|
166
216
|
MIT
|
|
167
217
|
|
|
168
|
-
|
|
@@ -1,6 +1,18 @@
|
|
|
1
|
-
app.get("/health", (_req, res) =>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
app.
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
app.get("/health", async (_req, res, _next) => {
|
|
2
|
+
res.send("ok");
|
|
3
|
+
});
|
|
4
|
+
app.post("/items", async (req, res, _next) => {
|
|
5
|
+
res.json(req.body);
|
|
6
|
+
});
|
|
7
|
+
app.put("/items/:id", async (req, res, _next) => {
|
|
8
|
+
res.send(req.params["id"] ?? "");
|
|
9
|
+
});
|
|
10
|
+
app.delete("/items/:id", async (_req, res, _next) => {
|
|
11
|
+
res.sendStatus(204);
|
|
12
|
+
});
|
|
13
|
+
app.patch("/items/:id", async (_req, res, _next) => {
|
|
14
|
+
res.sendStatus(204);
|
|
15
|
+
});
|
|
16
|
+
app.all("/anything", async (_req, res, _next) => {
|
|
17
|
+
res.send("matched");
|
|
18
|
+
});
|