hono-problem-details 0.1.1 → 0.1.3
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 +10 -0
- package/dist/chunk-S67AKA3K.js +61 -0
- package/dist/chunk-S67AKA3K.js.map +1 -0
- package/dist/chunk-SLBPERKR.js +121 -0
- package/dist/chunk-SLBPERKR.js.map +1 -0
- package/dist/index.cjs +82 -43
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +7 -96
- package/dist/index.js.map +1 -1
- package/dist/integrations/openapi.cjs +27 -1
- package/dist/integrations/openapi.cjs.map +1 -1
- package/dist/integrations/openapi.d.cts +1 -31
- package/dist/integrations/openapi.d.ts +1 -31
- package/dist/integrations/openapi.js +1 -1
- package/dist/integrations/openapi.js.map +1 -1
- package/dist/integrations/standard-schema.cjs +7 -1
- package/dist/integrations/standard-schema.cjs.map +1 -1
- package/dist/integrations/standard-schema.js +6 -1
- package/dist/integrations/standard-schema.js.map +1 -1
- package/dist/integrations/valibot.cjs +7 -1
- package/dist/integrations/valibot.cjs.map +1 -1
- package/dist/integrations/valibot.js +6 -1
- package/dist/integrations/valibot.js.map +1 -1
- package/dist/integrations/zod.cjs +7 -1
- package/dist/integrations/zod.cjs.map +1 -1
- package/dist/integrations/zod.js +6 -1
- package/dist/integrations/zod.js.map +1 -1
- package/package.json +2 -1
- package/dist/chunk-ANLH2WEX.js +0 -37
- package/dist/chunk-ANLH2WEX.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# hono-problem-details
|
|
2
2
|
|
|
3
|
+
[](https://coderabbit.ai)
|
|
4
|
+
[](https://app.devin.ai/org/ryota-ikezawa/wiki/paveg/hono-problem-details)
|
|
5
|
+
|
|
3
6
|
[RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) Problem Details middleware for [Hono](https://hono.dev).
|
|
4
7
|
|
|
5
8
|
Returns `application/problem+json` structured error responses with a single `app.onError` setup.
|
|
@@ -282,6 +285,13 @@ problemDetailsHandler({
|
|
|
282
285
|
});
|
|
283
286
|
```
|
|
284
287
|
|
|
288
|
+
## Used By
|
|
289
|
+
|
|
290
|
+
The following Hono middleware libraries use `hono-problem-details` as an optional dependency for RFC 9457 error responses:
|
|
291
|
+
|
|
292
|
+
- [hono-idempotency](https://github.com/paveg/hono-idempotency) — Idempotency key middleware for Hono
|
|
293
|
+
- [hono-webhook-verify](https://github.com/paveg/hono-webhook-verify) — Webhook signature verification middleware for Hono
|
|
294
|
+
|
|
285
295
|
## License
|
|
286
296
|
|
|
287
297
|
MIT
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// src/status.ts
|
|
2
|
+
var STATUS_PHRASES = {
|
|
3
|
+
400: "Bad Request",
|
|
4
|
+
401: "Unauthorized",
|
|
5
|
+
402: "Payment Required",
|
|
6
|
+
403: "Forbidden",
|
|
7
|
+
404: "Not Found",
|
|
8
|
+
405: "Method Not Allowed",
|
|
9
|
+
406: "Not Acceptable",
|
|
10
|
+
407: "Proxy Authentication Required",
|
|
11
|
+
408: "Request Timeout",
|
|
12
|
+
409: "Conflict",
|
|
13
|
+
410: "Gone",
|
|
14
|
+
411: "Length Required",
|
|
15
|
+
412: "Precondition Failed",
|
|
16
|
+
413: "Content Too Large",
|
|
17
|
+
414: "URI Too Long",
|
|
18
|
+
415: "Unsupported Media Type",
|
|
19
|
+
416: "Range Not Satisfiable",
|
|
20
|
+
417: "Expectation Failed",
|
|
21
|
+
418: "I'm a Teapot",
|
|
22
|
+
421: "Misdirected Request",
|
|
23
|
+
422: "Unprocessable Content",
|
|
24
|
+
423: "Locked",
|
|
25
|
+
424: "Failed Dependency",
|
|
26
|
+
425: "Too Early",
|
|
27
|
+
426: "Upgrade Required",
|
|
28
|
+
428: "Precondition Required",
|
|
29
|
+
429: "Too Many Requests",
|
|
30
|
+
431: "Request Header Fields Too Large",
|
|
31
|
+
451: "Unavailable For Legal Reasons",
|
|
32
|
+
500: "Internal Server Error",
|
|
33
|
+
501: "Not Implemented",
|
|
34
|
+
502: "Bad Gateway",
|
|
35
|
+
503: "Service Unavailable",
|
|
36
|
+
504: "Gateway Timeout",
|
|
37
|
+
505: "HTTP Version Not Supported",
|
|
38
|
+
506: "Variant Also Negotiates",
|
|
39
|
+
507: "Insufficient Storage",
|
|
40
|
+
508: "Loop Detected",
|
|
41
|
+
510: "Not Extended",
|
|
42
|
+
511: "Network Authentication Required"
|
|
43
|
+
};
|
|
44
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
45
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
46
|
+
Number(code),
|
|
47
|
+
phrase.toLowerCase().replace(/ /g, "-")
|
|
48
|
+
])
|
|
49
|
+
);
|
|
50
|
+
function statusToPhrase(status) {
|
|
51
|
+
return STATUS_PHRASES[status];
|
|
52
|
+
}
|
|
53
|
+
function statusToSlug(status) {
|
|
54
|
+
return STATUS_SLUGS[status];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export {
|
|
58
|
+
statusToPhrase,
|
|
59
|
+
statusToSlug
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=chunk-S67AKA3K.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/status.ts"],"sourcesContent":["const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t402: \"Payment Required\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t407: \"Proxy Authentication Required\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t414: \"URI Too Long\",\n\t415: \"Unsupported Media Type\",\n\t416: \"Range Not Satisfiable\",\n\t417: \"Expectation Failed\",\n\t418: \"I'm a Teapot\",\n\t421: \"Misdirected Request\",\n\t422: \"Unprocessable Content\",\n\t423: \"Locked\",\n\t424: \"Failed Dependency\",\n\t425: \"Too Early\",\n\t426: \"Upgrade Required\",\n\t428: \"Precondition Required\",\n\t429: \"Too Many Requests\",\n\t431: \"Request Header Fields Too Large\",\n\t451: \"Unavailable For Legal Reasons\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n\t505: \"HTTP Version Not Supported\",\n\t506: \"Variant Also Negotiates\",\n\t507: \"Insufficient Storage\",\n\t508: \"Loop Detected\",\n\t510: \"Not Extended\",\n\t511: \"Network Authentication Required\",\n};\n\nconst STATUS_SLUGS: Record<number, string> = Object.fromEntries(\n\tObject.entries(STATUS_PHRASES).map(([code, phrase]) => [\n\t\tNumber(code),\n\t\tphrase.toLowerCase().replace(/ /g, \"-\"),\n\t]),\n);\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\treturn STATUS_SLUGS[status];\n}\n"],"mappings":";AAAA,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEA,IAAM,eAAuC,OAAO;AAAA,EACnD,OAAO,QAAQ,cAAc,EAAE,IAAI,CAAC,CAAC,MAAM,MAAM,MAAM;AAAA,IACtD,OAAO,IAAI;AAAA,IACX,OAAO,YAAY,EAAE,QAAQ,MAAM,GAAG;AAAA,EACvC,CAAC;AACF;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;AAEO,SAAS,aAAa,QAAoC;AAChE,SAAO,aAAa,MAAM;AAC3B;","names":[]}
|
|
@@ -0,0 +1,121 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,OAA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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":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var src_exports = {};
|
|
22
22
|
__export(src_exports, {
|
|
23
|
+
PROBLEM_JSON_CONTENT_TYPE: () => PROBLEM_JSON_CONTENT_TYPE,
|
|
23
24
|
ProblemDetailsError: () => ProblemDetailsError,
|
|
24
25
|
createProblemTypeRegistry: () => createProblemTypeRegistry,
|
|
25
26
|
problemDetails: () => problemDetails,
|
|
@@ -33,70 +34,73 @@ module.exports = __toCommonJS(src_exports);
|
|
|
33
34
|
var STATUS_PHRASES = {
|
|
34
35
|
400: "Bad Request",
|
|
35
36
|
401: "Unauthorized",
|
|
37
|
+
402: "Payment Required",
|
|
36
38
|
403: "Forbidden",
|
|
37
39
|
404: "Not Found",
|
|
38
40
|
405: "Method Not Allowed",
|
|
39
41
|
406: "Not Acceptable",
|
|
42
|
+
407: "Proxy Authentication Required",
|
|
40
43
|
408: "Request Timeout",
|
|
41
44
|
409: "Conflict",
|
|
42
45
|
410: "Gone",
|
|
43
46
|
411: "Length Required",
|
|
44
47
|
412: "Precondition Failed",
|
|
45
48
|
413: "Content Too Large",
|
|
49
|
+
414: "URI Too Long",
|
|
46
50
|
415: "Unsupported Media Type",
|
|
51
|
+
416: "Range Not Satisfiable",
|
|
52
|
+
417: "Expectation Failed",
|
|
53
|
+
418: "I'm a Teapot",
|
|
54
|
+
421: "Misdirected Request",
|
|
47
55
|
422: "Unprocessable Content",
|
|
56
|
+
423: "Locked",
|
|
57
|
+
424: "Failed Dependency",
|
|
58
|
+
425: "Too Early",
|
|
59
|
+
426: "Upgrade Required",
|
|
60
|
+
428: "Precondition Required",
|
|
48
61
|
429: "Too Many Requests",
|
|
62
|
+
431: "Request Header Fields Too Large",
|
|
63
|
+
451: "Unavailable For Legal Reasons",
|
|
49
64
|
500: "Internal Server Error",
|
|
50
65
|
501: "Not Implemented",
|
|
51
66
|
502: "Bad Gateway",
|
|
52
67
|
503: "Service Unavailable",
|
|
53
|
-
504: "Gateway Timeout"
|
|
68
|
+
504: "Gateway Timeout",
|
|
69
|
+
505: "HTTP Version Not Supported",
|
|
70
|
+
506: "Variant Also Negotiates",
|
|
71
|
+
507: "Insufficient Storage",
|
|
72
|
+
508: "Loop Detected",
|
|
73
|
+
510: "Not Extended",
|
|
74
|
+
511: "Network Authentication Required"
|
|
54
75
|
};
|
|
76
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
77
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
78
|
+
Number(code),
|
|
79
|
+
phrase.toLowerCase().replace(/ /g, "-")
|
|
80
|
+
])
|
|
81
|
+
);
|
|
55
82
|
function statusToPhrase(status) {
|
|
56
83
|
return STATUS_PHRASES[status];
|
|
57
84
|
}
|
|
58
85
|
function statusToSlug(status) {
|
|
59
|
-
|
|
60
|
-
if (!phrase) return void 0;
|
|
61
|
-
return phrase.toLowerCase().replace(/ /g, "-");
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// src/error.ts
|
|
65
|
-
function normalizeProblemDetails(input) {
|
|
66
|
-
return {
|
|
67
|
-
type: input.type ?? "about:blank",
|
|
68
|
-
status: input.status,
|
|
69
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
70
|
-
detail: input.detail,
|
|
71
|
-
instance: input.instance,
|
|
72
|
-
extensions: input.extensions
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
var ProblemDetailsError = class extends Error {
|
|
76
|
-
problemDetails;
|
|
77
|
-
constructor(input) {
|
|
78
|
-
const pd = normalizeProblemDetails(input);
|
|
79
|
-
super(pd.detail ?? pd.title);
|
|
80
|
-
this.name = "ProblemDetailsError";
|
|
81
|
-
this.problemDetails = pd;
|
|
82
|
-
}
|
|
83
|
-
getResponse() {
|
|
84
|
-
const { extensions, ...standard } = this.problemDetails;
|
|
85
|
-
const body = { ...extensions, ...standard };
|
|
86
|
-
return new Response(JSON.stringify(body), {
|
|
87
|
-
status: this.problemDetails.status,
|
|
88
|
-
headers: { "Content-Type": "application/problem+json" }
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// src/factory.ts
|
|
94
|
-
function problemDetails(input) {
|
|
95
|
-
return new ProblemDetailsError(input);
|
|
86
|
+
return STATUS_SLUGS[status];
|
|
96
87
|
}
|
|
97
88
|
|
|
98
89
|
// src/handler.ts
|
|
99
90
|
var import_http_exception = require("hono/http-exception");
|
|
91
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
92
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
93
|
+
function sanitizeExtensions(extensions) {
|
|
94
|
+
if (!extensions) return extensions;
|
|
95
|
+
let filtered;
|
|
96
|
+
for (const key of Object.keys(extensions)) {
|
|
97
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
98
|
+
if (!filtered) filtered = { ...extensions };
|
|
99
|
+
delete filtered[key];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
return filtered ?? extensions;
|
|
103
|
+
}
|
|
100
104
|
function buildType(status, options) {
|
|
101
105
|
if (options.typePrefix) {
|
|
102
106
|
const slug = statusToSlug(status);
|
|
@@ -117,18 +121,18 @@ function toResponse(input, c, options) {
|
|
|
117
121
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
118
122
|
}
|
|
119
123
|
const { extensions, ...rest } = pd;
|
|
120
|
-
const body = { ...extensions, ...rest };
|
|
124
|
+
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
121
125
|
c.set("problemDetails", pd);
|
|
126
|
+
const httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;
|
|
122
127
|
return new Response(JSON.stringify(body), {
|
|
123
|
-
status:
|
|
124
|
-
headers: { "Content-Type":
|
|
128
|
+
status: httpStatus,
|
|
129
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
125
130
|
});
|
|
126
131
|
}
|
|
127
132
|
function problemDetailsHandler(options = {}) {
|
|
128
133
|
return (error, c) => {
|
|
129
134
|
if (error instanceof ProblemDetailsError) {
|
|
130
|
-
|
|
131
|
-
return error.getResponse();
|
|
135
|
+
return toResponse(error.problemDetails, c, options);
|
|
132
136
|
}
|
|
133
137
|
if (options.mapError) {
|
|
134
138
|
const mapped = options.mapError(error);
|
|
@@ -161,6 +165,40 @@ function problemDetailsHandler(options = {}) {
|
|
|
161
165
|
};
|
|
162
166
|
}
|
|
163
167
|
|
|
168
|
+
// src/error.ts
|
|
169
|
+
function normalizeProblemDetails(input) {
|
|
170
|
+
return {
|
|
171
|
+
type: input.type ?? "about:blank",
|
|
172
|
+
status: input.status,
|
|
173
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
174
|
+
detail: input.detail,
|
|
175
|
+
instance: input.instance,
|
|
176
|
+
extensions: input.extensions
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
var ProblemDetailsError = class extends Error {
|
|
180
|
+
problemDetails;
|
|
181
|
+
constructor(input) {
|
|
182
|
+
const pd = normalizeProblemDetails(input);
|
|
183
|
+
super(pd.detail ?? pd.title);
|
|
184
|
+
this.name = "ProblemDetailsError";
|
|
185
|
+
this.problemDetails = pd;
|
|
186
|
+
}
|
|
187
|
+
getResponse() {
|
|
188
|
+
const { extensions, ...standard } = this.problemDetails;
|
|
189
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
190
|
+
return new Response(JSON.stringify(body), {
|
|
191
|
+
status: this.problemDetails.status,
|
|
192
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// src/factory.ts
|
|
198
|
+
function problemDetails(input) {
|
|
199
|
+
return new ProblemDetailsError(input);
|
|
200
|
+
}
|
|
201
|
+
|
|
164
202
|
// src/registry.ts
|
|
165
203
|
function createProblemTypeRegistry(definitions) {
|
|
166
204
|
return {
|
|
@@ -181,6 +219,7 @@ function createProblemTypeRegistry(definitions) {
|
|
|
181
219
|
}
|
|
182
220
|
// Annotate the CommonJS export names for ESM import in node:
|
|
183
221
|
0 && (module.exports = {
|
|
222
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
184
223
|
ProblemDetailsError,
|
|
185
224
|
createProblemTypeRegistry,
|
|
186
225
|
problemDetails,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/status.ts","../src/error.ts","../src/factory.ts","../src/handler.ts","../src/registry.ts"],"sourcesContent":["export type {\n\tProblemDetails,\n\tProblemDetailsHandlerOptions,\n\tProblemDetailsInput,\n} from \"./types.js\";\nexport { statusToPhrase, statusToSlug } from \"./status.js\";\nexport { ProblemDetailsError } from \"./error.js\";\nexport { problemDetails } from \"./factory.js\";\nexport { problemDetailsHandler } from \"./handler.js\";\nexport { createProblemTypeRegistry } from \"./registry.js\";\n","const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\tconst phrase = STATUS_PHRASES[status];\n\tif (!phrase) return undefined;\n\treturn phrase.toLowerCase().replace(/ /g, \"-\");\n}\n","import { 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 = { ...extensions, ...standard };\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: this.problemDetails.status,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t}\n}\n","import { ProblemDetailsError } from \"./error.js\";\nimport type { ProblemDetailsInput } from \"./types.js\";\n\nexport function problemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetailsError {\n\treturn new ProblemDetailsError(input);\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\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 = { ...extensions, ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: pd.status,\n\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\tc.set(\"problemDetails\", error.problemDetails);\n\t\t\treturn error.getResponse();\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 { ProblemDetailsError } from \"./error.js\";\n\ninterface ProblemTypeDefinition {\n\ttype: string;\n\tstatus: number;\n\ttitle: string;\n}\n\ninterface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {\n\tdetail?: string;\n\tinstance?: string;\n\textensions?: T;\n}\n\ninterface ProblemTypeRegistry<K extends string> {\n\tcreate: <T extends Record<string, unknown>>(\n\t\tkey: K,\n\t\toptions?: CreateOptions<T>,\n\t) => ProblemDetailsError;\n\tget: (key: K) => ProblemTypeDefinition;\n\ttypes: () => K[];\n}\n\n/**\n * Create a registry of pre-defined problem types.\n * Provides type-safe error creation from registered definitions.\n */\nexport function createProblemTypeRegistry<K extends string>(\n\tdefinitions: Record<K, ProblemTypeDefinition>,\n): ProblemTypeRegistry<K> {\n\treturn {\n\t\tcreate: (key, options) => {\n\t\t\tconst def = definitions[key];\n\t\t\treturn new ProblemDetailsError({\n\t\t\t\ttype: def.type,\n\t\t\t\tstatus: def.status,\n\t\t\t\ttitle: def.title,\n\t\t\t\tdetail: options?.detail,\n\t\t\t\tinstance: options?.instance,\n\t\t\t\textensions: options?.extensions,\n\t\t\t});\n\t\t},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;AAAA,IAC1B;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,qCAAe;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;;;ACtDO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY;AACzB,YAAM,MAAM,YAAY,GAAG;AAC3B,aAAO,IAAI,oBAAoB;AAAA,QAC9B,MAAM,IAAI;AAAA,QACV,QAAQ,IAAI;AAAA,QACZ,OAAO,IAAI;AAAA,QACX,QAAQ,SAAS;AAAA,QACjB,UAAU,SAAS;AAAA,QACnB,YAAY,SAAS;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,IACA,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/status.ts","../src/handler.ts","../src/error.ts","../src/factory.ts","../src/registry.ts"],"sourcesContent":["export type {\n\tProblemDetails,\n\tProblemDetailsHandlerOptions,\n\tProblemDetailsInput,\n} from \"./types.js\";\nexport { statusToPhrase, statusToSlug } from \"./status.js\";\nexport { ProblemDetailsError } from \"./error.js\";\nexport { problemDetails } from \"./factory.js\";\nexport { PROBLEM_JSON_CONTENT_TYPE, problemDetailsHandler } from \"./handler.js\";\nexport { createProblemTypeRegistry } from \"./registry.js\";\n","const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t402: \"Payment Required\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t407: \"Proxy Authentication Required\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t414: \"URI Too Long\",\n\t415: \"Unsupported Media Type\",\n\t416: \"Range Not Satisfiable\",\n\t417: \"Expectation Failed\",\n\t418: \"I'm a Teapot\",\n\t421: \"Misdirected Request\",\n\t422: \"Unprocessable Content\",\n\t423: \"Locked\",\n\t424: \"Failed Dependency\",\n\t425: \"Too Early\",\n\t426: \"Upgrade Required\",\n\t428: \"Precondition Required\",\n\t429: \"Too Many Requests\",\n\t431: \"Request Header Fields Too Large\",\n\t451: \"Unavailable For Legal Reasons\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n\t505: \"HTTP Version Not Supported\",\n\t506: \"Variant Also Negotiates\",\n\t507: \"Insufficient Storage\",\n\t508: \"Loop Detected\",\n\t510: \"Not Extended\",\n\t511: \"Network Authentication Required\",\n};\n\nconst STATUS_SLUGS: Record<number, string> = Object.fromEntries(\n\tObject.entries(STATUS_PHRASES).map(([code, phrase]) => [\n\t\tNumber(code),\n\t\tphrase.toLowerCase().replace(/ /g, \"-\"),\n\t]),\n);\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\treturn STATUS_SLUGS[status];\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","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","import { ProblemDetailsError } from \"./error.js\";\nimport type { ProblemDetailsInput } from \"./types.js\";\n\nexport function problemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetailsError {\n\treturn new ProblemDetailsError(input);\n}\n","import { ProblemDetailsError } from \"./error.js\";\n\ninterface ProblemTypeDefinition {\n\ttype: string;\n\tstatus: number;\n\ttitle: string;\n}\n\ninterface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {\n\tdetail?: string;\n\tinstance?: string;\n\textensions?: T;\n}\n\ninterface ProblemTypeRegistry<K extends string> {\n\tcreate: <T extends Record<string, unknown>>(\n\t\tkey: K,\n\t\toptions?: CreateOptions<T>,\n\t) => ProblemDetailsError;\n\tget: (key: K) => ProblemTypeDefinition;\n\ttypes: () => K[];\n}\n\n/**\n * Create a registry of pre-defined problem types.\n * Provides type-safe error creation from registered definitions.\n */\nexport function createProblemTypeRegistry<K extends string>(\n\tdefinitions: Record<K, ProblemTypeDefinition>,\n): ProblemTypeRegistry<K> {\n\treturn {\n\t\tcreate: (key, options) => {\n\t\t\tconst def = definitions[key];\n\t\t\treturn new ProblemDetailsError({\n\t\t\t\ttype: def.type,\n\t\t\t\tstatus: def.status,\n\t\t\t\ttitle: def.title,\n\t\t\t\tdetail: options?.detail,\n\t\t\t\tinstance: options?.instance,\n\t\t\t\textensions: options?.extensions,\n\t\t\t});\n\t\t},\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(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,IAAI,GAAG;AAAA,MACzC,QAAQ,KAAK,eAAe;AAAA,MAC5B,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;;;AChCO,SAAS,eACf,OACsB;AACtB,SAAO,IAAI,oBAAoB,KAAK;AACrC;;;ACoBO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY;AACzB,YAAM,MAAM,YAAY,GAAG;AAC3B,aAAO,IAAI,oBAAoB;AAAA,QAC9B,MAAM,IAAI;AAAA,QACV,QAAQ,IAAI;AAAA,QACZ,OAAO,IAAI;AAAA,QACX,QAAQ,SAAS;AAAA,QACjB,UAAU,SAAS;AAAA,QACnB,YAAY,SAAS;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,IACA,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ErrorHandler } from 'hono';
|
|
1
|
+
import { Context, ErrorHandler } from 'hono';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* RFC 9457 Problem Details object.
|
|
@@ -44,7 +43,7 @@ interface ProblemDetailsHandlerOptions {
|
|
|
44
43
|
/** Custom error to ProblemDetails mapping */
|
|
45
44
|
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
46
45
|
/** Localize title/detail before sending the response */
|
|
47
|
-
localize?: (pd: ProblemDetails, c:
|
|
46
|
+
localize?: (pd: ProblemDetails, c: Context) => ProblemDetails;
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
declare function statusToPhrase(status: number): string | undefined;
|
|
@@ -58,6 +57,7 @@ declare class ProblemDetailsError extends Error {
|
|
|
58
57
|
|
|
59
58
|
declare function problemDetails<T extends Record<string, unknown>>(input: ProblemDetailsInput<T>): ProblemDetailsError;
|
|
60
59
|
|
|
60
|
+
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
61
61
|
declare function problemDetailsHandler(options?: ProblemDetailsHandlerOptions): ErrorHandler;
|
|
62
62
|
|
|
63
63
|
interface ProblemTypeDefinition {
|
|
@@ -81,4 +81,4 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
81
81
|
*/
|
|
82
82
|
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
83
83
|
|
|
84
|
-
export { type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
|
84
|
+
export { PROBLEM_JSON_CONTENT_TYPE, type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { ErrorHandler } from 'hono';
|
|
1
|
+
import { Context, ErrorHandler } from 'hono';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* RFC 9457 Problem Details object.
|
|
@@ -44,7 +43,7 @@ interface ProblemDetailsHandlerOptions {
|
|
|
44
43
|
/** Custom error to ProblemDetails mapping */
|
|
45
44
|
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
46
45
|
/** Localize title/detail before sending the response */
|
|
47
|
-
localize?: (pd: ProblemDetails, c:
|
|
46
|
+
localize?: (pd: ProblemDetails, c: Context) => ProblemDetails;
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
declare function statusToPhrase(status: number): string | undefined;
|
|
@@ -58,6 +57,7 @@ declare class ProblemDetailsError extends Error {
|
|
|
58
57
|
|
|
59
58
|
declare function problemDetails<T extends Record<string, unknown>>(input: ProblemDetailsInput<T>): ProblemDetailsError;
|
|
60
59
|
|
|
60
|
+
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
61
61
|
declare function problemDetailsHandler(options?: ProblemDetailsHandlerOptions): ErrorHandler;
|
|
62
62
|
|
|
63
63
|
interface ProblemTypeDefinition {
|
|
@@ -81,4 +81,4 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
81
81
|
*/
|
|
82
82
|
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
83
83
|
|
|
84
|
-
export { type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
|
84
|
+
export { PROBLEM_JSON_CONTENT_TYPE, type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
package/dist/index.js
CHANGED
|
@@ -1,108 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
3
|
+
ProblemDetailsError,
|
|
4
|
+
problemDetailsHandler
|
|
5
|
+
} from "./chunk-SLBPERKR.js";
|
|
1
6
|
import {
|
|
2
7
|
statusToPhrase,
|
|
3
8
|
statusToSlug
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
|
|
6
|
-
// src/error.ts
|
|
7
|
-
function normalizeProblemDetails(input) {
|
|
8
|
-
return {
|
|
9
|
-
type: input.type ?? "about:blank",
|
|
10
|
-
status: input.status,
|
|
11
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
12
|
-
detail: input.detail,
|
|
13
|
-
instance: input.instance,
|
|
14
|
-
extensions: input.extensions
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
var ProblemDetailsError = class extends Error {
|
|
18
|
-
problemDetails;
|
|
19
|
-
constructor(input) {
|
|
20
|
-
const pd = normalizeProblemDetails(input);
|
|
21
|
-
super(pd.detail ?? pd.title);
|
|
22
|
-
this.name = "ProblemDetailsError";
|
|
23
|
-
this.problemDetails = pd;
|
|
24
|
-
}
|
|
25
|
-
getResponse() {
|
|
26
|
-
const { extensions, ...standard } = this.problemDetails;
|
|
27
|
-
const body = { ...extensions, ...standard };
|
|
28
|
-
return new Response(JSON.stringify(body), {
|
|
29
|
-
status: this.problemDetails.status,
|
|
30
|
-
headers: { "Content-Type": "application/problem+json" }
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
};
|
|
9
|
+
} from "./chunk-S67AKA3K.js";
|
|
34
10
|
|
|
35
11
|
// src/factory.ts
|
|
36
12
|
function problemDetails(input) {
|
|
37
13
|
return new ProblemDetailsError(input);
|
|
38
14
|
}
|
|
39
15
|
|
|
40
|
-
// src/handler.ts
|
|
41
|
-
import { HTTPException } from "hono/http-exception";
|
|
42
|
-
function buildType(status, options) {
|
|
43
|
-
if (options.typePrefix) {
|
|
44
|
-
const slug = statusToSlug(status);
|
|
45
|
-
if (slug) return `${options.typePrefix}/${slug}`;
|
|
46
|
-
}
|
|
47
|
-
return options.defaultType ?? "about:blank";
|
|
48
|
-
}
|
|
49
|
-
function toResponse(input, c, options) {
|
|
50
|
-
let pd = {
|
|
51
|
-
type: input.type ?? "about:blank",
|
|
52
|
-
status: input.status,
|
|
53
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
54
|
-
detail: input.detail,
|
|
55
|
-
instance: input.instance,
|
|
56
|
-
extensions: input.extensions
|
|
57
|
-
};
|
|
58
|
-
if (options.localize) {
|
|
59
|
-
pd = { ...pd, ...options.localize(pd, c) };
|
|
60
|
-
}
|
|
61
|
-
const { extensions, ...rest } = pd;
|
|
62
|
-
const body = { ...extensions, ...rest };
|
|
63
|
-
c.set("problemDetails", pd);
|
|
64
|
-
return new Response(JSON.stringify(body), {
|
|
65
|
-
status: pd.status,
|
|
66
|
-
headers: { "Content-Type": "application/problem+json" }
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
function problemDetailsHandler(options = {}) {
|
|
70
|
-
return (error, c) => {
|
|
71
|
-
if (error instanceof ProblemDetailsError) {
|
|
72
|
-
c.set("problemDetails", error.problemDetails);
|
|
73
|
-
return error.getResponse();
|
|
74
|
-
}
|
|
75
|
-
if (options.mapError) {
|
|
76
|
-
const mapped = options.mapError(error);
|
|
77
|
-
if (mapped) {
|
|
78
|
-
return toResponse(mapped, c, options);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
if (error instanceof HTTPException) {
|
|
82
|
-
return toResponse(
|
|
83
|
-
{
|
|
84
|
-
status: error.status,
|
|
85
|
-
type: buildType(error.status, options),
|
|
86
|
-
title: statusToPhrase(error.status),
|
|
87
|
-
detail: error.message
|
|
88
|
-
},
|
|
89
|
-
c,
|
|
90
|
-
options
|
|
91
|
-
);
|
|
92
|
-
}
|
|
93
|
-
return toResponse(
|
|
94
|
-
{
|
|
95
|
-
status: 500,
|
|
96
|
-
type: buildType(500, options),
|
|
97
|
-
title: "Internal Server Error",
|
|
98
|
-
detail: options.includeStack ? error.stack : void 0
|
|
99
|
-
},
|
|
100
|
-
c,
|
|
101
|
-
options
|
|
102
|
-
);
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
|
|
106
16
|
// src/registry.ts
|
|
107
17
|
function createProblemTypeRegistry(definitions) {
|
|
108
18
|
return {
|
|
@@ -122,6 +32,7 @@ function createProblemTypeRegistry(definitions) {
|
|
|
122
32
|
};
|
|
123
33
|
}
|
|
124
34
|
export {
|
|
35
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
125
36
|
ProblemDetailsError,
|
|
126
37
|
createProblemTypeRegistry,
|
|
127
38
|
problemDetails,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/factory.ts","../src/registry.ts"],"sourcesContent":["import { ProblemDetailsError } from \"./error.js\";\nimport type { ProblemDetailsInput } from \"./types.js\";\n\nexport function problemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetailsError {\n\treturn new ProblemDetailsError(input);\n}\n","import { ProblemDetailsError } from \"./error.js\";\n\ninterface ProblemTypeDefinition {\n\ttype: string;\n\tstatus: number;\n\ttitle: string;\n}\n\ninterface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {\n\tdetail?: string;\n\tinstance?: string;\n\textensions?: T;\n}\n\ninterface ProblemTypeRegistry<K extends string> {\n\tcreate: <T extends Record<string, unknown>>(\n\t\tkey: K,\n\t\toptions?: CreateOptions<T>,\n\t) => ProblemDetailsError;\n\tget: (key: K) => ProblemTypeDefinition;\n\ttypes: () => K[];\n}\n\n/**\n * Create a registry of pre-defined problem types.\n * Provides type-safe error creation from registered definitions.\n */\nexport function createProblemTypeRegistry<K extends string>(\n\tdefinitions: Record<K, ProblemTypeDefinition>,\n): ProblemTypeRegistry<K> {\n\treturn {\n\t\tcreate: (key, options) => {\n\t\t\tconst def = definitions[key];\n\t\t\treturn new ProblemDetailsError({\n\t\t\t\ttype: def.type,\n\t\t\t\tstatus: def.status,\n\t\t\t\ttitle: def.title,\n\t\t\t\tdetail: options?.detail,\n\t\t\t\tinstance: options?.instance,\n\t\t\t\textensions: options?.extensions,\n\t\t\t});\n\t\t},\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;;;;;;AAGO,SAAS,eACf,OACsB;AACtB,SAAO,IAAI,oBAAoB,KAAK;AACrC;;;ACoBO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY;AACzB,YAAM,MAAM,YAAY,GAAG;AAC3B,aAAO,IAAI,oBAAoB;AAAA,QAC9B,MAAM,IAAI;AAAA,QACV,QAAQ,IAAI;AAAA,QACZ,OAAO,IAAI;AAAA,QACX,QAAQ,SAAS;AAAA,QACjB,UAAU,SAAS;AAAA,QACnB,YAAY,SAAS;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,IACA,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
|
@@ -31,25 +31,51 @@ var import_zod_openapi = require("@hono/zod-openapi");
|
|
|
31
31
|
var STATUS_PHRASES = {
|
|
32
32
|
400: "Bad Request",
|
|
33
33
|
401: "Unauthorized",
|
|
34
|
+
402: "Payment Required",
|
|
34
35
|
403: "Forbidden",
|
|
35
36
|
404: "Not Found",
|
|
36
37
|
405: "Method Not Allowed",
|
|
37
38
|
406: "Not Acceptable",
|
|
39
|
+
407: "Proxy Authentication Required",
|
|
38
40
|
408: "Request Timeout",
|
|
39
41
|
409: "Conflict",
|
|
40
42
|
410: "Gone",
|
|
41
43
|
411: "Length Required",
|
|
42
44
|
412: "Precondition Failed",
|
|
43
45
|
413: "Content Too Large",
|
|
46
|
+
414: "URI Too Long",
|
|
44
47
|
415: "Unsupported Media Type",
|
|
48
|
+
416: "Range Not Satisfiable",
|
|
49
|
+
417: "Expectation Failed",
|
|
50
|
+
418: "I'm a Teapot",
|
|
51
|
+
421: "Misdirected Request",
|
|
45
52
|
422: "Unprocessable Content",
|
|
53
|
+
423: "Locked",
|
|
54
|
+
424: "Failed Dependency",
|
|
55
|
+
425: "Too Early",
|
|
56
|
+
426: "Upgrade Required",
|
|
57
|
+
428: "Precondition Required",
|
|
46
58
|
429: "Too Many Requests",
|
|
59
|
+
431: "Request Header Fields Too Large",
|
|
60
|
+
451: "Unavailable For Legal Reasons",
|
|
47
61
|
500: "Internal Server Error",
|
|
48
62
|
501: "Not Implemented",
|
|
49
63
|
502: "Bad Gateway",
|
|
50
64
|
503: "Service Unavailable",
|
|
51
|
-
504: "Gateway Timeout"
|
|
65
|
+
504: "Gateway Timeout",
|
|
66
|
+
505: "HTTP Version Not Supported",
|
|
67
|
+
506: "Variant Also Negotiates",
|
|
68
|
+
507: "Insufficient Storage",
|
|
69
|
+
508: "Loop Detected",
|
|
70
|
+
510: "Not Extended",
|
|
71
|
+
511: "Network Authentication Required"
|
|
52
72
|
};
|
|
73
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
74
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
75
|
+
Number(code),
|
|
76
|
+
phrase.toLowerCase().replace(/ /g, "-")
|
|
77
|
+
])
|
|
78
|
+
);
|
|
53
79
|
function statusToPhrase(status) {
|
|
54
80
|
return STATUS_PHRASES[status];
|
|
55
81
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/openapi.ts","../../src/status.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/openapi.ts","../../src/status.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema: z.ZodObject<{\n\ttype: z.ZodString;\n\tstatus: z.ZodNumber;\n\ttitle: z.ZodString;\n\tdetail: z.ZodOptional<z.ZodString>;\n\tinstance: z.ZodOptional<z.ZodString>;\n}> = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(\n\textensions: z.ZodObject<T>,\n): z.AnyZodObject {\n\treturn ProblemDetailsSchema.merge(extensions).openapi({ title: \"ProblemDetails\" });\n}\n\n/**\n * Create an OpenAPI response object for Problem Details.\n * Use in createRoute() responses: { 422: problemDetailsResponse(422) }\n */\nexport function problemDetailsResponse(\n\tstatus: number,\n\tdescription?: string,\n\tschema: z.ZodTypeAny = ProblemDetailsSchema,\n): {\n\tcontent: { \"application/problem+json\": { schema: z.ZodTypeAny } };\n\tdescription: string;\n} {\n\treturn {\n\t\tcontent: {\n\t\t\t\"application/problem+json\": { schema },\n\t\t},\n\t\tdescription: description ?? statusToPhrase(status) ?? \"Error\",\n\t};\n}\n","const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t402: \"Payment Required\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t407: \"Proxy Authentication Required\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t414: \"URI Too Long\",\n\t415: \"Unsupported Media Type\",\n\t416: \"Range Not Satisfiable\",\n\t417: \"Expectation Failed\",\n\t418: \"I'm a Teapot\",\n\t421: \"Misdirected Request\",\n\t422: \"Unprocessable Content\",\n\t423: \"Locked\",\n\t424: \"Failed Dependency\",\n\t425: \"Too Early\",\n\t426: \"Upgrade Required\",\n\t428: \"Precondition Required\",\n\t429: \"Too Many Requests\",\n\t431: \"Request Header Fields Too Large\",\n\t451: \"Unavailable For Legal Reasons\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n\t505: \"HTTP Version Not Supported\",\n\t506: \"Variant Also Negotiates\",\n\t507: \"Insufficient Storage\",\n\t508: \"Loop Detected\",\n\t510: \"Not Extended\",\n\t511: \"Network Authentication Required\",\n};\n\nconst STATUS_SLUGS: Record<number, string> = Object.fromEntries(\n\tObject.entries(STATUS_PHRASES).map(([code, phrase]) => [\n\t\tNumber(code),\n\t\tphrase.toLowerCase().replace(/ /g, \"-\"),\n\t]),\n);\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\treturn STATUS_SLUGS[status];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAkB;;;ACAlB,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,K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B,SAAS,2BACf,YACiB;AACjB,SAAO,qBAAqB,MAAM,UAAU,EAAE,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAClF;AAMO,SAAS,uBACf,QACA,aACA,SAAuB,sBAItB;AACD,SAAO;AAAA,IACN,SAAS;AAAA,MACR,4BAA4B,EAAE,OAAO;AAAA,IACtC;AAAA,IACA,aAAa,eAAe,eAAe,MAAM,KAAK;AAAA,EACvD;AACD;","names":[]}
|
|
@@ -10,42 +10,12 @@ declare const ProblemDetailsSchema: z.ZodObject<{
|
|
|
10
10
|
title: z.ZodString;
|
|
11
11
|
detail: z.ZodOptional<z.ZodString>;
|
|
12
12
|
instance: z.ZodOptional<z.ZodString>;
|
|
13
|
-
}, "strip", z.ZodTypeAny, {
|
|
14
|
-
type: string;
|
|
15
|
-
title: string;
|
|
16
|
-
status: number;
|
|
17
|
-
detail?: string | undefined;
|
|
18
|
-
instance?: string | undefined;
|
|
19
|
-
}, {
|
|
20
|
-
type: string;
|
|
21
|
-
title: string;
|
|
22
|
-
status: number;
|
|
23
|
-
detail?: string | undefined;
|
|
24
|
-
instance?: string | undefined;
|
|
25
13
|
}>;
|
|
26
14
|
/**
|
|
27
15
|
* Create a Problem Details schema with typed extension members.
|
|
28
16
|
* Extensions are merged at top level per RFC 9457.
|
|
29
17
|
*/
|
|
30
|
-
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.
|
|
31
|
-
type: z.ZodString;
|
|
32
|
-
status: z.ZodNumber;
|
|
33
|
-
title: z.ZodString;
|
|
34
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
35
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
36
|
-
}, T>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<z.objectUtil.extendShape<{
|
|
37
|
-
type: z.ZodString;
|
|
38
|
-
status: z.ZodNumber;
|
|
39
|
-
title: z.ZodString;
|
|
40
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
41
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
42
|
-
}, T>>, any> extends infer T_1 ? { [k in keyof T_1]: T_1[k]; } : never, z.baseObjectInputType<z.objectUtil.extendShape<{
|
|
43
|
-
type: z.ZodString;
|
|
44
|
-
status: z.ZodNumber;
|
|
45
|
-
title: z.ZodString;
|
|
46
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
47
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
48
|
-
}, T>> extends infer T_2 ? { [k_1 in keyof T_2]: T_2[k_1]; } : never>;
|
|
18
|
+
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.AnyZodObject;
|
|
49
19
|
/**
|
|
50
20
|
* Create an OpenAPI response object for Problem Details.
|
|
51
21
|
* Use in createRoute() responses: { 422: problemDetailsResponse(422) }
|
|
@@ -10,42 +10,12 @@ declare const ProblemDetailsSchema: z.ZodObject<{
|
|
|
10
10
|
title: z.ZodString;
|
|
11
11
|
detail: z.ZodOptional<z.ZodString>;
|
|
12
12
|
instance: z.ZodOptional<z.ZodString>;
|
|
13
|
-
}, "strip", z.ZodTypeAny, {
|
|
14
|
-
type: string;
|
|
15
|
-
title: string;
|
|
16
|
-
status: number;
|
|
17
|
-
detail?: string | undefined;
|
|
18
|
-
instance?: string | undefined;
|
|
19
|
-
}, {
|
|
20
|
-
type: string;
|
|
21
|
-
title: string;
|
|
22
|
-
status: number;
|
|
23
|
-
detail?: string | undefined;
|
|
24
|
-
instance?: string | undefined;
|
|
25
13
|
}>;
|
|
26
14
|
/**
|
|
27
15
|
* Create a Problem Details schema with typed extension members.
|
|
28
16
|
* Extensions are merged at top level per RFC 9457.
|
|
29
17
|
*/
|
|
30
|
-
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.
|
|
31
|
-
type: z.ZodString;
|
|
32
|
-
status: z.ZodNumber;
|
|
33
|
-
title: z.ZodString;
|
|
34
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
35
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
36
|
-
}, T>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<z.objectUtil.extendShape<{
|
|
37
|
-
type: z.ZodString;
|
|
38
|
-
status: z.ZodNumber;
|
|
39
|
-
title: z.ZodString;
|
|
40
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
41
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
42
|
-
}, T>>, any> extends infer T_1 ? { [k in keyof T_1]: T_1[k]; } : never, z.baseObjectInputType<z.objectUtil.extendShape<{
|
|
43
|
-
type: z.ZodString;
|
|
44
|
-
status: z.ZodNumber;
|
|
45
|
-
title: z.ZodString;
|
|
46
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
47
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
48
|
-
}, T>> extends infer T_2 ? { [k_1 in keyof T_2]: T_2[k_1]; } : never>;
|
|
18
|
+
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.AnyZodObject;
|
|
49
19
|
/**
|
|
50
20
|
* Create an OpenAPI response object for Problem Details.
|
|
51
21
|
* Use in createRoute() responses: { 422: problemDetailsResponse(422) }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/openapi.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/openapi.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema: z.ZodObject<{\n\ttype: z.ZodString;\n\tstatus: z.ZodNumber;\n\ttitle: z.ZodString;\n\tdetail: z.ZodOptional<z.ZodString>;\n\tinstance: z.ZodOptional<z.ZodString>;\n}> = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(\n\textensions: z.ZodObject<T>,\n): z.AnyZodObject {\n\treturn ProblemDetailsSchema.merge(extensions).openapi({ title: \"ProblemDetails\" });\n}\n\n/**\n * Create an OpenAPI response object for Problem Details.\n * Use in createRoute() responses: { 422: problemDetailsResponse(422) }\n */\nexport function problemDetailsResponse(\n\tstatus: number,\n\tdescription?: string,\n\tschema: z.ZodTypeAny = ProblemDetailsSchema,\n): {\n\tcontent: { \"application/problem+json\": { schema: z.ZodTypeAny } };\n\tdescription: string;\n} {\n\treturn {\n\t\tcontent: {\n\t\t\t\"application/problem+json\": { schema },\n\t\t},\n\t\tdescription: description ?? statusToPhrase(status) ?? \"Error\",\n\t};\n}\n"],"mappings":";;;;;AAAA,SAAS,SAAS;AAOX,IAAM,uBAMR,EACH,OAAO;AAAA,EACP,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,cAAc,CAAC;AAAA,EACpF,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,IAAI,CAAC;AAAA,EAClF,OAAO,EACL,OAAO,EACP,QAAQ,EAAE,aAAa,qCAAqC,SAAS,cAAc,CAAC;AAAA,EACtF,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,6BAA6B,CAAC;AAAA,EACnF,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,iCAAiC,CAAC;AAC1F,CAAC,EACA,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAM9B,SAAS,2BACf,YACiB;AACjB,SAAO,qBAAqB,MAAM,UAAU,EAAE,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAClF;AAMO,SAAS,uBACf,QACA,aACA,SAAuB,sBAItB;AACD,SAAO;AAAA,IACN,SAAS;AAAA,MACR,4BAA4B,EAAE,OAAO;AAAA,IACtC;AAAA,IACA,aAAa,eAAe,eAAe,MAAM,KAAK;AAAA,EACvD;AACD;","names":[]}
|
|
@@ -23,6 +23,12 @@ __export(standard_schema_exports, {
|
|
|
23
23
|
standardSchemaProblemHook: () => standardSchemaProblemHook
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(standard_schema_exports);
|
|
26
|
+
|
|
27
|
+
// src/handler.ts
|
|
28
|
+
var import_http_exception = require("hono/http-exception");
|
|
29
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
30
|
+
|
|
31
|
+
// src/integrations/standard-schema.ts
|
|
26
32
|
function formatPath(path) {
|
|
27
33
|
return path.map((segment) => typeof segment === "object" ? String(segment.key) : String(segment)).join(".");
|
|
28
34
|
}
|
|
@@ -44,7 +50,7 @@ function standardSchemaProblemHook(options) {
|
|
|
44
50
|
};
|
|
45
51
|
return new Response(JSON.stringify(body), {
|
|
46
52
|
status: 422,
|
|
47
|
-
headers: { "Content-Type":
|
|
53
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
48
54
|
});
|
|
49
55
|
};
|
|
50
56
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\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(
|
|
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,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PROBLEM_JSON_CONTENT_TYPE
|
|
3
|
+
} from "../chunk-SLBPERKR.js";
|
|
4
|
+
import "../chunk-S67AKA3K.js";
|
|
5
|
+
|
|
1
6
|
// src/integrations/standard-schema.ts
|
|
2
7
|
function formatPath(path) {
|
|
3
8
|
return path.map((segment) => typeof segment === "object" ? String(segment.key) : String(segment)).join(".");
|
|
@@ -20,7 +25,7 @@ function standardSchemaProblemHook(options) {
|
|
|
20
25
|
};
|
|
21
26
|
return new Response(JSON.stringify(body), {
|
|
22
27
|
status: 422,
|
|
23
|
-
headers: { "Content-Type":
|
|
28
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
24
29
|
});
|
|
25
30
|
};
|
|
26
31
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\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(
|
|
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":[]}
|
|
@@ -23,6 +23,12 @@ __export(valibot_exports, {
|
|
|
23
23
|
valibotProblemHook: () => valibotProblemHook
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(valibot_exports);
|
|
26
|
+
|
|
27
|
+
// src/handler.ts
|
|
28
|
+
var import_http_exception = require("hono/http-exception");
|
|
29
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
30
|
+
|
|
31
|
+
// src/integrations/valibot.ts
|
|
26
32
|
function formatIssues(issues) {
|
|
27
33
|
return issues.map((issue) => ({
|
|
28
34
|
field: issue.path?.map((p) => p.key).join(".") ?? "",
|
|
@@ -42,7 +48,7 @@ function valibotProblemHook(options) {
|
|
|
42
48
|
};
|
|
43
49
|
return new Response(JSON.stringify(body), {
|
|
44
50
|
status: 422,
|
|
45
|
-
headers: { "Content-Type":
|
|
51
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
46
52
|
});
|
|
47
53
|
};
|
|
48
54
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\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(
|
|
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,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PROBLEM_JSON_CONTENT_TYPE
|
|
3
|
+
} from "../chunk-SLBPERKR.js";
|
|
4
|
+
import "../chunk-S67AKA3K.js";
|
|
5
|
+
|
|
1
6
|
// src/integrations/valibot.ts
|
|
2
7
|
function formatIssues(issues) {
|
|
3
8
|
return issues.map((issue) => ({
|
|
@@ -18,7 +23,7 @@ function valibotProblemHook(options) {
|
|
|
18
23
|
};
|
|
19
24
|
return new Response(JSON.stringify(body), {
|
|
20
25
|
status: 422,
|
|
21
|
-
headers: { "Content-Type":
|
|
26
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
22
27
|
});
|
|
23
28
|
};
|
|
24
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\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(
|
|
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":[]}
|
|
@@ -23,6 +23,12 @@ __export(zod_exports, {
|
|
|
23
23
|
zodProblemHook: () => zodProblemHook
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(zod_exports);
|
|
26
|
+
|
|
27
|
+
// src/handler.ts
|
|
28
|
+
var import_http_exception = require("hono/http-exception");
|
|
29
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
30
|
+
|
|
31
|
+
// src/integrations/zod.ts
|
|
26
32
|
function formatErrors(zodError) {
|
|
27
33
|
return zodError.issues.map((issue) => ({
|
|
28
34
|
field: issue.path.join("."),
|
|
@@ -42,7 +48,7 @@ function zodProblemHook(options) {
|
|
|
42
48
|
};
|
|
43
49
|
return new Response(JSON.stringify(body), {
|
|
44
50
|
status: 422,
|
|
45
|
-
headers: { "Content-Type":
|
|
51
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
46
52
|
});
|
|
47
53
|
};
|
|
48
54
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\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(
|
|
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":[]}
|
package/dist/integrations/zod.js
CHANGED
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PROBLEM_JSON_CONTENT_TYPE
|
|
3
|
+
} from "../chunk-SLBPERKR.js";
|
|
4
|
+
import "../chunk-S67AKA3K.js";
|
|
5
|
+
|
|
1
6
|
// src/integrations/zod.ts
|
|
2
7
|
function formatErrors(zodError) {
|
|
3
8
|
return zodError.issues.map((issue) => ({
|
|
@@ -18,7 +23,7 @@ function zodProblemHook(options) {
|
|
|
18
23
|
};
|
|
19
24
|
return new Response(JSON.stringify(body), {
|
|
20
25
|
status: 422,
|
|
21
|
-
headers: { "Content-Type":
|
|
26
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
22
27
|
});
|
|
23
28
|
};
|
|
24
29
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\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(
|
|
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":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-problem-details",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "RFC 9457 Problem Details middleware for Hono",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -80,6 +80,7 @@
|
|
|
80
80
|
"homepage": "https://github.com/paveg/hono-problem-details#readme",
|
|
81
81
|
"bugs": "https://github.com/paveg/hono-problem-details/issues",
|
|
82
82
|
"license": "MIT",
|
|
83
|
+
"author": "Ryota Ikezawa",
|
|
83
84
|
"peerDependencies": {
|
|
84
85
|
"hono": ">=4.0.0"
|
|
85
86
|
},
|
package/dist/chunk-ANLH2WEX.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// src/status.ts
|
|
2
|
-
var STATUS_PHRASES = {
|
|
3
|
-
400: "Bad Request",
|
|
4
|
-
401: "Unauthorized",
|
|
5
|
-
403: "Forbidden",
|
|
6
|
-
404: "Not Found",
|
|
7
|
-
405: "Method Not Allowed",
|
|
8
|
-
406: "Not Acceptable",
|
|
9
|
-
408: "Request Timeout",
|
|
10
|
-
409: "Conflict",
|
|
11
|
-
410: "Gone",
|
|
12
|
-
411: "Length Required",
|
|
13
|
-
412: "Precondition Failed",
|
|
14
|
-
413: "Content Too Large",
|
|
15
|
-
415: "Unsupported Media Type",
|
|
16
|
-
422: "Unprocessable Content",
|
|
17
|
-
429: "Too Many Requests",
|
|
18
|
-
500: "Internal Server Error",
|
|
19
|
-
501: "Not Implemented",
|
|
20
|
-
502: "Bad Gateway",
|
|
21
|
-
503: "Service Unavailable",
|
|
22
|
-
504: "Gateway Timeout"
|
|
23
|
-
};
|
|
24
|
-
function statusToPhrase(status) {
|
|
25
|
-
return STATUS_PHRASES[status];
|
|
26
|
-
}
|
|
27
|
-
function statusToSlug(status) {
|
|
28
|
-
const phrase = STATUS_PHRASES[status];
|
|
29
|
-
if (!phrase) return void 0;
|
|
30
|
-
return phrase.toLowerCase().replace(/ /g, "-");
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export {
|
|
34
|
-
statusToPhrase,
|
|
35
|
-
statusToSlug
|
|
36
|
-
};
|
|
37
|
-
//# sourceMappingURL=chunk-ANLH2WEX.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/status.ts"],"sourcesContent":["const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\tconst phrase = STATUS_PHRASES[status];\n\tif (!phrase) return undefined;\n\treturn phrase.toLowerCase().replace(/ /g, \"-\");\n}\n"],"mappings":";AAAA,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;AAEO,SAAS,aAAa,QAAoC;AAChE,QAAM,SAAS,eAAe,MAAM;AACpC,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,OAAO,YAAY,EAAE,QAAQ,MAAM,GAAG;AAC9C;","names":[]}
|