hono-problem-details 0.12.2 → 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.
@@ -1,199 +1,14 @@
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
- // src/integrations/valibot.ts
21
- var valibot_exports = {};
22
- __export(valibot_exports, {
23
- valibotProblemHook: () => valibotProblemHook
24
- });
25
- module.exports = __toCommonJS(valibot_exports);
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
-
80
- // src/utils.ts
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
- }
97
- var FALLBACK_BODY = JSON.stringify({
98
- type: "about:blank",
99
- status: 500,
100
- title: "Internal Server Error"
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
- };
155
-
156
- // src/integrations/validation.ts
157
- var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
158
- function stripControlChars(input) {
159
- return input.replace(CONTROL_CHAR_RE, "");
160
- }
161
- function sanitizeError(error) {
162
- return {
163
- field: stripControlChars(error.field),
164
- message: stripControlChars(error.message),
165
- ...error.code !== void 0 ? { code: stripControlChars(error.code) } : {}
166
- };
167
- }
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
- }
180
- const body = {
181
- type: "about:blank",
182
- status: 422,
183
- title,
184
- detail,
185
- errors: sanitizedErrors
186
- };
187
- return new Response(JSON.stringify(body), {
188
- status: 422,
189
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
190
- });
191
- }
3
+ var _chunkQC5C2ZNYcjs = require('../chunk-QC5C2ZNY.cjs');
4
+ require('../chunk-MIFBVO2E.cjs');
5
+ require('../chunk-4NRZFPAP.cjs');
6
+ require('../chunk-YLXWVI2T.cjs');
192
7
 
193
8
  // src/integrations/valibot.ts
194
9
  function formatIssues(issues) {
195
10
  return issues.map((issue) => ({
196
- field: issue.path?.map((p) => p.key).join(".") ?? "",
11
+ field: _nullishCoalesce(_optionalChain([issue, 'access', _ => _.path, 'optionalAccess', _2 => _2.map, 'call', _3 => _3((p) => p.key), 'access', _4 => _4.join, 'call', _5 => _5(".")]), () => ( "")),
197
12
  message: issue.message,
198
13
  code: issue.type
199
14
  }));
@@ -201,10 +16,9 @@ function formatIssues(issues) {
201
16
  function valibotProblemHook(options) {
202
17
  return (result, _c) => {
203
18
  if (result.success) return;
204
- return buildValidationResponse(formatIssues(result.issues), options);
19
+ return _chunkQC5C2ZNYcjs.buildValidationResponse.call(void 0, formatIssues(result.issues), options);
205
20
  };
206
21
  }
207
- // Annotate the CommonJS export names for ESM import in node:
208
- 0 && (module.exports = {
209
- valibotProblemHook
210
- });
22
+
23
+
24
+ exports.valibotProblemHook = valibotProblemHook;
@@ -1,194 +1,9 @@
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});
19
2
 
20
- // src/integrations/zod.ts
21
- var zod_exports = {};
22
- __export(zod_exports, {
23
- zodProblemHook: () => zodProblemHook
24
- });
25
- module.exports = __toCommonJS(zod_exports);
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
-
80
- // src/utils.ts
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
- }
97
- var FALLBACK_BODY = JSON.stringify({
98
- type: "about:blank",
99
- status: 500,
100
- title: "Internal Server Error"
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
- };
155
-
156
- // src/integrations/validation.ts
157
- var CONTROL_CHAR_RE = /[\x00-\x1f\x7f]/g;
158
- function stripControlChars(input) {
159
- return input.replace(CONTROL_CHAR_RE, "");
160
- }
161
- function sanitizeError(error) {
162
- return {
163
- field: stripControlChars(error.field),
164
- message: stripControlChars(error.message),
165
- ...error.code !== void 0 ? { code: stripControlChars(error.code) } : {}
166
- };
167
- }
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
- }
180
- const body = {
181
- type: "about:blank",
182
- status: 422,
183
- title,
184
- detail,
185
- errors: sanitizedErrors
186
- };
187
- return new Response(JSON.stringify(body), {
188
- status: 422,
189
- headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
190
- });
191
- }
3
+ var _chunkQC5C2ZNYcjs = require('../chunk-QC5C2ZNY.cjs');
4
+ require('../chunk-MIFBVO2E.cjs');
5
+ require('../chunk-4NRZFPAP.cjs');
6
+ require('../chunk-YLXWVI2T.cjs');
192
7
 
193
8
  // src/integrations/zod.ts
194
9
  function formatErrors(zodError) {
@@ -201,10 +16,9 @@ function formatErrors(zodError) {
201
16
  function zodProblemHook(options) {
202
17
  return (result, _c) => {
203
18
  if (result.success) return;
204
- return buildValidationResponse(formatErrors(result.error), options);
19
+ return _chunkQC5C2ZNYcjs.buildValidationResponse.call(void 0, formatErrors(result.error), options);
205
20
  };
206
21
  }
207
- // Annotate the CommonJS export names for ESM import in node:
208
- 0 && (module.exports = {
209
- zodProblemHook
210
- });
22
+
23
+
24
+ exports.zodProblemHook = zodProblemHook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hono-problem-details",
3
- "version": "0.12.2",
3
+ "version": "0.12.3",
4
4
  "description": "RFC 9457 Problem Details middleware for Hono",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -153,7 +153,7 @@
153
153
  "@biomejs/biome": "^2.4.14",
154
154
  "@changesets/changelog-github": "1.0.1",
155
155
  "@changesets/cli": "3.0.3",
156
- "@hono/standard-validator": "0.4.0",
156
+ "@hono/standard-validator": "0.4.1",
157
157
  "@hono/valibot-validator": "^0.6.1",
158
158
  "@hono/zod-openapi": "1.6.3",
159
159
  "@hono/zod-validator": "^0.9.0",
@@ -161,9 +161,9 @@
161
161
  "@standard-schema/spec": "1.1.0",
162
162
  "@vitest/coverage-v8": "^4.1.12",
163
163
  "hono": "^4.13.7",
164
- "knip": "6.41.0",
165
- "lefthook": "2.2.1",
166
- "tsup": "^8.0.0",
164
+ "knip": "6.44.1",
165
+ "lefthook": "2.2.2",
166
+ "tsup": "^9.0.0",
167
167
  "typescript": "^6.0.3",
168
168
  "valibot": "^1.3.1",
169
169
  "vitest": "^4.1.12",
@@ -188,6 +188,7 @@
188
188
  "typecheck": "tsc --noEmit",
189
189
  "test:compat": "tsc -p tsconfig.compat.json",
190
190
  "test:attw": "attw --pack .",
191
+ "test:dist": "node --test \"tests/package/*.test.mjs\"",
191
192
  "release": "pnpm build && changeset publish",
192
193
  "version-packages": "changeset version && pnpm lint:fix"
193
194
  }