hono-problem-details 0.1.3 → 0.1.5

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 (39) hide show
  1. package/README.md +3 -0
  2. package/dist/chunk-LMBOCQPX.js +23 -0
  3. package/dist/chunk-LMBOCQPX.js.map +1 -0
  4. package/dist/{chunk-S67AKA3K.js → chunk-NFHWC5GI.js} +2 -2
  5. package/dist/{chunk-S67AKA3K.js.map → chunk-NFHWC5GI.js.map} +1 -1
  6. package/dist/chunk-X2MUB5YP.js +59 -0
  7. package/dist/chunk-X2MUB5YP.js.map +1 -0
  8. package/dist/index.cjs +61 -63
  9. package/dist/index.cjs.map +1 -1
  10. package/dist/index.d.cts +1 -0
  11. package/dist/index.d.ts +1 -0
  12. package/dist/index.js +72 -15
  13. package/dist/index.js.map +1 -1
  14. package/dist/integrations/openapi.cjs +1 -1
  15. package/dist/integrations/openapi.cjs.map +1 -1
  16. package/dist/integrations/openapi.js +1 -1
  17. package/dist/integrations/standard-schema.cjs +23 -14
  18. package/dist/integrations/standard-schema.cjs.map +1 -1
  19. package/dist/integrations/standard-schema.d.cts +3 -6
  20. package/dist/integrations/standard-schema.d.ts +3 -6
  21. package/dist/integrations/standard-schema.js +6 -15
  22. package/dist/integrations/standard-schema.js.map +1 -1
  23. package/dist/integrations/valibot.cjs +23 -14
  24. package/dist/integrations/valibot.cjs.map +1 -1
  25. package/dist/integrations/valibot.d.cts +3 -6
  26. package/dist/integrations/valibot.d.ts +3 -6
  27. package/dist/integrations/valibot.js +6 -15
  28. package/dist/integrations/valibot.js.map +1 -1
  29. package/dist/integrations/zod.cjs +23 -14
  30. package/dist/integrations/zod.cjs.map +1 -1
  31. package/dist/integrations/zod.d.cts +3 -6
  32. package/dist/integrations/zod.d.ts +3 -6
  33. package/dist/integrations/zod.js +6 -15
  34. package/dist/integrations/zod.js.map +1 -1
  35. package/dist/validation-DqPEnoIw.d.cts +6 -0
  36. package/dist/validation-DqPEnoIw.d.ts +6 -0
  37. package/package.json +1 -1
  38. package/dist/chunk-SLBPERKR.js +0 -121
  39. package/dist/chunk-SLBPERKR.js.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/integrations/standard-schema.ts","../../src/handler.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport { PROBLEM_JSON_CONTENT_TYPE } from \"../handler.js\";\n\nexport interface StandardSchemaProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n}\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: StandardSchemaProblemHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t};\n}\n","import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\t// HTTP status must be 100-599; use body status as-is (RFC 9457 advisory)\n\tconst httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: httpStatus,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\treturn toResponse(error.problemDetails, c, options);\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;AAKvB,IAAM,4BAA4B;;;ADQzC,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/integrations/standard-schema.ts","../../src/utils.ts","../../src/integrations/validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as StandardSchemaProblemHookOptions };\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.error), options);\n\t};\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;ACjBM,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;;;AFpBA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
@@ -1,11 +1,8 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  import { Context } from 'hono';
3
+ import { V as ValidationHookOptions } from '../validation-DqPEnoIw.cjs';
3
4
 
