@web-ts-toolkit/express-response-handler 0.4.0 → 0.5.0
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 +24 -18
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -1,48 +1,40 @@
|
|
|
1
|
-
# express-response-handler
|
|
1
|
+
# `@web-ts-toolkit/express-response-handler`
|
|
2
2
|
|
|
3
3
|
FastAPI-style return-value responses for Express.
|
|
4
4
|
|
|
5
|
-
Instead of calling `res.json(...)` in every route, return a value. This package turns that return value into a `200 OK` JSON response, while still letting you return explicit response wrappers or throw errors when needed.
|
|
6
|
-
|
|
7
5
|
## Installation
|
|
8
6
|
|
|
9
7
|
```sh
|
|
10
|
-
|
|
8
|
+
pnpm add @web-ts-toolkit/express-response-handler
|
|
11
9
|
```
|
|
12
10
|
|
|
13
|
-
##
|
|
11
|
+
## Highlights
|
|
14
12
|
|
|
15
|
-
-
|
|
16
|
-
-
|
|
13
|
+
- return plain JSON values instead of calling `res.json(...)`
|
|
14
|
+
- return explicit `HttpResponse` wrappers for status control
|
|
15
|
+
- throw typed HTTP errors
|
|
16
|
+
- switch between simple, AIP-193, and RFC 9457-style error payloads
|
|
17
17
|
|
|
18
|
-
##
|
|
18
|
+
## Quick Start
|
|
19
19
|
|
|
20
20
|
```ts
|
|
21
21
|
import express from 'express';
|
|
22
|
-
|
|
23
22
|
import apiHandler from '@web-ts-toolkit/express-response-handler';
|
|
24
23
|
import { NotFoundError } from '@web-ts-toolkit/http-errors';
|
|
25
24
|
|
|
26
25
|
const { handleResponse, HttpResponse } = apiHandler;
|
|
27
|
-
|
|
28
26
|
const app = express();
|
|
29
27
|
|
|
30
28
|
app.get(
|
|
31
29
|
'/health',
|
|
32
|
-
handleResponse(() => {
|
|
33
|
-
return { ok: true };
|
|
34
|
-
}),
|
|
30
|
+
handleResponse(() => ({ ok: true })),
|
|
35
31
|
);
|
|
36
32
|
|
|
37
33
|
app.get(
|
|
38
34
|
'/users/:id',
|
|
39
35
|
handleResponse(async (req) => {
|
|
40
36
|
const user = await getUser(req.params.id);
|
|
41
|
-
|
|
42
|
-
if (!user) {
|
|
43
|
-
throw new NotFoundError('user not found');
|
|
44
|
-
}
|
|
45
|
-
|
|
37
|
+
if (!user) throw new NotFoundError('user not found');
|
|
46
38
|
return user;
|
|
47
39
|
}),
|
|
48
40
|
);
|
|
@@ -55,3 +47,17 @@ app.post(
|
|
|
55
47
|
}),
|
|
56
48
|
);
|
|
57
49
|
```
|
|
50
|
+
|
|
51
|
+
## Main Exports
|
|
52
|
+
|
|
53
|
+
- default handler instance
|
|
54
|
+
- `handleResponse(...)`
|
|
55
|
+
- `HttpResponse`
|
|
56
|
+
- `createHandler(...)`
|
|
57
|
+
- `ErrorFormats`
|
|
58
|
+
|
|
59
|
+
## Documentation
|
|
60
|
+
|
|
61
|
+
Full package documentation lives in `website/docs/packages/express-response-handler.md`.
|
|
62
|
+
|
|
63
|
+
- live docs: https://web-ts-toolkit.pages.dev/docs/packages/express-response-handler
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@web-ts-toolkit/express-response-handler",
|
|
3
|
-
"description": "
|
|
4
|
-
"
|
|
3
|
+
"description": "FastAPI-style return-value response handling for Express",
|
|
4
|
+
"homepage": "https://web-ts-toolkit.pages.dev/docs/packages/express-response-handler",
|
|
5
|
+
"version": "0.5.0",
|
|
6
|
+
"sideEffects": false,
|
|
5
7
|
"keywords": [
|
|
6
8
|
"express",
|
|
7
9
|
"api",
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
+
"responses",
|
|
11
|
+
"http",
|
|
12
|
+
"error-handling"
|
|
10
13
|
],
|
|
11
14
|
"main": "./index.js",
|
|
12
15
|
"module": "./index.mjs",
|
|
@@ -43,18 +46,18 @@
|
|
|
43
46
|
"default": "./responses/success.js"
|
|
44
47
|
}
|
|
45
48
|
},
|
|
49
|
+
"engines": {
|
|
50
|
+
"node": ">=20"
|
|
51
|
+
},
|
|
46
52
|
"dependencies": {
|
|
47
53
|
"@fast-csv/format": "^5.0.5",
|
|
48
|
-
"@web-ts-toolkit/http-errors": "0.
|
|
49
|
-
"@web-ts-toolkit/utils": "0.
|
|
54
|
+
"@web-ts-toolkit/http-errors": "0.5.0",
|
|
55
|
+
"@web-ts-toolkit/utils": "0.5.0"
|
|
50
56
|
},
|
|
51
57
|
"author": "Junmin Ahn",
|
|
52
58
|
"bugs": {
|
|
53
59
|
"url": "https://github.com/egose/web-ts-toolkit/issues"
|
|
54
60
|
},
|
|
55
|
-
"engines": {
|
|
56
|
-
"node": ">=20"
|
|
57
|
-
},
|
|
58
61
|
"license": "Apache-2.0",
|
|
59
62
|
"repository": {
|
|
60
63
|
"type": "git",
|