hono-problem-details 0.10.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +76 -3
- package/dist/chunk-LIQQKPXQ.js +34 -0
- package/dist/{chunk-BBJKRRB3.js → chunk-R6NMRTVW.js} +18 -4
- package/dist/{chunk-VPS467Z5.js → chunk-URAOTABQ.js} +1 -0
- package/dist/index.cjs +40 -11
- package/dist/index.d.cts +11 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.js +44 -38
- package/dist/integrations/openapi-json-schema.cjs +127 -0
- package/dist/integrations/openapi-json-schema.d.cts +40 -0
- package/dist/integrations/openapi-json-schema.d.ts +40 -0
- package/dist/integrations/openapi-json-schema.js +45 -0
- package/dist/integrations/standard-schema.cjs +135 -3
- package/dist/integrations/standard-schema.d.cts +1 -1
- package/dist/integrations/standard-schema.d.ts +1 -1
- package/dist/integrations/standard-schema.js +3 -2
- package/dist/integrations/valibot.cjs +135 -3
- package/dist/integrations/valibot.d.cts +1 -1
- package/dist/integrations/valibot.d.ts +1 -1
- package/dist/integrations/valibot.js +3 -2
- package/dist/integrations/zod.cjs +135 -3
- package/dist/integrations/zod.d.cts +1 -1
- package/dist/integrations/zod.d.ts +1 -1
- package/dist/integrations/zod.js +3 -2
- package/dist/validation-CZHjSmb_.d.cts +14 -0
- package/dist/validation-CZHjSmb_.d.ts +14 -0
- package/package.json +22 -7
- package/dist/validation-DqPEnoIw.d.cts +0 -6
- package/dist/validation-DqPEnoIw.d.ts +0 -6
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DANGEROUS_KEYS
|
|
3
|
+
} from "../chunk-URAOTABQ.js";
|
|
4
|
+
import {
|
|
5
|
+
statusToPhrase
|
|
6
|
+
} from "../chunk-P4D7EVU3.js";
|
|
7
|
+
|
|
8
|
+
// src/integrations/openapi-json-schema.ts
|
|
9
|
+
var STANDARD_FIELD_KEYS = /* @__PURE__ */ new Set(["type", "status", "title", "detail", "instance"]);
|
|
10
|
+
function problemDetailsJsonSchema(options) {
|
|
11
|
+
const properties = {
|
|
12
|
+
type: { type: "string", description: "Problem type URI", examples: ["about:blank"] },
|
|
13
|
+
status: { type: "integer", description: "HTTP status code", examples: [400] },
|
|
14
|
+
title: {
|
|
15
|
+
type: "string",
|
|
16
|
+
description: "Short summary of the problem type",
|
|
17
|
+
examples: ["Bad Request"]
|
|
18
|
+
},
|
|
19
|
+
detail: { type: "string", description: "Human-readable explanation" },
|
|
20
|
+
instance: { type: "string", description: "URI identifying the occurrence" }
|
|
21
|
+
};
|
|
22
|
+
for (const [key, schema] of Object.entries(options?.extensions ?? {})) {
|
|
23
|
+
if (!STANDARD_FIELD_KEYS.has(key) && !DANGEROUS_KEYS.has(key)) {
|
|
24
|
+
properties[key] = structuredClone(schema);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return {
|
|
28
|
+
title: "ProblemDetails",
|
|
29
|
+
type: "object",
|
|
30
|
+
properties,
|
|
31
|
+
required: ["type", "status", "title"]
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function problemDetailsResponseJsonSchema(status, description, schema) {
|
|
35
|
+
return {
|
|
36
|
+
content: {
|
|
37
|
+
"application/problem+json": { schema: schema ?? problemDetailsJsonSchema() }
|
|
38
|
+
},
|
|
39
|
+
description: description ?? statusToPhrase(status) ?? "Error"
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
problemDetailsJsonSchema,
|
|
44
|
+
problemDetailsResponseJsonSchema
|
|
45
|
+
};
|
|
@@ -24,13 +24,134 @@ __export(standard_schema_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(standard_schema_exports);
|
|
26
26
|
|
|
27
|
+
// src/status.ts
|
|
28
|
+
var STATUS_PHRASES = {
|
|
29
|
+
400: "Bad Request",
|
|
30
|
+
401: "Unauthorized",
|
|
31
|
+
402: "Payment Required",
|
|
32
|
+
403: "Forbidden",
|
|
33
|
+
404: "Not Found",
|
|
34
|
+
405: "Method Not Allowed",
|
|
35
|
+
406: "Not Acceptable",
|
|
36
|
+
407: "Proxy Authentication Required",
|
|
37
|
+
408: "Request Timeout",
|
|
38
|
+
409: "Conflict",
|
|
39
|
+
410: "Gone",
|
|
40
|
+
411: "Length Required",
|
|
41
|
+
412: "Precondition Failed",
|
|
42
|
+
413: "Content Too Large",
|
|
43
|
+
414: "URI Too Long",
|
|
44
|
+
415: "Unsupported Media Type",
|
|
45
|
+
416: "Range Not Satisfiable",
|
|
46
|
+
417: "Expectation Failed",
|
|
47
|
+
418: "I'm a Teapot",
|
|
48
|
+
421: "Misdirected Request",
|
|
49
|
+
422: "Unprocessable Content",
|
|
50
|
+
423: "Locked",
|
|
51
|
+
424: "Failed Dependency",
|
|
52
|
+
425: "Too Early",
|
|
53
|
+
426: "Upgrade Required",
|
|
54
|
+
428: "Precondition Required",
|
|
55
|
+
429: "Too Many Requests",
|
|
56
|
+
431: "Request Header Fields Too Large",
|
|
57
|
+
451: "Unavailable For Legal Reasons",
|
|
58
|
+
500: "Internal Server Error",
|
|
59
|
+
501: "Not Implemented",
|
|
60
|
+
502: "Bad Gateway",
|
|
61
|
+
503: "Service Unavailable",
|
|
62
|
+
504: "Gateway Timeout",
|
|
63
|
+
505: "HTTP Version Not Supported",
|
|
64
|
+
506: "Variant Also Negotiates",
|
|
65
|
+
507: "Insufficient Storage",
|
|
66
|
+
508: "Loop Detected",
|
|
67
|
+
510: "Not Extended",
|
|
68
|
+
511: "Network Authentication Required"
|
|
69
|
+
};
|
|
70
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
71
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
72
|
+
Number(code),
|
|
73
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
74
|
+
])
|
|
75
|
+
);
|
|
76
|
+
function statusToPhrase(status) {
|
|
77
|
+
return STATUS_PHRASES[status];
|
|
78
|
+
}
|
|
79
|
+
|
|
27
80
|
// src/utils.ts
|
|
28
81
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
82
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
83
|
+
function sanitizeExtensions(extensions) {
|
|
84
|
+
if (!extensions) return extensions;
|
|
85
|
+
let filtered;
|
|
86
|
+
for (const key of Object.keys(extensions)) {
|
|
87
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
88
|
+
if (!filtered) filtered = { ...extensions };
|
|
89
|
+
delete filtered[key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return filtered ?? extensions;
|
|
93
|
+
}
|
|
94
|
+
function clampHttpStatus(status) {
|
|
95
|
+
return Number.isInteger(status) && status >= 200 && status <= 599 ? status : 500;
|
|
96
|
+
}
|
|
29
97
|
var FALLBACK_BODY = JSON.stringify({
|
|
30
98
|
type: "about:blank",
|
|
31
99
|
status: 500,
|
|
32
100
|
title: "Internal Server Error"
|
|
33
101
|
});
|
|
102
|
+
function normalizeProblemDetails(input) {
|
|
103
|
+
return {
|
|
104
|
+
type: input.type ?? "about:blank",
|
|
105
|
+
status: input.status,
|
|
106
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
107
|
+
detail: input.detail,
|
|
108
|
+
instance: input.instance,
|
|
109
|
+
extensions: input.extensions
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function buildProblemResponse(pd) {
|
|
113
|
+
const { extensions, ...standard } = pd;
|
|
114
|
+
const status = clampHttpStatus(pd.status);
|
|
115
|
+
const body = { ...sanitizeExtensions(extensions), ...standard, status };
|
|
116
|
+
const { json, fallback } = safeStringify(body);
|
|
117
|
+
return new Response(json, {
|
|
118
|
+
status: fallback ? 500 : status,
|
|
119
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function safeStringify(body) {
|
|
123
|
+
try {
|
|
124
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
125
|
+
} catch {
|
|
126
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/error.ts
|
|
131
|
+
var ProblemDetailsError = class extends Error {
|
|
132
|
+
/** The normalized RFC 9457 Problem Details object. */
|
|
133
|
+
problemDetails;
|
|
134
|
+
/**
|
|
135
|
+
* Whether the input carried an explicit `type`. Lets the handler apply
|
|
136
|
+
* `typePrefix`/`defaultType` only when the thrower left `type` unset.
|
|
137
|
+
*/
|
|
138
|
+
hasExplicitType;
|
|
139
|
+
/**
|
|
140
|
+
* @param input - Missing `type` defaults to `"about:blank"`;
|
|
141
|
+
* missing `title` is derived from the HTTP status code.
|
|
142
|
+
*/
|
|
143
|
+
constructor(input) {
|
|
144
|
+
const pd = normalizeProblemDetails(input);
|
|
145
|
+
super(pd.detail ?? pd.title);
|
|
146
|
+
this.name = "ProblemDetailsError";
|
|
147
|
+
this.problemDetails = pd;
|
|
148
|
+
this.hasExplicitType = input.type !== void 0;
|
|
149
|
+
}
|
|
150
|
+
/** Build a standalone `application/problem+json` Response without the handler middleware. */
|
|
151
|
+
getResponse() {
|
|
152
|
+
return buildProblemResponse(this.problemDetails);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
34
155
|
|
|
35
156
|
// src/integrations/validation.ts
|
|
36
157
|
var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
|
|
@@ -45,12 +166,23 @@ function sanitizeError(error) {
|
|
|
45
166
|
};
|
|
46
167
|
}
|
|
47
168
|
function buildValidationResponse(errors, options) {
|
|
169
|
+
const title = options?.title ?? "Validation Error";
|
|
170
|
+
const detail = options?.detail ?? "Request validation failed";
|
|
171
|
+
const sanitizedErrors = errors.map(sanitizeError);
|
|
172
|
+
if (options?.delegateToHandler) {
|
|
173
|
+
throw new ProblemDetailsError({
|
|
174
|
+
status: 422,
|
|
175
|
+
title,
|
|
176
|
+
detail,
|
|
177
|
+
extensions: { errors: sanitizedErrors }
|
|
178
|
+
});
|
|
179
|
+
}
|
|
48
180
|
const body = {
|
|
49
181
|
type: "about:blank",
|
|
50
182
|
status: 422,
|
|
51
|
-
title
|
|
52
|
-
detail
|
|
53
|
-
errors:
|
|
183
|
+
title,
|
|
184
|
+
detail,
|
|
185
|
+
errors: sanitizedErrors
|
|
54
186
|
};
|
|
55
187
|
return new Response(JSON.stringify(body), {
|
|
56
188
|
status: 422,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import { Context } from 'hono';
|
|
3
|
-
import { V as ValidationHookOptions } from '../validation-
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-CZHjSmb_.cjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a `@hono/standard-validator` hook that returns RFC 9457 Problem Details
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import { Context } from 'hono';
|
|
3
|
-
import { V as ValidationHookOptions } from '../validation-
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-CZHjSmb_.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a `@hono/standard-validator` hook that returns RFC 9457 Problem Details
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildValidationResponse
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-R6NMRTVW.js";
|
|
4
|
+
import "../chunk-LIQQKPXQ.js";
|
|
5
|
+
import "../chunk-URAOTABQ.js";
|
|
5
6
|
import "../chunk-P4D7EVU3.js";
|
|
6
7
|
|
|
7
8
|
// src/integrations/standard-schema.ts
|
|
@@ -24,13 +24,134 @@ __export(valibot_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(valibot_exports);
|
|
26
26
|
|
|
27
|
+
// src/status.ts
|
|
28
|
+
var STATUS_PHRASES = {
|
|
29
|
+
400: "Bad Request",
|
|
30
|
+
401: "Unauthorized",
|
|
31
|
+
402: "Payment Required",
|
|
32
|
+
403: "Forbidden",
|
|
33
|
+
404: "Not Found",
|
|
34
|
+
405: "Method Not Allowed",
|
|
35
|
+
406: "Not Acceptable",
|
|
36
|
+
407: "Proxy Authentication Required",
|
|
37
|
+
408: "Request Timeout",
|
|
38
|
+
409: "Conflict",
|
|
39
|
+
410: "Gone",
|
|
40
|
+
411: "Length Required",
|
|
41
|
+
412: "Precondition Failed",
|
|
42
|
+
413: "Content Too Large",
|
|
43
|
+
414: "URI Too Long",
|
|
44
|
+
415: "Unsupported Media Type",
|
|
45
|
+
416: "Range Not Satisfiable",
|
|
46
|
+
417: "Expectation Failed",
|
|
47
|
+
418: "I'm a Teapot",
|
|
48
|
+
421: "Misdirected Request",
|
|
49
|
+
422: "Unprocessable Content",
|
|
50
|
+
423: "Locked",
|
|
51
|
+
424: "Failed Dependency",
|
|
52
|
+
425: "Too Early",
|
|
53
|
+
426: "Upgrade Required",
|
|
54
|
+
428: "Precondition Required",
|
|
55
|
+
429: "Too Many Requests",
|
|
56
|
+
431: "Request Header Fields Too Large",
|
|
57
|
+
451: "Unavailable For Legal Reasons",
|
|
58
|
+
500: "Internal Server Error",
|
|
59
|
+
501: "Not Implemented",
|
|
60
|
+
502: "Bad Gateway",
|
|
61
|
+
503: "Service Unavailable",
|
|
62
|
+
504: "Gateway Timeout",
|
|
63
|
+
505: "HTTP Version Not Supported",
|
|
64
|
+
506: "Variant Also Negotiates",
|
|
65
|
+
507: "Insufficient Storage",
|
|
66
|
+
508: "Loop Detected",
|
|
67
|
+
510: "Not Extended",
|
|
68
|
+
511: "Network Authentication Required"
|
|
69
|
+
};
|
|
70
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
71
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
72
|
+
Number(code),
|
|
73
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
74
|
+
])
|
|
75
|
+
);
|
|
76
|
+
function statusToPhrase(status) {
|
|
77
|
+
return STATUS_PHRASES[status];
|
|
78
|
+
}
|
|
79
|
+
|
|
27
80
|
// src/utils.ts
|
|
28
81
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
82
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
83
|
+
function sanitizeExtensions(extensions) {
|
|
84
|
+
if (!extensions) return extensions;
|
|
85
|
+
let filtered;
|
|
86
|
+
for (const key of Object.keys(extensions)) {
|
|
87
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
88
|
+
if (!filtered) filtered = { ...extensions };
|
|
89
|
+
delete filtered[key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return filtered ?? extensions;
|
|
93
|
+
}
|
|
94
|
+
function clampHttpStatus(status) {
|
|
95
|
+
return Number.isInteger(status) && status >= 200 && status <= 599 ? status : 500;
|
|
96
|
+
}
|
|
29
97
|
var FALLBACK_BODY = JSON.stringify({
|
|
30
98
|
type: "about:blank",
|
|
31
99
|
status: 500,
|
|
32
100
|
title: "Internal Server Error"
|
|
33
101
|
});
|
|
102
|
+
function normalizeProblemDetails(input) {
|
|
103
|
+
return {
|
|
104
|
+
type: input.type ?? "about:blank",
|
|
105
|
+
status: input.status,
|
|
106
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
107
|
+
detail: input.detail,
|
|
108
|
+
instance: input.instance,
|
|
109
|
+
extensions: input.extensions
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function buildProblemResponse(pd) {
|
|
113
|
+
const { extensions, ...standard } = pd;
|
|
114
|
+
const status = clampHttpStatus(pd.status);
|
|
115
|
+
const body = { ...sanitizeExtensions(extensions), ...standard, status };
|
|
116
|
+
const { json, fallback } = safeStringify(body);
|
|
117
|
+
return new Response(json, {
|
|
118
|
+
status: fallback ? 500 : status,
|
|
119
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function safeStringify(body) {
|
|
123
|
+
try {
|
|
124
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
125
|
+
} catch {
|
|
126
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/error.ts
|
|
131
|
+
var ProblemDetailsError = class extends Error {
|
|
132
|
+
/** The normalized RFC 9457 Problem Details object. */
|
|
133
|
+
problemDetails;
|
|
134
|
+
/**
|
|
135
|
+
* Whether the input carried an explicit `type`. Lets the handler apply
|
|
136
|
+
* `typePrefix`/`defaultType` only when the thrower left `type` unset.
|
|
137
|
+
*/
|
|
138
|
+
hasExplicitType;
|
|
139
|
+
/**
|
|
140
|
+
* @param input - Missing `type` defaults to `"about:blank"`;
|
|
141
|
+
* missing `title` is derived from the HTTP status code.
|
|
142
|
+
*/
|
|
143
|
+
constructor(input) {
|
|
144
|
+
const pd = normalizeProblemDetails(input);
|
|
145
|
+
super(pd.detail ?? pd.title);
|
|
146
|
+
this.name = "ProblemDetailsError";
|
|
147
|
+
this.problemDetails = pd;
|
|
148
|
+
this.hasExplicitType = input.type !== void 0;
|
|
149
|
+
}
|
|
150
|
+
/** Build a standalone `application/problem+json` Response without the handler middleware. */
|
|
151
|
+
getResponse() {
|
|
152
|
+
return buildProblemResponse(this.problemDetails);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
34
155
|
|
|
35
156
|
// src/integrations/validation.ts
|
|
36
157
|
var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
|
|
@@ -45,12 +166,23 @@ function sanitizeError(error) {
|
|
|
45
166
|
};
|
|
46
167
|
}
|
|
47
168
|
function buildValidationResponse(errors, options) {
|
|
169
|
+
const title = options?.title ?? "Validation Error";
|
|
170
|
+
const detail = options?.detail ?? "Request validation failed";
|
|
171
|
+
const sanitizedErrors = errors.map(sanitizeError);
|
|
172
|
+
if (options?.delegateToHandler) {
|
|
173
|
+
throw new ProblemDetailsError({
|
|
174
|
+
status: 422,
|
|
175
|
+
title,
|
|
176
|
+
detail,
|
|
177
|
+
extensions: { errors: sanitizedErrors }
|
|
178
|
+
});
|
|
179
|
+
}
|
|
48
180
|
const body = {
|
|
49
181
|
type: "about:blank",
|
|
50
182
|
status: 422,
|
|
51
|
-
title
|
|
52
|
-
detail
|
|
53
|
-
errors:
|
|
183
|
+
title,
|
|
184
|
+
detail,
|
|
185
|
+
errors: sanitizedErrors
|
|
54
186
|
};
|
|
55
187
|
return new Response(JSON.stringify(body), {
|
|
56
188
|
status: 422,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { GenericSchema, GenericSchemaAsync, SafeParseResult } from 'valibot';
|
|
3
|
-
import { V as ValidationHookOptions } from '../validation-
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-CZHjSmb_.cjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a `@hono/valibot-validator` hook that returns RFC 9457 Problem Details
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { GenericSchema, GenericSchemaAsync, SafeParseResult } from 'valibot';
|
|
3
|
-
import { V as ValidationHookOptions } from '../validation-
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-CZHjSmb_.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a `@hono/valibot-validator` hook that returns RFC 9457 Problem Details
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildValidationResponse
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-R6NMRTVW.js";
|
|
4
|
+
import "../chunk-LIQQKPXQ.js";
|
|
5
|
+
import "../chunk-URAOTABQ.js";
|
|
5
6
|
import "../chunk-P4D7EVU3.js";
|
|
6
7
|
|
|
7
8
|
// src/integrations/valibot.ts
|
|
@@ -24,13 +24,134 @@ __export(zod_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(zod_exports);
|
|
26
26
|
|
|
27
|
+
// src/status.ts
|
|
28
|
+
var STATUS_PHRASES = {
|
|
29
|
+
400: "Bad Request",
|
|
30
|
+
401: "Unauthorized",
|
|
31
|
+
402: "Payment Required",
|
|
32
|
+
403: "Forbidden",
|
|
33
|
+
404: "Not Found",
|
|
34
|
+
405: "Method Not Allowed",
|
|
35
|
+
406: "Not Acceptable",
|
|
36
|
+
407: "Proxy Authentication Required",
|
|
37
|
+
408: "Request Timeout",
|
|
38
|
+
409: "Conflict",
|
|
39
|
+
410: "Gone",
|
|
40
|
+
411: "Length Required",
|
|
41
|
+
412: "Precondition Failed",
|
|
42
|
+
413: "Content Too Large",
|
|
43
|
+
414: "URI Too Long",
|
|
44
|
+
415: "Unsupported Media Type",
|
|
45
|
+
416: "Range Not Satisfiable",
|
|
46
|
+
417: "Expectation Failed",
|
|
47
|
+
418: "I'm a Teapot",
|
|
48
|
+
421: "Misdirected Request",
|
|
49
|
+
422: "Unprocessable Content",
|
|
50
|
+
423: "Locked",
|
|
51
|
+
424: "Failed Dependency",
|
|
52
|
+
425: "Too Early",
|
|
53
|
+
426: "Upgrade Required",
|
|
54
|
+
428: "Precondition Required",
|
|
55
|
+
429: "Too Many Requests",
|
|
56
|
+
431: "Request Header Fields Too Large",
|
|
57
|
+
451: "Unavailable For Legal Reasons",
|
|
58
|
+
500: "Internal Server Error",
|
|
59
|
+
501: "Not Implemented",
|
|
60
|
+
502: "Bad Gateway",
|
|
61
|
+
503: "Service Unavailable",
|
|
62
|
+
504: "Gateway Timeout",
|
|
63
|
+
505: "HTTP Version Not Supported",
|
|
64
|
+
506: "Variant Also Negotiates",
|
|
65
|
+
507: "Insufficient Storage",
|
|
66
|
+
508: "Loop Detected",
|
|
67
|
+
510: "Not Extended",
|
|
68
|
+
511: "Network Authentication Required"
|
|
69
|
+
};
|
|
70
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
71
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
72
|
+
Number(code),
|
|
73
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
74
|
+
])
|
|
75
|
+
);
|
|
76
|
+
function statusToPhrase(status) {
|
|
77
|
+
return STATUS_PHRASES[status];
|
|
78
|
+
}
|
|
79
|
+
|
|
27
80
|
// src/utils.ts
|
|
28
81
|
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
82
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
83
|
+
function sanitizeExtensions(extensions) {
|
|
84
|
+
if (!extensions) return extensions;
|
|
85
|
+
let filtered;
|
|
86
|
+
for (const key of Object.keys(extensions)) {
|
|
87
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
88
|
+
if (!filtered) filtered = { ...extensions };
|
|
89
|
+
delete filtered[key];
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return filtered ?? extensions;
|
|
93
|
+
}
|
|
94
|
+
function clampHttpStatus(status) {
|
|
95
|
+
return Number.isInteger(status) && status >= 200 && status <= 599 ? status : 500;
|
|
96
|
+
}
|
|
29
97
|
var FALLBACK_BODY = JSON.stringify({
|
|
30
98
|
type: "about:blank",
|
|
31
99
|
status: 500,
|
|
32
100
|
title: "Internal Server Error"
|
|
33
101
|
});
|
|
102
|
+
function normalizeProblemDetails(input) {
|
|
103
|
+
return {
|
|
104
|
+
type: input.type ?? "about:blank",
|
|
105
|
+
status: input.status,
|
|
106
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
107
|
+
detail: input.detail,
|
|
108
|
+
instance: input.instance,
|
|
109
|
+
extensions: input.extensions
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
function buildProblemResponse(pd) {
|
|
113
|
+
const { extensions, ...standard } = pd;
|
|
114
|
+
const status = clampHttpStatus(pd.status);
|
|
115
|
+
const body = { ...sanitizeExtensions(extensions), ...standard, status };
|
|
116
|
+
const { json, fallback } = safeStringify(body);
|
|
117
|
+
return new Response(json, {
|
|
118
|
+
status: fallback ? 500 : status,
|
|
119
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
function safeStringify(body) {
|
|
123
|
+
try {
|
|
124
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
125
|
+
} catch {
|
|
126
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/error.ts
|
|
131
|
+
var ProblemDetailsError = class extends Error {
|
|
132
|
+
/** The normalized RFC 9457 Problem Details object. */
|
|
133
|
+
problemDetails;
|
|
134
|
+
/**
|
|
135
|
+
* Whether the input carried an explicit `type`. Lets the handler apply
|
|
136
|
+
* `typePrefix`/`defaultType` only when the thrower left `type` unset.
|
|
137
|
+
*/
|
|
138
|
+
hasExplicitType;
|
|
139
|
+
/**
|
|
140
|
+
* @param input - Missing `type` defaults to `"about:blank"`;
|
|
141
|
+
* missing `title` is derived from the HTTP status code.
|
|
142
|
+
*/
|
|
143
|
+
constructor(input) {
|
|
144
|
+
const pd = normalizeProblemDetails(input);
|
|
145
|
+
super(pd.detail ?? pd.title);
|
|
146
|
+
this.name = "ProblemDetailsError";
|
|
147
|
+
this.problemDetails = pd;
|
|
148
|
+
this.hasExplicitType = input.type !== void 0;
|
|
149
|
+
}
|
|
150
|
+
/** Build a standalone `application/problem+json` Response without the handler middleware. */
|
|
151
|
+
getResponse() {
|
|
152
|
+
return buildProblemResponse(this.problemDetails);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
34
155
|
|
|
35
156
|
// src/integrations/validation.ts
|
|
36
157
|
var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
|
|
@@ -45,12 +166,23 @@ function sanitizeError(error) {
|
|
|
45
166
|
};
|
|
46
167
|
}
|
|
47
168
|
function buildValidationResponse(errors, options) {
|
|
169
|
+
const title = options?.title ?? "Validation Error";
|
|
170
|
+
const detail = options?.detail ?? "Request validation failed";
|
|
171
|
+
const sanitizedErrors = errors.map(sanitizeError);
|
|
172
|
+
if (options?.delegateToHandler) {
|
|
173
|
+
throw new ProblemDetailsError({
|
|
174
|
+
status: 422,
|
|
175
|
+
title,
|
|
176
|
+
detail,
|
|
177
|
+
extensions: { errors: sanitizedErrors }
|
|
178
|
+
});
|
|
179
|
+
}
|
|
48
180
|
const body = {
|
|
49
181
|
type: "about:blank",
|
|
50
182
|
status: 422,
|
|
51
|
-
title
|
|
52
|
-
detail
|
|
53
|
-
errors:
|
|
183
|
+
title,
|
|
184
|
+
detail,
|
|
185
|
+
errors: sanitizedErrors
|
|
54
186
|
};
|
|
55
187
|
return new Response(JSON.stringify(body), {
|
|
56
188
|
status: 422,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { ZodError } from 'zod';
|
|
3
|
-
import { V as ValidationHookOptions } from '../validation-
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-CZHjSmb_.cjs';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a `@hono/zod-validator` hook that returns RFC 9457 Problem Details
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Context } from 'hono';
|
|
2
2
|
import { ZodError } from 'zod';
|
|
3
|
-
import { V as ValidationHookOptions } from '../validation-
|
|
3
|
+
import { V as ValidationHookOptions } from '../validation-CZHjSmb_.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Create a `@hono/zod-validator` hook that returns RFC 9457 Problem Details
|
package/dist/integrations/zod.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import {
|
|
2
2
|
buildValidationResponse
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-R6NMRTVW.js";
|
|
4
|
+
import "../chunk-LIQQKPXQ.js";
|
|
5
|
+
import "../chunk-URAOTABQ.js";
|
|
5
6
|
import "../chunk-P4D7EVU3.js";
|
|
6
7
|
|
|
7
8
|
// src/integrations/zod.ts
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface ValidationHookOptions {
|
|
2
|
+
title?: string;
|
|
3
|
+
detail?: string;
|
|
4
|
+
/**
|
|
5
|
+
* Throw a `ProblemDetailsError` instead of returning a Response, so
|
|
6
|
+
* `problemDetailsHandler` builds the response and its options (`typePrefix`,
|
|
7
|
+
* `defaultType`, `localize`, `autoInstance`, `otelApi`) apply. Without
|
|
8
|
+
* `app.onError(problemDetailsHandler())`, Hono's default error handler falls back to
|
|
9
|
+
* `ProblemDetailsError.getResponse()`, returning the plain 422. Default: `false`.
|
|
10
|
+
*/
|
|
11
|
+
delegateToHandler?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type { ValidationHookOptions as V };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface ValidationHookOptions {
|
|
2
|
+
title?: string;
|
|
3
|
+
detail?: string;
|
|
4
|
+
/**
|
|
5
|
+
* Throw a `ProblemDetailsError` instead of returning a Response, so
|
|
6
|
+
* `problemDetailsHandler` builds the response and its options (`typePrefix`,
|
|
7
|
+
* `defaultType`, `localize`, `autoInstance`, `otelApi`) apply. Without
|
|
8
|
+
* `app.onError(problemDetailsHandler())`, Hono's default error handler falls back to
|
|
9
|
+
* `ProblemDetailsError.getResponse()`, returning the plain 422. Default: `false`.
|
|
10
|
+
*/
|
|
11
|
+
delegateToHandler?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type { ValidationHookOptions as V };
|