4
- interface StandardSchemaProblemHookOptions {
5
- title?: string;
6
- detail?: string;
7
- }
8
- declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOptions): (result: {
5
+ declare function standardSchemaProblemHook(options?: ValidationHookOptions): (result: {
9
6
  success: true;
10
7
  data: unknown;
11
8
  } | {
@@ -13,4 +10,4 @@ declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOp
13
10
  error: readonly StandardSchemaV1.Issue[];
14
11
  }, c: Context) => Response | undefined;
15
12
 
16
- export { type StandardSchemaProblemHookOptions, standardSchemaProblemHook };
13
+ export { ValidationHookOptions as StandardSchemaProblemHookOptions, standardSchemaProblemHook };
@@ -1,11 +1,8 @@
1
1
  import { StandardSchemaV1 } from '@standard-schema/spec';
2
2
  import { Context } from 'hono';
3
+ import { V as ValidationHookOptions } from '../validation-DqPEnoIw.js';
3
4
 
4
- interface StandardSchemaProblemHookOptions {
5
- title?: string;
6
- detail?: string;
7
- }
8
- declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOptions): (result: {
5
+ declare function standardSchemaProblemHook(options?: ValidationHookOptions): (result: {
9
6
  success: true;
10
7
  data: unknown;
11
8
  } | {
@@ -13,4 +10,4 @@ declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOp
13
10
  error: readonly StandardSchemaV1.Issue[];
14
11
  }, c: Context) => Response | undefined;
15
12
 
16
- export { type StandardSchemaProblemHookOptions, standardSchemaProblemHook };
13
+ export { ValidationHookOptions as StandardSchemaProblemHookOptions, standardSchemaProblemHook };
@@ -1,7 +1,8 @@
1
1
  import {
2
- PROBLEM_JSON_CONTENT_TYPE
3
- } from "../chunk-SLBPERKR.js";
4
- import "../chunk-S67AKA3K.js";
2
+ buildValidationResponse
3
+ } from "../chunk-LMBOCQPX.js";
4
+ import "../chunk-X2MUB5YP.js";
5
+ import "../chunk-NFHWC5GI.js";
5
6
 
6
7
  // src/integrations/standard-schema.ts
7
8
  function formatPath(path) {
@@ -14,19 +15,9 @@ function formatIssues(issues) {
14
15
  }));
15
16
  }
16
17
  function standardSchemaProblemHook(options) {
17
- return (result, c) => {
18
+ return (result, _c) => {
18
19
  if (result.success) return;
19
- const body = {
20
- type: "about:blank",
21
- status: 422,
22
- title: options?.title ?? "Validation Error",
23
- detail: options?.detail ?? "Request validation failed",
24
- errors: formatIssues(result.error)
25
- };
26
- return new Response(JSON.stringify(body), {
27
- status: 422,
28
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
29
- });
20
+ return buildValidationResponse(formatIssues(result.error), options);
30
21
  };
31
22
  }
32
23
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport { PROBLEM_JSON_CONTENT_TYPE } from \"../handler.js\";\n\nexport interface StandardSchemaProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n}\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: StandardSchemaProblemHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t};\n}\n"],"mappings":";;;;;;AAcA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as StandardSchemaProblemHookOptions };\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.error), options);\n\t};\n}\n"],"mappings":";;;;;;;AAUA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
@@ -24,9 +24,28 @@ __export(valibot_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(valibot_exports);
26
26
 
27
- // src/handler.ts
28
- var import_http_exception = require("hono/http-exception");
27
+ // src/utils.ts
29
28
  var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
29
+ var FALLBACK_BODY = JSON.stringify({
30
+ type: "about:blank",
31
+ status: 500,
32
+ title: "Internal Server Error"
33
+ });
34
+
35
+ // src/integrations/validation.ts
36
+ function buildValidationResponse(errors, options) {
37
+ const body = {
38
+ type: "about:blank",
39
+ status: 422,
40
+ title: options?.title ?? "Validation Error",
41
+ detail: options?.detail ?? "Request validation failed",
42
+ errors
43
+ };
44
+ return new Response(JSON.stringify(body), {
45
+ status: 422,
46
+ headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
47
+ });
48
+ }
30
49
 
31
50
  // src/integrations/valibot.ts
32
51
  function formatIssues(issues) {
@@ -37,19 +56,9 @@ function formatIssues(issues) {
37
56
  }));
38
57
  }
