hono-problem-details 0.12.0 → 0.12.2
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 +13 -2
- package/dist/index.cjs +22 -5
- package/dist/index.d.cts +3 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +22 -5
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -539,14 +539,19 @@ problemDetailsHandler({
|
|
|
539
539
|
|
|
540
540
|
The callback receives the fully-built `ProblemDetails` object and the Hono `Context`, allowing access to headers like `Accept-Language`. Return a new `ProblemDetails` with translated fields.
|
|
541
541
|
|
|
542
|
+
> **Note on extensions**: The patch is merged shallowly, so returning `extensions` replaces
|
|
543
|
+
> all extension members (`code`, `traceId`, validation `errors`, ...). Spread the original
|
|
544
|
+
> to keep them: `return { extensions: { ...pd.extensions, hint: translate(lang, "hint") } }`.
|
|
545
|
+
|
|
542
546
|
> **Note on caching**: If your responses vary by `Accept-Language`, add `Vary: Accept-Language`
|
|
543
547
|
> from your own middleware so CDNs and browser caches don't serve the wrong translation.
|
|
544
548
|
> This middleware intentionally does not set `Vary` — error handlers shouldn't mutate
|
|
545
549
|
> request-scope headers that also apply to successful responses.
|
|
546
550
|
|
|
547
551
|
> **Note on failures**: If your `localize` callback throws, the handler falls back to the
|
|
548
|
-
> un-localized `ProblemDetails` and continues.
|
|
549
|
-
>
|
|
552
|
+
> un-localized `ProblemDetails` and continues. Hono hands an error thrown from inside
|
|
553
|
+
> `app.onError` back to `onError` (several times when middleware is present), and if it keeps
|
|
554
|
+
> throwing, the request rejects, so the swallow is deliberate. Catch errors inside your
|
|
550
555
|
> callback if you need to observe them.
|
|
551
556
|
|
|
552
557
|
## OpenTelemetry Integration
|
|
@@ -607,6 +612,12 @@ problemDetailsHandler({
|
|
|
607
612
|
});
|
|
608
613
|
```
|
|
609
614
|
|
|
615
|
+
> **Note on a throw after `await next()`**: the route's response already exists at that point, and
|
|
616
|
+
> Hono copies its headers onto whatever `onError` returns, except `Content-Type`. Behind `compress()`,
|
|
617
|
+
> this can send `Content-Encoding: gzip` on an uncompressed problem body, which clients cannot decode.
|
|
618
|
+
> Any `onError` handler is affected, not only this one. To avoid it, throw before the response is
|
|
619
|
+
> produced, or register `compress()` outside the middleware that throws.
|
|
620
|
+
|
|
610
621
|
## Used By
|
|
611
622
|
|
|
612
623
|
The following Hono middleware libraries use `hono-problem-details` as an optional dependency for RFC 9457 error responses:
|
package/dist/index.cjs
CHANGED
|
@@ -183,11 +183,22 @@ function getOtelTraceId(api) {
|
|
|
183
183
|
|
|
184
184
|
// src/handler.ts
|
|
185
185
|
var REPRESENTATION_HEADERS = /* @__PURE__ */ new Set([
|
|
186
|
-
"
|
|
186
|
+
"content-type",
|
|
187
|
+
"content-encoding",
|
|
188
|
+
"content-language",
|
|
189
|
+
"content-length",
|
|
190
|
+
"content-location",
|
|
191
|
+
"content-range",
|
|
192
|
+
"content-disposition",
|
|
193
|
+
"content-md5",
|
|
194
|
+
"content-digest",
|
|
195
|
+
"content-signature",
|
|
196
|
+
"repr-digest",
|
|
197
|
+
"digest",
|
|
187
198
|
"etag",
|
|
188
199
|
"last-modified",
|
|
189
|
-
"
|
|
190
|
-
"
|
|
200
|
+
"accept-ranges",
|
|
201
|
+
"transfer-encoding"
|
|
191
202
|
]);
|
|
192
203
|
function buildType(status, options) {
|
|
193
204
|
if (options.typePrefix) {
|
|
@@ -199,7 +210,7 @@ function buildType(status, options) {
|
|
|
199
210
|
function copyResHeaders(error, response) {
|
|
200
211
|
if (error instanceof import_http_exception.HTTPException) {
|
|
201
212
|
error.res?.headers.forEach((value, name) => {
|
|
202
|
-
if (!
|
|
213
|
+
if (!REPRESENTATION_HEADERS.has(name)) {
|
|
203
214
|
response.headers.append(name, value);
|
|
204
215
|
}
|
|
205
216
|
});
|
|
@@ -233,7 +244,7 @@ function toResponse(input, c, options) {
|
|
|
233
244
|
return buildProblemResponse(pd);
|
|
234
245
|
}
|
|
235
246
|
function problemDetailsHandler(options = {}) {
|
|
236
|
-
|
|
247
|
+
const handle = (error, c) => {
|
|
237
248
|
if (error instanceof ProblemDetailsError) {
|
|
238
249
|
const pd = error.problemDetails;
|
|
239
250
|
return toResponse(error.hasExplicitType ? pd : { ...pd, type: void 0 }, c, options);
|
|
@@ -278,6 +289,12 @@ function problemDetailsHandler(options = {}) {
|
|
|
278
289
|
options
|
|
279
290
|
);
|
|
280
291
|
};
|
|
292
|
+
const dispatch = (first, second) => {
|
|
293
|
+
if (typeof second !== "function") return handle(first, second);
|
|
294
|
+
const c = first;
|
|
295
|
+
return c.error ? handle(c.error, c) : second();
|
|
296
|
+
};
|
|
297
|
+
return dispatch;
|
|
281
298
|
}
|
|
282
299
|
|
|
283
300
|
// src/registry.ts
|
package/dist/index.d.cts
CHANGED
|
@@ -75,6 +75,9 @@ interface ProblemDetailsHandlerOptions {
|
|
|
75
75
|
* Localize title/detail before sending the response.
|
|
76
76
|
* Returned fields are merged onto the original ProblemDetails, so callers may
|
|
77
77
|
* return a partial patch (e.g. `{ title: "..." }`) or omit the return entirely.
|
|
78
|
+
* The merge is shallow: a returned `extensions` replaces all extension members,
|
|
79
|
+
* so spread `pd.extensions` to keep `code`, `traceId`, or validation `errors`.
|
|
80
|
+
* A throwing callback is swallowed and the un-localized response is sent (ADR-0003).
|
|
78
81
|
*/
|
|
79
82
|
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
80
83
|
/**
|
package/dist/index.d.ts
CHANGED
|
@@ -75,6 +75,9 @@ interface ProblemDetailsHandlerOptions {
|
|
|
75
75
|
* Localize title/detail before sending the response.
|
|
76
76
|
* Returned fields are merged onto the original ProblemDetails, so callers may
|
|
77
77
|
* return a partial patch (e.g. `{ title: "..." }`) or omit the return entirely.
|
|
78
|
+
* The merge is shallow: a returned `extensions` replaces all extension members,
|
|
79
|
+
* so spread `pd.extensions` to keep `code`, `traceId`, or validation `errors`.
|
|
80
|
+
* A throwing callback is swallowed and the un-localized response is sent (ADR-0003).
|
|
78
81
|
*/
|
|
79
82
|
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
80
83
|
/**
|
package/dist/index.js
CHANGED
|
@@ -32,11 +32,22 @@ function getOtelTraceId(api) {
|
|
|
32
32
|
|
|
33
33
|
// src/handler.ts
|
|
34
34
|
var REPRESENTATION_HEADERS = /* @__PURE__ */ new Set([
|
|
35
|
-
"
|
|
35
|
+
"content-type",
|
|
36
|
+
"content-encoding",
|
|
37
|
+
"content-language",
|
|
38
|
+
"content-length",
|
|
39
|
+
"content-location",
|
|
40
|
+
"content-range",
|
|
41
|
+
"content-disposition",
|
|
42
|
+
"content-md5",
|
|
43
|
+
"content-digest",
|
|
44
|
+
"content-signature",
|
|
45
|
+
"repr-digest",
|
|
46
|
+
"digest",
|
|
36
47
|
"etag",
|
|
37
48
|
"last-modified",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
49
|
+
"accept-ranges",
|
|
50
|
+
"transfer-encoding"
|
|
40
51
|
]);
|
|
41
52
|
function buildType(status, options) {
|
|
42
53
|
if (options.typePrefix) {
|
|
@@ -48,7 +59,7 @@ function buildType(status, options) {
|
|
|
48
59
|
function copyResHeaders(error, response) {
|
|
49
60
|
if (error instanceof HTTPException) {
|
|
50
61
|
error.res?.headers.forEach((value, name) => {
|
|
51
|
-
if (!
|
|
62
|
+
if (!REPRESENTATION_HEADERS.has(name)) {
|
|
52
63
|
response.headers.append(name, value);
|
|
53
64
|
}
|
|
54
65
|
});
|
|
@@ -82,7 +93,7 @@ function toResponse(input, c, options) {
|
|
|
82
93
|
return buildProblemResponse(pd);
|
|
83
94
|
}
|
|
84
95
|
function problemDetailsHandler(options = {}) {
|
|
85
|
-
|
|
96
|
+
const handle = (error, c) => {
|
|
86
97
|
if (error instanceof ProblemDetailsError) {
|
|
87
98
|
const pd = error.problemDetails;
|
|
88
99
|
return toResponse(error.hasExplicitType ? pd : { ...pd, type: void 0 }, c, options);
|
|
@@ -127,6 +138,12 @@ function problemDetailsHandler(options = {}) {
|
|
|
127
138
|
options
|
|
128
139
|
);
|
|
129
140
|
};
|
|
141
|
+
const dispatch = (first, second) => {
|
|
142
|
+
if (typeof second !== "function") return handle(first, second);
|
|
143
|
+
const c = first;
|
|
144
|
+
return c.error ? handle(c.error, c) : second();
|
|
145
|
+
};
|
|
146
|
+
return dispatch;
|
|
130
147
|
}
|
|
131
148
|
|
|
132
149
|
// src/registry.ts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-problem-details",
|
|
3
|
-
"version": "0.12.
|
|
3
|
+
"version": "0.12.2",
|
|
4
4
|
"description": "RFC 9457 Problem Details middleware for Hono",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -151,22 +151,22 @@
|
|
|
151
151
|
"devDependencies": {
|
|
152
152
|
"@arethetypeswrong/cli": "0.18.5",
|
|
153
153
|
"@biomejs/biome": "^2.4.14",
|
|
154
|
-
"@changesets/changelog-github": "0.
|
|
155
|
-
"@changesets/cli": "
|
|
154
|
+
"@changesets/changelog-github": "1.0.1",
|
|
155
|
+
"@changesets/cli": "3.0.3",
|
|
156
156
|
"@hono/standard-validator": "0.4.0",
|
|
157
157
|
"@hono/valibot-validator": "^0.6.1",
|
|
158
158
|
"@hono/zod-openapi": "1.6.3",
|
|
159
159
|
"@hono/zod-validator": "^0.9.0",
|
|
160
160
|
"@opentelemetry/api": "^1.0.0",
|
|
161
161
|
"@standard-schema/spec": "1.1.0",
|
|
162
|
-
"@vitest/coverage-v8": "^4.1.
|
|
162
|
+
"@vitest/coverage-v8": "^4.1.12",
|
|
163
163
|
"hono": "^4.13.7",
|
|
164
|
-
"knip": "6.
|
|
165
|
-
"lefthook": "2.1
|
|
164
|
+
"knip": "6.41.0",
|
|
165
|
+
"lefthook": "2.2.1",
|
|
166
166
|
"tsup": "^8.0.0",
|
|
167
167
|
"typescript": "^6.0.3",
|
|
168
168
|
"valibot": "^1.3.1",
|
|
169
|
-
"vitest": "^4.1.
|
|
169
|
+
"vitest": "^4.1.12",
|
|
170
170
|
"zod": "^4.0.0"
|
|
171
171
|
},
|
|
172
172
|
"engines": {
|