hono-problem-details 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +287 -0
- package/dist/chunk-ANLH2WEX.js +37 -0
- package/dist/chunk-ANLH2WEX.js.map +1 -0
- package/dist/index.cjs +191 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +84 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.js +132 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/openapi.cjs +82 -0
- package/dist/integrations/openapi.cjs.map +1 -0
- package/dist/integrations/openapi.d.cts +62 -0
- package/dist/integrations/openapi.d.ts +62 -0
- package/dist/integrations/openapi.js +30 -0
- package/dist/integrations/openapi.js.map +1 -0
- package/dist/integrations/standard-schema.cjs +55 -0
- package/dist/integrations/standard-schema.cjs.map +1 -0
- package/dist/integrations/standard-schema.d.cts +16 -0
- package/dist/integrations/standard-schema.d.ts +16 -0
- package/dist/integrations/standard-schema.js +30 -0
- package/dist/integrations/standard-schema.js.map +1 -0
- package/dist/integrations/valibot.cjs +53 -0
- package/dist/integrations/valibot.cjs.map +1 -0
- package/dist/integrations/valibot.d.cts +12 -0
- package/dist/integrations/valibot.d.ts +12 -0
- package/dist/integrations/valibot.js +28 -0
- package/dist/integrations/valibot.js.map +1 -0
- package/dist/integrations/zod.cjs +53 -0
- package/dist/integrations/zod.cjs.map +1 -0
- package/dist/integrations/zod.d.cts +16 -0
- package/dist/integrations/zod.d.ts +16 -0
- package/dist/integrations/zod.js +28 -0
- package/dist/integrations/zod.js.map +1 -0
- package/package.json +145 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 hono-problem-details contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
# hono-problem-details
|
|
2
|
+
|
|
3
|
+
[RFC 9457](https://www.rfc-editor.org/rfc/rfc9457.html) Problem Details middleware for [Hono](https://hono.dev).
|
|
4
|
+
|
|
5
|
+
Returns `application/problem+json` structured error responses with a single `app.onError` setup.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **RFC 9457 compliant** — standard 5 fields + extension members
|
|
10
|
+
- **Hono native** — `app.onError` handler + `createMiddleware()` patterns
|
|
11
|
+
- **Zod integration** — `@hono/zod-validator` hook for validation errors
|
|
12
|
+
- **Valibot integration** — `@hono/valibot-validator` hook for validation errors
|
|
13
|
+
- **OpenAPI integration** — `@hono/zod-openapi` schemas for API documentation
|
|
14
|
+
- **Standard Schema** — `@hono/standard-validator` hook (works with any schema library)
|
|
15
|
+
- **Type-safe** — full TypeScript support with inference
|
|
16
|
+
- **Zero external dependencies** — only `hono` as peer dependency
|
|
17
|
+
- **Localization** — `localize` callback for title/detail translation
|
|
18
|
+
- **Edge-first** — works on Cloudflare Workers, Deno, Bun, and Node.js
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install hono-problem-details
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Quick Start
|
|
27
|
+
|
|
28
|
+
```ts
|
|
29
|
+
import { Hono } from "hono";
|
|
30
|
+
import { problemDetailsHandler } from "hono-problem-details";
|
|
31
|
+
|
|
32
|
+
const app = new Hono();
|
|
33
|
+
|
|
34
|
+
app.onError(problemDetailsHandler());
|
|
35
|
+
|
|
36
|
+
app.get("/not-found", (c) => {
|
|
37
|
+
throw new HTTPException(404, { message: "Resource not found" });
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
// Response:
|
|
41
|
+
// HTTP/1.1 404 Not Found
|
|
42
|
+
// Content-Type: application/problem+json
|
|
43
|
+
// {
|
|
44
|
+
// "type": "about:blank",
|
|
45
|
+
// "status": 404,
|
|
46
|
+
// "title": "Not Found",
|
|
47
|
+
// "detail": "Resource not found"
|
|
48
|
+
// }
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Throwing Problem Details
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { problemDetails } from "hono-problem-details";
|
|
55
|
+
|
|
56
|
+
app.post("/orders", (c) => {
|
|
57
|
+
throw problemDetails({
|
|
58
|
+
status: 409,
|
|
59
|
+
title: "Conflict",
|
|
60
|
+
detail: `Order ${id} already exists`,
|
|
61
|
+
type: "https://api.example.com/problems/order-conflict",
|
|
62
|
+
instance: `/orders/${id}`,
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Extension Members
|
|
68
|
+
|
|
69
|
+
Extension members are flattened to top level per RFC 9457:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
throw problemDetails({
|
|
73
|
+
status: 422,
|
|
74
|
+
title: "Validation Error",
|
|
75
|
+
extensions: {
|
|
76
|
+
errors: [
|
|
77
|
+
{ field: "email", message: "must be a valid email" },
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Response body:
|
|
83
|
+
// {
|
|
84
|
+
// "type": "about:blank",
|
|
85
|
+
// "status": 422,
|
|
86
|
+
// "title": "Validation Error",
|
|
87
|
+
// "errors": [{ "field": "email", "message": "must be a valid email" }]
|
|
88
|
+
// }
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## Problem Type Registry
|
|
92
|
+
|
|
93
|
+
Pre-define your API's error types for type-safe error creation:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
import { createProblemTypeRegistry } from "hono-problem-details";
|
|
97
|
+
|
|
98
|
+
const problems = createProblemTypeRegistry({
|
|
99
|
+
ORDER_CONFLICT: {
|
|
100
|
+
type: "https://api.example.com/problems/order-conflict",
|
|
101
|
+
status: 409,
|
|
102
|
+
title: "Order Conflict",
|
|
103
|
+
},
|
|
104
|
+
RATE_LIMITED: {
|
|
105
|
+
type: "https://api.example.com/problems/rate-limited",
|
|
106
|
+
status: 429,
|
|
107
|
+
title: "Too Many Requests",
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Type-safe error creation
|
|
112
|
+
app.post("/orders", (c) => {
|
|
113
|
+
throw problems.create("ORDER_CONFLICT", {
|
|
114
|
+
detail: `Order ${id} already exists`,
|
|
115
|
+
instance: `/orders/${id}`,
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// With extensions
|
|
120
|
+
throw problems.create("RATE_LIMITED", {
|
|
121
|
+
extensions: { retryAfter: 60 },
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Zod Validator Integration
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
import { zValidator } from "@hono/zod-validator";
|
|
129
|
+
import { zodProblemHook } from "hono-problem-details/zod";
|
|
130
|
+
import { z } from "zod";
|
|
131
|
+
|
|
132
|
+
const schema = z.object({
|
|
133
|
+
email: z.string().email(),
|
|
134
|
+
age: z.number().positive(),
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
app.post("/users", zValidator("json", schema, zodProblemHook()), (c) => {
|
|
138
|
+
const data = c.req.valid("json");
|
|
139
|
+
// ...
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
// Validation error response:
|
|
143
|
+
// HTTP/1.1 422 Unprocessable Content
|
|
144
|
+
// Content-Type: application/problem+json
|
|
145
|
+
// {
|
|
146
|
+
// "type": "about:blank",
|
|
147
|
+
// "status": 422,
|
|
148
|
+
// "title": "Validation Error",
|
|
149
|
+
// "detail": "Request validation failed",
|
|
150
|
+
// "errors": [{ "field": "email", "message": "Invalid email", "code": "invalid_string" }]
|
|
151
|
+
// }
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Valibot Validator Integration
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
import { vValidator } from "@hono/valibot-validator";
|
|
158
|
+
import { valibotProblemHook } from "hono-problem-details/valibot";
|
|
159
|
+
import * as v from "valibot";
|
|
160
|
+
|
|
161
|
+
const schema = v.object({
|
|
162
|
+
email: v.pipe(v.string(), v.email()),
|
|
163
|
+
age: v.pipe(v.number(), v.minValue(1)),
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
app.post("/users", vValidator("json", schema, valibotProblemHook()), (c) => {
|
|
167
|
+
const data = c.req.valid("json");
|
|
168
|
+
// ...
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Standard Schema Integration
|
|
173
|
+
|
|
174
|
+
Works with any [Standard Schema](https://standardschema.dev/) compatible library (Zod, Valibot, ArkType, etc.):
|
|
175
|
+
|
|
176
|
+
```ts
|
|
177
|
+
import { sValidator } from "@hono/standard-validator";
|
|
178
|
+
import { standardSchemaProblemHook } from "hono-problem-details/standard-schema";
|
|
179
|
+
import { z } from "zod"; // or valibot, arktype, etc.
|
|
180
|
+
|
|
181
|
+
const schema = z.object({
|
|
182
|
+
email: z.string().email(),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
app.post("/users", sValidator("json", schema, standardSchemaProblemHook()), (c) => {
|
|
186
|
+
const data = c.req.valid("json");
|
|
187
|
+
// ...
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## OpenAPI Integration
|
|
192
|
+
|
|
193
|
+
Use with `@hono/zod-openapi` to document Problem Details error responses in your OpenAPI spec:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
import { OpenAPIHono, createRoute, z } from "@hono/zod-openapi";
|
|
197
|
+
import { problemDetailsHandler } from "hono-problem-details";
|
|
198
|
+
import {
|
|
199
|
+
ProblemDetailsSchema,
|
|
200
|
+
createProblemDetailsSchema,
|
|
201
|
+
problemDetailsResponse,
|
|
202
|
+
} from "hono-problem-details/openapi";
|
|
203
|
+
|
|
204
|
+
const app = new OpenAPIHono();
|
|
205
|
+
app.onError(problemDetailsHandler());
|
|
206
|
+
|
|
207
|
+
// Use problemDetailsResponse() in route definitions
|
|
208
|
+
const route = createRoute({
|
|
209
|
+
method: "get",
|
|
210
|
+
path: "/users/{id}",
|
|
211
|
+
request: {
|
|
212
|
+
params: z.object({ id: z.string() }),
|
|
213
|
+
},
|
|
214
|
+
responses: {
|
|
215
|
+
200: {
|
|
216
|
+
content: {
|
|
217
|
+
"application/json": {
|
|
218
|
+
schema: z.object({ id: z.string(), name: z.string() }),
|
|
219
|
+
},
|
|
220
|
+
},
|
|
221
|
+
description: "User found",
|
|
222
|
+
},
|
|
223
|
+
404: problemDetailsResponse(404),
|
|
224
|
+
422: problemDetailsResponse(422, "Validation Error"),
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
// With extension members
|
|
229
|
+
const errorWithExtensions = createProblemDetailsSchema(
|
|
230
|
+
z.object({
|
|
231
|
+
errors: z.array(z.object({ field: z.string(), message: z.string() })),
|
|
232
|
+
}),
|
|
233
|
+
);
|
|
234
|
+
// Use: problemDetailsResponse(422, "Validation Error", errorWithExtensions)
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
## Localization
|
|
238
|
+
|
|
239
|
+
Use the `localize` callback to translate `title` and `detail` based on the request context:
|
|
240
|
+
|
|
241
|
+
```ts
|
|
242
|
+
problemDetailsHandler({
|
|
243
|
+
localize: (pd, c) => {
|
|
244
|
+
const lang = c.req.header("Accept-Language");
|
|
245
|
+
if (lang?.startsWith("ja")) {
|
|
246
|
+
return { ...pd, title: translate("ja", pd.title) };
|
|
247
|
+
}
|
|
248
|
+
return pd;
|
|
249
|
+
},
|
|
250
|
+
});
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
The callback receives the fully-built `ProblemDetails` object and the Hono `Context`, allowing access to headers like `Accept-Language`. Return a new `ProblemDetails` with translated fields.
|
|
254
|
+
|
|
255
|
+
## Handler Options
|
|
256
|
+
|
|
257
|
+
```ts
|
|
258
|
+
problemDetailsHandler({
|
|
259
|
+
// Prefix for type URI (e.g., "https://api.example.com/problems")
|
|
260
|
+
typePrefix: "https://api.example.com/problems",
|
|
261
|
+
|
|
262
|
+
// Default type URI (default: "about:blank")
|
|
263
|
+
defaultType: "about:blank",
|
|
264
|
+
|
|
265
|
+
// Include stack trace in detail (for development)
|
|
266
|
+
includeStack: process.env.NODE_ENV === "development",
|
|
267
|
+
|
|
268
|
+
// Localize title/detail before sending the response
|
|
269
|
+
localize: (pd, c) => ({ ...pd, title: `[${lang}] ${pd.title}` }),
|
|
270
|
+
|
|
271
|
+
// Custom error mapping
|
|
272
|
+
mapError: (error) => {
|
|
273
|
+
if (error instanceof MyCustomError) {
|
|
274
|
+
return {
|
|
275
|
+
status: error.statusCode,
|
|
276
|
+
title: error.name,
|
|
277
|
+
detail: error.message,
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
return undefined; // fallback to default handling
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## License
|
|
286
|
+
|
|
287
|
+
MIT
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// src/status.ts
|
|
2
|
+
var STATUS_PHRASES = {
|
|
3
|
+
400: "Bad Request",
|
|
4
|
+
401: "Unauthorized",
|
|
5
|
+
403: "Forbidden",
|
|
6
|
+
404: "Not Found",
|
|
7
|
+
405: "Method Not Allowed",
|
|
8
|
+
406: "Not Acceptable",
|
|
9
|
+
408: "Request Timeout",
|
|
10
|
+
409: "Conflict",
|
|
11
|
+
410: "Gone",
|
|
12
|
+
411: "Length Required",
|
|
13
|
+
412: "Precondition Failed",
|
|
14
|
+
413: "Content Too Large",
|
|
15
|
+
415: "Unsupported Media Type",
|
|
16
|
+
422: "Unprocessable Content",
|
|
17
|
+
429: "Too Many Requests",
|
|
18
|
+
500: "Internal Server Error",
|
|
19
|
+
501: "Not Implemented",
|
|
20
|
+
502: "Bad Gateway",
|
|
21
|
+
503: "Service Unavailable",
|
|
22
|
+
504: "Gateway Timeout"
|
|
23
|
+
};
|
|
24
|
+
function statusToPhrase(status) {
|
|
25
|
+
return STATUS_PHRASES[status];
|
|
26
|
+
}
|
|
27
|
+
function statusToSlug(status) {
|
|
28
|
+
const phrase = STATUS_PHRASES[status];
|
|
29
|
+
if (!phrase) return void 0;
|
|
30
|
+
return phrase.toLowerCase().replace(/ /g, "-");
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export {
|
|
34
|
+
statusToPhrase,
|
|
35
|
+
statusToSlug
|
|
36
|
+
};
|
|
37
|
+
//# sourceMappingURL=chunk-ANLH2WEX.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\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":[]}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
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);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
ProblemDetailsError: () => ProblemDetailsError,
|
|
24
|
+
createProblemTypeRegistry: () => createProblemTypeRegistry,
|
|
25
|
+
problemDetails: () => problemDetails,
|
|
26
|
+
problemDetailsHandler: () => problemDetailsHandler,
|
|
27
|
+
statusToPhrase: () => statusToPhrase,
|
|
28
|
+
statusToSlug: () => statusToSlug
|
|
29
|
+
});
|
|
30
|
+
module.exports = __toCommonJS(src_exports);
|
|
31
|
+
|
|
32
|
+
// src/status.ts
|
|
33
|
+
var STATUS_PHRASES = {
|
|
34
|
+
400: "Bad Request",
|
|
35
|
+
401: "Unauthorized",
|
|
36
|
+
403: "Forbidden",
|
|
37
|
+
404: "Not Found",
|
|
38
|
+
405: "Method Not Allowed",
|
|
39
|
+
406: "Not Acceptable",
|
|
40
|
+
408: "Request Timeout",
|
|
41
|
+
409: "Conflict",
|
|
42
|
+
410: "Gone",
|
|
43
|
+
411: "Length Required",
|
|
44
|
+
412: "Precondition Failed",
|
|
45
|
+
413: "Content Too Large",
|
|
46
|
+
415: "Unsupported Media Type",
|
|
47
|
+
422: "Unprocessable Content",
|
|
48
|
+
429: "Too Many Requests",
|
|
49
|
+
500: "Internal Server Error",
|
|
50
|
+
501: "Not Implemented",
|
|
51
|
+
502: "Bad Gateway",
|
|
52
|
+
503: "Service Unavailable",
|
|
53
|
+
504: "Gateway Timeout"
|
|
54
|
+
};
|
|
55
|
+
function statusToPhrase(status) {
|
|
56
|
+
return STATUS_PHRASES[status];
|
|
57
|
+
}
|
|
58
|
+
function statusToSlug(status) {
|
|
59
|
+
const phrase = STATUS_PHRASES[status];
|
|
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);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/handler.ts
|
|
99
|
+
var import_http_exception = require("hono/http-exception");
|
|
100
|
+
function buildType(status, options) {
|
|
101
|
+
if (options.typePrefix) {
|
|
102
|
+
const slug = statusToSlug(status);
|
|
103
|
+
if (slug) return `${options.typePrefix}/${slug}`;
|
|
104
|
+
}
|
|
105
|
+
return options.defaultType ?? "about:blank";
|
|
106
|
+
}
|
|
107
|
+
function toResponse(input, c, options) {
|
|
108
|
+
let pd = {
|
|
109
|
+
type: input.type ?? "about:blank",
|
|
110
|
+
status: input.status,
|
|
111
|
+
title: input.title ?? statusToPhrase(input.status) ?? "Unknown Error",
|
|
112
|
+
detail: input.detail,
|
|
113
|
+
instance: input.instance,
|
|
114
|
+
extensions: input.extensions
|
|
115
|
+
};
|
|
116
|
+
if (options.localize) {
|
|
117
|
+
pd = { ...pd, ...options.localize(pd, c) };
|
|
118
|
+
}
|
|
119
|
+
const { extensions, ...rest } = pd;
|
|
120
|
+
const body = { ...rest, ...extensions };
|
|
121
|
+
c.set("problemDetails", pd);
|
|
122
|
+
return new Response(JSON.stringify(body), {
|
|
123
|
+
status: pd.status,
|
|
124
|
+
headers: { "Content-Type": "application/problem+json" }
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
function problemDetailsHandler(options = {}) {
|
|
128
|
+
return (error, c) => {
|
|
129
|
+
if (error instanceof ProblemDetailsError) {
|
|
130
|
+
c.set("problemDetails", error.problemDetails);
|
|
131
|
+
return error.getResponse();
|
|
132
|
+
}
|
|
133
|
+
if (options.mapError) {
|
|
134
|
+
const mapped = options.mapError(error);
|
|
135
|
+
if (mapped) {
|
|
136
|
+
return toResponse(mapped, c, options);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (error instanceof import_http_exception.HTTPException) {
|
|
140
|
+
return toResponse(
|
|
141
|
+
{
|
|
142
|
+
status: error.status,
|
|
143
|
+
type: buildType(error.status, options),
|
|
144
|
+
title: statusToPhrase(error.status),
|
|
145
|
+
detail: error.message
|
|
146
|
+
},
|
|
147
|
+
c,
|
|
148
|
+
options
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
return toResponse(
|
|
152
|
+
{
|
|
153
|
+
status: 500,
|
|
154
|
+
type: buildType(500, options),
|
|
155
|
+
title: "Internal Server Error",
|
|
156
|
+
detail: options.includeStack ? error.stack : void 0
|
|
157
|
+
},
|
|
158
|
+
c,
|
|
159
|
+
options
|
|
160
|
+
);
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// src/registry.ts
|
|
165
|
+
function createProblemTypeRegistry(definitions) {
|
|
166
|
+
return {
|
|
167
|
+
create: (key, options) => {
|
|
168
|
+
const def = definitions[key];
|
|
169
|
+
return new ProblemDetailsError({
|
|
170
|
+
type: def.type,
|
|
171
|
+
status: def.status,
|
|
172
|
+
title: def.title,
|
|
173
|
+
detail: options?.detail,
|
|
174
|
+
instance: options?.instance,
|
|
175
|
+
extensions: options?.extensions
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
get: (key) => definitions[key],
|
|
179
|
+
types: () => Object.keys(definitions)
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
183
|
+
0 && (module.exports = {
|
|
184
|
+
ProblemDetailsError,
|
|
185
|
+
createProblemTypeRegistry,
|
|
186
|
+
problemDetails,
|
|
187
|
+
problemDetailsHandler,
|
|
188
|
+
statusToPhrase,
|
|
189
|
+
statusToSlug
|
|
190
|
+
});
|
|
191
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +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,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;;;AC5BA,SAAS,wBACR,OACoB;AACpB,SAAO;AAAA,IACN,MAAM,MAAM,QAAQ;AAAA,IACpB,QAAQ,MAAM;AAAA,IACd,OAAO,MAAM,SAAS,eAAe,MAAM,MAAM,KAAK;AAAA,IACtD,QAAQ,MAAM;AAAA,IACd,UAAU,MAAM;AAAA,IAChB,YAAY,MAAM;AAAA,EACnB;AACD;AAEO,IAAM,sBAAN,cAAkC,MAAM;AAAA,EACrC;AAAA,EAET,YAAY,OAA4B;AACvC,UAAM,KAAK,wBAAwB,KAAK;AACxC,UAAM,GAAG,UAAU,GAAG,KAAK;AAC3B,SAAK,OAAO;AACZ,SAAK,iBAAiB;AAAA,EACvB;AAAA,EAEA,cAAwB;AACvB,UAAM,EAAE,YAAY,GAAG,SAAS,IAAI,KAAK;AACzC,UAAM,OAAO,EAAE,GAAG,UAAU,GAAG,WAAW;AAC1C,WAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,MACzC,QAAQ,KAAK,eAAe;AAAA,MAC5B,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,IACvD,CAAC;AAAA,EACF;AACD;;;AC/BO,SAAS,eACf,OACsB;AACtB,SAAO,IAAI,oBAAoB,KAAK;AACrC;;;ACNA,4BAA8B;AAK9B,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,eAAe,MAAM,MAAM,KAAK;AAAA,IACtD,QAAQ,MAAM;AAAA,IACd,UAAU,MAAM;AAAA,IAChB,YAAY,MAAM;AAAA,EACnB;AAEA,MAAI,QAAQ,UAAU;AACrB,SAAK,EAAE,GAAG,IAAI,GAAG,QAAQ,SAAS,IAAI,CAAC,EAAE;AAAA,EAC1C;AAEA,QAAM,EAAE,YAAY,GAAG,KAAK,IAAI;AAChC,QAAM,OAAO,EAAE,GAAG,MAAM,GAAG,WAAW;AAEtC,IAAE,IAAI,kBAAkB,EAAE;AAE1B,SAAO,IAAI,SAAS,KAAK,UAAU,IAAI,GAAG;AAAA,IACzC,QAAQ,GAAG;AAAA,IACX,SAAS,EAAE,gBAAgB,2BAA2B;AAAA,EACvD,CAAC;AACF;AAEO,SAAS,sBAAsB,UAAwC,CAAC,GAAiB;AAC/F,SAAO,CAAC,OAAO,MAAM;AACpB,QAAI,iBAAiB,qBAAqB;AACzC,QAAE,IAAI,kBAAkB,MAAM,cAAc;AAC5C,aAAO,MAAM,YAAY;AAAA,IAC1B;AAEA,QAAI,QAAQ,UAAU;AACrB,YAAM,SAAS,QAAQ,SAAS,KAAK;AACrC,UAAI,QAAQ;AACX,eAAO,WAAW,QAAQ,GAAG,OAAO;AAAA,MACrC;AAAA,IACD;AAEA,QAAI,iBAAiB,qCAAe;AACnC,aAAO;AAAA,QACN;AAAA,UACC,QAAQ,MAAM;AAAA,UACd,MAAM,UAAU,MAAM,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":[]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as hono from 'hono';
|
|
2
|
+
import { ErrorHandler } from 'hono';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* RFC 9457 Problem Details object.
|
|
6
|
+
* Supports 5 standard fields + extension members.
|
|
7
|
+
*/
|
|
8
|
+
interface ProblemDetails<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
9
|
+
/** Problem type URI. Default: "about:blank" */
|
|
10
|
+
type: string;
|
|
11
|
+
/** HTTP status code */
|
|
12
|
+
status: number;
|
|
13
|
+
/** Short summary of the problem type */
|
|
14
|
+
title: string;
|
|
15
|
+
/** Human-readable explanation specific to this occurrence */
|
|
16
|
+
detail?: string;
|
|
17
|
+
/** URI that identifies the specific occurrence */
|
|
18
|
+
instance?: string;
|
|
19
|
+
/** RFC 9457 extension members (flattened to top level on serialization) */
|
|
20
|
+
extensions?: T;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Input for problemDetails() factory.
|
|
24
|
+
* type and title are optional (auto-derived from status).
|
|
25
|
+
*/
|
|
26
|
+
interface ProblemDetailsInput<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
27
|
+
status: number;
|
|
28
|
+
type?: string;
|
|
29
|
+
title?: string;
|
|
30
|
+
detail?: string;
|
|
31
|
+
instance?: string;
|
|
32
|
+
extensions?: T;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Options for problemDetailsHandler().
|
|
36
|
+
*/
|
|
37
|
+
interface ProblemDetailsHandlerOptions {
|
|
38
|
+
/** Prefix for type URI. When set, status-based suffix is appended */
|
|
39
|
+
typePrefix?: string;
|
|
40
|
+
/** Default type URI. Defaults to "about:blank" */
|
|
41
|
+
defaultType?: string;
|
|
42
|
+
/** Include stack trace in detail (for development) */
|
|
43
|
+
includeStack?: boolean;
|
|
44
|
+
/** Custom error to ProblemDetails mapping */
|
|
45
|
+
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
46
|
+
/** Localize title/detail before sending the response */
|
|
47
|
+
localize?: (pd: ProblemDetails, c: hono.Context) => ProblemDetails;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
declare function statusToPhrase(status: number): string | undefined;
|
|
51
|
+
declare function statusToSlug(status: number): string | undefined;
|
|
52
|
+
|
|
53
|
+
declare class ProblemDetailsError extends Error {
|
|
54
|
+
readonly problemDetails: ProblemDetails;
|
|
55
|
+
constructor(input: ProblemDetailsInput);
|
|
56
|
+
getResponse(): Response;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
declare function problemDetails<T extends Record<string, unknown>>(input: ProblemDetailsInput<T>): ProblemDetailsError;
|
|
60
|
+
|
|
61
|
+
declare function problemDetailsHandler(options?: ProblemDetailsHandlerOptions): ErrorHandler;
|
|
62
|
+
|
|
63
|
+
interface ProblemTypeDefinition {
|
|
64
|
+
type: string;
|
|
65
|
+
status: number;
|
|
66
|
+
title: string;
|
|
67
|
+
}
|
|
68
|
+
interface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
69
|
+
detail?: string;
|
|
70
|
+
instance?: string;
|
|
71
|
+
extensions?: T;
|
|
72
|
+
}
|
|
73
|
+
interface ProblemTypeRegistry<K extends string> {
|
|
74
|
+
create: <T extends Record<string, unknown>>(key: K, options?: CreateOptions<T>) => ProblemDetailsError;
|
|
75
|
+
get: (key: K) => ProblemTypeDefinition;
|
|
76
|
+
types: () => K[];
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Create a registry of pre-defined problem types.
|
|
80
|
+
* Provides type-safe error creation from registered definitions.
|
|
81
|
+
*/
|
|
82
|
+
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>): ProblemTypeRegistry<K>;
|
|
83
|
+
|
|
84
|
+
export { type ProblemDetails, ProblemDetailsError, type ProblemDetailsHandlerOptions, type ProblemDetailsInput, createProblemTypeRegistry, problemDetails, problemDetailsHandler, statusToPhrase, statusToSlug };
|