39
58
  function valibotProblemHook(options) {
40
- return (result, c) => {
59
+ return (result, _c) => {
41
60
  if (result.success) return;
42
- const body = {
43
- type: "about:blank",
44
- status: 422,
45
- title: options?.title ?? "Validation Error",
46
- detail: options?.detail ?? "Request validation failed",
47
- errors: formatIssues(result.issues)
48
- };
49
- return new Response(JSON.stringify(body), {
50
- status: 422,
51
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
52
- });
61
+ return buildValidationResponse(formatIssues(result.issues), options);
53
62
  };
54
63
  }
55
64
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/integrations/valibot.ts","../../src/handler.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport { PROBLEM_JSON_CONTENT_TYPE } from \"../handler.js\";\n\nexport interface ValibotProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValibotProblemHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.issues),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t};\n}\n","import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\t// HTTP status must be 100-599; use body status as-is (RFC 9457 advisory)\n\tconst httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: httpStatus,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\treturn toResponse(error.problemDetails, c, options);\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;AAKvB,IAAM,4BAA4B;;;ADSzC,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,MAAM;AAAA,IACnC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/integrations/valibot.ts","../../src/utils.ts","../../src/integrations/validation.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ValibotProblemHookOptions };\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValidationHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.issues), options);\n\t};\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;ACjBM,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;;;AFpBA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,MAAM,GAAG,OAAO;AAAA,EACpE;AACD;","names":[]}
@@ -1,12 +1,9 @@
1
1
  import { Context } from 'hono';
2
2
  import { SafeParseResult } from 'valibot';
3
+ import { V as ValidationHookOptions } from '../validation-DqPEnoIw.cjs';
3
4
 
4
- interface ValibotProblemHookOptions {
5
- title?: string;
6
- detail?: string;
7
- }
8
- declare function valibotProblemHook(options?: ValibotProblemHookOptions): (result: SafeParseResult<never> & {
5
+ declare function valibotProblemHook(options?: ValidationHookOptions): (result: SafeParseResult<never> & {
9
6
  target: string;
10
7
  }, c: Context) => Response | undefined;
11
8
 
12
- export { type ValibotProblemHookOptions, valibotProblemHook };
9
+ export { ValidationHookOptions as ValibotProblemHookOptions, valibotProblemHook };
@@ -1,12 +1,9 @@
1
1
  import { Context } from 'hono';
2
2
  import { SafeParseResult } from 'valibot';
3
+ import { V as ValidationHookOptions } from '../validation-DqPEnoIw.js';
3
4
 
4
- interface ValibotProblemHookOptions {
5
- title?: string;
6
- detail?: string;
7
- }
8
- declare function valibotProblemHook(options?: ValibotProblemHookOptions): (result: SafeParseResult<never> & {
5
+ declare function valibotProblemHook(options?: ValidationHookOptions): (result: SafeParseResult<never> & {
9
6
  target: string;
10
7
  }, c: Context) => Response | undefined;
11
8
 
12
- export { type ValibotProblemHookOptions, valibotProblemHook };
9
+ export { ValidationHookOptions as ValibotProblemHookOptions, valibotProblemHook };
@@ -1,7 +1,8 @@
1
1
  import {
2
- PROBLEM_JSON_CONTENT_TYPE
3
- } from "../chunk-SLBPERKR.js";
4
- import "../chunk-S67AKA3K.js";
2
+ buildValidationResponse
3
+ } from "../chunk-LMBOCQPX.js";
4
+ import "../chunk-X2MUB5YP.js";
5
+ import "../chunk-NFHWC5GI.js";
5
6
 
6
7
  // src/integrations/valibot.ts
7
8
  function formatIssues(issues) {
@@ -12,19 +13,9 @@ function formatIssues(issues) {
12
13
  }));
13
14
  }
14
15
  function valibotProblemHook(options) {
15
- return (result, c) => {
16
+ return (result, _c) => {
16
17
  if (result.success) return;
17
- const body = {
18
- type: "about:blank",
19
- status: 422,
20
- title: options?.title ?? "Validation Error",
21
- detail: options?.detail ?? "Request validation failed",
22
- errors: formatIssues(result.issues)
23
- };
24
- return new Response(JSON.stringify(body), {
25
- status: 422,
26
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
27
- });
18
+ return buildValidationResponse(formatIssues(result.issues), options);
28
19
  };
29
20
  }
30
21
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport { PROBLEM_JSON_CONTENT_TYPE } from \"../handler.js\";\n\nexport interface ValibotProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValibotProblemHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.issues),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t};\n}\n"],"mappings":";;;;;;AAeA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,MAAM;AAAA,IACnC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ValibotProblemHookOptions };\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValidationHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.issues), options);\n\t};\n}\n"],"mappings":";;;;;;;AAUA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,MAAM,GAAG,OAAO;AAAA,EACpE;AACD;","names":[]}
@@ -24,9 +24,28 @@ __export(zod_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(zod_exports);
26
26
 
27
- // src/handler.ts
28
- var import_http_exception = require("hono/http-exception");
27
+ // src/utils.ts
29
28
  var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
29
+ var FALLBACK_BODY = JSON.stringify({
30
+ type: "about:blank",
31
+ status: 500,
32
+ title: "Internal Server Error"
33
+ });
34
+
35
+ // src/integrations/validation.ts
36
+ function buildValidationResponse(errors, options) {
37
+ const body = {
38
+ type: "about:blank",
39
+ status: 422,
40
+ title: options?.title ?? "Validation Error",
41
+ detail: options?.detail ?? "Request validation failed",
42
+ errors
43
+ };
44
+ return new Response(JSON.stringify(body), {
45
+ status: 422,
46
+ headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
47
+ });
48
+ }
30
49
 
31
50
  // src/integrations/zod.ts
32
51
  function formatErrors(zodError) {
@@ -37,19 +56,9 @@ function formatErrors(zodError) {
37
56
  }));
