@web-ts-toolkit/express-json-router 0.4.0 → 0.6.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 +22 -14
- package/index.d.mts +10 -1
- package/index.d.ts +10 -1
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@web-ts-toolkit/express-json-router`
|
|
2
2
|
|
|
3
|
-
Express router wrapper that
|
|
3
|
+
Express router wrapper that routes handler return values through `@web-ts-toolkit/express-response-handler`.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -8,12 +8,14 @@ Express router wrapper that wires route handlers through `@web-ts-toolkit/expres
|
|
|
8
8
|
pnpm add @web-ts-toolkit/express-json-router express
|
|
9
9
|
```
|
|
10
10
|
|
|
11
|
-
##
|
|
11
|
+
## Highlights
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
-
|
|
13
|
+
- return plain values from route handlers
|
|
14
|
+
- throw typed HTTP errors
|
|
15
|
+
- use custom response-handler instances when needed
|
|
16
|
+
- inspect registered endpoints with `getEndpoints()`
|
|
15
17
|
|
|
16
|
-
##
|
|
18
|
+
## Quick Start
|
|
17
19
|
|
|
18
20
|
```ts
|
|
19
21
|
import express from 'express';
|
|
@@ -28,13 +30,19 @@ router.get('/users/:id', () => {
|
|
|
28
30
|
throw new JsonRouter.clientErrors.NotFoundError('User not found');
|
|
29
31
|
});
|
|
30
32
|
|
|
31
|
-
JsonRouter.errorMessageProvider = (error) => {
|
|
32
|
-
if (error instanceof Error) {
|
|
33
|
-
return { message: error.message };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return { message: String(error) };
|
|
37
|
-
};
|
|
38
|
-
|
|
39
33
|
app.use(router.original);
|
|
40
34
|
```
|
|
35
|
+
|
|
36
|
+
## Main Exports
|
|
37
|
+
|
|
38
|
+
- `JsonRouter`
|
|
39
|
+
- `JsonRouter.HttpResponse`
|
|
40
|
+
- `JsonRouter.clientErrors`
|
|
41
|
+
- `JsonRouter.createHandler(...)`
|
|
42
|
+
- `JsonRouter.ErrorFormats`
|
|
43
|
+
|
|
44
|
+
## Documentation
|
|
45
|
+
|
|
46
|
+
Full package documentation lives in `website/docs/packages/express-json-router.md`.
|
|
47
|
+
|
|
48
|
+
- live docs: https://web-ts-toolkit.pages.dev/docs/packages/express-json-router
|
package/index.d.mts
CHANGED
|
@@ -17,6 +17,15 @@ type Endpoint = {
|
|
|
17
17
|
};
|
|
18
18
|
type ExpressRouter = ReturnType<typeof express.Router>;
|
|
19
19
|
type JsonRouterMiddlewares = JsonRouterCallback | JsonRouterCallback[];
|
|
20
|
+
/**
|
|
21
|
+
* Express router that serializes route handler return values as JSON and
|
|
22
|
+
* converts thrown `HttpError`s into structured error responses.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import JsonRouter from '@web-ts-toolkit/express-json-router';
|
|
26
|
+
* const router = new JsonRouter('/api');
|
|
27
|
+
* router.get('/health', () => ({ ok: true }));
|
|
28
|
+
*/
|
|
20
29
|
declare class JsonRouter {
|
|
21
30
|
readonly methods: RouteMethod[];
|
|
22
31
|
readonly endpoints: Endpoint[];
|
|
@@ -69,7 +78,7 @@ declare class JsonRouter {
|
|
|
69
78
|
created: (data: unknown) => Created<unknown>;
|
|
70
79
|
accepted: (data: unknown) => Accepted<unknown>;
|
|
71
80
|
nonAuthoritativeInfo: (data: unknown) => NonAuthoritativeInfo<unknown>;
|
|
72
|
-
noContent: (
|
|
81
|
+
noContent: () => NoContent;
|
|
73
82
|
resetContent: (data: unknown) => ResetContent<unknown>;
|
|
74
83
|
partialContent: (data: unknown) => PartialContent<unknown>;
|
|
75
84
|
multiStatus: (data: unknown) => MultiStatus<unknown>;
|
package/index.d.ts
CHANGED
|
@@ -17,6 +17,15 @@ type Endpoint = {
|
|
|
17
17
|
};
|
|
18
18
|
type ExpressRouter = ReturnType<typeof express.Router>;
|
|
19
19
|
type JsonRouterMiddlewares = JsonRouterCallback | JsonRouterCallback[];
|
|
20
|
+
/**
|
|
21
|
+
* Express router that serializes route handler return values as JSON and
|
|
22
|
+
* converts thrown `HttpError`s into structured error responses.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* import JsonRouter from '@web-ts-toolkit/express-json-router';
|
|
26
|
+
* const router = new JsonRouter('/api');
|
|
27
|
+
* router.get('/health', () => ({ ok: true }));
|
|
28
|
+
*/
|
|
20
29
|
declare class JsonRouter {
|
|
21
30
|
readonly methods: RouteMethod[];
|
|
22
31
|
readonly endpoints: Endpoint[];
|
|
@@ -69,7 +78,7 @@ declare class JsonRouter {
|
|
|
69
78
|
created: (data: unknown) => Created<unknown>;
|
|
70
79
|
accepted: (data: unknown) => Accepted<unknown>;
|
|
71
80
|
nonAuthoritativeInfo: (data: unknown) => NonAuthoritativeInfo<unknown>;
|
|
72
|
-
noContent: (
|
|
81
|
+
noContent: () => NoContent;
|
|
73
82
|
resetContent: (data: unknown) => ResetContent<unknown>;
|
|
74
83
|
partialContent: (data: unknown) => PartialContent<unknown>;
|
|
75
84
|
multiStatus: (data: unknown) => MultiStatus<unknown>;
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@web-ts-toolkit/express-json-router",
|
|
3
|
-
"description": "Express router wrapper for JSON responses",
|
|
4
|
-
"
|
|
3
|
+
"description": "Express router wrapper for return-value JSON responses",
|
|
4
|
+
"homepage": "https://web-ts-toolkit.pages.dev/docs/packages/express-json-router",
|
|
5
|
+
"version": "0.6.0",
|
|
6
|
+
"sideEffects": false,
|
|
5
7
|
"keywords": [
|
|
6
8
|
"express",
|
|
7
9
|
"api",
|
|
8
10
|
"json",
|
|
9
|
-
"router"
|
|
11
|
+
"router",
|
|
12
|
+
"http"
|
|
10
13
|
],
|
|
11
14
|
"main": "./index.js",
|
|
12
15
|
"module": "./index.mjs",
|
|
@@ -19,19 +22,19 @@
|
|
|
19
22
|
"default": "./index.js"
|
|
20
23
|
}
|
|
21
24
|
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">=20"
|
|
27
|
+
},
|
|
22
28
|
"dependencies": {
|
|
23
|
-
"@web-ts-toolkit/express-response-handler": "0.
|
|
24
|
-
"@web-ts-toolkit/http-errors": "0.
|
|
25
|
-
"@web-ts-toolkit/utils": "0.
|
|
29
|
+
"@web-ts-toolkit/express-response-handler": "0.6.0",
|
|
30
|
+
"@web-ts-toolkit/http-errors": "0.6.0",
|
|
31
|
+
"@web-ts-toolkit/utils": "0.6.0",
|
|
26
32
|
"express": "^5.2.1"
|
|
27
33
|
},
|
|
28
34
|
"author": "Junmin Ahn",
|
|
29
35
|
"bugs": {
|
|
30
36
|
"url": "https://github.com/egose/web-ts-toolkit/issues"
|
|
31
37
|
},
|
|
32
|
-
"engines": {
|
|
33
|
-
"node": ">=20"
|
|
34
|
-
},
|
|
35
38
|
"license": "Apache-2.0",
|
|
36
39
|
"repository": {
|
|
37
40
|
"type": "git",
|