hono-problem-details 0.8.1 → 0.9.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 CHANGED
@@ -53,7 +53,7 @@ npm install hono-problem-details
53
53
 
54
54
  ### Requirements
55
55
 
56
- - Hono `>= 4.12.14` (peer dependency)
56
+ - Hono `>= 4.12.27` (peer dependency; floor raised for upstream security fixes)
57
57
  - TypeScript `>= 5.0` — the published `.d.ts` files are CI-tested against TS 5.0, 5.4, 5.7, 5.9, and 6.0. Older TS versions may work but are not verified.
58
58
  - Node.js `>= 22` (Node 20 reached end-of-life in April 2026; v0.6.0 raised the floor)
59
59
 
@@ -74,7 +74,7 @@ app.get("/not-found", (c) => {
74
74
 
75
75
  // Response:
76
76
  // HTTP/1.1 404 Not Found
77
- // Content-Type: application/problem+json
77
+ // Content-Type: application/problem+json; charset=utf-8
78
78
  // {
79
79
  // "type": "about:blank",
80
80
  // "status": 404,
@@ -180,7 +180,7 @@ app.get("/boom", () => {
180
180
  });
181
181
 
182
182
  // HTTP/1.1 500 Internal Server Error
183
- // Content-Type: application/problem+json
183
+ // Content-Type: application/problem+json; charset=utf-8
184
184
  // {
185
185
  // "type": "about:blank",
186
186
  // "status": 500,
@@ -297,7 +297,7 @@ app.post("/users", zValidator("json", schema, zodProblemHook()), (c) => {
297
297
 
298
298
  // Validation error response:
299
299
  // HTTP/1.1 422 Unprocessable Content
300
- // Content-Type: application/problem+json
300
+ // Content-Type: application/problem+json; charset=utf-8
301
301
  // {
302
302
  // "type": "about:blank",
303
303
  // "status": 422,
@@ -447,7 +447,7 @@ problemDetailsHandler({
447
447
  });
448
448
  ```
449
449
 
450
- When enabled, the handler adds a `traceId` extension member to all problem details responses when a trace is active.
450
+ When enabled, the handler adds a `traceId` extension member to every response it produces (thrown `ProblemDetailsError`s, `HTTPException`s, and unhandled errors) when a trace is active. Validation-hook responses (`zodProblemHook`, `valibotProblemHook`, `standardSchemaProblemHook`) are returned directly by the validator and never pass through the handler, so they do not carry `traceId`.
451
451
  This allows clients to correlate errors with server-side traces for easier debugging.
452
452
 
453
453
  ## Handler Options
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  PROBLEM_JSON_CONTENT_TYPE
3
- } from "./chunk-63332VQA.js";
3
+ } from "./chunk-VPS467Z5.js";
4
4
 
5
5
  // src/integrations/validation.ts
6
6
  var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
@@ -31,4 +31,3 @@ function buildValidationResponse(errors, options) {
31
31
  export {
32
32
  buildValidationResponse
33
33
  };
34
- //# sourceMappingURL=chunk-O5AEI2VV.js.map
@@ -58,4 +58,3 @@ export {
58
58
  statusToPhrase,
59
59
  statusToSlug
60
60
  };
61
- //# sourceMappingURL=chunk-JW7PXR25.js.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  statusToPhrase
3
- } from "./chunk-JW7PXR25.js";
3
+ } from "./chunk-P4D7EVU3.js";
4
4
 
5
5
  // src/utils.ts
6
6
  var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
@@ -36,10 +36,11 @@ function normalizeProblemDetails(input) {
36
36
  }
37
37
  function buildProblemResponse(pd) {
38
38
  const { extensions, ...standard } = pd;
39
- const body = { ...sanitizeExtensions(extensions), ...standard };
39
+ const status = clampHttpStatus(pd.status);
40
+ const body = { ...sanitizeExtensions(extensions), ...standard, status };
40
41
  const { json, fallback } = safeStringify(body);
41
42
  return new Response(json, {
42
- status: fallback ? 500 : clampHttpStatus(pd.status),
43
+ status: fallback ? 500 : status,
43
44
  headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
44
45
  });
45
46
  }
@@ -56,4 +57,3 @@ export {
56
57
  normalizeProblemDetails,
57
58
  buildProblemResponse
58
59
  };
59
- //# sourceMappingURL=chunk-63332VQA.js.map
package/dist/index.cjs CHANGED
@@ -120,10 +120,11 @@ function normalizeProblemDetails(input) {
120
120
  }
121
121
  function buildProblemResponse(pd) {
122
122
  const { extensions, ...standard } = pd;
123
- const body = { ...sanitizeExtensions(extensions), ...standard };
123
+ const status = clampHttpStatus(pd.status);
124
+ const body = { ...sanitizeExtensions(extensions), ...standard, status };
124
125
  const { json, fallback } = safeStringify(body);
125
126
  return new Response(json, {
126
- status: fallback ? 500 : clampHttpStatus(pd.status),
127
+ status: fallback ? 500 : status,
127
128
  headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
128
129
  });
129
130
  }
@@ -139,6 +140,11 @@ function safeStringify(body) {
139
140
  var ProblemDetailsError = class extends Error {
140
141
  /** The normalized RFC 9457 Problem Details object. */
141
142
  problemDetails;
143
+ /**
144
+ * Whether the input carried an explicit `type`. Lets the handler apply
145
+ * `typePrefix`/`defaultType` only when the thrower left `type` unset.
146
+ */
147
+ hasExplicitType;
142
148
  /**
143
149
  * @param input - Missing `type` defaults to `"about:blank"`;
144
150
  * missing `title` is derived from the HTTP status code.
@@ -148,6 +154,7 @@ var ProblemDetailsError = class extends Error {
148
154
  super(pd.detail ?? pd.title);
149
155
  this.name = "ProblemDetailsError";
150
156
  this.problemDetails = pd;
157
+ this.hasExplicitType = input.type !== void 0;
151
158
  }
152
159
  /** Build a standalone `application/problem+json` Response without the handler middleware. */
153
160
  getResponse() {
@@ -183,7 +190,10 @@ function buildType(status, options) {
183
190
  return options.defaultType ?? "about:blank";
184
191
  }
185
192
  function toResponse(input, c, options) {
186
- let pd = normalizeProblemDetails(input);
193
+ let pd = normalizeProblemDetails({
194
+ ...input,
195
+ type: input.type ?? buildType(input.status, options)
196
+ });
187
197
  if (options.autoInstance && pd.instance === void 0) {
188
198
  pd = { ...pd, instance: c.req.path };
189
199
  }
@@ -208,7 +218,8 @@ function toResponse(input, c, options) {
208
218
  function problemDetailsHandler(options = {}) {
209
219
  return (error, c) => {
210
220
  if (error instanceof ProblemDetailsError) {
211
- return toResponse(error.problemDetails, c, options);
221
+ const pd = error.problemDetails;
222
+ return toResponse(error.hasExplicitType ? pd : { ...pd, type: void 0 }, c, options);
212
223
  }
213
224
  if (options.mapError) {
214
225
  const mapped = options.mapError(error);
@@ -220,7 +231,6 @@ function problemDetailsHandler(options = {}) {
220
231
  return toResponse(
221
232
  {
222
233
  status: error.status,
223
- type: buildType(error.status, options),
224
234
  title: statusToPhrase(error.status),
225
235
  detail: error.message
226
236
  },
@@ -231,7 +241,6 @@ function problemDetailsHandler(options = {}) {
231
241
  return toResponse(
232
242
  {
233
243
  status: 500,
234
- type: buildType(500, options),
235
244
  title: "Internal Server Error",
236
245
  detail: "An unexpected error occurred",
237
246
  extensions: options.includeStack ? { stack: error.stack } : void 0
@@ -260,4 +269,3 @@ function createProblemTypeRegistry(definitions) {
260
269
  statusToPhrase,
261
270
  statusToSlug
262
271
  });
263
- //# sourceMappingURL=index.cjs.map
package/dist/index.d.cts CHANGED
@@ -96,6 +96,11 @@ interface OtelApiLike {
96
96
  declare class ProblemDetailsError extends Error {
97
97
  /** The normalized RFC 9457 Problem Details object. */
98
98
  readonly problemDetails: ProblemDetails;
99
+ /**
100
+ * Whether the input carried an explicit `type`. Lets the handler apply
101
+ * `typePrefix`/`defaultType` only when the thrower left `type` unset.
102
+ */
103
+ readonly hasExplicitType: boolean;
99
104
  /**
100
105
  * @param input - Missing `type` defaults to `"about:blank"`;
101
106
  * missing `title` is derived from the HTTP status code.
package/dist/index.d.ts CHANGED
@@ -96,6 +96,11 @@ interface OtelApiLike {
96
96
  declare class ProblemDetailsError extends Error {
97
97
  /** The normalized RFC 9457 Problem Details object. */
98
98
  readonly problemDetails: ProblemDetails;
99
+ /**
100
+ * Whether the input carried an explicit `type`. Lets the handler apply
101
+ * `typePrefix`/`defaultType` only when the thrower left `type` unset.
102
+ */
103
+ readonly hasExplicitType: boolean;
99
104
  /**
100
105
  * @param input - Missing `type` defaults to `"about:blank"`;
101
106
  * missing `title` is derived from the HTTP status code.
package/dist/index.js CHANGED
@@ -2,16 +2,21 @@ import {
2
2
  PROBLEM_JSON_CONTENT_TYPE,
3
3
  buildProblemResponse,
4
4
  normalizeProblemDetails
5
- } from "./chunk-63332VQA.js";
5
+ } from "./chunk-VPS467Z5.js";
6
6
  import {
7
7
  statusToPhrase,
8
8
  statusToSlug
9
- } from "./chunk-JW7PXR25.js";
9
+ } from "./chunk-P4D7EVU3.js";
10
10
 
11
11
  // src/error.ts
12
12
  var ProblemDetailsError = class extends Error {
13
13
  /** The normalized RFC 9457 Problem Details object. */
14
14
  problemDetails;
15
+ /**
16
+ * Whether the input carried an explicit `type`. Lets the handler apply
17
+ * `typePrefix`/`defaultType` only when the thrower left `type` unset.
18
+ */
19
+ hasExplicitType;
15
20
  /**
16
21
  * @param input - Missing `type` defaults to `"about:blank"`;
17
22
  * missing `title` is derived from the HTTP status code.
@@ -21,6 +26,7 @@ var ProblemDetailsError = class extends Error {
21
26
  super(pd.detail ?? pd.title);
22
27
  this.name = "ProblemDetailsError";
23
28
  this.problemDetails = pd;
29
+ this.hasExplicitType = input.type !== void 0;
24
30
  }
25
31
  /** Build a standalone `application/problem+json` Response without the handler middleware. */
26
32
  getResponse() {
@@ -56,7 +62,10 @@ function buildType(status, options) {
56
62
  return options.defaultType ?? "about:blank";
57
63
  }
58
64
  function toResponse(input, c, options) {
59
- let pd = normalizeProblemDetails(input);
65
+ let pd = normalizeProblemDetails({
66
+ ...input,
67
+ type: input.type ?? buildType(input.status, options)
68
+ });
60
69
  if (options.autoInstance && pd.instance === void 0) {
61
70
  pd = { ...pd, instance: c.req.path };
62
71
  }
@@ -81,7 +90,8 @@ function toResponse(input, c, options) {
81
90
  function problemDetailsHandler(options = {}) {
82
91
  return (error, c) => {
83
92
  if (error instanceof ProblemDetailsError) {
84
- return toResponse(error.problemDetails, c, options);
93
+ const pd = error.problemDetails;
94
+ return toResponse(error.hasExplicitType ? pd : { ...pd, type: void 0 }, c, options);
85
95
  }
86
96
  if (options.mapError) {
87
97
  const mapped = options.mapError(error);
@@ -93,7 +103,6 @@ function problemDetailsHandler(options = {}) {
93
103
  return toResponse(
94
104
  {
95
105
  status: error.status,
96
- type: buildType(error.status, options),
97
106
  title: statusToPhrase(error.status),
98
107
  detail: error.message
99
108
  },
@@ -104,7 +113,6 @@ function problemDetailsHandler(options = {}) {
104
113
  return toResponse(
105
114
  {
106
115
  status: 500,
107
- type: buildType(500, options),
108
116
  title: "Internal Server Error",
109
117
  detail: "An unexpected error occurred",
110
118
  extensions: options.includeStack ? { stack: error.stack } : void 0
@@ -132,4 +140,3 @@ export {
132
140
  statusToPhrase,
133
141
  statusToSlug
134
142
  };
135
- //# sourceMappingURL=index.js.map
@@ -115,4 +115,3 @@ function problemDetailsResponse(status, description, schema) {
115
115
  getProblemDetailsSchema,
116
116
  problemDetailsResponse
117
117
  });
118
- //# sourceMappingURL=openapi.cjs.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  statusToPhrase
3
- } from "../chunk-JW7PXR25.js";
3
+ } from "../chunk-P4D7EVU3.js";
4
4
 
5
5
  // src/integrations/openapi.ts
6
6
  import { z } from "@hono/zod-openapi";
@@ -37,4 +37,3 @@ export {
37
37
  getProblemDetailsSchema,
38
38
  problemDetailsResponse
39
39
  };
40
- //# sourceMappingURL=openapi.js.map
@@ -78,4 +78,3 @@ function standardSchemaProblemHook(options) {
78
78
  0 && (module.exports = {
79
79
  standardSchemaProblemHook
80
80
  });
81
- //# sourceMappingURL=standard-schema.cjs.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  buildValidationResponse
3
- } from "../chunk-O5AEI2VV.js";
4
- import "../chunk-63332VQA.js";
5
- import "../chunk-JW7PXR25.js";
3
+ } from "../chunk-BBJKRRB3.js";
4
+ import "../chunk-VPS467Z5.js";
5
+ import "../chunk-P4D7EVU3.js";
6
6
 
7
7
  // src/integrations/standard-schema.ts
8
8
  function formatPath(path) {
@@ -23,4 +23,3 @@ function standardSchemaProblemHook(options) {
23
23
  export {
24
24
  standardSchemaProblemHook
25
25
  };
26
- //# sourceMappingURL=standard-schema.js.map
@@ -76,4 +76,3 @@ function valibotProblemHook(options) {
76
76
  0 && (module.exports = {
77
77
  valibotProblemHook
78
78
  });
79
- //# sourceMappingURL=valibot.cjs.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  buildValidationResponse
3
- } from "../chunk-O5AEI2VV.js";
4
- import "../chunk-63332VQA.js";
5
- import "../chunk-JW7PXR25.js";
3
+ } from "../chunk-BBJKRRB3.js";
4
+ import "../chunk-VPS467Z5.js";
5
+ import "../chunk-P4D7EVU3.js";
6
6
 
7
7
  // src/integrations/valibot.ts
8
8
  function formatIssues(issues) {
@@ -21,4 +21,3 @@ function valibotProblemHook(options) {
21
21
  export {
22
22
  valibotProblemHook
23
23
  };
24
- //# sourceMappingURL=valibot.js.map
@@ -76,4 +76,3 @@ function zodProblemHook(options) {
76
76
  0 && (module.exports = {
77
77
  zodProblemHook
78
78
  });
79
- //# sourceMappingURL=zod.cjs.map
@@ -1,8 +1,8 @@
1
1
  import {
2
2
  buildValidationResponse
3
- } from "../chunk-O5AEI2VV.js";
4
- import "../chunk-63332VQA.js";
5
- import "../chunk-JW7PXR25.js";
3
+ } from "../chunk-BBJKRRB3.js";
4
+ import "../chunk-VPS467Z5.js";
5
+ import "../chunk-P4D7EVU3.js";
6
6
 
7
7
  // src/integrations/zod.ts
8
8
  function formatErrors(zodError) {
@@ -21,4 +21,3 @@ function zodProblemHook(options) {
21
21
  export {
22
22
  zodProblemHook
23
23
  };
24
- //# sourceMappingURL=zod.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-problem-details",
3
- "version": "0.8.1",
3
+ "version": "0.9.0",
4
4
  "description": "RFC 9457 Problem Details middleware for Hono",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -75,8 +75,7 @@
75
75
  }
76
76
  },
77
77
  "files": [
78
- "dist",
79
- "!dist/**/*.map"
78
+ "dist"
80
79
  ],
81
80
  "sideEffects": false,
82
81
  "keywords": [
@@ -106,7 +105,7 @@
106
105
  "@hono/zod-validator": "^0.7.5",
107
106
  "@opentelemetry/api": "^1.0.0",
108
107
  "@standard-schema/spec": "^1.0.0",
109
- "hono": ">=4.12.14",
108
+ "hono": ">=4.12.27",
110
109
  "valibot": "^1.0.0",
111
110
  "zod": "^3.25.0 || ^4.0.0"
112
111
  },
@@ -146,13 +145,14 @@
146
145
  "@hono/zod-validator": "^0.7.6",
147
146
  "@opentelemetry/api": "^1.0.0",
148
147
  "@standard-schema/spec": "1.1.0",
149
- "@vitest/coverage-v8": "^3.0.0",
150
- "hono": "^4.12.14",
148
+ "@vitest/coverage-v8": "^4.1.8",
149
+ "hono": "^4.12.27",
150
+ "knip": "6.14.2",
151
151
  "lefthook": "2.1.6",
152
152
  "tsup": "^8.0.0",
153
153
  "typescript": "^6.0.3",
154
154
  "valibot": "^1.3.1",
155
- "vitest": "^3.0.0",
155
+ "vitest": "^4.1.8",
156
156
  "zod": "^4.0.0"
157
157
  },
158
158
  "engines": {
@@ -169,6 +169,7 @@
169
169
  "test:watch": "vitest",
170
170
  "lint": "biome check .",
171
171
  "lint:fix": "biome check --write .",
172
+ "knip": "knip",
172
173
  "format": "biome format --write .",
173
174
  "typecheck": "tsc --noEmit",
174
175
  "test:compat": "tsc -p tsconfig.compat.json",