hono-problem-details 0.9.0 → 0.10.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 +47 -0
- package/dist/index.cjs +19 -4
- package/dist/index.d.cts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +19 -4
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -268,6 +268,53 @@ throw problems.create("RATE_LIMITED", {
|
|
|
268
268
|
});
|
|
269
269
|
```
|
|
270
270
|
|
|
271
|
+
### Auto-deriving a `code` extension
|
|
272
|
+
|
|
273
|
+
Pass `{ autoCode: true }` to derive a `code` extension from each registry key, so clients can
|
|
274
|
+
`switch` on a short flat string instead of parsing the `type` URI:
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
const problems = createProblemTypeRegistry(
|
|
278
|
+
{
|
|
279
|
+
ORDER_CONFLICT: {
|
|
280
|
+
type: "https://api.example.com/problems/order-conflict",
|
|
281
|
+
status: 409,
|
|
282
|
+
title: "Order Conflict",
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
{ autoCode: true },
|
|
286
|
+
);
|
|
287
|
+
|
|
288
|
+
const error = problems.create("ORDER_CONFLICT");
|
|
289
|
+
error.problemDetails.extensions; // { code: "order-conflict" }
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
The default transformation lowercases, collapses runs of underscores into a single hyphen, and
|
|
293
|
+
strips leading/trailing hyphens (`V2_AUTH` → `v2-auth`, `AUTH__TOKEN_EXPIRED` → `auth-token-expired`,
|
|
294
|
+
`_AUTH_` → `auth`). Keys are expected to be `SCREAMING_SNAKE_CASE`.
|
|
295
|
+
|
|
296
|
+
Two overrides take priority over the derived value, in this order:
|
|
297
|
+
|
|
298
|
+
1. An explicit `code` on the registry definition (emitted even without `autoCode`):
|
|
299
|
+
```ts
|
|
300
|
+
createProblemTypeRegistry(
|
|
301
|
+
{
|
|
302
|
+
ORDER_CONFLICT: {
|
|
303
|
+
type: "https://api.example.com/problems/order-conflict",
|
|
304
|
+
status: 409,
|
|
305
|
+
title: "Order Conflict",
|
|
306
|
+
code: "legacy/order_conflict",
|
|
307
|
+
},
|
|
308
|
+
},
|
|
309
|
+
{ autoCode: true },
|
|
310
|
+
);
|
|
311
|
+
```
|
|
312
|
+
2. `extensions.code` passed to `.create()`, which wins over both the derived value and an
|
|
313
|
+
explicit registry `code`.
|
|
314
|
+
|
|
315
|
+
`get(key)` returns the same resolved `code` that `create(key)` emits, so docs or schemas built
|
|
316
|
+
from the registry stay in sync with the responses.
|
|
317
|
+
|
|
271
318
|
### When to use the registry vs `problemDetails()`
|
|
272
319
|
|
|
273
320
|
Reach for `createProblemTypeRegistry` when your API has a fixed set of domain errors and you
|
package/dist/index.cjs
CHANGED
|
@@ -252,11 +252,26 @@ function problemDetailsHandler(options = {}) {
|
|
|
252
252
|
}
|
|
253
253
|
|
|
254
254
|
// src/registry.ts
|
|
255
|
-
function
|
|
255
|
+
function deriveCode(key) {
|
|
256
|
+
return key.toLowerCase().replace(/_+/g, "-").replace(/^-+|-+$/g, "");
|
|
257
|
+
}
|
|
258
|
+
function createProblemTypeRegistry(definitions, options) {
|
|
259
|
+
const autoCode = options?.autoCode ?? false;
|
|
260
|
+
const resolved = new Map(
|
|
261
|
+
Object.keys(definitions).map((key) => {
|
|
262
|
+
const definition = definitions[key];
|
|
263
|
+
const code = definition.code ?? (autoCode ? deriveCode(key) : void 0);
|
|
264
|
+
return [key, code === void 0 ? definition : { ...definition, code }];
|
|
265
|
+
})
|
|
266
|
+
);
|
|
256
267
|
return {
|
|
257
|
-
create: (key,
|
|
258
|
-
|
|
259
|
-
|
|
268
|
+
create: (key, createOptions) => {
|
|
269
|
+
const { code, ...definition } = resolved.get(key) ?? {};
|
|
270
|
+
const extensions = code !== void 0 ? { code, ...createOptions?.extensions } : createOptions?.extensions;
|
|
271
|
+
return new ProblemDetailsError({ ...definition, ...createOptions, extensions });
|
|
272
|
+
},
|
|
273
|
+
get: (key) => resolved.get(key),
|
|
274
|
+
types: () => [...resolved.keys()]
|
|
260
275
|
};
|
|
261
276
|
}
|
|
262
277
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.d.cts
CHANGED
|
@@ -143,16 +143,31 @@ interface ProblemTypeDefinition {
|
|
|
143
143
|
type: string;
|
|
144
144
|
status: number;
|
|
145
145
|
title: string;
|
|
146
|
+
/**
|
|
147
|
+
* Explicit `code` extension for this type. Always emitted, regardless of
|
|
148
|
+
* `autoCode`, and takes priority over an `autoCode`-derived value.
|
|
149
|
+
*/
|
|
150
|
+
code?: string;
|
|
146
151
|
}
|
|
147
152
|
interface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
148
153
|
detail?: string;
|
|
149
154
|
instance?: string;
|
|
150
155
|
extensions?: T;
|
|
151
156
|
}
|
|
157
|
+
interface RegistryOptions {
|
|
158
|
+
/**
|
|
159
|
+
* Derive a `code` extension from each registry key (SCREAMING_SNAKE -> kebab-case)
|
|
160
|
+
* when the definition doesn't set an explicit `code`. Default: `false`.
|
|
161
|
+
*/
|
|
162
|
+
autoCode?: boolean;
|
|
163
|
+
}
|
|
152
164
|
interface ProblemTypeRegistry<K extends string> {
|
|
153
165
|
/** Create a {@link ProblemDetailsError} from a registered problem type key. */
|
|
154
166
|
create: <T extends Record<string, unknown>>(key: K, options?: CreateOptions<T>) => ProblemDetailsError;
|
|
155
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* Get the resolved definition for a registered key, including any
|
|
169
|
+
* `autoCode`-derived or explicit `code` that `create()` would emit.
|
|
170
|
+
*/
|
|
156
171
|
get: (key: K) => ProblemTypeDefinition;
|
|
157
172
|
/** List all registered problem type keys. */
|
|
158
173
|
types: () => K[];
|
|
@@ -172,8 +187,11 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
172
187
|
* });
|
|
173
188
|
* throw problems.create("ORDER_CONFLICT", { detail: "Already exists" });
|
|
174
189
|
* ```
|
|
190
|
+
*
|
|
191
|
+
* Pass `{ autoCode: true }` to derive a `code` extension from each key, unless the
|
|
192
|
+
* definition sets an explicit `code` or the caller passes `extensions.code` to `create()`.
|
|
175
193
|
*/
|
|
176
|
-
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition
|
|
194
|
+
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>, options?: RegistryOptions): ProblemTypeRegistry<K>;
|
|
177
195
|
|
|
178
196
|
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
|
179
197
|
declare function statusToPhrase(status: number): string | undefined;
|
package/dist/index.d.ts
CHANGED
|
@@ -143,16 +143,31 @@ interface ProblemTypeDefinition {
|
|
|
143
143
|
type: string;
|
|
144
144
|
status: number;
|
|
145
145
|
title: string;
|
|
146
|
+
/**
|
|
147
|
+
* Explicit `code` extension for this type. Always emitted, regardless of
|
|
148
|
+
* `autoCode`, and takes priority over an `autoCode`-derived value.
|
|
149
|
+
*/
|
|
150
|
+
code?: string;
|
|
146
151
|
}
|
|
147
152
|
interface CreateOptions<T extends Record<string, unknown> = Record<string, unknown>> {
|
|
148
153
|
detail?: string;
|
|
149
154
|
instance?: string;
|
|
150
155
|
extensions?: T;
|
|
151
156
|
}
|
|
157
|
+
interface RegistryOptions {
|
|
158
|
+
/**
|
|
159
|
+
* Derive a `code` extension from each registry key (SCREAMING_SNAKE -> kebab-case)
|
|
160
|
+
* when the definition doesn't set an explicit `code`. Default: `false`.
|
|
161
|
+
*/
|
|
162
|
+
autoCode?: boolean;
|
|
163
|
+
}
|
|
152
164
|
interface ProblemTypeRegistry<K extends string> {
|
|
153
165
|
/** Create a {@link ProblemDetailsError} from a registered problem type key. */
|
|
154
166
|
create: <T extends Record<string, unknown>>(key: K, options?: CreateOptions<T>) => ProblemDetailsError;
|
|
155
|
-
/**
|
|
167
|
+
/**
|
|
168
|
+
* Get the resolved definition for a registered key, including any
|
|
169
|
+
* `autoCode`-derived or explicit `code` that `create()` would emit.
|
|
170
|
+
*/
|
|
156
171
|
get: (key: K) => ProblemTypeDefinition;
|
|
157
172
|
/** List all registered problem type keys. */
|
|
158
173
|
types: () => K[];
|
|
@@ -172,8 +187,11 @@ interface ProblemTypeRegistry<K extends string> {
|
|
|
172
187
|
* });
|
|
173
188
|
* throw problems.create("ORDER_CONFLICT", { detail: "Already exists" });
|
|
174
189
|
* ```
|
|
190
|
+
*
|
|
191
|
+
* Pass `{ autoCode: true }` to derive a `code` extension from each key, unless the
|
|
192
|
+
* definition sets an explicit `code` or the caller passes `extensions.code` to `create()`.
|
|
175
193
|
*/
|
|
176
|
-
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition
|
|
194
|
+
declare function createProblemTypeRegistry<K extends string>(definitions: Record<K, ProblemTypeDefinition>, options?: RegistryOptions): ProblemTypeRegistry<K>;
|
|
177
195
|
|
|
178
196
|
/** Look up the standard HTTP reason phrase for a status code (e.g. 404 → `"Not Found"`). */
|
|
179
197
|
declare function statusToPhrase(status: number): string | undefined;
|
package/dist/index.js
CHANGED
|
@@ -124,11 +124,26 @@ function problemDetailsHandler(options = {}) {
|
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
// src/registry.ts
|
|
127
|
-
function
|
|
127
|
+
function deriveCode(key) {
|
|
128
|
+
return key.toLowerCase().replace(/_+/g, "-").replace(/^-+|-+$/g, "");
|
|
129
|
+
}
|
|
130
|
+
function createProblemTypeRegistry(definitions, options) {
|
|
131
|
+
const autoCode = options?.autoCode ?? false;
|
|
132
|
+
const resolved = new Map(
|
|
133
|
+
Object.keys(definitions).map((key) => {
|
|
134
|
+
const definition = definitions[key];
|
|
135
|
+
const code = definition.code ?? (autoCode ? deriveCode(key) : void 0);
|
|
136
|
+
return [key, code === void 0 ? definition : { ...definition, code }];
|
|
137
|
+
})
|
|
138
|
+
);
|
|
128
139
|
return {
|
|
129
|
-
create: (key,
|
|
130
|
-
|
|
131
|
-
|
|
140
|
+
create: (key, createOptions) => {
|
|
141
|
+
const { code, ...definition } = resolved.get(key) ?? {};
|
|
142
|
+
const extensions = code !== void 0 ? { code, ...createOptions?.extensions } : createOptions?.extensions;
|
|
143
|
+
return new ProblemDetailsError({ ...definition, ...createOptions, extensions });
|
|
144
|
+
},
|
|
145
|
+
get: (key) => resolved.get(key),
|
|
146
|
+
types: () => [...resolved.keys()]
|
|
132
147
|
};
|
|
133
148
|
}
|
|
134
149
|
export {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hono-problem-details",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "RFC 9457 Problem Details middleware for Hono",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -137,18 +137,18 @@
|
|
|
137
137
|
},
|
|
138
138
|
"devDependencies": {
|
|
139
139
|
"@biomejs/biome": "^2.4.14",
|
|
140
|
-
"@changesets/changelog-github": "0.
|
|
141
|
-
"@changesets/cli": "2.31.
|
|
142
|
-
"@hono/standard-validator": "0.
|
|
140
|
+
"@changesets/changelog-github": "0.7.0",
|
|
141
|
+
"@changesets/cli": "2.31.1",
|
|
142
|
+
"@hono/standard-validator": "0.3.0",
|
|
143
143
|
"@hono/valibot-validator": "^0.6.1",
|
|
144
|
-
"@hono/zod-openapi": "1.
|
|
145
|
-
"@hono/zod-validator": "^0.
|
|
144
|
+
"@hono/zod-openapi": "1.5.1",
|
|
145
|
+
"@hono/zod-validator": "^0.9.0",
|
|
146
146
|
"@opentelemetry/api": "^1.0.0",
|
|
147
147
|
"@standard-schema/spec": "1.1.0",
|
|
148
148
|
"@vitest/coverage-v8": "^4.1.8",
|
|
149
149
|
"hono": "^4.12.27",
|
|
150
|
-
"knip": "6.
|
|
151
|
-
"lefthook": "2.1.
|
|
150
|
+
"knip": "6.27.0",
|
|
151
|
+
"lefthook": "2.1.10",
|
|
152
152
|
"tsup": "^8.0.0",
|
|
153
153
|
"typescript": "^6.0.3",
|
|
154
154
|
"valibot": "^1.3.1",
|