hono-problem-details 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/dist/chunk-LMBOCQPX.js +23 -0
- package/dist/chunk-LMBOCQPX.js.map +1 -0
- package/dist/chunk-X2MUB5YP.js +59 -0
- package/dist/chunk-X2MUB5YP.js.map +1 -0
- package/dist/index.cjs +22 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +71 -14
- package/dist/index.js.map +1 -1
- package/dist/integrations/standard-schema.cjs +17 -15
- package/dist/integrations/standard-schema.cjs.map +1 -1
- package/dist/integrations/standard-schema.d.cts +3 -6
- package/dist/integrations/standard-schema.d.ts +3 -6
- package/dist/integrations/standard-schema.js +5 -14
- package/dist/integrations/standard-schema.js.map +1 -1
- package/dist/integrations/valibot.cjs +17 -15
- package/dist/integrations/valibot.cjs.map +1 -1
- package/dist/integrations/valibot.d.cts +3 -6
- package/dist/integrations/valibot.d.ts +3 -6
- package/dist/integrations/valibot.js +5 -14
- package/dist/integrations/valibot.js.map +1 -1
- package/dist/integrations/zod.cjs +17 -15
- package/dist/integrations/zod.cjs.map +1 -1
- package/dist/integrations/zod.d.cts +3 -6
- package/dist/integrations/zod.d.ts +3 -6
- package/dist/integrations/zod.js +5 -14
- package/dist/integrations/zod.js.map +1 -1
- package/dist/validation-DqPEnoIw.d.cts +6 -0
- package/dist/validation-DqPEnoIw.d.ts +6 -0
- package/package.json +1 -1
- package/dist/chunk-X2E2GZLL.js +0 -137
- package/dist/chunk-X2E2GZLL.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# hono-problem-details
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/hono-problem-details)
|
|
4
|
+
[](https://github.com/paveg/hono-problem-details/actions/workflows/ci.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
3
6
|
[](https://coderabbit.ai)
|
|
4
7
|
[](https://app.devin.ai/org/ryota-ikezawa/wiki/paveg/hono-problem-details)
|
|
5
8
|
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PROBLEM_JSON_CONTENT_TYPE
|
|
3
|
+
} from "./chunk-X2MUB5YP.js";
|
|
4
|
+
|
|
5
|
+
// src/integrations/validation.ts
|
|
6
|
+
function buildValidationResponse(errors, options) {
|
|
7
|
+
const body = {
|
|
8
|
+
type: "about:blank",
|
|
9
|
+
status: 422,
|
|
10
|
+
title: options?.title ?? "Validation Error",
|
|
11
|
+
detail: options?.detail ?? "Request validation failed",
|
|
12
|
+
errors
|
|
13
|
+
};
|
|
14
|
+
return new Response(JSON.stringify(body), {
|
|
15
|
+
status: 422,
|
|
16
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export {
|
|
21
|
+
buildValidationResponse
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=chunk-LMBOCQPX.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/integrations/validation.ts"],"sourcesContent":["import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;AAcO,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;","names":[]}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import {
|
|
2
|
+
statusToPhrase
|
|
3
|
+
} from "./chunk-NFHWC5GI.js";
|
|
4
|
+
|
|
5
|
+
// src/utils.ts
|
|
6
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
7
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
8
|
+
function sanitizeExtensions(extensions) {
|
|
9
|
+
if (!extensions) return extensions;
|
|
10
|
+
let filtered;
|
|
11
|
+
for (const key of Object.keys(extensions)) {
|
|
12
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
13
|
+
if (!filtered) filtered = { ...extensions };
|
|
14
|
+
delete filtered[key];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return filtered ?? extensions;
|
|
18
|
+
}
|
|
19
|
+
function clampHttpStatus(status) {
|
|
20
|
+
return status >= 200 && status <= 599 ? status : 500;
|
|
21
|
+
}
|
|
22
|
+
var FALLBACK_BODY = JSON.stringify({
|
|
23
|
+
type: "about:blank",
|
|
24
|
+
status: 500,
|
|
25
|
+
title: "Internal Server Error"
|
|
26
|
+
});
|
|
27
|
+
function normalizeProblemDetails(input) {
|
|
28
|
+
return {
|
|
29
|
+
type: input.type ?? "about:blank",
|
|
30
|
+
status: input.status,
|
|
31
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
32
|
+
detail: input.detail,
|
|
33
|
+
instance: input.instance,
|
|
34
|
+
extensions: input.extensions
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function buildProblemResponse(pd) {
|
|
38
|
+
const { extensions, ...standard } = pd;
|
|
39
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
40
|
+
const { json, fallback } = safeStringify(body);
|
|
41
|
+
return new Response(json, {
|
|
42
|
+
status: fallback ? 500 : clampHttpStatus(pd.status),
|
|
43
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
function safeStringify(body) {
|
|
47
|
+
try {
|
|
48
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
49
|
+
} catch {
|
|
50
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export {
|
|
55
|
+
PROBLEM_JSON_CONTENT_TYPE,
|
|
56
|
+
normalizeProblemDetails,
|
|
57
|
+
buildProblemResponse
|
|
58
|
+
};
|
|
59
|
+
//# sourceMappingURL=chunk-X2MUB5YP.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n"],"mappings":";;;;;AAGO,IAAM,4BAA4B;AAEzC,IAAM,iBAAiB,oBAAI,IAAI,CAAC,aAAa,eAAe,WAAW,CAAC;AAGjE,SAAS,mBACf,YACsC;AACtC,MAAI,CAAC,WAAY,QAAO;AACxB,MAAI;AACJ,aAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AAC1C,QAAI,eAAe,IAAI,GAAG,GAAG;AAC5B,UAAI,CAAC,SAAU,YAAW,EAAE,GAAG,WAAW;AAC1C,aAAO,SAAS,GAAG;AAAA,IACpB;AAAA,EACD;AACA,SAAO,YAAY;AACpB;AAGO,SAAS,gBAAgB,QAAwB;AACvD,SAAO,UAAU,OAAO,UAAU,MAAM,SAAS;AAClD;AAEA,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;AAGM,SAAS,wBACf,OACoB;AACpB,SAAO;AAAA,IACN,MAAM,MAAM,QAAQ;AAAA,IACpB,QAAQ,MAAM;AAAA,IACd,OAAO,MAAM,SAAS,eAAe,MAAM,MAAM,KAAK;AAAA,IACtD,QAAQ,MAAM;AAAA,IACd,UAAU,MAAM;AAAA,IAChB,YAAY,MAAM;AAAA,EACnB;AACD;AAGO,SAAS,qBAAqB,IAA8B;AAClE,QAAM,EAAE,YAAY,GAAG,SAAS,IAAI;AACpC,QAAM,OAAO,EAAE,GAAG,mBAAmB,UAAU,GAAG,GAAG,SAAS;AAC9D,QAAM,EAAE,MAAM,SAAS,IAAI,cAAc,IAAI;AAC7C,SAAO,IAAI,SAAS,MAAM;AAAA,IACzB,QAAQ,WAAW,MAAM,gBAAgB,GAAG,MAAM;AAAA,IAClD,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;AAGO,SAAS,cAAc,MAAoD;AACjF,MAAI;AACH,WAAO,EAAE,MAAM,KAAK,UAAU,IAAI,GAAG,UAAU,MAAM;AAAA,EACtD,QAAQ;AACP,WAAO,EAAE,MAAM,eAAe,UAAU,KAAK;AAAA,EAC9C;AACD;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -108,15 +108,6 @@ var FALLBACK_BODY = JSON.stringify({
|
|
|
108
108
|
status: 500,
|
|
109
109
|
title: "Internal Server Error"
|
|
110
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
111
|
function normalizeProblemDetails(input) {
|
|
121
112
|
return {
|
|
122
113
|
type: input.type ?? "about:blank",
|
|
@@ -127,6 +118,24 @@ function normalizeProblemDetails(input) {
|
|
|
127
118
|
extensions: input.extensions
|
|
128
119
|
};
|
|
129
120
|
}
|
|
121
|
+
function buildProblemResponse(pd) {
|
|
122
|
+
const { extensions, ...standard } = pd;
|
|
123
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
124
|
+
const { json, fallback } = safeStringify(body);
|
|
125
|
+
return new Response(json, {
|
|
126
|
+
status: fallback ? 500 : clampHttpStatus(pd.status),
|
|
127
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
function safeStringify(body) {
|
|
131
|
+
try {
|
|
132
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
133
|
+
} catch {
|
|
134
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// src/error.ts
|
|
130
139
|
var ProblemDetailsError = class extends Error {
|
|
131
140
|
problemDetails;
|
|
132
141
|
constructor(input) {
|
|
@@ -136,13 +145,7 @@ var ProblemDetailsError = class extends Error {
|
|
|
136
145
|
this.problemDetails = pd;
|
|
137
146
|
}
|
|
138
147
|
getResponse() {
|
|
139
|
-
|
|
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
|
-
});
|
|
148
|
+
return buildProblemResponse(this.problemDetails);
|
|
146
149
|
}
|
|
147
150
|
};
|
|
148
151
|
|
|
@@ -161,25 +164,12 @@ function buildType(status, options) {
|
|
|
161
164
|
return options.defaultType ?? "about:blank";
|
|
162
165
|
}
|
|
163
166
|
function toResponse(input, c, options) {
|
|
164
|
-
let pd =
|
|
165
|
-
type: input.type ?? "about:blank",
|
|
166
|
-
status: input.status,
|
|
167
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
168
|
-
detail: input.detail,
|
|
169
|
-
instance: input.instance,
|
|
170
|
-
extensions: input.extensions
|
|
171
|
-
};
|
|
167
|
+
let pd = normalizeProblemDetails(input);
|
|
172
168
|
if (options.localize) {
|
|
173
169
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
174
170
|
}
|
|
175
|
-
const { extensions, ...rest } = pd;
|
|
176
|
-
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
177
171
|
c.set("problemDetails", pd);
|
|
178
|
-
|
|
179
|
-
return new Response(json, {
|
|
180
|
-
status: fallback ? 500 : clampHttpStatus(pd.status),
|
|
181
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
182
|
-
});
|
|
172
|
+
return buildProblemResponse(pd);
|
|
183
173
|
}
|
|
184
174
|
function problemDetailsHandler(options = {}) {
|
|
185
175
|
return (error, c) => {
|
|
@@ -220,17 +210,7 @@ function problemDetailsHandler(options = {}) {
|
|
|
220
210
|
// src/registry.ts
|
|
221
211
|
function createProblemTypeRegistry(definitions) {
|
|
222
212
|
return {
|
|
223
|
-
create: (key, options) => {
|
|
224
|
-
const def = definitions[key];
|
|
225
|
-
return new ProblemDetailsError({
|
|
226
|
-
type: def.type,
|
|
227
|
-
status: def.status,
|
|
228
|
-
title: def.title,
|
|
229
|
-
detail: options?.detail,
|
|
230
|
-
instance: options?.instance,
|
|
231
|
-
extensions: options?.extensions
|
|
232
|
-
});
|
|
233
|
-
},
|
|
213
|
+
create: (key, options) => new ProblemDetailsError({ ...definitions[key], ...options }),
|
|
234
214
|
get: (key) => definitions[key],
|
|
235
215
|
types: () => Object.keys(definitions)
|
|
236
216
|
};
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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,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,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;;;AC/DO,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 } from \"./utils.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\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","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\nimport { buildProblemResponse, normalizeProblemDetails } from \"./utils.js\";\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\treturn buildProblemResponse(this.problemDetails);\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 { buildProblemResponse, normalizeProblemDetails } from \"./utils.js\";\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = normalizeProblemDetails(input);\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tc.set(\"problemDetails\", pd);\n\n\treturn buildProblemResponse(pd);\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) => new ProblemDetailsError({ ...definitions[key], ...options }),\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAA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;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;;;ACzCO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY,IAAI,oBAAoB,EAAE,GAAG,YAAY,GAAG,GAAG,GAAG,QAAQ,CAAC;AAAA,IACrF,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,32 +1,89 @@
|
|
|
1
1
|
import {
|
|
2
2
|
PROBLEM_JSON_CONTENT_TYPE,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
} from "./chunk-
|
|
3
|
+
buildProblemResponse,
|
|
4
|
+
normalizeProblemDetails
|
|
5
|
+
} from "./chunk-X2MUB5YP.js";
|
|
6
6
|
import {
|
|
7
7
|
statusToPhrase,
|
|
8
8
|
statusToSlug
|
|
9
9
|
} from "./chunk-NFHWC5GI.js";
|
|
10
10
|
|
|
11
|
+
// src/error.ts
|
|
12
|
+
var ProblemDetailsError = class extends Error {
|
|
13
|
+
problemDetails;
|
|
14
|
+
constructor(input) {
|
|
15
|
+
const pd = normalizeProblemDetails(input);
|
|
16
|
+
super(pd.detail ?? pd.title);
|
|
17
|
+
this.name = "ProblemDetailsError";
|
|
18
|
+
this.problemDetails = pd;
|
|
19
|
+
}
|
|
20
|
+
getResponse() {
|
|
21
|
+
return buildProblemResponse(this.problemDetails);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
|
|
11
25
|
// src/factory.ts
|
|
12
26
|
function problemDetails(input) {
|
|
13
27
|
return new ProblemDetailsError(input);
|
|
14
28
|
}
|
|
15
29
|
|
|
30
|
+
// src/handler.ts
|
|
31
|
+
import { HTTPException } from "hono/http-exception";
|
|
32
|
+
function buildType(status, options) {
|
|
33
|
+
if (options.typePrefix) {
|
|
34
|
+
const slug = statusToSlug(status);
|
|
35
|
+
if (slug) return `${options.typePrefix}/${slug}`;
|
|
36
|
+
}
|
|
37
|
+
return options.defaultType ?? "about:blank";
|
|
38
|
+
}
|
|
39
|
+
function toResponse(input, c, options) {
|
|
40
|
+
let pd = normalizeProblemDetails(input);
|
|
41
|
+
if (options.localize) {
|
|
42
|
+
pd = { ...pd, ...options.localize(pd, c) };
|
|
43
|
+
}
|
|
44
|
+
c.set("problemDetails", pd);
|
|
45
|
+
return buildProblemResponse(pd);
|
|
46
|
+
}
|
|
47
|
+
function problemDetailsHandler(options = {}) {
|
|
48
|
+
return (error, c) => {
|
|
49
|
+
if (error instanceof ProblemDetailsError) {
|
|
50
|
+
return toResponse(error.problemDetails, c, options);
|
|
51
|
+
}
|
|
52
|
+
if (options.mapError) {
|
|
53
|
+
const mapped = options.mapError(error);
|
|
54
|
+
if (mapped) {
|
|
55
|
+
return toResponse(mapped, c, options);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (error instanceof HTTPException) {
|
|
59
|
+
return toResponse(
|
|
60
|
+
{
|
|
61
|
+
status: error.status,
|
|
62
|
+
type: buildType(error.status, options),
|
|
63
|
+
title: statusToPhrase(error.status),
|
|
64
|
+
detail: error.message
|
|
65
|
+
},
|
|
66
|
+
c,
|
|
67
|
+
options
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
return toResponse(
|
|
71
|
+
{
|
|
72
|
+
status: 500,
|
|
73
|
+
type: buildType(500, options),
|
|
74
|
+
title: "Internal Server Error",
|
|
75
|
+
detail: options.includeStack ? error.stack : void 0
|
|
76
|
+
},
|
|
77
|
+
c,
|
|
78
|
+
options
|
|
79
|
+
);
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
16
83
|
// src/registry.ts
|
|
17
84
|
function createProblemTypeRegistry(definitions) {
|
|
18
85
|
return {
|
|
19
|
-
create: (key, options) => {
|
|
20
|
-
const def = definitions[key];
|
|
21
|
-
return new ProblemDetailsError({
|
|
22
|
-
type: def.type,
|
|
23
|
-
status: def.status,
|
|
24
|
-
title: def.title,
|
|
25
|
-
detail: options?.detail,
|
|
26
|
-
instance: options?.instance,
|
|
27
|
-
extensions: options?.extensions
|
|
28
|
-
});
|
|
29
|
-
},
|
|
86
|
+
create: (key, options) => new ProblemDetailsError({ ...definitions[key], ...options }),
|
|
30
87
|
get: (key) => definitions[key],
|
|
31
88
|
types: () => Object.keys(definitions)
|
|
32
89
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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) => {
|
|
1
|
+
{"version":3,"sources":["../src/error.ts","../src/factory.ts","../src/handler.ts","../src/registry.ts"],"sourcesContent":["import type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\nimport { buildProblemResponse, normalizeProblemDetails } from \"./utils.js\";\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\treturn buildProblemResponse(this.problemDetails);\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 { buildProblemResponse, normalizeProblemDetails } from \"./utils.js\";\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = normalizeProblemDetails(input);\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tc.set(\"problemDetails\", pd);\n\n\treturn buildProblemResponse(pd);\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) => new ProblemDetailsError({ ...definitions[key], ...options }),\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;;;;;;AAGO,IAAM,sBAAN,cAAkC,MAAM;AAAA,EACrC;AAAA,EAET,YAAY,OAA4B;AACvC,UAAM,KAAK,wBAAwB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A,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;;;ACzCO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY,IAAI,oBAAoB,EAAE,GAAG,YAAY,GAAG,GAAG,GAAG,QAAQ,CAAC;AAAA,IACrF,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
|
@@ -24,9 +24,6 @@ __export(standard_schema_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(standard_schema_exports);
|
|
26
26
|
|
|
27
|
-
// src/handler.ts
|
|
28
|
-
var import_http_exception = require("hono/http-exception");
|
|
29
|
-
|
|
30
27
|
// src/utils.ts
|
|
31
28
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
32
29
|
var FALLBACK_BODY = JSON.stringify({
|
|
@@ -35,6 +32,21 @@ var FALLBACK_BODY = JSON.stringify({
|
|
|
35
32
|
title: "Internal Server Error"
|
|
36
33
|
});
|
|
37
34
|
|
|
35
|
+
// src/integrations/validation.ts
|
|
36
|
+
function buildValidationResponse(errors, options) {
|
|
37
|
+
const body = {
|
|
38
|
+
type: "about:blank",
|
|
39
|
+
status: 422,
|
|
40
|
+
title: options?.title ?? "Validation Error",
|
|
41
|
+
detail: options?.detail ?? "Request validation failed",
|
|
42
|
+
errors
|
|
43
|
+
};
|
|
44
|
+
return new Response(JSON.stringify(body), {
|
|
45
|
+
status: 422,
|
|
46
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
// src/integrations/standard-schema.ts
|
|
39
51
|
function formatPath(path) {
|
|
40
52
|
return path.map((segment) => typeof segment === "object" ? String(segment.key) : String(segment)).join(".");
|
|
@@ -46,19 +58,9 @@ function formatIssues(issues) {
|
|
|
46
58
|
}));
|
|
47
59
|
}
|
|
48
60
|
function standardSchemaProblemHook(options) {
|
|
49
|
-
return (result,
|
|
61
|
+
return (result, _c) => {
|
|
50
62
|
if (result.success) return;
|
|
51
|
-
|
|
52
|
-
type: "about:blank",
|
|
53
|
-
status: 422,
|
|
54
|
-
title: options?.title ?? "Validation Error",
|
|
55
|
-
detail: options?.detail ?? "Request validation failed",
|
|
56
|
-
errors: formatIssues(result.error)
|
|
57
|
-
};
|
|
58
|
-
return new Response(JSON.stringify(body), {
|
|
59
|
-
status: 422,
|
|
60
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
61
|
-
});
|
|
63
|
+
return buildValidationResponse(formatIssues(result.error), options);
|
|
62
64
|
};
|
|
63
65
|
}
|
|
64
66
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/standard-schema.ts","../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/standard-schema.ts","../../src/utils.ts","../../src/integrations/validation.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as StandardSchemaProblemHookOptions };\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.error), options);\n\t};\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;ACjBM,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;;;AFpBA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import { Context } from 'hono';
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-DqPEnoIw.cjs';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
title?: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
}
|
|
8
|
-
declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOptions): (result: {
|
|
5
|
+
declare function standardSchemaProblemHook(options?: ValidationHookOptions): (result: {
|
|
9
6
|
success: true;
|
|
10
7
|
data: unknown;
|
|
11
8
|
} | {
|
|
@@ -13,4 +10,4 @@ declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOp
|
|
|
13
10
|
error: readonly StandardSchemaV1.Issue[];
|
|
14
11
|
}, c: Context) => Response | undefined;
|
|
15
12
|
|
|
16
|
-
export {
|
|
13
|
+
export { ValidationHookOptions as StandardSchemaProblemHookOptions, standardSchemaProblemHook };
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import { Context } from 'hono';
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-DqPEnoIw.js';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
title?: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
}
|
|
8
|
-
declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOptions): (result: {
|
|
5
|
+
declare function standardSchemaProblemHook(options?: ValidationHookOptions): (result: {
|
|
9
6
|
success: true;
|
|
10
7
|
data: unknown;
|
|
11
8
|
} | {
|
|
@@ -13,4 +10,4 @@ declare function standardSchemaProblemHook(options?: StandardSchemaProblemHookOp
|
|
|
13
10
|
error: readonly StandardSchemaV1.Issue[];
|
|
14
11
|
}, c: Context) => Response | undefined;
|
|
15
12
|
|
|
16
|
-
export {
|
|
13
|
+
export { ValidationHookOptions as StandardSchemaProblemHookOptions, standardSchemaProblemHook };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-
|
|
2
|
+
buildValidationResponse
|
|
3
|
+
} from "../chunk-LMBOCQPX.js";
|
|
4
|
+
import "../chunk-X2MUB5YP.js";
|
|
4
5
|
import "../chunk-NFHWC5GI.js";
|
|
5
6
|
|
|
6
7
|
// src/integrations/standard-schema.ts
|
|
@@ -14,19 +15,9 @@ function formatIssues(issues) {
|
|
|
14
15
|
}));
|
|
15
16
|
}
|
|
16
17
|
function standardSchemaProblemHook(options) {
|
|
17
|
-
return (result,
|
|
18
|
+
return (result, _c) => {
|
|
18
19
|
if (result.success) return;
|
|
19
|
-
|
|
20
|
-
type: "about:blank",
|
|
21
|
-
status: 422,
|
|
22
|
-
title: options?.title ?? "Validation Error",
|
|
23
|
-
detail: options?.detail ?? "Request validation failed",
|
|
24
|
-
errors: formatIssues(result.error)
|
|
25
|
-
};
|
|
26
|
-
return new Response(JSON.stringify(body), {
|
|
27
|
-
status: 422,
|
|
28
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
29
|
-
});
|
|
20
|
+
return buildValidationResponse(formatIssues(result.error), options);
|
|
30
21
|
};
|
|
31
22
|
}
|
|
32
23
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as StandardSchemaProblemHookOptions };\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.error), options);\n\t};\n}\n"],"mappings":";;;;;;;AAUA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
|
|
@@ -24,9 +24,6 @@ __export(valibot_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(valibot_exports);
|
|
26
26
|
|
|
27
|
-
// src/handler.ts
|
|
28
|
-
var import_http_exception = require("hono/http-exception");
|
|
29
|
-
|
|
30
27
|
// src/utils.ts
|
|
31
28
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
32
29
|
var FALLBACK_BODY = JSON.stringify({
|
|
@@ -35,6 +32,21 @@ var FALLBACK_BODY = JSON.stringify({
|
|
|
35
32
|
title: "Internal Server Error"
|
|
36
33
|
});
|
|
37
34
|
|
|
35
|
+
// src/integrations/validation.ts
|
|
36
|
+
function buildValidationResponse(errors, options) {
|
|
37
|
+
const body = {
|
|
38
|
+
type: "about:blank",
|
|
39
|
+
status: 422,
|
|
40
|
+
title: options?.title ?? "Validation Error",
|
|
41
|
+
detail: options?.detail ?? "Request validation failed",
|
|
42
|
+
errors
|
|
43
|
+
};
|
|
44
|
+
return new Response(JSON.stringify(body), {
|
|
45
|
+
status: 422,
|
|
46
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
// src/integrations/valibot.ts
|
|
39
51
|
function formatIssues(issues) {
|
|
40
52
|
return issues.map((issue) => ({
|
|
@@ -44,19 +56,9 @@ function formatIssues(issues) {
|
|
|
44
56
|
}));
|
|
45
57
|
}
|
|
46
58
|
function valibotProblemHook(options) {
|
|
47
|
-
return (result,
|
|
59
|
+
return (result, _c) => {
|
|
48
60
|
if (result.success) return;
|
|
49
|
-
|
|
50
|
-
type: "about:blank",
|
|
51
|
-
status: 422,
|
|
52
|
-
title: options?.title ?? "Validation Error",
|
|
53
|
-
detail: options?.detail ?? "Request validation failed",
|
|
54
|
-
errors: formatIssues(result.issues)
|
|
55
|
-
};
|
|
56
|
-
return new Response(JSON.stringify(body), {
|
|
57
|
-
status: 422,
|
|
58
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
59
|
-
});
|
|
61
|
+
return buildValidationResponse(formatIssues(result.issues), options);
|
|
60
62
|
};
|
|
61
63
|
}
|
|
62
64
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/valibot.ts","../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/valibot.ts","../../src/utils.ts","../../src/integrations/validation.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ValibotProblemHookOptions };\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValidationHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.issues), options);\n\t};\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;ACjBM,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;;;AFpBA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,MAAM,GAAG,OAAO;AAAA,EACpE;AACD;","names":[]}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { SafeParseResult } from 'valibot';
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-DqPEnoIw.cjs';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
title?: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
}
|
|
8
|
-
declare function valibotProblemHook(options?: ValibotProblemHookOptions): (result: SafeParseResult<never> & {
|
|
5
|
+
declare function valibotProblemHook(options?: ValidationHookOptions): (result: SafeParseResult<never> & {
|
|
9
6
|
target: string;
|
|
10
7
|
}, c: Context) => Response | undefined;
|
|
11
8
|
|
|
12
|
-
export {
|
|
9
|
+
export { ValidationHookOptions as ValibotProblemHookOptions, valibotProblemHook };
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { SafeParseResult } from 'valibot';
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-DqPEnoIw.js';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
title?: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
}
|
|
8
|
-
declare function valibotProblemHook(options?: ValibotProblemHookOptions): (result: SafeParseResult<never> & {
|
|
5
|
+
declare function valibotProblemHook(options?: ValidationHookOptions): (result: SafeParseResult<never> & {
|
|
9
6
|
target: string;
|
|
10
7
|
}, c: Context) => Response | undefined;
|
|
11
8
|
|
|
12
|
-
export {
|
|
9
|
+
export { ValidationHookOptions as ValibotProblemHookOptions, valibotProblemHook };
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-
|
|
2
|
+
buildValidationResponse
|
|
3
|
+
} from "../chunk-LMBOCQPX.js";
|
|
4
|
+
import "../chunk-X2MUB5YP.js";
|
|
4
5
|
import "../chunk-NFHWC5GI.js";
|
|
5
6
|
|
|
6
7
|
// src/integrations/valibot.ts
|
|
@@ -12,19 +13,9 @@ function formatIssues(issues) {
|
|
|
12
13
|
}));
|
|
13
14
|
}
|
|
14
15
|
function valibotProblemHook(options) {
|
|
15
|
-
return (result,
|
|
16
|
+
return (result, _c) => {
|
|
16
17
|
if (result.success) return;
|
|
17
|
-
|
|
18
|
-
type: "about:blank",
|
|
19
|
-
status: 422,
|
|
20
|
-
title: options?.title ?? "Validation Error",
|
|
21
|
-
detail: options?.detail ?? "Request validation failed",
|
|
22
|
-
errors: formatIssues(result.issues)
|
|
23
|
-
};
|
|
24
|
-
return new Response(JSON.stringify(body), {
|
|
25
|
-
status: 422,
|
|
26
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
27
|
-
});
|
|
18
|
+
return buildValidationResponse(formatIssues(result.issues), options);
|
|
28
19
|
};
|
|
29
20
|
}
|
|
30
21
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ValibotProblemHookOptions };\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValidationHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatIssues(result.issues), options);\n\t};\n}\n"],"mappings":";;;;;;;AAUA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,MAAM,GAAG,OAAO;AAAA,EACpE;AACD;","names":[]}
|
|
@@ -24,9 +24,6 @@ __export(zod_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(zod_exports);
|
|
26
26
|
|
|
27
|
-
// src/handler.ts
|
|
28
|
-
var import_http_exception = require("hono/http-exception");
|
|
29
|
-
|
|
30
27
|
// src/utils.ts
|
|
31
28
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
32
29
|
var FALLBACK_BODY = JSON.stringify({
|
|
@@ -35,6 +32,21 @@ var FALLBACK_BODY = JSON.stringify({
|
|
|
35
32
|
title: "Internal Server Error"
|
|
36
33
|
});
|
|
37
34
|
|
|
35
|
+
// src/integrations/validation.ts
|
|
36
|
+
function buildValidationResponse(errors, options) {
|
|
37
|
+
const body = {
|
|
38
|
+
type: "about:blank",
|
|
39
|
+
status: 422,
|
|
40
|
+
title: options?.title ?? "Validation Error",
|
|
41
|
+
detail: options?.detail ?? "Request validation failed",
|
|
42
|
+
errors
|
|
43
|
+
};
|
|
44
|
+
return new Response(JSON.stringify(body), {
|
|
45
|
+
status: 422,
|
|
46
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
// src/integrations/zod.ts
|
|
39
51
|
function formatErrors(zodError) {
|
|
40
52
|
return zodError.issues.map((issue) => ({
|
|
@@ -44,19 +56,9 @@ function formatErrors(zodError) {
|
|
|
44
56
|
}));
|
|
45
57
|
}
|
|
46
58
|
function zodProblemHook(options) {
|
|
47
|
-
return (result,
|
|
59
|
+
return (result, _c) => {
|
|
48
60
|
if (result.success) return;
|
|
49
|
-
|
|
50
|
-
type: "about:blank",
|
|
51
|
-
status: 422,
|
|
52
|
-
title: options?.title ?? "Validation Error",
|
|
53
|
-
detail: options?.detail ?? "Request validation failed",
|
|
54
|
-
errors: formatErrors(result.error)
|
|
55
|
-
};
|
|
56
|
-
return new Response(JSON.stringify(body), {
|
|
57
|
-
status: 422,
|
|
58
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
59
|
-
});
|
|
61
|
+
return buildValidationResponse(formatErrors(result.error), options);
|
|
60
62
|
};
|
|
61
63
|
}
|
|
62
64
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/zod.ts","../../src/
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/zod.ts","../../src/utils.ts","../../src/integrations/validation.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ZodProblemHookOptions };\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatErrors(result.error), options);\n\t};\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\n/** Clamp HTTP status to 200-599 range; returns 500 for out-of-range values */\nexport function clampHttpStatus(status: number): number {\n\treturn status >= 200 && status <= 599 ? status : 500;\n}\n\nconst FALLBACK_BODY = JSON.stringify({\n\ttype: \"about:blank\",\n\tstatus: 500,\n\ttitle: \"Internal Server Error\",\n});\n\n/** Normalize ProblemDetailsInput to ProblemDetails with defaults for type and title */\nexport function normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\n/** Build a RFC 9457 Problem Details Response from a ProblemDetails object */\nexport function buildProblemResponse(pd: ProblemDetails): Response {\n\tconst { extensions, ...standard } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\tconst { json, fallback } = safeStringify(body);\n\treturn new Response(json, {\n\t\tstatus: fallback ? 500 : clampHttpStatus(pd.status),\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\n/** JSON.stringify with fallback for non-serializable values (circular refs, BigInt) */\nexport function safeStringify(body: unknown): { json: string; fallback: boolean } {\n\ttry {\n\t\treturn { json: JSON.stringify(body), fallback: false };\n\t} catch {\n\t\treturn { json: FALLBACK_BODY, fallback: true };\n\t}\n}\n","import { PROBLEM_JSON_CONTENT_TYPE } from \"../utils.js\";\n\nexport interface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode?: string;\n}\n\nexport interface ValidationHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\n/** Build a RFC 9457 validation error response from formatted errors */\nexport function buildValidationResponse(\n\terrors: ValidationError[],\n\toptions?: ValidationHookOptions,\n): Response {\n\tconst body = {\n\t\ttype: \"about:blank\",\n\t\tstatus: 422,\n\t\ttitle: options?.title ?? \"Validation Error\",\n\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\terrors,\n\t};\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: 422,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,4BAA4B;AAwBzC,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;;;ACjBM,SAAS,wBACf,QACA,SACW;AACX,QAAM,OAAO;AAAA,IACZ,MAAM;AAAA,IACN,QAAQ;AAAA,IACR,OAAO,SAAS,SAAS;AAAA,IACzB,QAAQ,SAAS,UAAU;AAAA,IAC3B;AAAA,EACD;AAEA,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ;AAAA,IACR,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,EACtD,CAAC;AACF;;;AFpBA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { ZodError } from 'zod';
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-DqPEnoIw.cjs';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
title?: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
}
|
|
8
|
-
declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
|
|
5
|
+
declare function zodProblemHook(options?: ValidationHookOptions): (result: {
|
|
9
6
|
success: true;
|
|
10
7
|
data: unknown;
|
|
11
8
|
} | {
|
|
@@ -13,4 +10,4 @@ declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
|
|
|
13
10
|
error: ZodError;
|
|
14
11
|
}, c: Context) => Response | undefined;
|
|
15
12
|
|
|
16
|
-
export {
|
|
13
|
+
export { ValidationHookOptions as ZodProblemHookOptions, zodProblemHook };
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { ZodError } from 'zod';
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-DqPEnoIw.js';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
title?: string;
|
|
6
|
-
detail?: string;
|
|
7
|
-
}
|
|
8
|
-
declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
|
|
5
|
+
declare function zodProblemHook(options?: ValidationHookOptions): (result: {
|
|
9
6
|
success: true;
|
|
10
7
|
data: unknown;
|
|
11
8
|
} | {
|
|
@@ -13,4 +10,4 @@ declare function zodProblemHook(options?: ZodProblemHookOptions): (result: {
|
|
|
13
10
|
error: ZodError;
|
|
14
11
|
}, c: Context) => Response | undefined;
|
|
15
12
|
|
|
16
|
-
export {
|
|
13
|
+
export { ValidationHookOptions as ZodProblemHookOptions, zodProblemHook };
|
package/dist/integrations/zod.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
} from "../chunk-
|
|
2
|
+
buildValidationResponse
|
|
3
|
+
} from "../chunk-LMBOCQPX.js";
|
|
4
|
+
import "../chunk-X2MUB5YP.js";
|
|
4
5
|
import "../chunk-NFHWC5GI.js";
|
|
5
6
|
|
|
6
7
|
// src/integrations/zod.ts
|
|
@@ -12,19 +13,9 @@ function formatErrors(zodError) {
|
|
|
12
13
|
}));
|
|
13
14
|
}
|
|
14
15
|
function zodProblemHook(options) {
|
|
15
|
-
return (result,
|
|
16
|
+
return (result, _c) => {
|
|
16
17
|
if (result.success) return;
|
|
17
|
-
|
|
18
|
-
type: "about:blank",
|
|
19
|
-
status: 422,
|
|
20
|
-
title: options?.title ?? "Validation Error",
|
|
21
|
-
detail: options?.detail ?? "Request validation failed",
|
|
22
|
-
errors: formatErrors(result.error)
|
|
23
|
-
};
|
|
24
|
-
return new Response(JSON.stringify(body), {
|
|
25
|
-
status: 422,
|
|
26
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
27
|
-
});
|
|
18
|
+
return buildValidationResponse(formatErrors(result.error), options);
|
|
28
19
|
};
|
|
29
20
|
}
|
|
30
21
|
export {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport {
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\nimport {\n\ttype ValidationError,\n\ttype ValidationHookOptions,\n\tbuildValidationResponse,\n} from \"./validation.js\";\n\nexport type { ValidationHookOptions as ZodProblemHookOptions };\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ValidationHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, _c) => {\n\t\tif (result.success) return;\n\t\treturn buildValidationResponse(formatErrors(result.error), options);\n\t};\n}\n"],"mappings":";;;;;;;AAUA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,OAAO;AACtB,QAAI,OAAO,QAAS;AACpB,WAAO,wBAAwB,aAAa,OAAO,KAAK,GAAG,OAAO;AAAA,EACnE;AACD;","names":[]}
|
package/package.json
CHANGED
package/dist/chunk-X2E2GZLL.js
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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;AAC1C,aAAO,SAAS,GAAG;AAAA,IACpB;AAAA,EACD;AACA,SAAO,YAAY;AACpB;AAGO,SAAS,gBAAgB,QAAwB;AACvD,SAAO,UAAU,OAAO,UAAU,MAAM,SAAS;AAClD;AAEA,IAAM,gBAAgB,KAAK,UAAU;AAAA,EACpC,MAAM;AAAA,EACN,QAAQ;AAAA,EACR,OAAO;AACR,CAAC;AAGM,SAAS,cAAc,MAAoD;AACjF,MAAI;AACH,WAAO,EAAE,MAAM,KAAK,UAAU,IAAI,GAAG,UAAU,MAAM;AAAA,EACtD,QAAQ;AACP,WAAO,EAAE,MAAM,eAAe,UAAU,KAAK;AAAA,EAC9C;AACD;;;AC5BA,SAAS,wBACR,OACoB;AACpB,SAAO;AAAA,IACN,MAAM,MAAM,QAAQ;AAAA,IACpB,QAAQ,MAAM;AAAA,IACd,OAAO,MAAM,SAAS,eAAe,MAAM,MAAM,KAAK;AAAA,IACtD,QAAQ,MAAM;AAAA,IACd,UAAU,MAAM;AAAA,IAChB,YAAY,MAAM;AAAA,EACnB;AACD;AAEO,IAAM,sBAA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