hono-problem-details 0.12.1 → 0.12.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +6 -0
- package/dist/chunk-4NRZFPAP.cjs +60 -0
- package/dist/chunk-MIFBVO2E.cjs +34 -0
- package/dist/chunk-QC5C2ZNY.cjs +47 -0
- package/dist/chunk-YLXWVI2T.cjs +60 -0
- package/dist/index.cjs +56 -197
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +15 -4
- package/dist/integrations/openapi-json-schema.cjs +11 -93
- package/dist/integrations/openapi-json-schema.d.cts +2 -1
- package/dist/integrations/openapi-json-schema.d.ts +2 -1
- package/dist/integrations/openapi.cjs +16 -94
- package/dist/integrations/standard-schema.cjs +9 -195
- package/dist/integrations/valibot.cjs +10 -196
- package/dist/integrations/zod.cjs +9 -195
- package/package.json +6 -5
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2026 hono-problem-details contributors
|
|
3
|
+
Copyright (c) 2026 - present, Ryota Ikezawa and hono-problem-details contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
package/README.md
CHANGED
|
@@ -612,6 +612,12 @@ problemDetailsHandler({
|
|
|
612
612
|
});
|
|
613
613
|
```
|
|
614
614
|
|
|
615
|
+
> **Note on a throw after `await next()`**: the route's response already exists at that point, and
|
|
616
|
+
> Hono copies its headers onto whatever `onError` returns, except `Content-Type`. Behind `compress()`,
|
|
617
|
+
> this can send `Content-Encoding: gzip` on an uncompressed problem body, which clients cannot decode.
|
|
618
|
+
> Any `onError` handler is affected, not only this one. To avoid it, throw before the response is
|
|
619
|
+
> produced, or register `compress()` outside the middleware that throws.
|
|
620
|
+
|
|
615
621
|
## Used By
|
|
616
622
|
|
|
617
623
|
The following Hono middleware libraries use `hono-problem-details` as an optional dependency for RFC 9457 error responses:
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
var _chunkYLXWVI2Tcjs = require('./chunk-YLXWVI2T.cjs');
|
|
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 _nullishCoalesce(filtered, () => ( extensions));
|
|
18
|
+
}
|
|
19
|
+
function clampHttpStatus(status) {
|
|
20
|
+
return Number.isInteger(status) && 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: _nullishCoalesce(input.type, () => ( "about:blank")),
|
|
30
|
+
status: input.status,
|
|
31
|
+
title: _nullishCoalesce(_nullishCoalesce(input.title, () => ( _chunkYLXWVI2Tcjs.statusToPhrase.call(void 0, 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 status = clampHttpStatus(pd.status);
|
|
40
|
+
const body = { ...sanitizeExtensions(extensions), ...standard, status };
|
|
41
|
+
const { json, fallback } = safeStringify(body);
|
|
42
|
+
return new Response(json, {
|
|
43
|
+
status: fallback ? 500 : status,
|
|
44
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
function safeStringify(body) {
|
|
48
|
+
try {
|
|
49
|
+
return { json: JSON.stringify(body), fallback: false };
|
|
50
|
+
} catch (e) {
|
|
51
|
+
return { json: FALLBACK_BODY, fallback: true };
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
exports.PROBLEM_JSON_CONTENT_TYPE = PROBLEM_JSON_CONTENT_TYPE; exports.DANGEROUS_KEYS = DANGEROUS_KEYS; exports.normalizeProblemDetails = normalizeProblemDetails; exports.buildProblemResponse = buildProblemResponse;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
var _chunk4NRZFPAPcjs = require('./chunk-4NRZFPAP.cjs');
|
|
5
|
+
|
|
6
|
+
// src/error.ts
|
|
7
|
+
var ProblemDetailsError = class extends Error {
|
|
8
|
+
/** The normalized RFC 9457 Problem Details object. */
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Whether the input carried an explicit `type`. Lets the handler apply
|
|
12
|
+
* `typePrefix`/`defaultType` only when the thrower left `type` unset.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param input - Missing `type` defaults to `"about:blank"`;
|
|
17
|
+
* missing `title` is derived from the HTTP status code.
|
|
18
|
+
*/
|
|
19
|
+
constructor(input) {
|
|
20
|
+
const pd = _chunk4NRZFPAPcjs.normalizeProblemDetails.call(void 0, input);
|
|
21
|
+
super(_nullishCoalesce(pd.detail, () => ( pd.title)));
|
|
22
|
+
this.name = "ProblemDetailsError";
|
|
23
|
+
this.problemDetails = pd;
|
|
24
|
+
this.hasExplicitType = input.type !== void 0;
|
|
25
|
+
}
|
|
26
|
+
/** Build a standalone `application/problem+json` Response without the handler middleware. */
|
|
27
|
+
getResponse() {
|
|
28
|
+
return _chunk4NRZFPAPcjs.buildProblemResponse.call(void 0, this.problemDetails);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
exports.ProblemDetailsError = ProblemDetailsError;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
|
+
|
|
3
|
+
var _chunkMIFBVO2Ecjs = require('./chunk-MIFBVO2E.cjs');
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
var _chunk4NRZFPAPcjs = require('./chunk-4NRZFPAP.cjs');
|
|
7
|
+
|
|
8
|
+
// src/integrations/validation.ts
|
|
9
|
+
var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
|
|
10
|
+
function stripControlChars(input) {
|
|
11
|
+
return input.replace(CONTROL_CHAR_RE, "");
|
|
12
|
+
}
|
|
13
|
+
function sanitizeError(error) {
|
|
14
|
+
return {
|
|
15
|
+
field: stripControlChars(error.field),
|
|
16
|
+
message: stripControlChars(error.message),
|
|
17
|
+
...error.code !== void 0 ? { code: stripControlChars(error.code) } : {}
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function buildValidationResponse(errors, options) {
|
|
21
|
+
const title = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _ => _.title]), () => ( "Validation Error"));
|
|
22
|
+
const detail = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _2 => _2.detail]), () => ( "Request validation failed"));
|
|
23
|
+
const sanitizedErrors = errors.map(sanitizeError);
|
|
24
|
+
if (_optionalChain([options, 'optionalAccess', _3 => _3.delegateToHandler])) {
|
|
25
|
+
throw new (0, _chunkMIFBVO2Ecjs.ProblemDetailsError)({
|
|
26
|
+
status: 422,
|
|
27
|
+
title,
|
|
28
|
+
detail,
|
|
29
|
+
extensions: { errors: sanitizedErrors }
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const body = {
|
|
33
|
+
type: "about:blank",
|
|
34
|
+
status: 422,
|
|
35
|
+
title,
|
|
36
|
+
detail,
|
|
37
|
+
errors: sanitizedErrors
|
|
38
|
+
};
|
|
39
|
+
return new Response(JSON.stringify(body), {
|
|
40
|
+
status: 422,
|
|
41
|
+
headers: { "Content-Type": _chunk4NRZFPAPcjs.PROBLEM_JSON_CONTENT_TYPE }
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
exports.buildValidationResponse = buildValidationResponse;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/status.ts
|
|
2
|
+
var STATUS_PHRASES = {
|
|
3
|
+
400: "Bad Request",
|
|
4
|
+
401: "Unauthorized",
|
|
5
|
+
402: "Payment Required",
|
|
6
|
+
403: "Forbidden",
|
|
7
|
+
404: "Not Found",
|
|
8
|
+
405: "Method Not Allowed",
|
|
9
|
+
406: "Not Acceptable",
|
|
10
|
+
407: "Proxy Authentication Required",
|
|
11
|
+
408: "Request Timeout",
|
|
12
|
+
409: "Conflict",
|
|
13
|
+
410: "Gone",
|
|
14
|
+
411: "Length Required",
|
|
15
|
+
412: "Precondition Failed",
|
|
16
|
+
413: "Content Too Large",
|
|
17
|
+
414: "URI Too Long",
|
|
18
|
+
415: "Unsupported Media Type",
|
|
19
|
+
416: "Range Not Satisfiable",
|
|
20
|
+
417: "Expectation Failed",
|
|
21
|
+
418: "I'm a Teapot",
|
|
22
|
+
421: "Misdirected Request",
|
|
23
|
+
422: "Unprocessable Content",
|
|
24
|
+
423: "Locked",
|
|
25
|
+
424: "Failed Dependency",
|
|
26
|
+
425: "Too Early",
|
|
27
|
+
426: "Upgrade Required",
|
|
28
|
+
428: "Precondition Required",
|
|
29
|
+
429: "Too Many Requests",
|
|
30
|
+
431: "Request Header Fields Too Large",
|
|
31
|
+
451: "Unavailable For Legal Reasons",
|
|
32
|
+
500: "Internal Server Error",
|
|
33
|
+
501: "Not Implemented",
|
|
34
|
+
502: "Bad Gateway",
|
|
35
|
+
503: "Service Unavailable",
|
|
36
|
+
504: "Gateway Timeout",
|
|
37
|
+
505: "HTTP Version Not Supported",
|
|
38
|
+
506: "Variant Also Negotiates",
|
|
39
|
+
507: "Insufficient Storage",
|
|
40
|
+
508: "Loop Detected",
|
|
41
|
+
510: "Not Extended",
|
|
42
|
+
511: "Network Authentication Required"
|
|
43
|
+
};
|
|
44
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
45
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
46
|
+
Number(code),
|
|
47
|
+
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
48
|
+
])
|
|
49
|
+
);
|
|
50
|
+
function statusToPhrase(status) {
|
|
51
|
+
return STATUS_PHRASES[status];
|
|
52
|
+
}
|
|
53
|
+
function statusToSlug(status) {
|
|
54
|
+
return STATUS_SLUGS[status];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
exports.statusToPhrase = statusToPhrase; exports.statusToSlug = statusToSlug;
|
package/dist/index.cjs
CHANGED
|
@@ -1,220 +1,80 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
19
2
|
|
|
20
|
-
|
|
21
|
-
var src_exports = {};
|
|
22
|
-
__export(src_exports, {
|
|
23
|
-
PROBLEM_JSON_CONTENT_TYPE: () => PROBLEM_JSON_CONTENT_TYPE,
|
|
24
|
-
ProblemDetailsError: () => ProblemDetailsError,
|
|
25
|
-
createProblemTypeRegistry: () => createProblemTypeRegistry,
|
|
26
|
-
problemDetails: () => problemDetails,
|
|
27
|
-
problemDetailsHandler: () => problemDetailsHandler,
|
|
28
|
-
statusToPhrase: () => statusToPhrase,
|
|
29
|
-
statusToSlug: () => statusToSlug
|
|
30
|
-
});
|
|
31
|
-
module.exports = __toCommonJS(src_exports);
|
|
3
|
+
var _chunkMIFBVO2Ecjs = require('./chunk-MIFBVO2E.cjs');
|
|
32
4
|
|
|
33
|
-
// src/status.ts
|
|
34
|
-
var STATUS_PHRASES = {
|
|
35
|
-
400: "Bad Request",
|
|
36
|
-
401: "Unauthorized",
|
|
37
|
-
402: "Payment Required",
|
|
38
|
-
403: "Forbidden",
|
|
39
|
-
404: "Not Found",
|
|
40
|
-
405: "Method Not Allowed",
|
|
41
|
-
406: "Not Acceptable",
|
|
42
|
-
407: "Proxy Authentication Required",
|
|
43
|
-
408: "Request Timeout",
|
|
44
|
-
409: "Conflict",
|
|
45
|
-
410: "Gone",
|
|
46
|
-
411: "Length Required",
|
|
47
|
-
412: "Precondition Failed",
|
|
48
|
-
413: "Content Too Large",
|
|
49
|
-
414: "URI Too Long",
|
|
50
|
-
415: "Unsupported Media Type",
|
|
51
|
-
416: "Range Not Satisfiable",
|
|
52
|
-
417: "Expectation Failed",
|
|
53
|
-
418: "I'm a Teapot",
|
|
54
|
-
421: "Misdirected Request",
|
|
55
|
-
422: "Unprocessable Content",
|
|
56
|
-
423: "Locked",
|
|
57
|
-
424: "Failed Dependency",
|
|
58
|
-
425: "Too Early",
|
|
59
|
-
426: "Upgrade Required",
|
|
60
|
-
428: "Precondition Required",
|
|
61
|
-
429: "Too Many Requests",
|
|
62
|
-
431: "Request Header Fields Too Large",
|
|
63
|
-
451: "Unavailable For Legal Reasons",
|
|
64
|
-
500: "Internal Server Error",
|
|
65
|
-
501: "Not Implemented",
|
|
66
|
-
502: "Bad Gateway",
|
|
67
|
-
503: "Service Unavailable",
|
|
68
|
-
504: "Gateway Timeout",
|
|
69
|
-
505: "HTTP Version Not Supported",
|
|
70
|
-
506: "Variant Also Negotiates",
|
|
71
|
-
507: "Insufficient Storage",
|
|
72
|
-
508: "Loop Detected",
|
|
73
|
-
510: "Not Extended",
|
|
74
|
-
511: "Network Authentication Required"
|
|
75
|
-
};
|
|
76
|
-
var STATUS_SLUGS = Object.fromEntries(
|
|
77
|
-
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
78
|
-
Number(code),
|
|
79
|
-
phrase.toLowerCase().replace(/'/g, "").replace(/[^a-z0-9]+/g, "-").replace(/^-|-$/g, "")
|
|
80
|
-
])
|
|
81
|
-
);
|
|
82
|
-
function statusToPhrase(status) {
|
|
83
|
-
return STATUS_PHRASES[status];
|
|
84
|
-
}
|
|
85
|
-
function statusToSlug(status) {
|
|
86
|
-
return STATUS_SLUGS[status];
|
|
87
|
-
}
|
|
88
5
|
|
|
89
|
-
// src/utils.ts
|
|
90
|
-
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
91
|
-
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
92
|
-
function sanitizeExtensions(extensions) {
|
|
93
|
-
if (!extensions) return extensions;
|
|
94
|
-
let filtered;
|
|
95
|
-
for (const key of Object.keys(extensions)) {
|
|
96
|
-
if (DANGEROUS_KEYS.has(key)) {
|
|
97
|
-
if (!filtered) filtered = { ...extensions };
|
|
98
|
-
delete filtered[key];
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
return filtered ?? extensions;
|
|
102
|
-
}
|
|
103
|
-
function clampHttpStatus(status) {
|
|
104
|
-
return Number.isInteger(status) && status >= 200 && status <= 599 ? status : 500;
|
|
105
|
-
}
|
|
106
|
-
var FALLBACK_BODY = JSON.stringify({
|
|
107
|
-
type: "about:blank",
|
|
108
|
-
status: 500,
|
|
109
|
-
title: "Internal Server Error"
|
|
110
|
-
});
|
|
111
|
-
function normalizeProblemDetails(input) {
|
|
112
|
-
return {
|
|
113
|
-
type: input.type ?? "about:blank",
|
|
114
|
-
status: input.status,
|
|
115
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
116
|
-
detail: input.detail,
|
|
117
|
-
instance: input.instance,
|
|
118
|
-
extensions: input.extensions
|
|
119
|
-
};
|
|
120
|
-
}
|
|
121
|
-
function buildProblemResponse(pd) {
|
|
122
|
-
const { extensions, ...standard } = pd;
|
|
123
|
-
const status = clampHttpStatus(pd.status);
|
|
124
|
-
const body = { ...sanitizeExtensions(extensions), ...standard, status };
|
|
125
|
-
const { json, fallback } = safeStringify(body);
|
|
126
|
-
return new Response(json, {
|
|
127
|
-
status: fallback ? 500 : status,
|
|
128
|
-
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
function safeStringify(body) {
|
|
132
|
-
try {
|
|
133
|
-
return { json: JSON.stringify(body), fallback: false };
|
|
134
|
-
} catch {
|
|
135
|
-
return { json: FALLBACK_BODY, fallback: true };
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
6
|
|
|
139
|
-
|
|
140
|
-
var
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
* `typePrefix`/`defaultType` only when the thrower left `type` unset.
|
|
146
|
-
*/
|
|
147
|
-
hasExplicitType;
|
|
148
|
-
/**
|
|
149
|
-
* @param input - Missing `type` defaults to `"about:blank"`;
|
|
150
|
-
* missing `title` is derived from the HTTP status code.
|
|
151
|
-
*/
|
|
152
|
-
constructor(input) {
|
|
153
|
-
const pd = normalizeProblemDetails(input);
|
|
154
|
-
super(pd.detail ?? pd.title);
|
|
155
|
-
this.name = "ProblemDetailsError";
|
|
156
|
-
this.problemDetails = pd;
|
|
157
|
-
this.hasExplicitType = input.type !== void 0;
|
|
158
|
-
}
|
|
159
|
-
/** Build a standalone `application/problem+json` Response without the handler middleware. */
|
|
160
|
-
getResponse() {
|
|
161
|
-
return buildProblemResponse(this.problemDetails);
|
|
162
|
-
}
|
|
163
|
-
};
|
|
7
|
+
|
|
8
|
+
var _chunk4NRZFPAPcjs = require('./chunk-4NRZFPAP.cjs');
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
var _chunkYLXWVI2Tcjs = require('./chunk-YLXWVI2T.cjs');
|
|
164
13
|
|
|
165
14
|
// src/factory.ts
|
|
166
15
|
function problemDetails(input) {
|
|
167
|
-
return new ProblemDetailsError(input);
|
|
16
|
+
return new (0, _chunkMIFBVO2Ecjs.ProblemDetailsError)(input);
|
|
168
17
|
}
|
|
169
18
|
|
|
170
19
|
// src/handler.ts
|
|
171
|
-
var
|
|
20
|
+
var _httpexception = require('hono/http-exception');
|
|
172
21
|
|
|
173
22
|
// src/integrations/opentelemetry.ts
|
|
174
23
|
function getOtelTraceId(api) {
|
|
175
24
|
try {
|
|
176
25
|
const span = api.trace.getSpan(api.context.active());
|
|
177
|
-
const traceId = span
|
|
26
|
+
const traceId = _optionalChain([span, 'optionalAccess', _ => _.spanContext, 'call', _2 => _2(), 'access', _3 => _3.traceId]);
|
|
178
27
|
return traceId;
|
|
179
|
-
} catch {
|
|
28
|
+
} catch (e) {
|
|
180
29
|
return void 0;
|
|
181
30
|
}
|
|
182
31
|
}
|
|
183
32
|
|
|
184
33
|
// src/handler.ts
|
|
185
34
|
var REPRESENTATION_HEADERS = /* @__PURE__ */ new Set([
|
|
186
|
-
"
|
|
35
|
+
"content-type",
|
|
36
|
+
"content-encoding",
|
|
37
|
+
"content-language",
|
|
38
|
+
"content-length",
|
|
39
|
+
"content-location",
|
|
40
|
+
"content-range",
|
|
41
|
+
"content-disposition",
|
|
42
|
+
"content-md5",
|
|
43
|
+
"content-digest",
|
|
44
|
+
"content-signature",
|
|
45
|
+
"repr-digest",
|
|
46
|
+
"digest",
|
|
187
47
|
"etag",
|
|
188
48
|
"last-modified",
|
|
189
|
-
"
|
|
190
|
-
"
|
|
49
|
+
"accept-ranges",
|
|
50
|
+
"transfer-encoding"
|
|
191
51
|
]);
|
|
192
52
|
function buildType(status, options) {
|
|
193
53
|
if (options.typePrefix) {
|
|
194
|
-
const slug = statusToSlug(status);
|
|
54
|
+
const slug = _chunkYLXWVI2Tcjs.statusToSlug.call(void 0, status);
|
|
195
55
|
if (slug) return `${options.typePrefix}/${slug}`;
|
|
196
56
|
}
|
|
197
|
-
return options.defaultType
|
|
57
|
+
return _nullishCoalesce(options.defaultType, () => ( "about:blank"));
|
|
198
58
|
}
|
|
199
59
|
function copyResHeaders(error, response) {
|
|
200
|
-
if (error instanceof
|
|
201
|
-
error.res
|
|
202
|
-
if (!
|
|
60
|
+
if (error instanceof _httpexception.HTTPException) {
|
|
61
|
+
_optionalChain([error, 'access', _4 => _4.res, 'optionalAccess', _5 => _5.headers, 'access', _6 => _6.forEach, 'call', _7 => _7((value, name) => {
|
|
62
|
+
if (!REPRESENTATION_HEADERS.has(name)) {
|
|
203
63
|
response.headers.append(name, value);
|
|
204
64
|
}
|
|
205
|
-
});
|
|
65
|
+
})]);
|
|
206
66
|
}
|
|
207
67
|
return response;
|
|
208
68
|
}
|
|
209
69
|
function toResponse(input, c, options) {
|
|
210
|
-
let pd = normalizeProblemDetails({
|
|
70
|
+
let pd = _chunk4NRZFPAPcjs.normalizeProblemDetails.call(void 0, {
|
|
211
71
|
...input,
|
|
212
|
-
type: input.type
|
|
72
|
+
type: _nullishCoalesce(input.type, () => ( buildType(input.status, options)))
|
|
213
73
|
});
|
|
214
74
|
if (options.autoInstance && pd.instance === void 0) {
|
|
215
75
|
pd = { ...pd, instance: c.req.path };
|
|
216
76
|
}
|
|
217
|
-
if (options.otelApi && pd.extensions
|
|
77
|
+
if (options.otelApi && _optionalChain([pd, 'access', _8 => _8.extensions, 'optionalAccess', _9 => _9.traceId]) === void 0) {
|
|
218
78
|
const traceId = getOtelTraceId(options.otelApi);
|
|
219
79
|
if (traceId) {
|
|
220
80
|
if (!pd.extensions) {
|
|
@@ -226,15 +86,15 @@ function toResponse(input, c, options) {
|
|
|
226
86
|
if (options.localize) {
|
|
227
87
|
try {
|
|
228
88
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
229
|
-
} catch {
|
|
89
|
+
} catch (e2) {
|
|
230
90
|
}
|
|
231
91
|
}
|
|
232
92
|
c.set("problemDetails", pd);
|
|
233
|
-
return buildProblemResponse(pd);
|
|
93
|
+
return _chunk4NRZFPAPcjs.buildProblemResponse.call(void 0, pd);
|
|
234
94
|
}
|
|
235
95
|
function problemDetailsHandler(options = {}) {
|
|
236
96
|
const handle = (error, c) => {
|
|
237
|
-
if (error instanceof ProblemDetailsError) {
|
|
97
|
+
if (error instanceof _chunkMIFBVO2Ecjs.ProblemDetailsError) {
|
|
238
98
|
const pd = error.problemDetails;
|
|
239
99
|
return toResponse(error.hasExplicitType ? pd : { ...pd, type: void 0 }, c, options);
|
|
240
100
|
}
|
|
@@ -244,16 +104,16 @@ function problemDetailsHandler(options = {}) {
|
|
|
244
104
|
if (mapped) {
|
|
245
105
|
return copyResHeaders(error, toResponse(mapped, c, options));
|
|
246
106
|
}
|
|
247
|
-
} catch {
|
|
107
|
+
} catch (e3) {
|
|
248
108
|
}
|
|
249
109
|
}
|
|
250
|
-
if (error instanceof
|
|
110
|
+
if (error instanceof _httpexception.HTTPException) {
|
|
251
111
|
return copyResHeaders(
|
|
252
112
|
error,
|
|
253
113
|
toResponse(
|
|
254
114
|
{
|
|
255
115
|
status: error.status,
|
|
256
|
-
title: statusToPhrase(error.status),
|
|
116
|
+
title: _chunkYLXWVI2Tcjs.statusToPhrase.call(void 0, error.status),
|
|
257
117
|
detail: error.message || void 0
|
|
258
118
|
},
|
|
259
119
|
c,
|
|
@@ -264,7 +124,7 @@ function problemDetailsHandler(options = {}) {
|
|
|
264
124
|
if (options.onUnhandledError) {
|
|
265
125
|
try {
|
|
266
126
|
options.onUnhandledError(error, c);
|
|
267
|
-
} catch {
|
|
127
|
+
} catch (e4) {
|
|
268
128
|
}
|
|
269
129
|
}
|
|
270
130
|
return toResponse(
|
|
@@ -291,31 +151,30 @@ function deriveCode(key) {
|
|
|
291
151
|
return key.toLowerCase().replace(/_+/g, "-").replace(/^-+|-+$/g, "");
|
|
292
152
|
}
|
|
293
153
|
function createProblemTypeRegistry(definitions, options) {
|
|
294
|
-
const autoCode = options
|
|
154
|
+
const autoCode = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _10 => _10.autoCode]), () => ( false));
|
|
295
155
|
const resolved = new Map(
|
|
296
156
|
Object.keys(definitions).map((key) => {
|
|
297
157
|
const definition = definitions[key];
|
|
298
|
-
const code = definition.code
|
|
158
|
+
const code = _nullishCoalesce(definition.code, () => ( (autoCode ? deriveCode(key) : void 0)));
|
|
299
159
|
return [key, code === void 0 ? definition : { ...definition, code }];
|
|
300
160
|
})
|
|
301
161
|
);
|
|
302
162
|
return {
|
|
303
163
|
create: (key, createOptions) => {
|
|
304
|
-
const { code, ...definition } = resolved.get(key)
|
|
305
|
-
const extensions = code !== void 0 ? { code, ...createOptions
|
|
306
|
-
return new ProblemDetailsError({ ...definition, ...createOptions, extensions });
|
|
164
|
+
const { code, ...definition } = _nullishCoalesce(resolved.get(key), () => ( {}));
|
|
165
|
+
const extensions = code !== void 0 ? { code, ..._optionalChain([createOptions, 'optionalAccess', _11 => _11.extensions]) } : _optionalChain([createOptions, 'optionalAccess', _12 => _12.extensions]);
|
|
166
|
+
return new (0, _chunkMIFBVO2Ecjs.ProblemDetailsError)({ ...definition, ...createOptions, extensions });
|
|
307
167
|
},
|
|
308
168
|
get: (key) => resolved.get(key),
|
|
309
169
|
types: () => [...resolved.keys()]
|
|
310
170
|
};
|
|
311
171
|
}
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
});
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
exports.PROBLEM_JSON_CONTENT_TYPE = _chunk4NRZFPAPcjs.PROBLEM_JSON_CONTENT_TYPE; exports.ProblemDetailsError = _chunkMIFBVO2Ecjs.ProblemDetailsError; exports.createProblemTypeRegistry = createProblemTypeRegistry; exports.problemDetails = problemDetails; exports.problemDetailsHandler = problemDetailsHandler; exports.statusToPhrase = _chunkYLXWVI2Tcjs.statusToPhrase; exports.statusToSlug = _chunkYLXWVI2Tcjs.statusToSlug;
|
package/dist/index.d.cts
CHANGED
|
@@ -214,4 +214,5 @@ declare function statusToSlug(status: number): string | undefined;
|
|
|
214
214
|
/** RFC 9457 media type: `application/problem+json; charset=utf-8`. */
|
|
215
215
|
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
216
216
|
|
|
217
|
-
export { PROBLEM_JSON_CONTENT_TYPE,
|
|
217
|
+
export { PROBLEM_JSON_CONTENT_TYPE, ProblemDetailsError, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
|
218
|
+
export type { ProblemDetails, ProblemDetailsHandlerOptions, ProblemDetailsInput };
|
package/dist/index.d.ts
CHANGED
|
@@ -214,4 +214,5 @@ declare function statusToSlug(status: number): string | undefined;
|
|
|
214
214
|
/** RFC 9457 media type: `application/problem+json; charset=utf-8`. */
|
|
215
215
|
declare const PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
216
216
|
|
|
217
|
-
export { PROBLEM_JSON_CONTENT_TYPE,
|
|
217
|
+
export { PROBLEM_JSON_CONTENT_TYPE, ProblemDetailsError, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|
|
218
|
+
export type { ProblemDetails, ProblemDetailsHandlerOptions, ProblemDetailsInput };
|
package/dist/index.js
CHANGED
|
@@ -32,11 +32,22 @@ function getOtelTraceId(api) {
|
|
|
32
32
|
|
|
33
33
|
// src/handler.ts
|
|
34
34
|
var REPRESENTATION_HEADERS = /* @__PURE__ */ new Set([
|
|
35
|
-
"
|
|
35
|
+
"content-type",
|
|
36
|
+
"content-encoding",
|
|
37
|
+
"content-language",
|
|
38
|
+
"content-length",
|
|
39
|
+
"content-location",
|
|
40
|
+
"content-range",
|
|
41
|
+
"content-disposition",
|
|
42
|
+
"content-md5",
|
|
43
|
+
"content-digest",
|
|
44
|
+
"content-signature",
|
|
45
|
+
"repr-digest",
|
|
46
|
+
"digest",
|
|
36
47
|
"etag",
|
|
37
48
|
"last-modified",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
49
|
+
"accept-ranges",
|
|
50
|
+
"transfer-encoding"
|
|
40
51
|
]);
|
|
41
52
|
function buildType(status, options) {
|
|
42
53
|
if (options.typePrefix) {
|
|
@@ -48,7 +59,7 @@ function buildType(status, options) {
|
|
|
48
59
|
function copyResHeaders(error, response) {
|
|
49
60
|
if (error instanceof HTTPException) {
|
|
50
61
|
error.res?.headers.forEach((value, name) => {
|
|
51
|
-
if (!
|
|
62
|
+
if (!REPRESENTATION_HEADERS.has(name)) {
|
|
52
63
|
response.headers.append(name, value);
|
|
53
64
|
}
|
|
54
65
|
});
|