@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 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) => res.send("ok"));
80
- app.post("/items", (req, res) => res.json(req.body));
81
- app.put("/items/:id", (req, res) => res.send(req.param("id")));
82
- app.delete("/items/:id", (_req, res) => res.sendStatus(204));
83
- app.patch("/items/:id", (_req, res) => res.sendStatus(204));
84
- app.all("/anything", (_req, res) => res.send("matched"));
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
-
@@ -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,3 +1,8 @@
1
1
  const app = express.create();
2
2
  const router = express.Router();
3
3
 
4
+ router.get("/ping", async (_req, res, _next) => {
5
+ res.send("pong");
6
+ });
7
+
8
+ app.use("/api", router);
@@ -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,7 +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.param("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"));
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
+ });