@tsonic/express 10.0.26 → 10.0.28

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 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) => res.send("ok"));
85
- app.post("/items", (req, res) => res.json(req.body));
86
- app.put("/items/:id", (req, res) => res.send(req.params["id"] ?? ""));
87
- app.delete("/items/:id", (_req, res) => res.sendStatus(204));
88
- app.patch("/items/:id", (_req, res) => res.sendStatus(204));
89
- app.all("/anything", (_req, res) => res.send("matched"));
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
-
@@ -0,0 +1,8 @@
1
+ app.get("/set-cookie", async (_req, res, _next) => {
2
+ res.cookie("sid", "abc");
3
+ res.send("ok");
4
+ });
5
+
6
+ app.get("/read-cookie", async (req, res, _next) => {
7
+ res.json({ sid: req.cookies["sid"] });
8
+ });
@@ -0,0 +1,2 @@
1
+ app.use(express.cors());
2
+
@@ -1,7 +1,7 @@
1
1
  const app = express.create();
2
2
  const router = express.Router();
3
3
 
4
- router.get("/ping", (_req, res) => {
4
+ router.get("/ping", async (_req, res, _next) => {
5
5
  res.send("pong");
6
6
  });
7
7
 
@@ -1,3 +1,3 @@
1
- app.useError((err, _req, res, _next) => {
1
+ app.useError(async (err, _req, res, _next) => {
2
2
  res.status(500).json({ error: `${err}` });
3
3
  });
@@ -3,10 +3,9 @@ import { express } from "@tsonic/express/index.js";
3
3
  export function main(): void {
4
4
  const app = express.create();
5
5
 
6
- app.get("/", (_req, res) => {
6
+ app.get("/", async (_req, res, _next) => {
7
7
  res.send("hello");
8
8
  });
9
9
 
10
10
  app.listen(3000);
11
11
  }
12
-
@@ -1,5 +1,4 @@
1
- app.use((req, _res, next) => {
1
+ app.use(async (req, _res, next) => {
2
2
  // Do something with req
3
- next();
3
+ await next();
4
4
  });
5
-
@@ -0,0 +1,8 @@
1
+ const upload = express.multipart();
2
+
3
+ app.post("/upload", upload.single("avatar"), async (req, res, _next) => {
4
+ res.json({
5
+ filename: req.file?.originalname,
6
+ fields: req.body,
7
+ });
8
+ });
@@ -3,10 +3,9 @@ import { express } from "@tsonic/express/index.js";
3
3
  export function main(): void {
4
4
  const app = express.create();
5
5
 
6
- app.get("/", (_req, res) => {
6
+ app.get("/", async (_req, res, _next) => {
7
7
  res.json({ ok: true });
8
8
  });
9
9
 
10
10
  app.listen(3000);
11
11
  }
12
-
@@ -1,6 +1,18 @@
1
- app.get("/health", (_req, res) => res.send("ok"));
2
- app.post("/items", (req, res) => res.json(req.body));
3
- app.put("/items/:id", (req, res) => res.send(req.params["id"] ?? ""));
4
- app.delete("/items/:id", (_req, res) => res.sendStatus(204));
5
- app.patch("/items/:id", (_req, res) => res.sendStatus(204));
6
- app.all("/anything", (_req, res) => res.send("matched"));
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
+ });