38
57
  }
39
58
  function zodProblemHook(options) {
40
- return (result, c) => {
59
+ return (result, _c) => {
41
60
  if (result.success) return;
42
- const body = {
43
- type: "about:blank",
44
- status: 422,
45
- title: options?.title ?? "Validation Error",
46
- detail: options?.detail ?? "Request validation failed",
47
- errors: formatErrors(result.error)
48
- };
49
- return new Response(JSON.stringify(body), {
50
- status: 422,
51
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
52
- });
61
+ return buildValidationResponse(formatErrors(result.error), options);
53
62
  };
54
63
  }
55
64
  // Annotate the CommonJS export names for ESM import in node:
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/integrations/zod.ts","../../src/handler.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport { PROBLEM_JSON_CONTENT_TYPE } from \"../handler.js\";\n\nexport interface ZodProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ZodProblemHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatErrors(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t};\n}\n","import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\t// HTTP status must be 100-599; use body status as-is (RFC 9457 advisory)\n\tconst httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: httpStatus,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\treturn toResponse(error.problemDetails, c, options);\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;AAKvB,IAAM,4BAA4B;;;ADSzC,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/integrations/zod.ts","../../src/utils.ts","../../src/integrations/validation.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ZodProblemHookOptions };\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatErrors(result.error), options);\n\t};\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;ACjBM,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;;;AFpBA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
@@ -1,11 +1,8 @@
1
1
  import { Context } from 'hono';
2
2
  import { ZodError } from 'zod';
3
+ import { V as ValidationHookOptions } from '../validation-DqPEnoIw.cjs';
3
4
 
4
- interface ZodProblemHookOptions {
5
- title?: string;
6
- detail?: string;
7
- }
8
- declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
5
+ declare function zodProblemHook(options?: ValidationHookOptions): (result: {
9
6
  success: true;
10
7
  data: unknown;
11
8
  } | {
@@ -13,4 +10,4 @@ declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
13
10
  error: ZodError;
14
11
  }, c: Context) => Response | undefined;
15
12
 
16
- export { type ZodProblemHookOptions, zodProblemHook };
13
+ export { ValidationHookOptions as ZodProblemHookOptions, zodProblemHook };
@@ -1,11 +1,8 @@
1
1
  import { Context } from 'hono';
2
2
  import { ZodError } from 'zod';
3
+ import { V as ValidationHookOptions } from '../validation-DqPEnoIw.js';
3
4
 
4
- interface ZodProblemHookOptions {
5
- title?: string;
6
- detail?: string;
7
- }
8
- declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
5
+ declare function zodProblemHook(options?: ValidationHookOptions): (result: {
9
6
  success: true;
10
7
  data: unknown;
11
8
  } | {
@@ -13,4 +10,4 @@ declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
13
10
  error: ZodError;
14
11
  }, c: Context) => Response | undefined;
15
12
 
16
- export { type ZodProblemHookOptions, zodProblemHook };
13
+ export { ValidationHookOptions as ZodProblemHookOptions, zodProblemHook };
@@ -1,7 +1,8 @@
1
1
  import {
2
- PROBLEM_JSON_CONTENT_TYPE
3
- } from "../chunk-SLBPERKR.js";
4
- import "../chunk-S67AKA3K.js";
2
+ buildValidationResponse
3
+ } from "../chunk-LMBOCQPX.js";
4
+ import "../chunk-X2MUB5YP.js";
5
+ import "../chunk-NFHWC5GI.js";
5
6
 
