hono-problem-details 0.2.1 → 0.3.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/README.md +18 -4
- package/dist/index.cjs +3 -0
- package/dist/index.d.cts +12 -2
- package/dist/index.d.ts +12 -2
- package/dist/index.js +3 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -125,6 +125,11 @@ throw problemDetails({
|
|
|
125
125
|
`instance` points at the specific occurrence — clients can use it as a key for retry logic
|
|
126
126
|
or deduplication.
|
|
127
127
|
|
|
128
|
+
> **Auto-fill shortcuts.** `title` is optional — when omitted, the standard HTTP reason phrase
|
|
129
|
+
> for `status` is used (`404` → `"Not Found"`). Similarly, `instance` can be populated from
|
|
130
|
+
> the request path automatically via `problemDetailsHandler({ autoInstance: true })`. Both
|
|
131
|
+
> shortcuts skip the boilerplate in the example above; explicit values always win.
|
|
132
|
+
|
|
128
133
|
### Conflict — 409
|
|
129
134
|
|
|
130
135
|
```ts
|
|
@@ -337,14 +342,16 @@ const errorWithExtensions = createProblemDetailsSchema(
|
|
|
337
342
|
|
|
338
343
|
## Localization
|
|
339
344
|
|
|
340
|
-
Use the `localize` callback to translate `title` and `detail` based on the request context
|
|
345
|
+
Use the `localize` callback to translate `title` and `detail` based on the request context.
|
|
346
|
+
Return a partial patch with just the fields you want to override — everything else falls
|
|
347
|
+
through unchanged. Returning nothing (or `undefined`) leaves the response untouched.
|
|
341
348
|
|
|
342
349
|
```ts
|
|
343
350
|
problemDetailsHandler({
|
|
344
351
|
localize: (pd, c) => {
|
|
345
352
|
const lang = c.req.header("Accept-Language");
|
|
346
353
|
if (lang?.startsWith("ja")) {
|
|
347
|
-
return {
|
|
354
|
+
return { title: translate("ja", pd.title) };
|
|
348
355
|
}
|
|
349
356
|
return pd;
|
|
350
357
|
},
|
|
@@ -376,8 +383,15 @@ problemDetailsHandler({
|
|
|
376
383
|
// Include stack trace in detail (for development)
|
|
377
384
|
includeStack: process.env.NODE_ENV === "development",
|
|
378
385
|
|
|
379
|
-
//
|
|
380
|
-
|
|
386
|
+
// Populate `instance` from `c.req.path` when the thrown problem didn't specify one
|
|
387
|
+
autoInstance: true,
|
|
388
|
+
|
|
389
|
+
// Localize title/detail before sending the response.
|
|
390
|
+
// Return a partial patch — fields you omit fall through from the original.
|
|
391
|
+
localize: (pd, c) => {
|
|
392
|
+
const lang = c.req.header("Accept-Language") ?? "en";
|
|
393
|
+
return { title: `[${lang}] ${pd.title}` };
|
|
394
|
+
},
|
|
381
395
|
|
|
382
396
|
// Custom error mapping
|
|
383
397
|
mapError: (error) => {
|
package/dist/index.cjs
CHANGED
|
@@ -171,6 +171,9 @@ function buildType(status, options) {
|
|
|
171
171
|
}
|
|
172
172
|
function toResponse(input, c, options) {
|
|
173
173
|
let pd = normalizeProblemDetails(input);
|
|
174
|
+
if (options.autoInstance && pd.instance === void 0) {
|
|
175
|
+
pd = { ...pd, instance: c.req.path };
|
|
176
|
+
}
|
|
174
177
|
if (options.localize) {
|
|
175
178
|
try {
|
|
176
179
|
pd = { ...pd, ...options.localize(pd, c) };
|
package/dist/index.d.cts
CHANGED
|
@@ -40,10 +40,20 @@ interface ProblemDetailsHandlerOptions {
|
|
|
40
40
|
defaultType?: string;
|
|
41
41
|
/** Include stack trace in detail (for development) */
|
|
42
42
|
includeStack?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* When `true`, populate `instance` from the request path (`c.req.path`) if
|
|
45
|
+
* the thrown problem didn't specify one. Explicit values always win.
|
|
46
|
+
* Default: `false` — opt-in to avoid silently changing response shape.
|
|
47
|
+
*/
|
|
48
|
+
autoInstance?: boolean;
|
|
43
49
|
/** Custom error to ProblemDetails mapping */
|
|
44
50
|
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
45
|
-
/**
|
|
46
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Localize title/detail before sending the response.
|
|
53
|
+
* Returned fields are merged onto the original ProblemDetails, so callers may
|
|
54
|
+
* return a partial patch (e.g. `{ title: "..." }`) or omit the return entirely.
|
|
55
|
+
*/
|
|
56
|
+
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
47
57
|
}
|
|
48
58
|
|
|
49
59
|
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
package/dist/index.d.ts
CHANGED
|
@@ -40,10 +40,20 @@ interface ProblemDetailsHandlerOptions {
|
|
|
40
40
|
defaultType?: string;
|
|
41
41
|
/** Include stack trace in detail (for development) */
|
|
42
42
|
includeStack?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* When `true`, populate `instance` from the request path (`c.req.path`) if
|
|
45
|
+
* the thrown problem didn't specify one. Explicit values always win.
|
|
46
|
+
* Default: `false` — opt-in to avoid silently changing response shape.
|
|
47
|
+
*/
|
|
48
|
+
autoInstance?: boolean;
|
|
43
49
|
/** Custom error to ProblemDetails mapping */
|
|
44
50
|
mapError?: (error: Error) => ProblemDetailsInput | undefined;
|
|
45
|
-
/**
|
|
46
|
-
|
|
51
|
+
/**
|
|
52
|
+
* Localize title/detail before sending the response.
|
|
53
|
+
* Returned fields are merged onto the original ProblemDetails, so callers may
|
|
54
|
+
* return a partial patch (e.g. `{ title: "..." }`) or omit the return entirely.
|
|
55
|
+
*/
|
|
56
|
+
localize?: (pd: ProblemDetails, c: Context) => Partial<ProblemDetails> | undefined;
|
|
47
57
|
}
|
|
48
58
|
|
|
49
59
|
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
package/dist/index.js
CHANGED
|
@@ -44,6 +44,9 @@ function buildType(status, options) {
|
|
|
44
44
|
}
|
|
45
45
|
function toResponse(input, c, options) {
|
|
46
46
|
let pd = normalizeProblemDetails(input);
|
|
47
|
+
if (options.autoInstance && pd.instance === void 0) {
|
|
48
|
+
pd = { ...pd, instance: c.req.path };
|
|
49
|
+
}
|
|
47
50
|
if (options.localize) {
|
|
48
51
|
try {
|
|
49
52
|
pd = { ...pd, ...options.localize(pd, c) };
|