@tsonic/express 10.0.25 → 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 +69 -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 +5 -0
- 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 -7
- package/index/bindings.json +2433 -6262
- package/index/internal/index.d.ts +150 -232
- package/index.d.ts +9 -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,6 +75,11 @@ import { express } from "@tsonic/express/index.js";
|
|
|
69
75
|
const app = express.create();
|
|
70
76
|
const router = express.Router();
|
|
71
77
|
|
|
78
|
+
router.get("/ping", async (_req, res, _next) => {
|
|
79
|
+
res.send("pong");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
app.use("/api", router);
|
|
72
83
|
```
|
|
73
84
|
|
|
74
85
|
### Routing
|
|
@@ -76,29 +87,59 @@ const router = express.Router();
|
|
|
76
87
|
Common verbs:
|
|
77
88
|
|
|
78
89
|
```ts
|
|
79
|
-
app.get("/health", (_req, res) =>
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
app.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
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
|
+
});
|
|
86
108
|
```
|
|
87
109
|
|
|
88
110
|
### Middleware
|
|
89
111
|
|
|
90
112
|
```ts
|
|
91
|
-
app.use((req, _res, next) => {
|
|
113
|
+
app.use(async (req, _res, next) => {
|
|
92
114
|
// Do something with req
|
|
93
|
-
next();
|
|
115
|
+
await next();
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### CORS
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
app.use(express.cors());
|
|
123
|
+
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Cookies
|
|
127
|
+
|
|
128
|
+
```ts
|
|
129
|
+
app.get("/set-cookie", async (_req, res, _next) => {
|
|
130
|
+
res.cookie("sid", "abc");
|
|
131
|
+
res.send("ok");
|
|
94
132
|
});
|
|
95
133
|
|
|
134
|
+
app.get("/read-cookie", async (req, res, _next) => {
|
|
135
|
+
res.json({ sid: req.cookies["sid"] });
|
|
136
|
+
});
|
|
96
137
|
```
|
|
97
138
|
|
|
98
139
|
Error middleware:
|
|
99
140
|
|
|
100
141
|
```ts
|
|
101
|
-
app.useError((err, _req, res, _next) => {
|
|
142
|
+
app.useError(async (err, _req, res, _next) => {
|
|
102
143
|
res.status(500).json({ error: `${err}` });
|
|
103
144
|
});
|
|
104
145
|
```
|
|
@@ -108,7 +149,7 @@ app.useError((err, _req, res, _next) => {
|
|
|
108
149
|
`Request` highlights:
|
|
109
150
|
|
|
110
151
|
- `req.method`, `req.path`, `req.originalUrl`
|
|
111
|
-
- `req.query`, `req.params`
|
|
152
|
+
- `req.query`, `req.params`, `req.cookies`, `req.signedCookies`
|
|
112
153
|
- `req.body` (when using body parsers)
|
|
113
154
|
- `req.get(name)` / `req.header(name)`
|
|
114
155
|
|
|
@@ -130,6 +171,19 @@ app.use(express.raw());
|
|
|
130
171
|
|
|
131
172
|
```
|
|
132
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
|
+
|
|
133
187
|
### Static files
|
|
134
188
|
|
|
135
189
|
```ts
|
|
@@ -161,4 +215,3 @@ This repo is versioned by runtime major:
|
|
|
161
215
|
|
|
162
216
|
MIT
|
|
163
217
|
|
|
164
|
-
|
|
@@ -1,7 +1,18 @@
|
|
|
1
|
-
app.get("/health", (_req, res) =>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
app.
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
+
});
|