6
7
  // src/integrations/zod.ts
7
8
  function formatErrors(zodError) {
@@ -12,19 +13,9 @@ function formatErrors(zodError) {
12
13
  }));
13
14
  }
14
15
  function zodProblemHook(options) {
15
- return (result, c) => {
16
+ return (result, _c) => {
16
17
  if (result.success) return;
17
- const body = {
18
- type: "about:blank",
19
- status: 422,
20
- title: options?.title ?? "Validation Error",
21
- detail: options?.detail ?? "Request validation failed",
22
- errors: formatErrors(result.error)
23
- };
24
- return new Response(JSON.stringify(body), {
25
- status: 422,
26
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
27
- });
18
+ return buildValidationResponse(formatErrors(result.error), options);
28
19
  };
29
20
  }
30
21
  export {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport { PROBLEM_JSON_CONTENT_TYPE } from \"../handler.js\";\n\nexport interface ZodProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ZodProblemHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatErrors(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t};\n}\n"],"mappings":";;;;;;AAeA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;","names":[]}
1
+ {"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ZodProblemHookOptions };\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatErrors(result.error), options);\n\t};\n}\n"],"mappings":";;;;;;;AAUA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
@@ -0,0 +1,6 @@
1
+ interface ValidationHookOptions {
2
+ title?: string;
3
+ detail?: string;
4
+ }
5
+
6
+ export type { ValidationHookOptions as V };
@@ -0,0 +1,6 @@
1
+ interface ValidationHookOptions {
2
+ title?: string;
3
+ detail?: string;
4
+ }
5
+
6
+ export type { ValidationHookOptions as V };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-problem-details",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "RFC 9457 Problem Details middleware for Hono",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -1,121 +0,0 @@
1
- import {
2
- statusToPhrase,
3
- statusToSlug
4
- } from "./chunk-S67AKA3K.js";
5
-
6
- // src/handler.ts
7
- import { HTTPException } from "hono/http-exception";
8
-
9
- // src/error.ts
10
- function normalizeProblemDetails(input) {
11
- return {
12
- type: input.type ?? "about:blank",
13
- status: input.status,
14
- title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
15
- detail: input.detail,
16
- instance: input.instance,
17
- extensions: input.extensions
18
- };
19
- }
20
- var ProblemDetailsError = class extends Error {
21
- problemDetails;
22
- constructor(input) {
23
- const pd = normalizeProblemDetails(input);
24
- super(pd.detail ?? pd.title);
25
- this.name = "ProblemDetailsError";
26
- this.problemDetails = pd;
27
- }
28
- getResponse() {
29
- const { extensions, ...standard } = this.problemDetails;
30
- const body = { ...sanitizeExtensions(extensions), ...standard };
31
- return new Response(JSON.stringify(body), {
32
- status: this.problemDetails.status,
33
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
34
- });
35
- }
36
- };
37
-
38
- // src/handler.ts
39
- var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
40
- var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
41
- function sanitizeExtensions(extensions) {
42
- if (!extensions) return extensions;
43
- let filtered;
44
- for (const key of Object.keys(extensions)) {
45
- if (DANGEROUS_KEYS.has(key)) {
46
- if (!filtered) filtered = { ...extensions };
47
- delete filtered[key];
48
- }
49
- }
50
- return filtered ?? extensions;
51
- }
52
- function buildType(status, options) {
53
- if (options.typePrefix) {
54
- const slug = statusToSlug(status);
55
- if (slug) return `${options.typePrefix}/${slug}`;
56
- }
57
- return options.defaultType ?? "about:blank";
58
- }
59
- function toResponse(input, c, options) {
60
- let pd = {
61
- type: input.type ?? "about:blank",
62
- status: input.status,
63
- title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
64
- detail: input.detail,
65
- instance: input.instance,
66
- extensions: input.extensions
67
- };
68
- if (options.localize) {
69
- pd = { ...pd, ...options.localize(pd, c) };
70
- }
71
- const { extensions, ...rest } = pd;
72
- const body = { ...sanitizeExtensions(extensions), ...rest };
73
- c.set("problemDetails", pd);
74
- const httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;
75
- return new Response(JSON.stringify(body), {
76
- status: httpStatus,
77
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
78
- });
79
- }
80
- function problemDetailsHandler(options = {}) {
81
- return (error, c) => {
82
- if (error instanceof ProblemDetailsError) {
83
- return toResponse(error.problemDetails, c, options);
84
- }
85
- if (options.mapError) {
86
- const mapped = options.mapError(error);
87
- if (mapped) {
88
- return toResponse(mapped, c, options);
89
- }
90
- }
91
- if (error instanceof HTTPException) {
92
- return toResponse(
93
- {
94
- status: error.status,
95
- type: buildType(error.status, options),
96
- title: statusToPhrase(error.status),
97
- detail: error.message
98
- },
99
- c,
100
- options
101
- );
102
- }
103
- return toResponse(
104
- {
105
- status: 500,
106
- type: buildType(500, options),
107
- title: "Internal Server Error",
108
- detail: options.includeStack ? error.stack : void 0
109
- },
110
- c,
111
- options
112
- );
113
- };
114
- }
115
-
116
- export {
117
- PROBLEM_JSON_CONTENT_TYPE,
118
- problemDetailsHandler,
119
- ProblemDetailsError
120
- };
121
- //# sourceMappingURL=chunk-SLBPERKR.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/handler.ts","../src/error.ts"],"sourcesContent":["import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\t// HTTP status must be 100-599; use body status as-is (RFC 9457 advisory)\n\tconst httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: httpStatus,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\treturn toResponse(error.problemDetails, c, options);\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n","import { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions } from \"./handler.js\";\nimport { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nfunction normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\nexport class ProblemDetailsError extends Error {\n\treadonly problemDetails: ProblemDetails;\n\n\tconstructor(input: ProblemDetailsInput) {\n\t\tconst pd = normalizeProblemDetails(input);\n\t\tsuper(pd.detail ?? pd.title);\n\t\tthis.name = \"ProblemDetailsError\";\n\t\tthis.problemDetails = pd;\n\t}\n\n\tgetResponse(): Response {\n\t\tconst { extensions, ...standard } = this.problemDetails;\n\t\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: this.problemDetails.status,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t}\n}\n"],"mappings":";;;;;;AACA,SAAS,qBAAqB;;;ACG9B,SAAS,wBACR,OACoB;AACpB,SAAO;AAAA,IACN,MAAM,MAAM,QAAQ;AAAA,IACpB,QAAQ,MAAM;AAAA,IACd,OAAO,MAAM,SAAS,eAAe,MAAM,MAAM,KAAK;AAAA,IACtD,QAAQ,MAAM;AAAA,IACd,UAAU,MAAM;AAAA,IAChB,YAAY,MAAM;AAAA,EACnB;AACD;AAEO,IAAM,sBAAN,cAAkC,MAAM;AAAA,EACrC;AAAA,EAET,YAAY,OAA4B;AACvC,UAAM,KAAK,wBAAwB,KAAK;AACxC,UAAM,GAAG,UAAU,GAAG,KAAK;AAC3B,SAAK,OAAO;AACZ,SAAK,iBAAiB;AAAA,EACvB;AAAA,EAEA,cAAwB;AACvB,UAAM,EAAE,YAAY,GAAG,SAAS,IAAI,KAAK;AACzC,UAAM,OAAO,EAAE,GAAG,mBAAmB,UAAU,GAAG,GAAG,SAAS;AAC9D,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ,KAAK,eAAe;AAAA,MAC5B,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;;;AD7BO,IAAM,4BAA4B;AAEzC,IAAM,iBAAiB,oBAAI,IAAI,CAAC,aAAa,eAAe,WAAW,CAAC;AAGjE,SAAS,mBACf,YACsC;AACtC,MAAI,CAAC,WAAY,QAAO;AACxB,MAAI;AACJ,aAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AAC1C,QAAI,eAAe,IAAI,GAAG,GAAG;AAC5B,UAAI,CAAC,SAAU,YAAW,EAAE,GAAG,WAAW;AAC1C,aAAO,SAAS,GAAG;AAAA,IACpB;AAAA,EACD;AACA,SAAO,YAAY;AACpB;AAEA,SAAS,UAAU,QAAgB,SAA+C;AACjF,MAAI,QAAQ,YAAY;AACvB,UAAM,OAAO,aAAa,MAAM;AAChC,QAAI,KAAM,QAAO,GAAG,QAAQ,UAAU,IAAI,IAAI;AAAA,EAC/C;AACA,SAAO,QAAQ,eAAe;AAC/B;AAEA,SAAS,WACR,OACA,GACA,SACW;AACX,MAAI,KAAK;AAAA,IACR,MAAM,MAAM,QAAQ;AAAA,IACpB,QAAQ,MAAM;AAAA,IACd,OAAO,MAAM,SAAS,eAAe,MAAM,MAAM,KAAK;AAAA,IACtD,QAAQ,MAAM;AAAA,IACd,UAAU,MAAM;AAAA,IAChB,YAAY,MAAM;AAAA,EACnB;AAEA,MAAI,QAAQ,UAAU;AACrB,SAAK,EAAE,GAAG,IAAI,GAAG,QAAQ,SAAS,IAAI,CAAC,EAAE;AAAA,EAC1C;AAEA,QAAM,EAAE,YAAY,GAAG,KAAK,IAAI;AAChC,QAAM,OAAO,EAAE,GAAG,mBAAmB,UAAU,GAAG,GAAG,KAAK;AAE1D,IAAE,IAAI,kBAAkB,EAAE;AAG1B,QAAM,aAAa,GAAG,UAAU,OAAO,GAAG,UAAU,MAAM,GAAG,SAAS;AAEtE,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;AAEO,SAAS,sBAAsB,UAAwC,CAAC,GAAiB;AAC/F,SAAO,CAAC,OAAO,MAAM;AACpB,QAAI,iBAAiB,qBAAqB;AACzC,aAAO,WAAW,MAAM,gBAAgB,GAAG,OAAO;AAAA,IACnD;AAEA,QAAI,QAAQ,UAAU;AACrB,YAAM,SAAS,QAAQ,SAAS,KAAK;AACrC,UAAI,QAAQ;AACX,eAAO,WAAW,QAAQ,GAAG,OAAO;AAAA,MACrC;AAAA,IACD;AAEA,QAAI,iBAAiB,eAAe;AACnC,aAAO;AAAA,QACN;AAAA,UACC,QAAQ,MAAM;AAAA,UACd,MAAM,UAAU,MAAM,QAAQ,OAAO;AAAA,UACrC,OAAO,eAAe,MAAM,MAAM;AAAA,UAClC,QAAQ,MAAM;AAAA,QACf;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAEA,WAAO;AAAA,MACN;AAAA,QACC,QAAQ;AAAA,QACR,MAAM,UAAU,KAAK,OAAO;AAAA,QAC5B,OAAO;AAAA,QACP,QAAQ,QAAQ,eAAe,MAAM,QAAQ;AAAA,MAC9C;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AACD;","names":[]}