hono-problem-details 0.6.0 → 0.7.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
  ### Requirements
54
54
 
55
55
  - Hono `>= 4.12.14` (peer dependency)
56
- - TypeScript `>= 5.0` — the published `.d.ts` files are CI-tested against TS 5.0, 5.4, 5.7, and 5.9. Older TS versions may work but are not verified.
56
+ - 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.
57
57
  - Node.js `>= 22` (Node 20 reached end-of-life in April 2026; v0.6.0 raised the floor)
58
58
 
59
59
  ## Quick Start
@@ -356,8 +356,8 @@ Use with `@hono/zod-openapi` to document Problem Details error responses in your
356
356
  import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
357
357
  import { problemDetailsHandler } from "hono-problem-details";
358
358
  import {
359
- ProblemDetailsSchema,
360
359
  createProblemDetailsSchema,
360
+ getProblemDetailsSchema,
361
361
  problemDetailsResponse,
362
362
  } from "hono-problem-details/openapi";
363
363
 
@@ -385,6 +385,9 @@ const route = createRoute({
385
385
  },
386
386
  });
387
387
 
388
+ // Need the schema itself? Call the factory:
389
+ const ProblemDetailsSchema = getProblemDetailsSchema();
390
+
388
391
  // With extension members
389
392
  const errorWithExtensions = createProblemDetailsSchema(
390
393
  z.object({
@@ -394,6 +397,13 @@ const errorWithExtensions = createProblemDetailsSchema(
394
397
  // Use: problemDetailsResponse(422, "Validation Error", errorWithExtensions)
395
398
  ```
396
399
 
400
+ > **Migrating from v0.6.x**: `ProblemDetailsSchema` (the const export) was removed
401
+ > in v0.7.0. Replace `import { ProblemDetailsSchema }` with
402
+ > `import { getProblemDetailsSchema }` and call it where you previously read
403
+ > the const. The factory is memoized — repeat calls return the same instance.
404
+ > See [ADR-0004](./docs/adr/0004-defer-openapi-schema-construction-via-factory.md)
405
+ > for the bundler-related reason this changed.
406
+
397
407
  ## Localization
398
408
 
399
409
  Use the `localize` callback to translate `title` and `detail` based on the request context.
@@ -20,8 +20,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/integrations/openapi.ts
21
21
  var openapi_exports = {};
22
22
  __export(openapi_exports, {
23
- ProblemDetailsSchema: () => ProblemDetailsSchema,
24
23
  createProblemDetailsSchema: () => createProblemDetailsSchema,
24
+ getProblemDetailsSchema: () => getProblemDetailsSchema,
25
25
  problemDetailsResponse: () => problemDetailsResponse
26
26
  });
27
27
  module.exports = __toCommonJS(openapi_exports);
@@ -81,28 +81,38 @@ function statusToPhrase(status) {
81
81
  }
82
82
 
83
83
  // src/integrations/openapi.ts
84
- var ProblemDetailsSchema = import_zod_openapi.z.object({
85
- type: import_zod_openapi.z.string().openapi({ description: "Problem type URI", example: "about:blank" }),
86
- status: import_zod_openapi.z.number().int().openapi({ description: "HTTP status code", example: 400 }),
87
- title: import_zod_openapi.z.string().openapi({ description: "Short summary of the problem type", example: "Bad Request" }),
88
- detail: import_zod_openapi.z.string().optional().openapi({ description: "Human-readable explanation" }),
89
- instance: import_zod_openapi.z.string().optional().openapi({ description: "URI identifying the occurrence" })
90
- }).openapi({ title: "ProblemDetails" });
84
+ var _cached;
85
+ function getProblemDetailsSchema() {
86
+ if (!_cached) {
87
+ _cached = import_zod_openapi.z.object({
88
+ type: import_zod_openapi.z.string().openapi({ description: "Problem type URI", example: "about:blank" }),
89
+ status: import_zod_openapi.z.number().int().openapi({ description: "HTTP status code", example: 400 }),
90
+ title: import_zod_openapi.z.string().openapi({ description: "Short summary of the problem type", example: "Bad Request" }),
91
+ detail: import_zod_openapi.z.string().optional().openapi({ description: "Human-readable explanation" }),
92
+ instance: import_zod_openapi.z.string().optional().openapi({ description: "URI identifying the occurrence" })
93
+ }).openapi({ title: "ProblemDetails" });
94
+ }
95
+ return _cached;
96
+ }
97
+ var STANDARD_FIELD_KEYS = /* @__PURE__ */ new Set(["type", "status", "title", "detail", "instance"]);
91
98
  function createProblemDetailsSchema(extensions) {
92
- return ProblemDetailsSchema.extend(extensions.shape).openapi({ title: "ProblemDetails" });
99
+ const safeShape = Object.fromEntries(
100
+ Object.entries(extensions.shape).filter(([key]) => !STANDARD_FIELD_KEYS.has(key))
101
+ );
102
+ return getProblemDetailsSchema().extend(safeShape).openapi({ title: "ProblemDetails" });
93
103
  }
94
- function problemDetailsResponse(status, description, schema = ProblemDetailsSchema) {
104
+ function problemDetailsResponse(status, description, schema) {
95
105
  return {
96
106
  content: {
97
- "application/problem+json": { schema }
107
+ "application/problem+json": { schema: schema ?? getProblemDetailsSchema() }
98
108
  },
99
109
  description: description ?? statusToPhrase(status) ?? "Error"
100
110
  };
101
111
  }
102
112
  // Annotate the CommonJS export names for ESM import in node:
103
113
  0 && (module.exports = {
104
- ProblemDetailsSchema,
105
114
  createProblemDetailsSchema,
115
+ getProblemDetailsSchema,
106
116
  problemDetailsResponse
107
117
  });
108
118
  //# sourceMappingURL=openapi.cjs.map
@@ -1,19 +1,29 @@
1
1
  import { z } from '@hono/zod-openapi';
2
2
 
3
- /**
4
- * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.
5
- * Use in createRoute() response definitions with @hono/zod-openapi.
6
- */
7
- declare const ProblemDetailsSchema: z.ZodObject<{
3
+ type ProblemDetailsShape = {
8
4
  type: z.ZodString;
9
5
  status: z.ZodNumber;
10
6
  title: z.ZodString;
11
7
  detail: z.ZodOptional<z.ZodString>;
12
8
  instance: z.ZodOptional<z.ZodString>;
13
- }>;
9
+ };
10
+ /**
11
+ * Build the RFC 9457 Problem Details Zod schema for OpenAPI documentation.
12
+ *
13
+ * The schema is constructed lazily on first call and memoized for subsequent
14
+ * calls. Deferring construction past module load avoids `TypeError` failures
15
+ * under bundlers that resolve duplicate `zod` instances, because the
16
+ * `@hono/zod-openapi` prototype patch must be in place before `.openapi()`
17
+ * runs. See ADR-0004 for the full rationale.
18
+ */
19
+ declare function getProblemDetailsSchema(): z.ZodObject<ProblemDetailsShape>;
14
20
  /**
15
21
  * Create a Problem Details schema with typed extension members.
16
- * Extensions are merged at top level per RFC 9457.
22
+ *
23
+ * Extensions are merged at top level per RFC 9457 §3.1, and **standard fields
24
+ * always win** over extension keys that collide (mirroring the runtime spread
25
+ * order in `problemDetails()`). Conflicting extension keys are silently
26
+ * dropped from the schema.
17
27
  */
18
28
  declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.ZodObject;
19
29
  /**
@@ -29,4 +39,4 @@ declare function problemDetailsResponse(status: number, description?: string, sc
29
39
  description: string;
30
40
  };
31
41
 
32
- export { ProblemDetailsSchema, createProblemDetailsSchema, problemDetailsResponse };
42
+ export { createProblemDetailsSchema, getProblemDetailsSchema, problemDetailsResponse };
@@ -1,19 +1,29 @@
1
1
  import { z } from '@hono/zod-openapi';
2
2
 
3
- /**
4
- * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.
5
- * Use in createRoute() response definitions with @hono/zod-openapi.
6
- */
7
- declare const ProblemDetailsSchema: z.ZodObject<{
3
+ type ProblemDetailsShape = {
8
4
  type: z.ZodString;
9
5
  status: z.ZodNumber;
10
6
  title: z.ZodString;
11
7
  detail: z.ZodOptional<z.ZodString>;
12
8
  instance: z.ZodOptional<z.ZodString>;
13
- }>;
9
+ };
10
+ /**
11
+ * Build the RFC 9457 Problem Details Zod schema for OpenAPI documentation.
12
+ *
13
+ * The schema is constructed lazily on first call and memoized for subsequent
14
+ * calls. Deferring construction past module load avoids `TypeError` failures
15
+ * under bundlers that resolve duplicate `zod` instances, because the
16
+ * `@hono/zod-openapi` prototype patch must be in place before `.openapi()`
17
+ * runs. See ADR-0004 for the full rationale.
18
+ */
19
+ declare function getProblemDetailsSchema(): z.ZodObject<ProblemDetailsShape>;
14
20
  /**
15
21
  * Create a Problem Details schema with typed extension members.
16
- * Extensions are merged at top level per RFC 9457.
22
+ *
23
+ * Extensions are merged at top level per RFC 9457 §3.1, and **standard fields
24
+ * always win** over extension keys that collide (mirroring the runtime spread
25
+ * order in `problemDetails()`). Conflicting extension keys are silently
26
+ * dropped from the schema.
17
27
  */
18
28
  declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.ZodObject;
19
29
  /**
@@ -29,4 +39,4 @@ declare function problemDetailsResponse(status: number, description?: string, sc
29
39
  description: string;
30
40
  };
31
41
 
32
- export { ProblemDetailsSchema, createProblemDetailsSchema, problemDetailsResponse };
42
+ export { createProblemDetailsSchema, getProblemDetailsSchema, problemDetailsResponse };
@@ -4,27 +4,37 @@ import {
4
4
 
5
5
  // src/integrations/openapi.ts
6
6
  import { z } from "@hono/zod-openapi";
7
- var ProblemDetailsSchema = z.object({
8
- type: z.string().openapi({ description: "Problem type URI", example: "about:blank" }),
9
- status: z.number().int().openapi({ description: "HTTP status code", example: 400 }),
10
- title: z.string().openapi({ description: "Short summary of the problem type", example: "Bad Request" }),
11
- detail: z.string().optional().openapi({ description: "Human-readable explanation" }),
12
- instance: z.string().optional().openapi({ description: "URI identifying the occurrence" })
13
- }).openapi({ title: "ProblemDetails" });
7
+ var _cached;
8
+ function getProblemDetailsSchema() {
9
+ if (!_cached) {
10
+ _cached = z.object({
11
+ type: z.string().openapi({ description: "Problem type URI", example: "about:blank" }),
12
+ status: z.number().int().openapi({ description: "HTTP status code", example: 400 }),
13
+ title: z.string().openapi({ description: "Short summary of the problem type", example: "Bad Request" }),
14
+ detail: z.string().optional().openapi({ description: "Human-readable explanation" }),
15
+ instance: z.string().optional().openapi({ description: "URI identifying the occurrence" })
16
+ }).openapi({ title: "ProblemDetails" });
17
+ }
18
+ return _cached;
19
+ }
20
+ var STANDARD_FIELD_KEYS = /* @__PURE__ */ new Set(["type", "status", "title", "detail", "instance"]);
14
21
  function createProblemDetailsSchema(extensions) {
15
- return ProblemDetailsSchema.extend(extensions.shape).openapi({ title: "ProblemDetails" });
22
+ const safeShape = Object.fromEntries(
23
+ Object.entries(extensions.shape).filter(([key]) => !STANDARD_FIELD_KEYS.has(key))
24
+ );
25
+ return getProblemDetailsSchema().extend(safeShape).openapi({ title: "ProblemDetails" });
16
26
  }
17
- function problemDetailsResponse(status, description, schema = ProblemDetailsSchema) {
27
+ function problemDetailsResponse(status, description, schema) {
18
28
  return {
19
29
  content: {
20
- "application/problem+json": { schema }
30
+ "application/problem+json": { schema: schema ?? getProblemDetailsSchema() }
21
31
  },
22
32
  description: description ?? statusToPhrase(status) ?? "Error"
23
33
  };
24
34
  }
25
35
  export {
26
- ProblemDetailsSchema,
27
36
  createProblemDetailsSchema,
37
+ getProblemDetailsSchema,
28
38
  problemDetailsResponse
29
39
  };
30
40
  //# sourceMappingURL=openapi.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-problem-details",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "RFC 9457 Problem Details middleware for Hono",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -145,7 +145,7 @@
145
145
  "hono": "^4.12.14",
146
146
  "lefthook": "2.1.6",
147
147
  "tsup": "^8.0.0",
148
- "typescript": "^5.7.0",
148
+ "typescript": "^6.0.3",
149
149
  "valibot": "^1.3.1",
150
150
  "vitest": "^3.0.0",
151
151
  "zod": "^4.0.0"