@web-ts-toolkit/express-json-router 0.2.0 → 0.4.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.
Files changed (2) hide show
  1. package/README.md +6 -110
  2. package/package.json +4 -4
package/README.md CHANGED
@@ -8,7 +8,12 @@ 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
- ## Usage
11
+ ## Documentation
12
+
13
+ - Full package documentation lives in `website/docs/packages/express-json-router.md`.
14
+ - Use the Docusaurus site in `website` for the complete guide and API notes.
15
+
16
+ ## Minimal Example
12
17
 
13
18
  ```ts
14
19
  import express from 'express';
@@ -33,112 +38,3 @@ JsonRouter.errorMessageProvider = (error) => {
33
38
 
34
39
  app.use(router.original);
35
40
  ```
36
-
37
- ## Structured Error Formats
38
-
39
- `JsonRouter` uses the shared default response handler out of the box. If you want a different error format such as RFC 9457, create a custom handler and pass it to the router constructor:
40
-
41
- ```ts
42
- import JsonRouter from '@web-ts-toolkit/express-json-router';
43
- import { BadRequestError } from '@web-ts-toolkit/http-errors';
44
-
45
- const responseHandler = JsonRouter.createHandler({
46
- errorFormat: JsonRouter.ErrorFormats.rfc9457,
47
- errorDomain: 'api.example.com',
48
- });
49
-
50
- const router = new JsonRouter('/api', undefined, responseHandler);
51
-
52
- router.get('/users', () => {
53
- throw new BadRequestError('invalid email', {
54
- type: 'https://api.example.com/problems/invalid-email',
55
- title: 'Invalid email address',
56
- errors: [
57
- {
58
- detail: 'must be a valid email address',
59
- pointer: '#/email',
60
- },
61
- ],
62
- });
63
- });
64
- ```
65
-
66
- The static hook properties such as `JsonRouter.preJson` and `JsonRouter.errorMessageProvider` still proxy the shared default handler. When you pass a custom handler instance, configure that handler directly before giving it to the router.
67
-
68
- ## Behavior
69
-
70
- - Route handlers can return plain values, promises, `JsonRouter.HttpResponse.*` helpers, or throw `JsonRouter.clientErrors.*` errors.
71
- - Router-level middleware can be passed as a single function or an array in the constructor.
72
- - A custom response-handler instance can be passed as the third constructor argument when you need `aip193` or `rfc9457` error formatting.
73
- - `router.route(path)` supports the same JSON-aware handler behavior as `router.get(path, ...)`, `router.post(path, ...)`, and the other Express router methods exposed by the instance.
74
- - `router.getEndpoints()` returns a snapshot of the registered endpoints in registration order.
75
-
76
- ## Hooks
77
-
78
- The package forwards the shared hooks from `@web-ts-toolkit/express-response-handler` through static properties on `JsonRouter`.
79
-
80
- ```ts
81
- JsonRouter.preJson = (value) => {
82
- console.log('about to serialize', value);
83
- };
84
-
85
- JsonRouter.postJson = (value) => {
86
- console.log('serialized', value);
87
- };
88
-
89
- JsonRouter.preError = (error) => {
90
- console.error('request failed', error);
91
- };
92
- ```
93
-
94
- These hooks are shared process-wide because they proxy the default response-handler instance.
95
-
96
- ## API
97
-
98
- `new JsonRouter(basePath?, middlewares?, responseHandler?)`
99
-
100
- Creates a JSON-aware Express router. `basePath` accepts values like `'/api'`, `'api'`, or `'api/'` and is normalized for route registration. `responseHandler` defaults to the shared handler instance from `@web-ts-toolkit/express-response-handler`.
101
-
102
- `router.original`
103
-
104
- Returns the underlying Express router so it can be mounted with `app.use(...)`.
105
-
106
- `router.route(path)`
107
-
108
- Builds chained route registrations such as `router.route('/users').get(...).post(...)`.
109
-
110
- `router.getEndpoints()`
111
-
112
- Returns `{ method, path }[]` for the routes registered through `JsonRouter`.
113
-
114
- `JsonRouter.clientErrors`
115
-
116
- Re-exports the HTTP error classes from `@web-ts-toolkit/http-errors`.
117
-
118
- `JsonRouter.success`
119
-
120
- Re-exports success response classes such as `JsonRouter.success.Created`.
121
-
122
- `JsonRouter.HttpResponse`
123
-
124
- Exposes helper constructors such as `JsonRouter.HttpResponse.ok(...)` and `JsonRouter.HttpResponse.created(...)`.
125
-
126
- `JsonRouter.defaultHandler`
127
-
128
- Exposes the shared default response-handler instance used by `JsonRouter` when no custom handler is provided.
129
-
130
- `JsonRouter.ErrorFormats`
131
-
132
- Exposes named error format constants such as `JsonRouter.ErrorFormats.rfc9457`.
133
-
134
- `JsonRouter.createHandler`
135
-
136
- Re-exports `createHandler(...)` from `@web-ts-toolkit/express-response-handler` so you can provide a custom handler instance to the router.
137
-
138
- `JsonRouter.errorMessageProvider`
139
-
140
- Overrides the error-to-payload mapping used for non-HTTP errors.
141
-
142
- `JsonRouter.preJson`, `JsonRouter.postJson`, `JsonRouter.preError`, `JsonRouter.postError`
143
-
144
- Expose the shared serialization and error hooks from `@web-ts-toolkit/express-response-handler`.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@web-ts-toolkit/express-json-router",
3
3
  "description": "Express router wrapper for JSON responses",
4
- "version": "0.2.0",
4
+ "version": "0.4.0",
5
5
  "keywords": [
6
6
  "express",
7
7
  "api",
@@ -20,9 +20,9 @@
20
20
  }
21
21
  },
22
22
  "dependencies": {
23
- "@web-ts-toolkit/express-response-handler": "0.2.0",
24
- "@web-ts-toolkit/http-errors": "0.2.0",
25
- "@web-ts-toolkit/utils": "0.2.0",
23
+ "@web-ts-toolkit/express-response-handler": "0.4.0",
24
+ "@web-ts-toolkit/http-errors": "0.4.0",
25
+ "@web-ts-toolkit/utils": "0.4.0",
26
26
  "express": "^5.2.1"
27
27
  },
28
28
  "author": "Junmin Ahn",