hono-problem-details 0.1.2 → 0.1.4
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 +3 -0
- package/dist/{chunk-UITHZH2F.js → chunk-NFHWC5GI.js} +23 -3
- package/dist/chunk-NFHWC5GI.js.map +1 -0
- package/dist/chunk-X2E2GZLL.js +137 -0
- package/dist/chunk-X2E2GZLL.js.map +1 -0
- package/dist/index.cjs +81 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +7 -109
- package/dist/index.js.map +1 -1
- package/dist/integrations/openapi.cjs +22 -2
- package/dist/integrations/openapi.cjs.map +1 -1
- package/dist/integrations/openapi.js +1 -1
- package/dist/integrations/standard-schema.cjs +14 -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 +14 -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 +14 -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-UITHZH2F.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.
|
|
@@ -2,29 +2,49 @@
|
|
|
2
2
|
var STATUS_PHRASES = {
|
|
3
3
|
400: "Bad Request",
|
|
4
4
|
401: "Unauthorized",
|
|
5
|
+
402: "Payment Required",
|
|
5
6
|
403: "Forbidden",
|
|
6
7
|
404: "Not Found",
|
|
7
8
|
405: "Method Not Allowed",
|
|
8
9
|
406: "Not Acceptable",
|
|
10
|
+
407: "Proxy Authentication Required",
|
|
9
11
|
408: "Request Timeout",
|
|
10
12
|
409: "Conflict",
|
|
11
13
|
410: "Gone",
|
|
12
14
|
411: "Length Required",
|
|
13
15
|
412: "Precondition Failed",
|
|
14
16
|
413: "Content Too Large",
|
|
17
|
+
414: "URI Too Long",
|
|
15
18
|
415: "Unsupported Media Type",
|
|
19
|
+
416: "Range Not Satisfiable",
|
|
20
|
+
417: "Expectation Failed",
|
|
21
|
+
418: "I'm a Teapot",
|
|
22
|
+
421: "Misdirected Request",
|
|
16
23
|
422: "Unprocessable Content",
|
|
24
|
+
423: "Locked",
|
|
25
|
+
424: "Failed Dependency",
|
|
26
|
+
425: "Too Early",
|
|
27
|
+
426: "Upgrade Required",
|
|
28
|
+
428: "Precondition Required",
|
|
17
29
|
429: "Too Many Requests",
|
|
30
|
+
431: "Request Header Fields Too Large",
|
|
31
|
+
451: "Unavailable For Legal Reasons",
|
|
18
32
|
500: "Internal Server Error",
|
|
19
33
|
501: "Not Implemented",
|
|
20
34
|
502: "Bad Gateway",
|
|
21
35
|
503: "Service Unavailable",
|
|
22
|
-
504: "Gateway Timeout"
|
|
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"
|
|
23
43
|
};
|
|
24
44
|
var STATUS_SLUGS = Object.fromEntries(
|
|
25
45
|
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
26
46
|
Number(code),
|
|
27
|
-
phrase.toLowerCase().replace(/ /g, "-")
|
|
47
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
28
48
|
])
|
|
29
49
|
);
|
|
30
50
|
function statusToPhrase(status) {
|
|
@@ -38,4 +58,4 @@ export {
|
|
|
38
58
|
statusToPhrase,
|
|
39
59
|
statusToSlug
|
|
40
60
|
};
|
|
41
|
-
//# sourceMappingURL=chunk-
|
|
61
|
+
//# sourceMappingURL=chunk-NFHWC5GI.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\n\t\t\t.toLowerCase()\n\t\t\t.replace(/'/g, \"\")\n\t\t\t.replace(/[^a-z0-9]+/g, \"-\")\n\t\t\t.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,OACE,YAAY,EACZ,QAAQ,MAAM,EAAE,EAChB,QAAQ,eAAe,GAAG,EAC1B,QAAQ,UAAU,EAAE;AAAA,EACvB,CAAC;AACF;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;AAEO,SAAS,aAAa,QAAoC;AAChE,SAAO,aAAa,MAAM;AAC3B;","names":[]}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import {
|
|
2
|
+
statusToPhrase,
|
|
3
|
+
statusToSlug
|
|
4
|
+
} from "./chunk-NFHWC5GI.js";
|
|
5
|
+
|
|
6
|
+
// src/utils.ts
|
|
7
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
8
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
9
|
+
function sanitizeExtensions(extensions) {
|
|
10
|
+
if (!extensions) return extensions;
|
|
11
|
+
let filtered;
|
|
12
|
+
for (const key of Object.keys(extensions)) {
|
|
13
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
14
|
+
if (!filtered) filtered = { ...extensions };
|
|
15
|
+
delete filtered[key];
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return filtered ?? extensions;
|
|
19
|
+
}
|
|
20
|
+
function clampHttpStatus(status) {
|
|
21
|
+
return status >= 200 && status <= 599 ? status : 500;
|
|
22
|
+
}
|
|
23
|
+
var FALLBACK_BODY = JSON.stringify({
|
|
24
|
+
type: "about:blank",
|
|
25
|
+
status: 500,
|
|
26
|
+
title: "Internal Server Error"
|
|
27
|
+
});
|
|
28
|
+
function safeStringify(body) {
|
|
29
|
+
try {
|
|
30
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
31
|
+
} catch {
|
|
32
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/error.ts
|
|
37
|
+
function normalizeProblemDetails(input) {
|
|
38
|
+
return {
|
|
39
|
+
type: input.type ?? "about:blank",
|
|
40
|
+
status: input.status,
|
|
41
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
42
|
+
detail: input.detail,
|
|
43
|
+
instance: input.instance,
|
|
44
|
+
extensions: input.extensions
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
var ProblemDetailsError = class extends Error {
|
|
48
|
+
problemDetails;
|
|
49
|
+
constructor(input) {
|
|
50
|
+
const pd = normalizeProblemDetails(input);
|
|
51
|
+
super(pd.detail ?? pd.title);
|
|
52
|
+
this.name = "ProblemDetailsError";
|
|
53
|
+
this.problemDetails = pd;
|
|
54
|
+
}
|
|
55
|
+
getResponse() {
|
|
56
|
+
const { extensions, ...standard } = this.problemDetails;
|
|
57
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
58
|
+
const { json, fallback } = safeStringify(body);
|
|
59
|
+
return new Response(json, {
|
|
60
|
+
status: fallback ? 500 : clampHttpStatus(this.problemDetails.status),
|
|
61
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// src/handler.ts
|
|
67
|
+
import { HTTPException } from "hono/http-exception";
|
|
68
|
+
function buildType(status, options) {
|
|
69
|
+
if (options.typePrefix) {
|
|
70
|
+
const slug = statusToSlug(status);
|
|
71
|
+
if (slug) return `${options.typePrefix}/${slug}`;
|
|
72
|
+
}
|
|
73
|
+
return options.defaultType ?? "about:blank";
|
|
74
|
+
}
|
|
75
|
+
function toResponse(input, c, options) {
|
|
76
|
+
let pd = {
|
|
77
|
+
type: input.type ?? "about:blank",
|
|
78
|
+
status: input.status,
|
|
79
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
80
|
+
detail: input.detail,
|
|
81
|
+
instance: input.instance,
|
|
82
|
+
extensions: input.extensions
|
|
83
|
+
};
|
|
84
|
+
if (options.localize) {
|
|
85
|
+
pd = { ...pd, ...options.localize(pd, c) };
|
|
86
|
+
}
|
|
87
|
+
const { extensions, ...rest } = pd;
|
|
88
|
+
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
89
|
+
c.set("problemDetails", pd);
|
|
90
|
+
const { json, fallback } = safeStringify(body);
|
|
91
|
+
return new Response(json, {
|
|
92
|
+
status: fallback ? 500 : clampHttpStatus(pd.status),
|
|
93
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
function problemDetailsHandler(options = {}) {
|
|
97
|
+
return (error, c) => {
|
|
98
|
+
if (error instanceof ProblemDetailsError) {
|
|
99
|
+
return toResponse(error.problemDetails, c, options);
|
|
100
|
+
}
|
|
101
|
+
if (options.mapError) {
|
|
102
|
+
const mapped = options.mapError(error);
|
|
103
|
+
if (mapped) {
|
|
104
|
+
return toResponse(mapped, c, options);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (error instanceof HTTPException) {
|
|
108
|
+
return toResponse(
|
|
109
|
+
{
|
|
110
|
+
status: error.status,
|
|
111
|
+
type: buildType(error.status, options),
|
|
112
|
+
title: statusToPhrase(error.status),
|
|
113
|
+
detail: error.message
|
|
114
|
+
},
|
|
115
|
+
c,
|
|
116
|
+
options
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return toResponse(
|
|
120
|
+
{
|
|
121
|
+
status: 500,
|
|
122
|
+
type: buildType(500, options),
|
|
123
|
+
title: "Internal Server Error",
|
|
124
|
+
detail: options.includeStack ? error.stack : void 0
|
|
125
|
+
},
|
|
126
|
+
c,
|
|
127
|
+
options
|
|
128
|
+
);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export {
|
|
133
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
134
|
+
ProblemDetailsError,
|
|
135
|
+
problemDetailsHandler
|
|
136
|
+
};
|
|
137
|
+
//# sourceMappingURL=chunk-X2E2GZLL.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts","../src/error.ts","../src/handler.ts"],"sourcesContent":["export const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to the range accepted by the Response constructor (200-599) */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.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\tconst { json, fallback } = safeStringify(body);\n\t\treturn new Response(json, {\n\t\t\tstatus: fallback ? 500 : clampHttpStatus(this.problemDetails.status),\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\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.js\";\n\nexport { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions };\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\tconst { json, fallback } = safeStringify(body);\n\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\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":";;;;;;AAAO,IAAM,4BAA4B;AAEzC,IAAM,iBAAiB,oBAAI,IAAI,CAAC,aAAa,eAAe,WAAW,CAAC;AAGjE,SAAS,mBACf,YACsC;AACtC,MAAI,CAAC,WAAY,QAAO;AACxB,MAAI;AACJ,aAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AAC1C,QAAI,eAAe,IAAI,GAAG,GAAG;AAC5B,UAAI,CAAC,SAAU,YAAW,EAAE,GAAG,WAAW;AA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M,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,29 +34,49 @@ 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
|
};
|
|
55
76
|
var STATUS_SLUGS = Object.fromEntries(
|
|
56
77
|
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
57
78
|
Number(code),
|
|
58
|
-
phrase.toLowerCase().replace(/ /g, "-")
|
|
79
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
59
80
|
])
|
|
60
81
|
);
|
|
61
82
|
function statusToPhrase(status) {
|
|
@@ -65,8 +86,7 @@ function statusToSlug(status) {
|
|
|
65
86
|
return STATUS_SLUGS[status];
|
|
66
87
|
}
|
|
67
88
|
|
|
68
|
-
// src/
|
|
69
|
-
var import_http_exception = require("hono/http-exception");
|
|
89
|
+
// src/utils.ts
|
|
70
90
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
71
91
|
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
72
92
|
function sanitizeExtensions(extensions) {
|
|
@@ -80,6 +100,59 @@ function sanitizeExtensions(extensions) {
|
|
|
80
100
|
}
|
|
81
101
|
return filtered ?? extensions;
|
|
82
102
|
}
|
|
103
|
+
function clampHttpStatus(status) {
|
|
104
|
+
return status >= 200 && status <= 599 ? status : 500;
|
|
105
|
+
}
|
|
106
|
+
var FALLBACK_BODY = JSON.stringify({
|
|
107
|
+
type: "about:blank",
|
|
108
|
+
status: 500,
|
|
109
|
+
title: "Internal Server Error"
|
|
110
|
+
});
|
|
111
|
+
function safeStringify(body) {
|
|
112
|
+
try {
|
|
113
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
114
|
+
} catch {
|
|
115
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// src/error.ts
|
|
120
|
+
function normalizeProblemDetails(input) {
|
|
121
|
+
return {
|
|
122
|
+
type: input.type ?? "about:blank",
|
|
123
|
+
status: input.status,
|
|
124
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
125
|
+
detail: input.detail,
|
|
126
|
+
instance: input.instance,
|
|
127
|
+
extensions: input.extensions
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
var ProblemDetailsError = class extends Error {
|
|
131
|
+
problemDetails;
|
|
132
|
+
constructor(input) {
|
|
133
|
+
const pd = normalizeProblemDetails(input);
|
|
134
|
+
super(pd.detail ?? pd.title);
|
|
135
|
+
this.name = "ProblemDetailsError";
|
|
136
|
+
this.problemDetails = pd;
|
|
137
|
+
}
|
|
138
|
+
getResponse() {
|
|
139
|
+
const { extensions, ...standard } = this.problemDetails;
|
|
140
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
141
|
+
const { json, fallback } = safeStringify(body);
|
|
142
|
+
return new Response(json, {
|
|
143
|
+
status: fallback ? 500 : clampHttpStatus(this.problemDetails.status),
|
|
144
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
// src/factory.ts
|
|
150
|
+
function problemDetails(input) {
|
|
151
|
+
return new ProblemDetailsError(input);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/handler.ts
|
|
155
|
+
var import_http_exception = require("hono/http-exception");
|
|
83
156
|
function buildType(status, options) {
|
|
84
157
|
if (options.typePrefix) {
|
|
85
158
|
const slug = statusToSlug(status);
|
|
@@ -102,9 +175,9 @@ function toResponse(input, c, options) {
|
|
|
102
175
|
const { extensions, ...rest } = pd;
|
|
103
176
|
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
104
177
|
c.set("problemDetails", pd);
|
|
105
|
-
const
|
|
106
|
-
return new Response(
|
|
107
|
-
status:
|
|
178
|
+
const { json, fallback } = safeStringify(body);
|
|
179
|
+
return new Response(json, {
|
|
180
|
+
status: fallback ? 500 : clampHttpStatus(pd.status),
|
|
108
181
|
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
109
182
|
});
|
|
110
183
|
}
|
|
@@ -144,40 +217,6 @@ function problemDetailsHandler(options = {}) {
|
|
|
144
217
|
};
|
|
145
218
|
}
|
|
146
219
|
|
|
147
|
-
// src/error.ts
|
|
148
|
-
function normalizeProblemDetails(input) {
|
|
149
|
-
return {
|
|
150
|
-
type: input.type ?? "about:blank",
|
|
151
|
-
status: input.status,
|
|
152
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
153
|
-
detail: input.detail,
|
|
154
|
-
instance: input.instance,
|
|
155
|
-
extensions: input.extensions
|
|
156
|
-
};
|
|
157
|
-
}
|
|
158
|
-
var ProblemDetailsError = class extends Error {
|
|
159
|
-
problemDetails;
|
|
160
|
-
constructor(input) {
|
|
161
|
-
const pd = normalizeProblemDetails(input);
|
|
162
|
-
super(pd.detail ?? pd.title);
|
|
163
|
-
this.name = "ProblemDetailsError";
|
|
164
|
-
this.problemDetails = pd;
|
|
165
|
-
}
|
|
166
|
-
getResponse() {
|
|
167
|
-
const { extensions, ...standard } = this.problemDetails;
|
|
168
|
-
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
169
|
-
return new Response(JSON.stringify(body), {
|
|
170
|
-
status: this.problemDetails.status,
|
|
171
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
172
|
-
});
|
|
173
|
-
}
|
|
174
|
-
};
|
|
175
|
-
|
|
176
|
-
// src/factory.ts
|
|
177
|
-
function problemDetails(input) {
|
|
178
|
-
return new ProblemDetailsError(input);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
220
|
// src/registry.ts
|
|
182
221
|
function createProblemTypeRegistry(definitions) {
|
|
183
222
|
return {
|
|
@@ -198,6 +237,7 @@ function createProblemTypeRegistry(definitions) {
|
|
|
198
237
|
}
|
|
199
238
|
// Annotate the CommonJS export names for ESM import in node:
|
|
200
239
|
0 && (module.exports = {
|
|
240
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
201
241
|
ProblemDetailsError,
|
|
202
242
|
createProblemTypeRegistry,
|
|
203
243
|
problemDetails,
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 { 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\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(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,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;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,QAA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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;;;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":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/status.ts","../src/utils.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 { 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\n\t\t\t.toLowerCase()\n\t\t\t.replace(/'/g, \"\")\n\t\t\t.replace(/[^a-z0-9]+/g, \"-\")\n\t\t\t.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","export const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to the range accepted by the Response constructor (200-599) */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.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\tconst { json, fallback } = safeStringify(body);\n\t\treturn new Response(json, {\n\t\t\tstatus: fallback ? 500 : clampHttpStatus(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 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\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.js\";\n\nexport { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions };\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\tconst { json, fallback } = safeStringify(body);\n\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\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 { 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":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,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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AQ,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,8 @@ 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
62
|
declare function problemDetailsHandler(options?: ProblemDetailsHandlerOptions): ErrorHandler;
|
|
62
63
|
|
|
63
64
|
interface ProblemTypeDefinition {
|
|
@@ -81,4 +82,4 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
81
82
|
*/
|
|
82
83
|
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
83
84
|
|
|
84
|
-
export { type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
|
85
|
+
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,8 @@ 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
62
|
declare function problemDetailsHandler(options?: ProblemDetailsHandlerOptions): ErrorHandler;
|
|
62
63
|
|
|
63
64
|
interface ProblemTypeDefinition {
|
|
@@ -81,4 +82,4 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
81
82
|
*/
|
|
82
83
|
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
83
84
|
|
|
84
|
-
export { type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
|
85
|
+
export { PROBLEM_JSON_CONTENT_TYPE, type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
package/dist/index.js
CHANGED
|
@@ -1,115 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
3
|
+
ProblemDetailsError,
|
|
4
|
+
problemDetailsHandler
|
|
5
|
+
} from "./chunk-X2E2GZLL.js";
|
|
1
6
|
import {
|
|
2
7
|
statusToPhrase,
|
|
3
8
|
statusToSlug
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
|
|
6
|
-
// src/handler.ts
|
|
7
|
-
import { HTTPException } from "hono/http-exception";
|
|
8
|
-
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
9
|
-
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
10
|
-
function sanitizeExtensions(extensions) {
|
|
11
|
-
if (!extensions) return extensions;
|
|
12
|
-
let filtered;
|
|
13
|
-
for (const key of Object.keys(extensions)) {
|
|
14
|
-
if (DANGEROUS_KEYS.has(key)) {
|
|
15
|
-
if (!filtered) filtered = { ...extensions };
|
|
16
|
-
delete filtered[key];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
return filtered ?? extensions;
|
|
20
|
-
}
|
|
21
|
-
function buildType(status, options) {
|
|
22
|
-
if (options.typePrefix) {
|
|
23
|
-
const slug = statusToSlug(status);
|
|
24
|
-
if (slug) return `${options.typePrefix}/${slug}`;
|
|
25
|
-
}
|
|
26
|
-
return options.defaultType ?? "about:blank";
|
|
27
|
-
}
|
|
28
|
-
function toResponse(input, c, options) {
|
|
29
|
-
let pd = {
|
|
30
|
-
type: input.type ?? "about:blank",
|
|
31
|
-
status: input.status,
|
|
32
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
33
|
-
detail: input.detail,
|
|
34
|
-
instance: input.instance,
|
|
35
|
-
extensions: input.extensions
|
|
36
|
-
};
|
|
37
|
-
if (options.localize) {
|
|
38
|
-
pd = { ...pd, ...options.localize(pd, c) };
|
|
39
|
-
}
|
|
40
|
-
const { extensions, ...rest } = pd;
|
|
41
|
-
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
42
|
-
c.set("problemDetails", pd);
|
|
43
|
-
const httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;
|
|
44
|
-
return new Response(JSON.stringify(body), {
|
|
45
|
-
status: httpStatus,
|
|
46
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
function problemDetailsHandler(options = {}) {
|
|
50
|
-
return (error, c) => {
|
|
51
|
-
if (error instanceof ProblemDetailsError) {
|
|
52
|
-
return toResponse(error.problemDetails, c, options);
|
|
53
|
-
}
|
|
54
|
-
if (options.mapError) {
|
|
55
|
-
const mapped = options.mapError(error);
|
|
56
|
-
if (mapped) {
|
|
57
|
-
return toResponse(mapped, c, options);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
if (error instanceof HTTPException) {
|
|
61
|
-
return toResponse(
|
|
62
|
-
{
|
|
63
|
-
status: error.status,
|
|
64
|
-
type: buildType(error.status, options),
|
|
65
|
-
title: statusToPhrase(error.status),
|
|
66
|
-
detail: error.message
|
|
67
|
-
},
|
|
68
|
-
c,
|
|
69
|
-
options
|
|
70
|
-
);
|
|
71
|
-
}
|
|
72
|
-
return toResponse(
|
|
73
|
-
{
|
|
74
|
-
status: 500,
|
|
75
|
-
type: buildType(500, options),
|
|
76
|
-
title: "Internal Server Error",
|
|
77
|
-
detail: options.includeStack ? error.stack : void 0
|
|
78
|
-
},
|
|
79
|
-
c,
|
|
80
|
-
options
|
|
81
|
-
);
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
// src/error.ts
|
|
86
|
-
function normalizeProblemDetails(input) {
|
|
87
|
-
return {
|
|
88
|
-
type: input.type ?? "about:blank",
|
|
89
|
-
status: input.status,
|
|
90
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
91
|
-
detail: input.detail,
|
|
92
|
-
instance: input.instance,
|
|
93
|
-
extensions: input.extensions
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
var ProblemDetailsError = class extends Error {
|
|
97
|
-
problemDetails;
|
|
98
|
-
constructor(input) {
|
|
99
|
-
const pd = normalizeProblemDetails(input);
|
|
100
|
-
super(pd.detail ?? pd.title);
|
|
101
|
-
this.name = "ProblemDetailsError";
|
|
102
|
-
this.problemDetails = pd;
|
|
103
|
-
}
|
|
104
|
-
getResponse() {
|
|
105
|
-
const { extensions, ...standard } = this.problemDetails;
|
|
106
|
-
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
107
|
-
return new Response(JSON.stringify(body), {
|
|
108
|
-
status: this.problemDetails.status,
|
|
109
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
110
|
-
});
|
|
111
|
-
}
|
|
112
|
-
};
|
|
9
|
+
} from "./chunk-NFHWC5GI.js";
|
|
113
10
|
|
|
114
11
|
// src/factory.ts
|
|
115
12
|
function problemDetails(input) {
|
|
@@ -135,6 +32,7 @@ function createProblemTypeRegistry(definitions) {
|
|
|
135
32
|
};
|
|
136
33
|
}
|
|
137
34
|
export {
|
|
35
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
138
36
|
ProblemDetailsError,
|
|
139
37
|
createProblemTypeRegistry,
|
|
140
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,29 +31,49 @@ 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
|
};
|
|
53
73
|
var STATUS_SLUGS = Object.fromEntries(
|
|
54
74
|
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
55
75
|
Number(code),
|
|
56
|
-
phrase.toLowerCase().replace(/ /g, "-")
|
|
76
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
57
77
|
])
|
|
58
78
|
);
|
|
59
79
|
function statusToPhrase(status) {
|
|
@@ -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.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\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\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,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,
|
|
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\n\t\t\t.toLowerCase()\n\t\t\t.replace(/'/g, \"\")\n\t\t\t.replace(/[^a-z0-9]+/g, \"-\")\n\t\t\t.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,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,OACE,YAAY,EACZ,QAAQ,MAAM,EAAE,EAChB,QAAQ,eAAe,GAAG,EAC1B,QAAQ,UAAU,EAAE;AAAA,EACvB,CAAC;AACF;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;;;ADjDO,IAAM,uBAMR,qBACH,OAAO;AAAA,EACP,MAAM,qBAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,cAAc,CAAC;AAAA,EACpF,QAAQ,qBAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,IAAI,CAAC;AAAA,EAClF,OAAO,qBACL,OAAO,EACP,QAAQ,EAAE,aAAa,qCAAqC,SAAS,cAAc,CAAC;AAAA,EACtF,QAAQ,qBAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,6BAA6B,CAAC;AAAA,EACnF,UAAU,qBAAE,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,19 @@ __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
|
+
|
|
30
|
+
// src/utils.ts
|
|
31
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
32
|
+
var FALLBACK_BODY = JSON.stringify({
|
|
33
|
+
type: "about:blank",
|
|
34
|
+
status: 500,
|
|
35
|
+
title: "Internal Server Error"
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// src/integrations/standard-schema.ts
|
|
26
39
|
function formatPath(path) {
|
|
27
40
|
return path.map((segment) => typeof segment === "object" ? String(segment.key) : String(segment)).join(".");
|
|
28
41
|
}
|
|
@@ -44,7 +57,7 @@ function standardSchemaProblemHook(options) {
|
|
|
44
57
|
};
|
|
45
58
|
return new Response(JSON.stringify(body), {
|
|
46
59
|
status: 422,
|
|
47
|
-
headers: { "Content-Type":
|
|
60
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
48
61
|
});
|
|
49
62
|
};
|
|
50
63
|
}
|
|
@@ -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(\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\": \"application/problem+json\"
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/standard-schema.ts","../../src/handler.ts","../../src/utils.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\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.js\";\n\nexport { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions };\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\tconst { json, fallback } = safeStringify(body);\n\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\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","export const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to the range accepted by the Response constructor (200-599) */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;;;ACDvB,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;AFdD,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-X2E2GZLL.js";
|
|
4
|
+
import "../chunk-NFHWC5GI.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(\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\":
|
|
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,19 @@ __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
|
+
|
|
30
|
+
// src/utils.ts
|
|
31
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
32
|
+
var FALLBACK_BODY = JSON.stringify({
|
|
33
|
+
type: "about:blank",
|
|
34
|
+
status: 500,
|
|
35
|
+
title: "Internal Server Error"
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// src/integrations/valibot.ts
|
|
26
39
|
function formatIssues(issues) {
|
|
27
40
|
return issues.map((issue) => ({
|
|
28
41
|
field: issue.path?.map((p) => p.key).join(".") ?? "",
|
|
@@ -42,7 +55,7 @@ function valibotProblemHook(options) {
|
|
|
42
55
|
};
|
|
43
56
|
return new Response(JSON.stringify(body), {
|
|
44
57
|
status: 422,
|
|
45
|
-
headers: { "Content-Type":
|
|
58
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
46
59
|
});
|
|
47
60
|
};
|
|
48
61
|
}
|
|
@@ -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(\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\": \"application/problem+json\"
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/valibot.ts","../../src/handler.ts","../../src/utils.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\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.js\";\n\nexport { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions };\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\tconst { json, fallback } = safeStringify(body);\n\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\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","export const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to the range accepted by the Response constructor (200-599) */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;;;ACDvB,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;AFbD,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-X2E2GZLL.js";
|
|
4
|
+
import "../chunk-NFHWC5GI.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(\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\":
|
|
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,19 @@ __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
|
+
|
|
30
|
+
// src/utils.ts
|
|
31
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
32
|
+
var FALLBACK_BODY = JSON.stringify({
|
|
33
|
+
type: "about:blank",
|
|
34
|
+
status: 500,
|
|
35
|
+
title: "Internal Server Error"
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// src/integrations/zod.ts
|
|
26
39
|
function formatErrors(zodError) {
|
|
27
40
|
return zodError.issues.map((issue) => ({
|
|
28
41
|
field: issue.path.join("."),
|
|
@@ -42,7 +55,7 @@ function zodProblemHook(options) {
|
|
|
42
55
|
};
|
|
43
56
|
return new Response(JSON.stringify(body), {
|
|
44
57
|
status: 422,
|
|
45
|
-
headers: { "Content-Type":
|
|
58
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
46
59
|
});
|
|
47
60
|
};
|
|
48
61
|
}
|
|
@@ -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(\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\": \"application/problem+json\"
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/zod.ts","../../src/handler.ts","../../src/utils.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\";\nimport {\n\tPROBLEM_JSON_CONTENT_TYPE,\n\tclampHttpStatus,\n\tsafeStringify,\n\tsanitizeExtensions,\n} from \"./utils.js\";\n\nexport { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions };\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\tconst { json, fallback } = safeStringify(body);\n\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\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","export const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to the range accepted by the Response constructor (200-599) */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCA,4BAA8B;;;ACDvB,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;AFbD,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-X2E2GZLL.js";
|
|
4
|
+
import "../chunk-NFHWC5GI.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(\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\":
|
|
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.4",
|
|
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
|
},
|
|
@@ -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\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;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":[]}
|