hono-problem-details 0.12.2 → 0.12.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +11 -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 +41 -193
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- 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 +8 -7
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
|
@@ -421,6 +421,17 @@ app.post("/users", sValidator("json", schema, standardSchemaProblemHook()), (c)
|
|
|
421
421
|
> `zod@^3.25.0` and `zod@^4.0.0` — the version constraint above only applies when you import
|
|
422
422
|
> from `hono-problem-details/openapi`.
|
|
423
423
|
|
|
424
|
+
> **Keep one copy of `@asteasolutions/zod-to-openapi`**: `@hono/zod-openapi` depends on it, and
|
|
425
|
+
> each installed copy keeps its own `.openapi()` metadata registry. If a schema is built against
|
|
426
|
+
> one copy (for example, in a pnpm monorepo package that pins a different `zod` version) and the
|
|
427
|
+
> document is generated by an `OpenAPIHono` that uses another, the metadata that schema got from
|
|
428
|
+
> `.openapi()` (`title`, `description`, `example`) is missing from the generated document. In the
|
|
429
|
+
> layouts we tested this raised no error; descriptions set with `.describe()` were kept. This
|
|
430
|
+
> applies to any schema built against the other copy, not only the ones from this package. Align
|
|
431
|
+
> the `zod` and `@hono/zod-openapi` versions across packages, and any direct dependency on
|
|
432
|
+
> `@asteasolutions/zod-to-openapi`, so that one copy is installed (check with
|
|
433
|
+
> `npm ls @asteasolutions/zod-to-openapi` or `pnpm why @asteasolutions/zod-to-openapi`).
|
|
434
|
+
|
|
424
435
|
Use with `@hono/zod-openapi` to document Problem Details error responses in your OpenAPI spec:
|
|
425
436
|
|
|
426
437
|
```ts
|
|
@@ -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,182 +1,31 @@
|
|
|
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
|
}
|
|
@@ -202,30 +51,30 @@ var REPRESENTATION_HEADERS = /* @__PURE__ */ new Set([
|
|
|
202
51
|
]);
|
|
203
52
|
function buildType(status, options) {
|
|
204
53
|
if (options.typePrefix) {
|
|
205
|
-
const slug = statusToSlug(status);
|
|
54
|
+
const slug = _chunkYLXWVI2Tcjs.statusToSlug.call(void 0, status);
|
|
206
55
|
if (slug) return `${options.typePrefix}/${slug}`;
|
|
207
56
|
}
|
|
208
|
-
return options.defaultType
|
|
57
|
+
return _nullishCoalesce(options.defaultType, () => ( "about:blank"));
|
|
209
58
|
}
|
|
210
59
|
function copyResHeaders(error, response) {
|
|
211
|
-
if (error instanceof
|
|
212
|
-
error.res
|
|
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) => {
|
|
213
62
|
if (!REPRESENTATION_HEADERS.has(name)) {
|
|
214
63
|
response.headers.append(name, value);
|
|
215
64
|
}
|
|
216
|
-
});
|
|
65
|
+
})]);
|
|
217
66
|
}
|
|
218
67
|
return response;
|
|
219
68
|
}
|
|
220
69
|
function toResponse(input, c, options) {
|
|
221
|
-
let pd = normalizeProblemDetails({
|
|
70
|
+
let pd = _chunk4NRZFPAPcjs.normalizeProblemDetails.call(void 0, {
|
|
222
71
|
...input,
|
|
223
|
-
type: input.type
|
|
72
|
+
type: _nullishCoalesce(input.type, () => ( buildType(input.status, options)))
|
|
224
73
|
});
|
|
225
74
|
if (options.autoInstance && pd.instance === void 0) {
|
|
226
75
|
pd = { ...pd, instance: c.req.path };
|
|
227
76
|
}
|
|
228
|
-
if (options.otelApi && pd.extensions
|
|
77
|
+
if (options.otelApi && _optionalChain([pd, 'access', _8 => _8.extensions, 'optionalAccess', _9 => _9.traceId]) === void 0) {
|
|
229
78
|
const traceId = getOtelTraceId(options.otelApi);
|
|
230
79
|
if (traceId) {
|
|
231
80
|
if (!pd.extensions) {
|
|
@@ -237,15 +86,15 @@ function toResponse(input, c, options) {
|
|
|
237
86
|
if (options.localize) {
|
|
238
87
|
try {
|
|
239
88
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
240
|
-
} catch {
|
|
89
|
+
} catch (e2) {
|
|
241
90
|
}
|
|
242
91
|
}
|
|
243
92
|
c.set("problemDetails", pd);
|
|
244
|
-
return buildProblemResponse(pd);
|
|
93
|
+
return _chunk4NRZFPAPcjs.buildProblemResponse.call(void 0, pd);
|
|
245
94
|
}
|
|
246
95
|
function problemDetailsHandler(options = {}) {
|
|
247
96
|
const handle = (error, c) => {
|
|
248
|
-
if (error instanceof ProblemDetailsError) {
|
|
97
|
+
if (error instanceof _chunkMIFBVO2Ecjs.ProblemDetailsError) {
|
|
249
98
|
const pd = error.problemDetails;
|
|
250
99
|
return toResponse(error.hasExplicitType ? pd : { ...pd, type: void 0 }, c, options);
|
|
251
100
|
}
|
|
@@ -255,16 +104,16 @@ function problemDetailsHandler(options = {}) {
|
|
|
255
104
|
if (mapped) {
|
|
256
105
|
return copyResHeaders(error, toResponse(mapped, c, options));
|
|
257
106
|
}
|
|
258
|
-
} catch {
|
|
107
|
+
} catch (e3) {
|
|
259
108
|
}
|
|
260
109
|
}
|
|
261
|
-
if (error instanceof
|
|
110
|
+
if (error instanceof _httpexception.HTTPException) {
|
|
262
111
|
return copyResHeaders(
|
|
263
112
|
error,
|
|
264
113
|
toResponse(
|
|
265
114
|
{
|
|
266
115
|
status: error.status,
|
|
267
|
-
title: statusToPhrase(error.status),
|
|
116
|
+
title: _chunkYLXWVI2Tcjs.statusToPhrase.call(void 0, error.status),
|
|
268
117
|
detail: error.message || void 0
|
|
269
118
|
},
|
|
270
119
|
c,
|
|
@@ -275,7 +124,7 @@ function problemDetailsHandler(options = {}) {
|
|
|
275
124
|
if (options.onUnhandledError) {
|
|
276
125
|
try {
|
|
277
126
|
options.onUnhandledError(error, c);
|
|
278
|
-
} catch {
|
|
127
|
+
} catch (e4) {
|
|
279
128
|
}
|
|
280
129
|
}
|
|
281
130
|
return toResponse(
|
|
@@ -302,31 +151,30 @@ function deriveCode(key) {
|
|
|
302
151
|
return key.toLowerCase().replace(/_+/g, "-").replace(/^-+|-+$/g, "");
|
|
303
152
|
}
|
|
304
153
|
function createProblemTypeRegistry(definitions, options) {
|
|
305
|
-
const autoCode = options
|
|
154
|
+
const autoCode = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _10 => _10.autoCode]), () => ( false));
|
|
306
155
|
const resolved = new Map(
|
|
307
156
|
Object.keys(definitions).map((key) => {
|
|
308
157
|
const definition = definitions[key];
|
|
309
|
-
const code = definition.code
|
|
158
|
+
const code = _nullishCoalesce(definition.code, () => ( (autoCode ? deriveCode(key) : void 0)));
|
|
310
159
|
return [key, code === void 0 ? definition : { ...definition, code }];
|
|
311
160
|
})
|
|
312
161
|
);
|
|
313
162
|
return {
|
|
314
163
|
create: (key, createOptions) => {
|
|
315
|
-
const { code, ...definition } = resolved.get(key)
|
|
316
|
-
const extensions = code !== void 0 ? { code, ...createOptions
|
|
317
|
-
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 });
|
|
318
167
|
},
|
|
319
168
|
get: (key) => resolved.get(key),
|
|
320
169
|
types: () => [...resolved.keys()]
|
|
321
170
|
};
|
|
322
171
|
}
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
});
|
|
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 };
|