hono-ban 0.2.0 → 0.2.4
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 +111 -16
- package/dist/cjs/formatters/rfc7807/index.js +364 -0
- package/dist/cjs/formatters/rfc7807/index.js.map +16 -0
- package/dist/cjs/index.js +85 -4221
- package/dist/cjs/index.js.map +16 -0
- package/dist/esm/formatters/rfc7807/index.js +333 -0
- package/dist/esm/formatters/rfc7807/index.js.map +16 -0
- package/dist/esm/index.js +145 -4209
- package/dist/esm/index.js.map +16 -0
- package/dist/types/formatters/rfc7807/formatter.d.ts +4 -4
- package/dist/types/formatters/rfc7807/hooks.d.ts +27 -2
- package/dist/types/formatters/rfc7807/index.d.ts +2 -2
- package/dist/types/formatters/rfc7807/schemas.d.ts +16 -16
- package/dist/types/types/rfc7807.d.ts +21 -38
- package/package.json +4 -4
- package/src/formatters/rfc7807/formatter.ts +16 -16
- package/src/formatters/rfc7807/hooks.ts +46 -20
- package/src/formatters/rfc7807/index.ts +7 -7
- package/src/formatters/rfc7807/schemas.ts +66 -35
- package/src/types/rfc7807.ts +31 -38
|
@@ -4,54 +4,85 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { z } from "@hono/zod-openapi";
|
|
7
|
-
import type {
|
|
8
|
-
ValidationParam,
|
|
9
|
-
ConstraintViolation,
|
|
10
|
-
ProblemDetails,
|
|
11
|
-
ProblemErrorData,
|
|
12
|
-
} from "../../types";
|
|
13
7
|
|
|
14
8
|
/**
|
|
15
|
-
* Schema for validation error parameters
|
|
9
|
+
* Schema for RFC 7807 validation error parameters
|
|
16
10
|
*/
|
|
17
|
-
export const
|
|
18
|
-
.
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
})
|
|
22
|
-
.openapi(
|
|
11
|
+
export const RFC7807ValidationParamSchema = z.object({
|
|
12
|
+
name: z.string().openapi({
|
|
13
|
+
example: "username",
|
|
14
|
+
description: "The field name that failed validation",
|
|
15
|
+
}),
|
|
16
|
+
reason: z.string().openapi({
|
|
17
|
+
example: "String must contain at least 3 character(s)",
|
|
18
|
+
description: "The reason for validation failure",
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
23
21
|
|
|
24
22
|
/**
|
|
25
|
-
* Schema for constraint violations
|
|
23
|
+
* Schema for RFC 7807 constraint violations
|
|
26
24
|
*/
|
|
27
|
-
export const
|
|
28
|
-
.
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
export const RFC7807ConstraintViolationSchema = z.object({
|
|
26
|
+
name: z.string().openapi({
|
|
27
|
+
example: "email",
|
|
28
|
+
description: "The field name that violated a constraint",
|
|
29
|
+
}),
|
|
30
|
+
reason: z.string().openapi({
|
|
31
|
+
example: "Email already exists",
|
|
32
|
+
description: "The reason for the constraint violation",
|
|
33
|
+
}),
|
|
34
|
+
resource: z.string().openapi({
|
|
35
|
+
example: "user",
|
|
36
|
+
description: "The resource type that contains the constraint",
|
|
37
|
+
}),
|
|
38
|
+
constraint: z.string().openapi({
|
|
39
|
+
example: "unique",
|
|
40
|
+
description: "The type of constraint that was violated",
|
|
41
|
+
}),
|
|
42
|
+
});
|
|
35
43
|
|
|
36
44
|
/**
|
|
37
|
-
* Schema for additional error data in problem details
|
|
45
|
+
* Schema for additional error data in RFC 7807 problem details
|
|
38
46
|
*/
|
|
39
|
-
export const
|
|
40
|
-
"invalid-params": z.array(
|
|
41
|
-
violations: z.array(
|
|
47
|
+
export const RFC7807ErrorDataSchema = z.object({
|
|
48
|
+
"invalid-params": z.array(RFC7807ValidationParamSchema).optional(),
|
|
49
|
+
violations: z.array(RFC7807ConstraintViolationSchema).optional(),
|
|
42
50
|
});
|
|
43
51
|
|
|
44
52
|
/**
|
|
45
53
|
* Schema for complete RFC 7807 problem details
|
|
46
54
|
*/
|
|
47
|
-
export const
|
|
55
|
+
export const RFC7807DetailsSchema = z
|
|
48
56
|
.object({
|
|
49
|
-
type: z.string().url()
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
57
|
+
type: z.string().url().openapi({
|
|
58
|
+
example: "https://api.example.com/problems/validation-error",
|
|
59
|
+
description: "A URI reference that identifies the problem type",
|
|
60
|
+
}),
|
|
61
|
+
title: z.string().openapi({
|
|
62
|
+
example: "Validation Failed",
|
|
63
|
+
description: "A short, human-readable summary of the problem type",
|
|
64
|
+
}),
|
|
65
|
+
status: z.number().int().min(400).max(599).openapi({
|
|
66
|
+
example: 400,
|
|
67
|
+
description: "The HTTP status code",
|
|
68
|
+
}),
|
|
69
|
+
|
|
70
|
+
// Optional fields per RFC7807
|
|
71
|
+
detail: z.string().optional().openapi({
|
|
72
|
+
example: "The request contains invalid fields",
|
|
73
|
+
description:
|
|
74
|
+
"A human-readable explanation specific to this occurrence of the problem",
|
|
75
|
+
}),
|
|
76
|
+
instance: z.string().url().optional().openapi({
|
|
77
|
+
example: "urn:uuid:6b56944d-5e89-4b4d-9ca7-c1be3d1f0e3f",
|
|
78
|
+
description:
|
|
79
|
+
"A URI reference that identifies the specific occurrence of the problem",
|
|
80
|
+
}),
|
|
81
|
+
|
|
82
|
+
// Extensions for validation errors
|
|
83
|
+
timestamp: z.string().datetime().optional().openapi({
|
|
84
|
+
example: "2025-02-26T12:34:56.789Z",
|
|
85
|
+
description: "When the error occurred",
|
|
86
|
+
}),
|
|
55
87
|
})
|
|
56
|
-
.merge(
|
|
57
|
-
.openapi("ProblemDetails");
|
|
88
|
+
.merge(RFC7807ErrorDataSchema);
|
package/src/types/rfc7807.ts
CHANGED
|
@@ -9,82 +9,75 @@
|
|
|
9
9
|
* @see {@link https://datatracker.ietf.org/doc/html/rfc7807} RFC 7807 Problem Details
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import { z } from "@hono/zod-openapi";
|
|
13
|
+
import {
|
|
14
|
+
RFC7807ValidationParamSchema,
|
|
15
|
+
RFC7807ConstraintViolationSchema,
|
|
16
|
+
RFC7807ErrorDataSchema,
|
|
17
|
+
RFC7807DetailsSchema,
|
|
18
|
+
} from "../formatters/rfc7807/schemas";
|
|
19
|
+
|
|
12
20
|
/**
|
|
13
21
|
* Represents a validation error parameter in an RFC 7807 problem details object.
|
|
14
22
|
* Used to indicate specific validation failures in request parameters.
|
|
15
23
|
*
|
|
16
|
-
* @
|
|
24
|
+
* @type RFC7807ValidationParam
|
|
17
25
|
* @property {string} name - The name of the parameter that failed validation
|
|
18
26
|
* @property {string} reason - The reason why the parameter failed validation
|
|
19
27
|
*/
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}
|
|
28
|
+
export type RFC7807ValidationParam = z.infer<
|
|
29
|
+
typeof RFC7807ValidationParamSchema
|
|
30
|
+
>;
|
|
24
31
|
|
|
25
32
|
/**
|
|
26
33
|
* Represents a constraint violation in an RFC 7807 problem details object.
|
|
27
34
|
* Used to indicate violations of business rules or data constraints.
|
|
28
35
|
*
|
|
29
|
-
* @
|
|
36
|
+
* @type RFC7807ConstraintViolation
|
|
30
37
|
* @property {string} name - The name of the violated constraint
|
|
31
38
|
* @property {string} reason - The reason why the constraint was violated
|
|
32
39
|
* @property {string} resource - The resource or entity where the violation occurred
|
|
33
40
|
* @property {string} constraint - The specific constraint that was violated
|
|
34
41
|
*/
|
|
35
|
-
export
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
resource: string;
|
|
39
|
-
constraint: string;
|
|
40
|
-
}
|
|
42
|
+
export type RFC7807ConstraintViolation = z.infer<
|
|
43
|
+
typeof RFC7807ConstraintViolationSchema
|
|
44
|
+
>;
|
|
41
45
|
|
|
42
46
|
/**
|
|
43
47
|
* Additional error data that can be included in an RFC 7807 problem details object.
|
|
44
48
|
* This interface extends the standard problem details with validation and constraint information.
|
|
45
49
|
*
|
|
46
|
-
* @
|
|
47
|
-
* @property {
|
|
48
|
-
* @property {
|
|
50
|
+
* @type RFC7807ErrorData
|
|
51
|
+
* @property {RFC7807ValidationParam[]} [invalid-params] - Array of validation errors
|
|
52
|
+
* @property {RFC7807ConstraintViolation[]} [violations] - Array of constraint violations
|
|
49
53
|
*/
|
|
50
|
-
export
|
|
51
|
-
"invalid-params"?: ValidationParam[];
|
|
52
|
-
violations?: ConstraintViolation[];
|
|
53
|
-
}
|
|
54
|
+
export type RFC7807ErrorData = z.infer<typeof RFC7807ErrorDataSchema>;
|
|
54
55
|
|
|
55
56
|
/**
|
|
56
57
|
* Complete RFC 7807 problem details object structure.
|
|
57
58
|
* This interface represents the full problem details format as defined in RFC 7807,
|
|
58
59
|
* with additional properties for validation and constraint violation data.
|
|
59
60
|
*
|
|
60
|
-
* @
|
|
61
|
-
* @extends ProblemErrorData
|
|
61
|
+
* @type RFC7807Details
|
|
62
62
|
* @property {string} type - URI reference that identifies the problem type
|
|
63
63
|
* @property {string} title - Short, human-readable summary of the problem
|
|
64
64
|
* @property {number} status - HTTP status code (400-599)
|
|
65
|
-
* @property {string} detail - Human-readable explanation specific to this occurrence
|
|
66
|
-
* @property {string} instance - URI reference that identifies the specific occurrence
|
|
67
|
-
* @property {string} timestamp - ISO 8601 datetime when the error occurred
|
|
65
|
+
* @property {string} [detail] - Human-readable explanation specific to this occurrence
|
|
66
|
+
* @property {string} [instance] - URI reference that identifies the specific occurrence
|
|
67
|
+
* @property {string} [timestamp] - ISO 8601 datetime when the error occurred
|
|
68
68
|
*/
|
|
69
|
-
export
|
|
70
|
-
type: string;
|
|
71
|
-
title: string;
|
|
72
|
-
status: number;
|
|
73
|
-
detail: string;
|
|
74
|
-
instance: string;
|
|
75
|
-
timestamp: string;
|
|
76
|
-
}
|
|
69
|
+
export type RFC7807Details = z.infer<typeof RFC7807DetailsSchema>;
|
|
77
70
|
|
|
78
71
|
/**
|
|
79
72
|
* Hook function for customizing RFC 7807 problem details before formatting.
|
|
80
73
|
* This type represents a function that can modify the problem details object
|
|
81
74
|
* before it is serialized into the final response.
|
|
82
75
|
*
|
|
83
|
-
* @callback
|
|
84
|
-
* @param {
|
|
85
|
-
* @returns {
|
|
76
|
+
* @callback RFC7807DetailsHook
|
|
77
|
+
* @param {RFC7807Details} details - The problem details object to modify
|
|
78
|
+
* @returns {RFC7807Details} The modified problem details object
|
|
86
79
|
*/
|
|
87
|
-
export type
|
|
80
|
+
export type RFC7807DetailsHook = (details: RFC7807Details) => RFC7807Details;
|
|
88
81
|
|
|
89
82
|
/**
|
|
90
83
|
* Configuration options for the RFC 7807 formatter.
|
|
@@ -92,9 +85,9 @@ export type ProblemDetailsHook = (details: ProblemDetails) => ProblemDetails;
|
|
|
92
85
|
*
|
|
93
86
|
* @interface RFC7807FormatterOptions
|
|
94
87
|
* @property {string} [baseUrl="https://api.example.com/problems"] - Base URL for problem type URIs
|
|
95
|
-
* @property {
|
|
88
|
+
* @property {RFC7807DetailsHook[]} [hooks=[]] - Array of hooks for customizing problem details
|
|
96
89
|
*/
|
|
97
90
|
export interface RFC7807FormatterOptions {
|
|
98
91
|
baseUrl?: string;
|
|
99
|
-
hooks?:
|
|
92
|
+
hooks?: RFC7807DetailsHook[];
|
|
100
93
|
}
|