hono-problem-details 0.1.0 → 0.1.2
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 +7 -0
- package/dist/{chunk-ANLH2WEX.js → chunk-UITHZH2F.js} +8 -4
- package/dist/chunk-UITHZH2F.js.map +1 -0
- package/dist/index.cjs +59 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +53 -40
- package/dist/index.js.map +1 -1
- package/dist/integrations/openapi.cjs +6 -0
- package/dist/integrations/openapi.cjs.map +1 -1
- package/dist/integrations/openapi.d.cts +1 -31
- package/dist/integrations/openapi.d.ts +1 -31
- package/dist/integrations/openapi.js +1 -1
- package/dist/integrations/openapi.js.map +1 -1
- package/dist/integrations/standard-schema.cjs.map +1 -1
- package/dist/integrations/standard-schema.js.map +1 -1
- package/dist/integrations/valibot.cjs.map +1 -1
- package/dist/integrations/valibot.js.map +1 -1
- package/dist/integrations/zod.cjs.map +1 -1
- package/dist/integrations/zod.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-ANLH2WEX.js.map +0 -1
package/README.md
CHANGED
|
@@ -282,6 +282,13 @@ problemDetailsHandler({
|
|
|
282
282
|
});
|
|
283
283
|
```
|
|
284
284
|
|
|
285
|
+
## Used By
|
|
286
|
+
|
|
287
|
+
The following Hono middleware libraries use `hono-problem-details` as an optional dependency for RFC 9457 error responses:
|
|
288
|
+
|
|
289
|
+
- [hono-idempotency](https://github.com/paveg/hono-idempotency) — Idempotency key middleware for Hono
|
|
290
|
+
- [hono-webhook-verify](https://github.com/paveg/hono-webhook-verify) — Webhook signature verification middleware for Hono
|
|
291
|
+
|
|
285
292
|
## License
|
|
286
293
|
|
|
287
294
|
MIT
|
|
@@ -21,17 +21,21 @@ var STATUS_PHRASES = {
|
|
|
21
21
|
503: "Service Unavailable",
|
|
22
22
|
504: "Gateway Timeout"
|
|
23
23
|
};
|
|
24
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
25
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
26
|
+
Number(code),
|
|
27
|
+
phrase.toLowerCase().replace(/ /g, "-")
|
|
28
|
+
])
|
|
29
|
+
);
|
|
24
30
|
function statusToPhrase(status) {
|
|
25
31
|
return STATUS_PHRASES[status];
|
|
26
32
|
}
|
|
27
33
|
function statusToSlug(status) {
|
|
28
|
-
|
|
29
|
-
if (!phrase) return void 0;
|
|
30
|
-
return phrase.toLowerCase().replace(/ /g, "-");
|
|
34
|
+
return STATUS_SLUGS[status];
|
|
31
35
|
}
|
|
32
36
|
|
|
33
37
|
export {
|
|
34
38
|
statusToPhrase,
|
|
35
39
|
statusToSlug
|
|
36
40
|
};
|
|
37
|
-
//# sourceMappingURL=chunk-
|
|
41
|
+
//# sourceMappingURL=chunk-UITHZH2F.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/status.ts"],"sourcesContent":["const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nconst STATUS_SLUGS: Record<number, string> = Object.fromEntries(\n\tObject.entries(STATUS_PHRASES).map(([code, phrase]) => [\n\t\tNumber(code),\n\t\tphrase.toLowerCase().replace(/ /g, \"-\"),\n\t]),\n);\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\treturn STATUS_SLUGS[status];\n}\n"],"mappings":";AAAA,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEA,IAAM,eAAuC,OAAO;AAAA,EACnD,OAAO,QAAQ,cAAc,EAAE,IAAI,CAAC,CAAC,MAAM,MAAM,MAAM;AAAA,IACtD,OAAO,IAAI;AAAA,IACX,OAAO,YAAY,EAAE,QAAQ,MAAM,GAAG;AAAA,EACvC,CAAC;AACF;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;AAEO,SAAS,aAAa,QAAoC;AAChE,SAAO,aAAa,MAAM;AAC3B;","names":[]}
|
package/dist/index.cjs
CHANGED
|
@@ -52,51 +52,34 @@ var STATUS_PHRASES = {
|
|
|
52
52
|
503: "Service Unavailable",
|
|
53
53
|
504: "Gateway Timeout"
|
|
54
54
|
};
|
|
55
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
56
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
57
|
+
Number(code),
|
|
58
|
+
phrase.toLowerCase().replace(/ /g, "-")
|
|
59
|
+
])
|
|
60
|
+
);
|
|
55
61
|
function statusToPhrase(status) {
|
|
56
62
|
return STATUS_PHRASES[status];
|
|
57
63
|
}
|
|
58
64
|
function statusToSlug(status) {
|
|
59
|
-
|
|
60
|
-
if (!phrase) return void 0;
|
|
61
|
-
return phrase.toLowerCase().replace(/ /g, "-");
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// src/error.ts
|
|
65
|
-
function normalizeProblemDetails(input) {
|
|
66
|
-
return {
|
|
67
|
-
type: input.type ?? "about:blank",
|
|
68
|
-
status: input.status,
|
|
69
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
70
|
-
detail: input.detail,
|
|
71
|
-
instance: input.instance,
|
|
72
|
-
extensions: input.extensions
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
var ProblemDetailsError = class extends Error {
|
|
76
|
-
problemDetails;
|
|
77
|
-
constructor(input) {
|
|
78
|
-
const pd = normalizeProblemDetails(input);
|
|
79
|
-
super(pd.detail ?? pd.title);
|
|
80
|
-
this.name = "ProblemDetailsError";
|
|
81
|
-
this.problemDetails = pd;
|
|
82
|
-
}
|
|
83
|
-
getResponse() {
|
|
84
|
-
const { extensions, ...standard } = this.problemDetails;
|
|
85
|
-
const body = { ...standard, ...extensions };
|
|
86
|
-
return new Response(JSON.stringify(body), {
|
|
87
|
-
status: this.problemDetails.status,
|
|
88
|
-
headers: { "Content-Type": "application/problem+json" }
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
// src/factory.ts
|
|
94
|
-
function problemDetails(input) {
|
|
95
|
-
return new ProblemDetailsError(input);
|
|
65
|
+
return STATUS_SLUGS[status];
|
|
96
66
|
}
|
|
97
67
|
|
|
98
68
|
// src/handler.ts
|
|
99
69
|
var import_http_exception = require("hono/http-exception");
|
|
70
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
71
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
72
|
+
function sanitizeExtensions(extensions) {
|
|
73
|
+
if (!extensions) return extensions;
|
|
74
|
+
let filtered;
|
|
75
|
+
for (const key of Object.keys(extensions)) {
|
|
76
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
77
|
+
if (!filtered) filtered = { ...extensions };
|
|
78
|
+
delete filtered[key];
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return filtered ?? extensions;
|
|
82
|
+
}
|
|
100
83
|
function buildType(status, options) {
|
|
101
84
|
if (options.typePrefix) {
|
|
102
85
|
const slug = statusToSlug(status);
|
|
@@ -117,18 +100,18 @@ function toResponse(input, c, options) {
|
|
|
117
100
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
118
101
|
}
|
|
119
102
|
const { extensions, ...rest } = pd;
|
|
120
|
-
const body = { ...
|
|
103
|
+
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
121
104
|
c.set("problemDetails", pd);
|
|
105
|
+
const httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;
|
|
122
106
|
return new Response(JSON.stringify(body), {
|
|
123
|
-
status:
|
|
124
|
-
headers: { "Content-Type":
|
|
107
|
+
status: httpStatus,
|
|
108
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
125
109
|
});
|
|
126
110
|
}
|
|
127
111
|
function problemDetailsHandler(options = {}) {
|
|
128
112
|
return (error, c) => {
|
|
129
113
|
if (error instanceof ProblemDetailsError) {
|
|
130
|
-
|
|
131
|
-
return error.getResponse();
|
|
114
|
+
return toResponse(error.problemDetails, c, options);
|
|
132
115
|
}
|
|
133
116
|
if (options.mapError) {
|
|
134
117
|
const mapped = options.mapError(error);
|
|
@@ -161,6 +144,40 @@ function problemDetailsHandler(options = {}) {
|
|
|
161
144
|
};
|
|
162
145
|
}
|
|
163
146
|
|
|
147
|
+
// src/error.ts
|
|
148
|
+
function normalizeProblemDetails(input) {
|
|
149
|
+
return {
|
|
150
|
+
type: input.type ?? "about:blank",
|
|
151
|
+
status: input.status,
|
|
152
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
153
|
+
detail: input.detail,
|
|
154
|
+
instance: input.instance,
|
|
155
|
+
extensions: input.extensions
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
var ProblemDetailsError = class extends Error {
|
|
159
|
+
problemDetails;
|
|
160
|
+
constructor(input) {
|
|
161
|
+
const pd = normalizeProblemDetails(input);
|
|
162
|
+
super(pd.detail ?? pd.title);
|
|
163
|
+
this.name = "ProblemDetailsError";
|
|
164
|
+
this.problemDetails = pd;
|
|
165
|
+
}
|
|
166
|
+
getResponse() {
|
|
167
|
+
const { extensions, ...standard } = this.problemDetails;
|
|
168
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
169
|
+
return new Response(JSON.stringify(body), {
|
|
170
|
+
status: this.problemDetails.status,
|
|
171
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
// src/factory.ts
|
|
177
|
+
function problemDetails(input) {
|
|
178
|
+
return new ProblemDetailsError(input);
|
|
179
|
+
}
|
|
180
|
+
|
|
164
181
|
// src/registry.ts
|
|
165
182
|
function createProblemTypeRegistry(definitions) {
|
|
166
183
|
return {
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/status.ts","../src/error.ts","../src/factory.ts","../src/handler.ts","../src/registry.ts"],"sourcesContent":["export type {\n\tProblemDetails,\n\tProblemDetailsHandlerOptions,\n\tProblemDetailsInput,\n} from \"./types.js\";\nexport { statusToPhrase, statusToSlug } from \"./status.js\";\nexport { ProblemDetailsError } from \"./error.js\";\nexport { problemDetails } from \"./factory.js\";\nexport { problemDetailsHandler } from \"./handler.js\";\nexport { createProblemTypeRegistry } from \"./registry.js\";\n","const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\tconst phrase = STATUS_PHRASES[status];\n\tif (!phrase) return undefined;\n\treturn phrase.toLowerCase().replace(/ /g, \"-\");\n}\n","import { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nfunction normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\nexport class ProblemDetailsError extends Error {\n\treadonly problemDetails: ProblemDetails;\n\n\tconstructor(input: ProblemDetailsInput) {\n\t\tconst pd = normalizeProblemDetails(input);\n\t\tsuper(pd.detail ?? pd.title);\n\t\tthis.name = \"ProblemDetailsError\";\n\t\tthis.problemDetails = pd;\n\t}\n\n\tgetResponse(): Response {\n\t\tconst { extensions, ...standard } = this.problemDetails;\n\t\tconst body = { ...standard, ...extensions };\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: this.problemDetails.status,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t}\n}\n","import { ProblemDetailsError } from \"./error.js\";\nimport type { ProblemDetailsInput } from \"./types.js\";\n\nexport function problemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetailsError {\n\treturn new ProblemDetailsError(input);\n}\n","import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...rest, ...extensions };\n\n\tc.set(\"problemDetails\", pd);\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: pd.status,\n\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\tc.set(\"problemDetails\", error.problemDetails);\n\t\t\treturn error.getResponse();\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n","import { ProblemDetailsError } from \"./error.js\";\n\ninterface ProblemTypeDefinition {\n\ttype: string;\n\tstatus: number;\n\ttitle: string;\n}\n\ninterface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {\n\tdetail?: string;\n\tinstance?: string;\n\textensions?: T;\n}\n\ninterface ProblemTypeRegistry<K extends string> {\n\tcreate: <T extends Record<string, unknown>>(\n\t\tkey: K,\n\t\toptions?: CreateOptions<T>,\n\t) => ProblemDetailsError;\n\tget: (key: K) => ProblemTypeDefinition;\n\ttypes: () => K[];\n}\n\n/**\n * Create a registry of pre-defined problem types.\n * Provides type-safe error creation from registered definitions.\n */\nexport function createProblemTypeRegistry<K extends string>(\n\tdefinitions: Record<K, ProblemTypeDefinition>,\n): ProblemTypeRegistry<K> {\n\treturn {\n\t\tcreate: (key, options) => {\n\t\t\tconst def = definitions[key];\n\t\t\treturn new ProblemDetailsError({\n\t\t\t\ttype: def.type,\n\t\t\t\tstatus: def.status,\n\t\t\t\ttitle: def.title,\n\t\t\t\tdetail: options?.detail,\n\t\t\t\tinstance: options?.instance,\n\t\t\t\textensions: options?.extensions,\n\t\t\t});\n\t\t},\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,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M,QAAQ,OAAO;AAAA,UACrC,OAAO,eAAe,MAAM,MAAM;AAAA,UAClC,QAAQ,MAAM;AAAA,QACf;AAAA,QACA;AAAA,QACA;AAAA,MACD;AAAA,IACD;AAEA,WAAO;AAAA,MACN;AAAA,QACC,QAAQ;AAAA,QACR,MAAM,UAAU,KAAK,OAAO;AAAA,QAC5B,OAAO;AAAA,QACP,QAAQ,QAAQ,eAAe,MAAM,QAAQ;AAAA,MAC9C;AAAA,MACA;AAAA,MACA;AAAA,IACD;AAAA,EACD;AACD;;;ACtDO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY;AACzB,YAAM,MAAM,YAAY,GAAG;AAC3B,aAAO,IAAI,oBAAoB;AAAA,QAC9B,MAAM,IAAI;AAAA,QACV,QAAQ,IAAI;AAAA,QACZ,OAAO,IAAI;AAAA,QACX,QAAQ,SAAS;AAAA,QACjB,UAAU,SAAS;AAAA,QACnB,YAAY,SAAS;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,IACA,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/status.ts","../src/handler.ts","../src/error.ts","../src/factory.ts","../src/registry.ts"],"sourcesContent":["export type {\n\tProblemDetails,\n\tProblemDetailsHandlerOptions,\n\tProblemDetailsInput,\n} from \"./types.js\";\nexport { statusToPhrase, statusToSlug } from \"./status.js\";\nexport { ProblemDetailsError } from \"./error.js\";\nexport { problemDetails } from \"./factory.js\";\nexport { problemDetailsHandler } from \"./handler.js\";\nexport { createProblemTypeRegistry } from \"./registry.js\";\n","const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nconst STATUS_SLUGS: Record<number, string> = Object.fromEntries(\n\tObject.entries(STATUS_PHRASES).map(([code, phrase]) => [\n\t\tNumber(code),\n\t\tphrase.toLowerCase().replace(/ /g, \"-\"),\n\t]),\n);\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\treturn STATUS_SLUGS[status];\n}\n","import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\t// HTTP status must be 100-599; use body status as-is (RFC 9457 advisory)\n\tconst httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: httpStatus,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\treturn toResponse(error.problemDetails, c, options);\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n","import { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions } from \"./handler.js\";\nimport { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nfunction normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\nexport class ProblemDetailsError extends Error {\n\treadonly problemDetails: ProblemDetails;\n\n\tconstructor(input: ProblemDetailsInput) {\n\t\tconst pd = normalizeProblemDetails(input);\n\t\tsuper(pd.detail ?? pd.title);\n\t\tthis.name = \"ProblemDetailsError\";\n\t\tthis.problemDetails = pd;\n\t}\n\n\tgetResponse(): Response {\n\t\tconst { extensions, ...standard } = this.problemDetails;\n\t\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: this.problemDetails.status,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t}\n}\n","import { ProblemDetailsError } from \"./error.js\";\nimport type { ProblemDetailsInput } from \"./types.js\";\n\nexport function problemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetailsError {\n\treturn new ProblemDetailsError(input);\n}\n","import { ProblemDetailsError } from \"./error.js\";\n\ninterface ProblemTypeDefinition {\n\ttype: string;\n\tstatus: number;\n\ttitle: string;\n}\n\ninterface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {\n\tdetail?: string;\n\tinstance?: string;\n\textensions?: T;\n}\n\ninterface ProblemTypeRegistry<K extends string> {\n\tcreate: <T extends Record<string, unknown>>(\n\t\tkey: K,\n\t\toptions?: CreateOptions<T>,\n\t) => ProblemDetailsError;\n\tget: (key: K) => ProblemTypeDefinition;\n\ttypes: () => K[];\n}\n\n/**\n * Create a registry of pre-defined problem types.\n * Provides type-safe error creation from registered definitions.\n */\nexport function createProblemTypeRegistry<K extends string>(\n\tdefinitions: Record<K, ProblemTypeDefinition>,\n): ProblemTypeRegistry<K> {\n\treturn {\n\t\tcreate: (key, options) => {\n\t\t\tconst def = definitions[key];\n\t\t\treturn new ProblemDetailsError({\n\t\t\t\ttype: def.type,\n\t\t\t\tstatus: def.status,\n\t\t\t\ttitle: def.title,\n\t\t\t\tdetail: options?.detail,\n\t\t\t\tinstance: options?.instance,\n\t\t\t\textensions: options?.extensions,\n\t\t\t});\n\t\t},\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEA,IAAM,eAAuC,OAAO;AAAA,EACnD,OAAO,QAAQ,cAAc,EAAE,IAAI,CAAC,CAAC,MAAM,MAAM,MAAM;AAAA,IACtD,OAAO,IAAI;AAAA,IACX,OAAO,YAAY,EAAE,QAAQ,MAAM,GAAG;AAAA,EACvC,CAAC;AACF;AAEO,SAAS,eAAe,QAAoC;AAClE,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AiB;AAAA,EACvB;AAAA,EAEA,cAAwB;AACvB,UAAM,EAAE,YAAY,GAAG,SAAS,IAAI,KAAK;AACzC,UAAM,OAAO,EAAE,GAAG,mBAAmB,UAAU,GAAG,GAAG,SAAS;AAC9D,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ,KAAK,eAAe;AAAA,MAC5B,SAAS,EAAE,gBAAgB,0BAA0B;AAAA,IACtD,CAAC;AAAA,EACF;AACD;;;AChCO,SAAS,eACf,OACsB;AACtB,SAAO,IAAI,oBAAoB,KAAK;AACrC;;;ACoBO,SAAS,0BACf,aACyB;AACzB,SAAO;AAAA,IACN,QAAQ,CAAC,KAAK,YAAY;AACzB,YAAM,MAAM,YAAY,GAAG;AAC3B,aAAO,IAAI,oBAAoB;AAAA,QAC9B,MAAM,IAAI;AAAA,QACV,QAAQ,IAAI;AAAA,QACZ,OAAO,IAAI;AAAA,QACX,QAAQ,SAAS;AAAA,QACjB,UAAU,SAAS;AAAA,QACnB,YAAY,SAAS;AAAA,MACtB,CAAC;AAAA,IACF;AAAA,IACA,KAAK,CAAC,QAAQ,YAAY,GAAG;AAAA,IAC7B,OAAO,MAAM,OAAO,KAAK,WAAW;AAAA,EACrC;AACD;","names":[]}
|
package/dist/index.js
CHANGED
|
@@ -1,44 +1,23 @@
|
|
|
1
1
|
import {
|
|
2
2
|
statusToPhrase,
|
|
3
3
|
statusToSlug
|
|
4
|
-
} from "./chunk-
|
|
5
|
-
|
|
6
|
-
// src/error.ts
|
|
7
|
-
function normalizeProblemDetails(input) {
|
|
8
|
-
return {
|
|
9
|
-
type: input.type ?? "about:blank",
|
|
10
|
-
status: input.status,
|
|
11
|
-
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
12
|
-
detail: input.detail,
|
|
13
|
-
instance: input.instance,
|
|
14
|
-
extensions: input.extensions
|
|
15
|
-
};
|
|
16
|
-
}
|
|
17
|
-
var ProblemDetailsError = class extends Error {
|
|
18
|
-
problemDetails;
|
|
19
|
-
constructor(input) {
|
|
20
|
-
const pd = normalizeProblemDetails(input);
|
|
21
|
-
super(pd.detail ?? pd.title);
|
|
22
|
-
this.name = "ProblemDetailsError";
|
|
23
|
-
this.problemDetails = pd;
|
|
24
|
-
}
|
|
25
|
-
getResponse() {
|
|
26
|
-
const { extensions, ...standard } = this.problemDetails;
|
|
27
|
-
const body = { ...standard, ...extensions };
|
|
28
|
-
return new Response(JSON.stringify(body), {
|
|
29
|
-
status: this.problemDetails.status,
|
|
30
|
-
headers: { "Content-Type": "application/problem+json" }
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
// src/factory.ts
|
|
36
|
-
function problemDetails(input) {
|
|
37
|
-
return new ProblemDetailsError(input);
|
|
38
|
-
}
|
|
4
|
+
} from "./chunk-UITHZH2F.js";
|
|
39
5
|
|
|
40
6
|
// src/handler.ts
|
|
41
7
|
import { HTTPException } from "hono/http-exception";
|
|
8
|
+
var PROBLEM_JSON_CONTENT_TYPE = "application/problem+json; charset=utf-8";
|
|
9
|
+
var DANGEROUS_KEYS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
10
|
+
function sanitizeExtensions(extensions) {
|
|
11
|
+
if (!extensions) return extensions;
|
|
12
|
+
let filtered;
|
|
13
|
+
for (const key of Object.keys(extensions)) {
|
|
14
|
+
if (DANGEROUS_KEYS.has(key)) {
|
|
15
|
+
if (!filtered) filtered = { ...extensions };
|
|
16
|
+
delete filtered[key];
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return filtered ?? extensions;
|
|
20
|
+
}
|
|
42
21
|
function buildType(status, options) {
|
|
43
22
|
if (options.typePrefix) {
|
|
44
23
|
const slug = statusToSlug(status);
|
|
@@ -59,18 +38,18 @@ function toResponse(input, c, options) {
|
|
|
59
38
|
pd = { ...pd, ...options.localize(pd, c) };
|
|
60
39
|
}
|
|
61
40
|
const { extensions, ...rest } = pd;
|
|
62
|
-
const body = { ...
|
|
41
|
+
const body = { ...sanitizeExtensions(extensions), ...rest };
|
|
63
42
|
c.set("problemDetails", pd);
|
|
43
|
+
const httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;
|
|
64
44
|
return new Response(JSON.stringify(body), {
|
|
65
|
-
status:
|
|
66
|
-
headers: { "Content-Type":
|
|
45
|
+
status: httpStatus,
|
|
46
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
67
47
|
});
|
|
68
48
|
}
|
|
69
49
|
function problemDetailsHandler(options = {}) {
|
|
70
50
|
return (error, c) => {
|
|
71
51
|
if (error instanceof ProblemDetailsError) {
|
|
72
|
-
|
|
73
|
-
return error.getResponse();
|
|
52
|
+
return toResponse(error.problemDetails, c, options);
|
|
74
53
|
}
|
|
75
54
|
if (options.mapError) {
|
|
76
55
|
const mapped = options.mapError(error);
|
|
@@ -103,6 +82,40 @@ function problemDetailsHandler(options = {}) {
|
|
|
103
82
|
};
|
|
104
83
|
}
|
|
105
84
|
|
|
85
|
+
// src/error.ts
|
|
86
|
+
function normalizeProblemDetails(input) {
|
|
87
|
+
return {
|
|
88
|
+
type: input.type ?? "about:blank",
|
|
89
|
+
status: input.status,
|
|
90
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
91
|
+
detail: input.detail,
|
|
92
|
+
instance: input.instance,
|
|
93
|
+
extensions: input.extensions
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
var ProblemDetailsError = class extends Error {
|
|
97
|
+
problemDetails;
|
|
98
|
+
constructor(input) {
|
|
99
|
+
const pd = normalizeProblemDetails(input);
|
|
100
|
+
super(pd.detail ?? pd.title);
|
|
101
|
+
this.name = "ProblemDetailsError";
|
|
102
|
+
this.problemDetails = pd;
|
|
103
|
+
}
|
|
104
|
+
getResponse() {
|
|
105
|
+
const { extensions, ...standard } = this.problemDetails;
|
|
106
|
+
const body = { ...sanitizeExtensions(extensions), ...standard };
|
|
107
|
+
return new Response(JSON.stringify(body), {
|
|
108
|
+
status: this.problemDetails.status,
|
|
109
|
+
headers: { "Content-Type": PROBLEM_JSON_CONTENT_TYPE }
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// src/factory.ts
|
|
115
|
+
function problemDetails(input) {
|
|
116
|
+
return new ProblemDetailsError(input);
|
|
117
|
+
}
|
|
118
|
+
|
|
106
119
|
// src/registry.ts
|
|
107
120
|
function createProblemTypeRegistry(definitions) {
|
|
108
121
|
return {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/
|
|
1
|
+
{"version":3,"sources":["../src/handler.ts","../src/error.ts","../src/factory.ts","../src/registry.ts"],"sourcesContent":["import type { Context, ErrorHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { ProblemDetailsError } from \"./error.js\";\nimport { statusToPhrase, statusToSlug } from \"./status.js\";\nimport type { ProblemDetailsHandlerOptions, ProblemDetailsInput } from \"./types.js\";\n\nexport const PROBLEM_JSON_CONTENT_TYPE = \"application/problem+json; charset=utf-8\";\n\nconst DANGEROUS_KEYS = new Set([\"__proto__\", \"constructor\", \"prototype\"]);\n\n/** Strip keys that could cause prototype pollution in downstream consumers */\nexport function sanitizeExtensions(\n\textensions: Record<string, unknown> | undefined,\n): Record<string, unknown> | undefined {\n\tif (!extensions) return extensions;\n\tlet filtered: Record<string, unknown> | undefined;\n\tfor (const key of Object.keys(extensions)) {\n\t\tif (DANGEROUS_KEYS.has(key)) {\n\t\t\tif (!filtered) filtered = { ...extensions };\n\t\t\tdelete filtered[key];\n\t\t}\n\t}\n\treturn filtered ?? extensions;\n}\n\nfunction buildType(status: number, options: ProblemDetailsHandlerOptions): string {\n\tif (options.typePrefix) {\n\t\tconst slug = statusToSlug(status);\n\t\tif (slug) return `${options.typePrefix}/${slug}`;\n\t}\n\treturn options.defaultType ?? \"about:blank\";\n}\n\nfunction toResponse(\n\tinput: ProblemDetailsInput,\n\tc: Context,\n\toptions: ProblemDetailsHandlerOptions,\n): Response {\n\tlet pd = {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n\n\tif (options.localize) {\n\t\tpd = { ...pd, ...options.localize(pd, c) };\n\t}\n\n\tconst { extensions, ...rest } = pd;\n\tconst body = { ...sanitizeExtensions(extensions), ...rest };\n\n\tc.set(\"problemDetails\", pd);\n\n\t// HTTP status must be 100-599; use body status as-is (RFC 9457 advisory)\n\tconst httpStatus = pd.status >= 100 && pd.status <= 599 ? pd.status : 500;\n\n\treturn new Response(JSON.stringify(body), {\n\t\tstatus: httpStatus,\n\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t});\n}\n\nexport function problemDetailsHandler(options: ProblemDetailsHandlerOptions = {}): ErrorHandler {\n\treturn (error, c) => {\n\t\tif (error instanceof ProblemDetailsError) {\n\t\t\treturn toResponse(error.problemDetails, c, options);\n\t\t}\n\n\t\tif (options.mapError) {\n\t\t\tconst mapped = options.mapError(error);\n\t\t\tif (mapped) {\n\t\t\t\treturn toResponse(mapped, c, options);\n\t\t\t}\n\t\t}\n\n\t\tif (error instanceof HTTPException) {\n\t\t\treturn toResponse(\n\t\t\t\t{\n\t\t\t\t\tstatus: error.status,\n\t\t\t\t\ttype: buildType(error.status, options),\n\t\t\t\t\ttitle: statusToPhrase(error.status),\n\t\t\t\t\tdetail: error.message,\n\t\t\t\t},\n\t\t\t\tc,\n\t\t\t\toptions,\n\t\t\t);\n\t\t}\n\n\t\treturn toResponse(\n\t\t\t{\n\t\t\t\tstatus: 500,\n\t\t\t\ttype: buildType(500, options),\n\t\t\t\ttitle: \"Internal Server Error\",\n\t\t\t\tdetail: options.includeStack ? error.stack : undefined,\n\t\t\t},\n\t\t\tc,\n\t\t\toptions,\n\t\t);\n\t};\n}\n","import { PROBLEM_JSON_CONTENT_TYPE, sanitizeExtensions } from \"./handler.js\";\nimport { statusToPhrase } from \"./status.js\";\nimport type { ProblemDetails, ProblemDetailsInput } from \"./types.js\";\n\nfunction normalizeProblemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetails<T> {\n\treturn {\n\t\ttype: input.type ?? \"about:blank\",\n\t\tstatus: input.status,\n\t\ttitle: input.title ?? statusToPhrase(input.status) ?? \"Unknown Error\",\n\t\tdetail: input.detail,\n\t\tinstance: input.instance,\n\t\textensions: input.extensions,\n\t};\n}\n\nexport class ProblemDetailsError extends Error {\n\treadonly problemDetails: ProblemDetails;\n\n\tconstructor(input: ProblemDetailsInput) {\n\t\tconst pd = normalizeProblemDetails(input);\n\t\tsuper(pd.detail ?? pd.title);\n\t\tthis.name = \"ProblemDetailsError\";\n\t\tthis.problemDetails = pd;\n\t}\n\n\tgetResponse(): Response {\n\t\tconst { extensions, ...standard } = this.problemDetails;\n\t\tconst body = { ...sanitizeExtensions(extensions), ...standard };\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: this.problemDetails.status,\n\t\t\theaders: { \"Content-Type\": PROBLEM_JSON_CONTENT_TYPE },\n\t\t});\n\t}\n}\n","import { ProblemDetailsError } from \"./error.js\";\nimport type { ProblemDetailsInput } from \"./types.js\";\n\nexport function problemDetails<T extends Record<string, unknown>>(\n\tinput: ProblemDetailsInput<T>,\n): ProblemDetailsError {\n\treturn new ProblemDetailsError(input);\n}\n","import { ProblemDetailsError } from \"./error.js\";\n\ninterface ProblemTypeDefinition {\n\ttype: string;\n\tstatus: number;\n\ttitle: string;\n}\n\ninterface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {\n\tdetail?: string;\n\tinstance?: string;\n\textensions?: T;\n}\n\ninterface ProblemTypeRegistry<K extends string> {\n\tcreate: <T extends Record<string, unknown>>(\n\t\tkey: K,\n\t\toptions?: CreateOptions<T>,\n\t) => ProblemDetailsError;\n\tget: (key: K) => ProblemTypeDefinition;\n\ttypes: () => K[];\n}\n\n/**\n * Create a registry of pre-defined problem types.\n * Provides type-safe error creation from registered definitions.\n */\nexport function createProblemTypeRegistry<K extends string>(\n\tdefinitions: Record<K, ProblemTypeDefinition>,\n): ProblemTypeRegistry<K> {\n\treturn {\n\t\tcreate: (key, options) => {\n\t\t\tconst def = definitions[key];\n\t\t\treturn new ProblemDetailsError({\n\t\t\t\ttype: def.type,\n\t\t\t\tstatus: def.status,\n\t\t\t\ttitle: def.title,\n\t\t\t\tdetail: options?.detail,\n\t\t\t\tinstance: options?.instance,\n\t\t\t\textensions: options?.extensions,\n\t\t\t});\n\t\t},\n\t\tget: (key) => definitions[key],\n\t\ttypes: () => Object.keys(definitions) as K[],\n\t};\n}\n"],"mappings":";;;;;;AACA,SAAS,qBAAqB;AAKvB,IAAM,4BAA4B;AAEzC,IAAM,iBAAiB,oBAAI,IAAI,CAAC,aAAa,eAAe,WAAW,CAAC;AAGjE,SAAS,mBACf,YACsC;AACtC,MAAI,CAAC,WAAY,QAAO;AACxB,MAAI;AACJ,aAAW,OAAO,OAAO,KAAK,UAAU,GAAG;AAC1C,QAAI,eAAe,IAAI,GAAG,GAAG;AAC5B,UAAI,CAAC,SAAU,YAAW,EAAE,GAAG,WAAW;AAC1C,aAAO,SAAS,GAAG;AAAA,IACpB;AAAA,EACD;AACA,SAAO,YAAY;AACpB;AAEA,SAAS,UAAU,QAAgB,SAA+C;AACjF,MAAI,QAAQ,YAAY;AACvB,UAAM,OAAO,aAAa,MAAM;AAChC,QAAI,KAAM,QAAO,GAAG,QAAQ,UAAU,IAAI,IAAI;AAAA,EAC/C;AACA,SAAO,QAAQ,eAAe;AAC/B;AAEA,SAAS,WACR,OACA,GACA,SACW;AACX,MAAI,KAAK;AAAA,IACR,MAAM,MAAM,QAAQ;AAAA,IACpB,QAAQ,MAAM;AAAA,IACd,OAAO,MAAM,SAAS,eAA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names":[]}
|
|
@@ -50,6 +50,12 @@ var STATUS_PHRASES = {
|
|
|
50
50
|
503: "Service Unavailable",
|
|
51
51
|
504: "Gateway Timeout"
|
|
52
52
|
};
|
|
53
|
+
var STATUS_SLUGS = Object.fromEntries(
|
|
54
|
+
Object.entries(STATUS_PHRASES).map(([code, phrase]) => [
|
|
55
|
+
Number(code),
|
|
56
|
+
phrase.toLowerCase().replace(/ /g, "-")
|
|
57
|
+
])
|
|
58
|
+
);
|
|
53
59
|
function statusToPhrase(status) {
|
|
54
60
|
return STATUS_PHRASES[status];
|
|
55
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/openapi.ts","../../src/status.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/openapi.ts","../../src/status.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema: z.ZodObject<{\n\ttype: z.ZodString;\n\tstatus: z.ZodNumber;\n\ttitle: z.ZodString;\n\tdetail: z.ZodOptional<z.ZodString>;\n\tinstance: z.ZodOptional<z.ZodString>;\n}> = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(\n\textensions: z.ZodObject<T>,\n): z.AnyZodObject {\n\treturn ProblemDetailsSchema.merge(extensions).openapi({ title: \"ProblemDetails\" });\n}\n\n/**\n * Create an OpenAPI response object for Problem Details.\n * Use in createRoute() responses: { 422: problemDetailsResponse(422) }\n */\nexport function problemDetailsResponse(\n\tstatus: number,\n\tdescription?: string,\n\tschema: z.ZodTypeAny = ProblemDetailsSchema,\n): {\n\tcontent: { \"application/problem+json\": { schema: z.ZodTypeAny } };\n\tdescription: string;\n} {\n\treturn {\n\t\tcontent: {\n\t\t\t\"application/problem+json\": { schema },\n\t\t},\n\t\tdescription: description ?? statusToPhrase(status) ?? \"Error\",\n\t};\n}\n","const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nconst STATUS_SLUGS: Record<number, string> = Object.fromEntries(\n\tObject.entries(STATUS_PHRASES).map(([code, phrase]) => [\n\t\tNumber(code),\n\t\tphrase.toLowerCase().replace(/ /g, \"-\"),\n\t]),\n);\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\treturn STATUS_SLUGS[status];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,yBAAkB;;;ACAlB,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEA,IAAM,eAAuC,OAAO;AAAA,EACnD,OAAO,QAAQ,cAAc,EAAE,IAAI,CAAC,CAAC,MAAM,MAAM,MAAM;AAAA,IACtD,OAAO,IAAI;AAAA,IACX,OAAO,YAAY,EAAE,QAAQ,MAAM,GAAG;AAAA,EACvC,CAAC;AACF;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;;;ADzBO,IAAM,uBAMR,qBACH,OAAO;AAAA,EACP,MAAM,qBAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,cAAc,CAAC;AAAA,EACpF,QAAQ,qBAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,IAAI,CAAC;AAAA,EAClF,OAAO,qBACL,OAAO,EACP,QAAQ,EAAE,aAAa,qCAAqC,SAAS,cAAc,CAAC;AAAA,EACtF,QAAQ,qBAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,6BAA6B,CAAC;AAAA,EACnF,UAAU,qBAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,iCAAiC,CAAC;AAC1F,CAAC,EACA,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAM9B,SAAS,2BACf,YACiB;AACjB,SAAO,qBAAqB,MAAM,UAAU,EAAE,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAClF;AAMO,SAAS,uBACf,QACA,aACA,SAAuB,sBAItB;AACD,SAAO;AAAA,IACN,SAAS;AAAA,MACR,4BAA4B,EAAE,OAAO;AAAA,IACtC;AAAA,IACA,aAAa,eAAe,eAAe,MAAM,KAAK;AAAA,EACvD;AACD;","names":[]}
|
|
@@ -10,42 +10,12 @@ declare const ProblemDetailsSchema: z.ZodObject<{
|
|
|
10
10
|
title: z.ZodString;
|
|
11
11
|
detail: z.ZodOptional<z.ZodString>;
|
|
12
12
|
instance: z.ZodOptional<z.ZodString>;
|
|
13
|
-
}, "strip", z.ZodTypeAny, {
|
|
14
|
-
status: number;
|
|
15
|
-
type: string;
|
|
16
|
-
title: string;
|
|
17
|
-
detail?: string | undefined;
|
|
18
|
-
instance?: string | undefined;
|
|
19
|
-
}, {
|
|
20
|
-
status: number;
|
|
21
|
-
type: string;
|
|
22
|
-
title: string;
|
|
23
|
-
detail?: string | undefined;
|
|
24
|
-
instance?: string | undefined;
|
|
25
13
|
}>;
|
|
26
14
|
/**
|
|
27
15
|
* Create a Problem Details schema with typed extension members.
|
|
28
16
|
* Extensions are merged at top level per RFC 9457.
|
|
29
17
|
*/
|
|
30
|
-
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.
|
|
31
|
-
type: z.ZodString;
|
|
32
|
-
status: z.ZodNumber;
|
|
33
|
-
title: z.ZodString;
|
|
34
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
35
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
36
|
-
}, T>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<z.objectUtil.extendShape<{
|
|
37
|
-
type: z.ZodString;
|
|
38
|
-
status: z.ZodNumber;
|
|
39
|
-
title: z.ZodString;
|
|
40
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
41
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
42
|
-
}, T>>, any> extends infer T_1 ? { [k in keyof T_1]: T_1[k]; } : never, z.baseObjectInputType<z.objectUtil.extendShape<{
|
|
43
|
-
type: z.ZodString;
|
|
44
|
-
status: z.ZodNumber;
|
|
45
|
-
title: z.ZodString;
|
|
46
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
47
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
48
|
-
}, T>> extends infer T_2 ? { [k_1 in keyof T_2]: T_2[k_1]; } : never>;
|
|
18
|
+
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.AnyZodObject;
|
|
49
19
|
/**
|
|
50
20
|
* Create an OpenAPI response object for Problem Details.
|
|
51
21
|
* Use in createRoute() responses: { 422: problemDetailsResponse(422) }
|
|
@@ -10,42 +10,12 @@ declare const ProblemDetailsSchema: z.ZodObject<{
|
|
|
10
10
|
title: z.ZodString;
|
|
11
11
|
detail: z.ZodOptional<z.ZodString>;
|
|
12
12
|
instance: z.ZodOptional<z.ZodString>;
|
|
13
|
-
}, "strip", z.ZodTypeAny, {
|
|
14
|
-
status: number;
|
|
15
|
-
type: string;
|
|
16
|
-
title: string;
|
|
17
|
-
detail?: string | undefined;
|
|
18
|
-
instance?: string | undefined;
|
|
19
|
-
}, {
|
|
20
|
-
status: number;
|
|
21
|
-
type: string;
|
|
22
|
-
title: string;
|
|
23
|
-
detail?: string | undefined;
|
|
24
|
-
instance?: string | undefined;
|
|
25
13
|
}>;
|
|
26
14
|
/**
|
|
27
15
|
* Create a Problem Details schema with typed extension members.
|
|
28
16
|
* Extensions are merged at top level per RFC 9457.
|
|
29
17
|
*/
|
|
30
|
-
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.
|
|
31
|
-
type: z.ZodString;
|
|
32
|
-
status: z.ZodNumber;
|
|
33
|
-
title: z.ZodString;
|
|
34
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
35
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
36
|
-
}, T>, z.UnknownKeysParam, z.ZodTypeAny, z.objectUtil.addQuestionMarks<z.baseObjectOutputType<z.objectUtil.extendShape<{
|
|
37
|
-
type: z.ZodString;
|
|
38
|
-
status: z.ZodNumber;
|
|
39
|
-
title: z.ZodString;
|
|
40
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
41
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
42
|
-
}, T>>, any> extends infer T_1 ? { [k in keyof T_1]: T_1[k]; } : never, z.baseObjectInputType<z.objectUtil.extendShape<{
|
|
43
|
-
type: z.ZodString;
|
|
44
|
-
status: z.ZodNumber;
|
|
45
|
-
title: z.ZodString;
|
|
46
|
-
detail: z.ZodOptional<z.ZodString>;
|
|
47
|
-
instance: z.ZodOptional<z.ZodString>;
|
|
48
|
-
}, T>> extends infer T_2 ? { [k_1 in keyof T_2]: T_2[k_1]; } : never>;
|
|
18
|
+
declare function createProblemDetailsSchema<T extends z.ZodRawShape>(extensions: z.ZodObject<T>): z.AnyZodObject;
|
|
49
19
|
/**
|
|
50
20
|
* Create an OpenAPI response object for Problem Details.
|
|
51
21
|
* Use in createRoute() responses: { 422: problemDetailsResponse(422) }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/openapi.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/openapi.ts"],"sourcesContent":["import { z } from \"@hono/zod-openapi\";\nimport { statusToPhrase } from \"../status.js\";\n\n/**\n * Base RFC 9457 Problem Details Zod schema for OpenAPI documentation.\n * Use in createRoute() response definitions with @hono/zod-openapi.\n */\nexport const ProblemDetailsSchema: z.ZodObject<{\n\ttype: z.ZodString;\n\tstatus: z.ZodNumber;\n\ttitle: z.ZodString;\n\tdetail: z.ZodOptional<z.ZodString>;\n\tinstance: z.ZodOptional<z.ZodString>;\n}> = z\n\t.object({\n\t\ttype: z.string().openapi({ description: \"Problem type URI\", example: \"about:blank\" }),\n\t\tstatus: z.number().int().openapi({ description: \"HTTP status code\", example: 400 }),\n\t\ttitle: z\n\t\t\t.string()\n\t\t\t.openapi({ description: \"Short summary of the problem type\", example: \"Bad Request\" }),\n\t\tdetail: z.string().optional().openapi({ description: \"Human-readable explanation\" }),\n\t\tinstance: z.string().optional().openapi({ description: \"URI identifying the occurrence\" }),\n\t})\n\t.openapi({ title: \"ProblemDetails\" });\n\n/**\n * Create a Problem Details schema with typed extension members.\n * Extensions are merged at top level per RFC 9457.\n */\nexport function createProblemDetailsSchema<T extends z.ZodRawShape>(\n\textensions: z.ZodObject<T>,\n): z.AnyZodObject {\n\treturn ProblemDetailsSchema.merge(extensions).openapi({ title: \"ProblemDetails\" });\n}\n\n/**\n * Create an OpenAPI response object for Problem Details.\n * Use in createRoute() responses: { 422: problemDetailsResponse(422) }\n */\nexport function problemDetailsResponse(\n\tstatus: number,\n\tdescription?: string,\n\tschema: z.ZodTypeAny = ProblemDetailsSchema,\n): {\n\tcontent: { \"application/problem+json\": { schema: z.ZodTypeAny } };\n\tdescription: string;\n} {\n\treturn {\n\t\tcontent: {\n\t\t\t\"application/problem+json\": { schema },\n\t\t},\n\t\tdescription: description ?? statusToPhrase(status) ?? \"Error\",\n\t};\n}\n"],"mappings":";;;;;AAAA,SAAS,SAAS;AAOX,IAAM,uBAMR,EACH,OAAO;AAAA,EACP,MAAM,EAAE,OAAO,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,cAAc,CAAC;AAAA,EACpF,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,aAAa,oBAAoB,SAAS,IAAI,CAAC;AAAA,EAClF,OAAO,EACL,OAAO,EACP,QAAQ,EAAE,aAAa,qCAAqC,SAAS,cAAc,CAAC;AAAA,EACtF,QAAQ,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,6BAA6B,CAAC;AAAA,EACnF,UAAU,EAAE,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,aAAa,iCAAiC,CAAC;AAC1F,CAAC,EACA,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAM9B,SAAS,2BACf,YACiB;AACjB,SAAO,qBAAqB,MAAM,UAAU,EAAE,QAAQ,EAAE,OAAO,iBAAiB,CAAC;AAClF;AAMO,SAAS,uBACf,QACA,aACA,SAAuB,sBAItB;AACD,SAAO;AAAA,IACN,SAAS;AAAA,MACR,4BAA4B,EAAE,OAAO;AAAA,IACtC;AAAA,IACA,aAAa,eAAe,eAAe,MAAM,KAAK;AAAA,EACvD;AACD;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\n\nexport interface StandardSchemaProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n}\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\n\nexport interface StandardSchemaProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n}\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: StandardSchemaProblemHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\n\nexport interface StandardSchemaProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n}\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/standard-schema.ts"],"sourcesContent":["import type { StandardSchemaV1 } from \"@standard-schema/spec\";\nimport type { Context } from \"hono\";\n\nexport interface StandardSchemaProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n}\n\nfunction formatPath(path: ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment>): string {\n\treturn path\n\t\t.map((segment) => (typeof segment === \"object\" ? String(segment.key) : String(segment)))\n\t\t.join(\".\");\n}\n\nfunction formatIssues(issues: readonly StandardSchemaV1.Issue[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path ? formatPath(issue.path) : \"\",\n\t\tmessage: issue.message,\n\t}));\n}\n\nexport function standardSchemaProblemHook(\n\toptions?: StandardSchemaProblemHookOptions,\n): (\n\tresult:\n\t\t| { success: true; data: unknown }\n\t\t| { success: false; error: readonly StandardSchemaV1.Issue[] },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t};\n}\n"],"mappings":";AAaA,SAAS,WAAW,MAAyE;AAC5F,SAAO,KACL,IAAI,CAAC,YAAa,OAAO,YAAY,WAAW,OAAO,QAAQ,GAAG,IAAI,OAAO,OAAO,CAAE,EACtF,KAAK,GAAG;AACX;AAEA,SAAS,aAAa,QAA8D;AACnF,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,OAAO,WAAW,MAAM,IAAI,IAAI;AAAA,IAC7C,SAAS,MAAM;AAAA,EAChB,EAAE;AACH;AAEO,SAAS,0BACf,SAMyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\n\nexport interface ValibotProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\n\nexport interface ValibotProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValibotProblemHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.issues),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,MAAM;AAAA,IACnC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\n\nexport interface ValibotProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/valibot.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { BaseIssue, SafeParseResult } from \"valibot\";\n\nexport interface ValibotProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatIssues(issues: BaseIssue<unknown>[]): ValidationError[] {\n\treturn issues.map((issue) => ({\n\t\tfield: issue.path?.map((p) => p.key).join(\".\") ?? \"\",\n\t\tmessage: issue.message,\n\t\tcode: issue.type,\n\t}));\n}\n\nexport function valibotProblemHook(\n\toptions?: ValibotProblemHookOptions,\n): (result: SafeParseResult<never> & { target: string }, c: Context) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatIssues(result.issues),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t};\n}\n"],"mappings":";AAcA,SAAS,aAAa,QAAiD;AACtE,SAAO,OAAO,IAAI,CAAC,WAAW;AAAA,IAC7B,OAAO,MAAM,MAAM,IAAI,CAAC,MAAM,EAAE,GAAG,EAAE,KAAK,GAAG,KAAK;AAAA,IAClD,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,mBACf,SAC4F;AAC5F,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,MAAM;AAAA,IACnC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\n\nexport interface ZodProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\n\nexport interface ZodProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ZodProblemHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatErrors(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAcA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;","names":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\n\nexport interface ZodProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(
|
|
1
|
+
{"version":3,"sources":["../../src/integrations/zod.ts"],"sourcesContent":["import type { Context } from \"hono\";\nimport type { ZodError } from \"zod\";\n\nexport interface ZodProblemHookOptions {\n\ttitle?: string;\n\tdetail?: string;\n}\n\ninterface ValidationError {\n\tfield: string;\n\tmessage: string;\n\tcode: string;\n}\n\nfunction formatErrors(zodError: ZodError): ValidationError[] {\n\treturn zodError.issues.map((issue) => ({\n\t\tfield: issue.path.join(\".\"),\n\t\tmessage: issue.message,\n\t\tcode: issue.code,\n\t}));\n}\n\nexport function zodProblemHook(\n\toptions?: ZodProblemHookOptions,\n): (\n\tresult: { success: true; data: unknown } | { success: false; error: ZodError },\n\tc: Context,\n) => Response | undefined {\n\treturn (result, c) => {\n\t\tif (result.success) return;\n\n\t\tconst body = {\n\t\t\ttype: \"about:blank\",\n\t\t\tstatus: 422,\n\t\t\ttitle: options?.title ?? \"Validation Error\",\n\t\t\tdetail: options?.detail ?? \"Request validation failed\",\n\t\t\terrors: formatErrors(result.error),\n\t\t};\n\n\t\treturn new Response(JSON.stringify(body), {\n\t\t\tstatus: 422,\n\t\t\theaders: { \"Content-Type\": \"application/problem+json\" },\n\t\t});\n\t};\n}\n"],"mappings":";AAcA,SAAS,aAAa,UAAuC;AAC5D,SAAO,SAAS,OAAO,IAAI,CAAC,WAAW;AAAA,IACtC,OAAO,MAAM,KAAK,KAAK,GAAG;AAAA,IAC1B,SAAS,MAAM;AAAA,IACf,MAAM,MAAM;AAAA,EACb,EAAE;AACH;AAEO,SAAS,eACf,SAIyB;AACzB,SAAO,CAAC,QAAQ,MAAM;AACrB,QAAI,OAAO,QAAS;AAEpB,UAAM,OAAO;AAAA,MACZ,MAAM;AAAA,MACN,QAAQ;AAAA,MACR,OAAO,SAAS,SAAS;AAAA,MACzB,QAAQ,SAAS,UAAU;AAAA,MAC3B,QAAQ,aAAa,OAAO,KAAK;AAAA,IAClC;AAEA,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ;AAAA,MACR,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;","names":[]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/status.ts"],"sourcesContent":["const STATUS_PHRASES: Record<number, string> = {\n\t400: \"Bad Request\",\n\t401: \"Unauthorized\",\n\t403: \"Forbidden\",\n\t404: \"Not Found\",\n\t405: \"Method Not Allowed\",\n\t406: \"Not Acceptable\",\n\t408: \"Request Timeout\",\n\t409: \"Conflict\",\n\t410: \"Gone\",\n\t411: \"Length Required\",\n\t412: \"Precondition Failed\",\n\t413: \"Content Too Large\",\n\t415: \"Unsupported Media Type\",\n\t422: \"Unprocessable Content\",\n\t429: \"Too Many Requests\",\n\t500: \"Internal Server Error\",\n\t501: \"Not Implemented\",\n\t502: \"Bad Gateway\",\n\t503: \"Service Unavailable\",\n\t504: \"Gateway Timeout\",\n};\n\nexport function statusToPhrase(status: number): string | undefined {\n\treturn STATUS_PHRASES[status];\n}\n\nexport function statusToSlug(status: number): string | undefined {\n\tconst phrase = STATUS_PHRASES[status];\n\tif (!phrase) return undefined;\n\treturn phrase.toLowerCase().replace(/ /g, \"-\");\n}\n"],"mappings":";AAAA,IAAM,iBAAyC;AAAA,EAC9C,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AAAA,EACL,KAAK;AACN;AAEO,SAAS,eAAe,QAAoC;AAClE,SAAO,eAAe,MAAM;AAC7B;AAEO,SAAS,aAAa,QAAoC;AAChE,QAAM,SAAS,eAAe,MAAM;AACpC,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,OAAO,YAAY,EAAE,QAAQ,MAAM,GAAG;AAC9C;","names":[]}
|