hono-ban 0.2.4 → 0.2.6
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/dist/esm/formatters/index.js +1 -0
- package/dist/esm/index.js +1 -311
- package/package.json +9 -24
- package/dist/cjs/formatters/rfc7807/index.js +0 -364
- package/dist/cjs/formatters/rfc7807/index.js.map +0 -16
- package/dist/cjs/index.js +0 -271
- package/dist/cjs/index.js.map +0 -16
- package/dist/esm/formatters/rfc7807/index.js +0 -333
- package/dist/esm/formatters/rfc7807/index.js.map +0 -16
- package/dist/esm/index.js.map +0 -16
- package/src/__tests__/core/convert-error.test.ts +0 -158
- package/src/__tests__/core/create-error.test.ts +0 -113
- package/src/__tests__/core/format-error.test.ts +0 -162
- package/src/__tests__/factories/factories.test.ts +0 -380
- package/src/__tests__/formatters/default-formatter.test.ts +0 -117
- package/src/__tests__/middleware/README.md +0 -49
- package/src/__tests__/middleware/ban-middleware.test.ts.disabled +0 -23
- package/src/__tests__/utils/sanitize.test.ts +0 -117
- package/src/__tests__/utils/validate.test.ts +0 -35
- package/src/constants/index.ts +0 -6
- package/src/constants/status-codes.ts +0 -52
- package/src/core/convert-error.ts +0 -74
- package/src/core/create-error.ts +0 -65
- package/src/core/format-error.ts +0 -76
- package/src/core/index.ts +0 -8
- package/src/factories/client-errors.ts +0 -492
- package/src/factories/index.ts +0 -7
- package/src/factories/server-errors.ts +0 -230
- package/src/formatters/default/index.ts +0 -72
- package/src/formatters/index.ts +0 -13
- package/src/formatters/interfaces.ts +0 -7
- package/src/formatters/rfc7807/formatter.ts +0 -105
- package/src/formatters/rfc7807/hooks.ts +0 -73
- package/src/formatters/rfc7807/index.ts +0 -20
- package/src/formatters/rfc7807/schemas.ts +0 -88
- package/src/index.ts +0 -23
- package/src/middleware/ban-middleware.ts +0 -77
- package/src/middleware/index.ts +0 -6
- package/src/types/common.ts +0 -14
- package/src/types/error.ts +0 -46
- package/src/types/formatter.ts +0 -61
- package/src/types/index.ts +0 -11
- package/src/types/options.ts +0 -71
- package/src/types/rfc7807.ts +0 -93
- package/src/utils/index.ts +0 -7
- package/src/utils/sanitize.ts +0 -34
- package/src/utils/validate.ts +0 -24
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error handling middleware
|
|
3
|
-
* @module hono-ban/middleware/ban-middleware
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { MiddlewareHandler } from "hono";
|
|
7
|
-
import { convertToBanError, formatError, createErrorResponse } from "../core";
|
|
8
|
-
import { defaultFormatter } from "../formatters";
|
|
9
|
-
import type { BanMiddlewareOptions } from "../types";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Default middleware options
|
|
13
|
-
*/
|
|
14
|
-
const DEFAULT_OPTIONS: Required<BanMiddlewareOptions> = {
|
|
15
|
-
formatter: defaultFormatter,
|
|
16
|
-
sanitize: [],
|
|
17
|
-
includeStackTrace: false,
|
|
18
|
-
headers: {},
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Create error handling middleware
|
|
23
|
-
* @param options - Middleware configuration options
|
|
24
|
-
*
|
|
25
|
-
* @example
|
|
26
|
-
* // Simple usage
|
|
27
|
-
* app.use(ban());
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* // Advanced usage
|
|
31
|
-
* app.use(ban({
|
|
32
|
-
* formatter: customFormatter,
|
|
33
|
-
* sanitize: ['password', 'token'],
|
|
34
|
-
* includeStackTrace: process.env.NODE_ENV !== 'production'
|
|
35
|
-
* }));
|
|
36
|
-
*/
|
|
37
|
-
export function ban(options: BanMiddlewareOptions = {}): MiddlewareHandler {
|
|
38
|
-
const resolvedOptions: Required<BanMiddlewareOptions> = {
|
|
39
|
-
...DEFAULT_OPTIONS,
|
|
40
|
-
...options,
|
|
41
|
-
formatter: options.formatter
|
|
42
|
-
? options.formatter
|
|
43
|
-
: DEFAULT_OPTIONS.formatter,
|
|
44
|
-
headers: {
|
|
45
|
-
...DEFAULT_OPTIONS.headers,
|
|
46
|
-
...options.headers,
|
|
47
|
-
},
|
|
48
|
-
sanitize: [...DEFAULT_OPTIONS.sanitize, ...(options.sanitize || [])],
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
// Return the middleware function that uses the pre-merged options
|
|
52
|
-
return async (_, next) => {
|
|
53
|
-
try {
|
|
54
|
-
await next();
|
|
55
|
-
} catch (err) {
|
|
56
|
-
// Get the pre-resolved formatter instance
|
|
57
|
-
const formatter = resolvedOptions.formatter;
|
|
58
|
-
|
|
59
|
-
const error = convertToBanError(err, {
|
|
60
|
-
formatter,
|
|
61
|
-
headers: resolvedOptions.headers,
|
|
62
|
-
sanitize: resolvedOptions.sanitize,
|
|
63
|
-
includeStackTrace: resolvedOptions.includeStackTrace,
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// Format the error using pre-resolved options
|
|
67
|
-
const formatted = formatError(error, formatter, {
|
|
68
|
-
headers: resolvedOptions.headers,
|
|
69
|
-
sanitize: resolvedOptions.sanitize,
|
|
70
|
-
includeStackTrace: resolvedOptions.includeStackTrace,
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
// Create and return response
|
|
74
|
-
return createErrorResponse(error, formatted);
|
|
75
|
-
}
|
|
76
|
-
};
|
|
77
|
-
}
|
package/src/middleware/index.ts
DELETED
package/src/types/common.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Common shared types
|
|
3
|
-
* @module hono-ban/types/common
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Type for HTTP headers
|
|
8
|
-
*/
|
|
9
|
-
export type Headers = Record<string, string>;
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Type for allowed HTTP methods
|
|
13
|
-
*/
|
|
14
|
-
export type AllowedMethods = string | string[];
|
package/src/types/error.ts
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Error type definitions
|
|
3
|
-
* @module hono-ban/types/error
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type {
|
|
7
|
-
ClientErrorStatusCode,
|
|
8
|
-
ServerErrorStatusCode,
|
|
9
|
-
} from "hono/utils/http-status";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Valid HTTP error status codes (4xx and 5xx)
|
|
13
|
-
*/
|
|
14
|
-
export type ErrorStatusCode = ClientErrorStatusCode | ServerErrorStatusCode;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Core error object interface
|
|
18
|
-
*/
|
|
19
|
-
export interface BanError<T = unknown> {
|
|
20
|
-
/** HTTP error status code */
|
|
21
|
-
status: ErrorStatusCode;
|
|
22
|
-
|
|
23
|
-
/** Error message */
|
|
24
|
-
message: string;
|
|
25
|
-
|
|
26
|
-
/** Additional error context data */
|
|
27
|
-
data?: T;
|
|
28
|
-
|
|
29
|
-
/** Response headers */
|
|
30
|
-
headers?: Record<string, string>;
|
|
31
|
-
|
|
32
|
-
/** Allowed HTTP methods for 405 errors */
|
|
33
|
-
allow?: readonly string[];
|
|
34
|
-
|
|
35
|
-
/** Stack trace if available */
|
|
36
|
-
stack?: string;
|
|
37
|
-
|
|
38
|
-
/** Original error if this wraps another error */
|
|
39
|
-
cause?: unknown;
|
|
40
|
-
|
|
41
|
-
/** Stack trace of the cause error */
|
|
42
|
-
causeStack?: string;
|
|
43
|
-
|
|
44
|
-
/** Type guard identifier */
|
|
45
|
-
readonly isBan: true;
|
|
46
|
-
}
|
package/src/types/formatter.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Formatter type definitions
|
|
3
|
-
* @module hono-ban/types/formatter
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { BanError } from "./error";
|
|
7
|
-
import type { Headers } from "./common";
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Default error output structure
|
|
11
|
-
*/
|
|
12
|
-
export interface DefaultErrorOutput {
|
|
13
|
-
/** HTTP status code */
|
|
14
|
-
statusCode: number;
|
|
15
|
-
|
|
16
|
-
/** Formatted error payload */
|
|
17
|
-
payload: {
|
|
18
|
-
/** HTTP status code */
|
|
19
|
-
statusCode: number;
|
|
20
|
-
|
|
21
|
-
/** Error name from status code */
|
|
22
|
-
error: string;
|
|
23
|
-
|
|
24
|
-
/** Error message if provided */
|
|
25
|
-
message?: string;
|
|
26
|
-
|
|
27
|
-
/** Stack trace if enabled */
|
|
28
|
-
stack?: string;
|
|
29
|
-
|
|
30
|
-
/** Cause stack trace if available */
|
|
31
|
-
causeStack?: string;
|
|
32
|
-
|
|
33
|
-
/** Additional error data */
|
|
34
|
-
data?: unknown;
|
|
35
|
-
|
|
36
|
-
/** Any other properties */
|
|
37
|
-
[key: string]: unknown;
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Error formatter interface
|
|
43
|
-
*/
|
|
44
|
-
export interface ErrorFormatter<T = unknown> {
|
|
45
|
-
/** Response content type */
|
|
46
|
-
readonly contentType: string;
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Format an error into the desired structure
|
|
50
|
-
* @param error - Error to format
|
|
51
|
-
* @param headers - Additional headers (used for HTTP headers, not included in response body)
|
|
52
|
-
* @param sanitize - Fields to remove
|
|
53
|
-
* @param includeStackTrace - Include stack trace
|
|
54
|
-
*/
|
|
55
|
-
format(
|
|
56
|
-
error: BanError,
|
|
57
|
-
headers?: Headers,
|
|
58
|
-
sanitize?: readonly string[],
|
|
59
|
-
includeStackTrace?: boolean
|
|
60
|
-
): T;
|
|
61
|
-
}
|
package/src/types/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core type definitions for the hono-ban library
|
|
3
|
-
* @module hono-ban/types
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
// Export all types from their respective files
|
|
7
|
-
export * from "./error";
|
|
8
|
-
export * from "./options";
|
|
9
|
-
export * from "./formatter";
|
|
10
|
-
export * from "./common";
|
|
11
|
-
export * from "./rfc7807";
|
package/src/types/options.ts
DELETED
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Options type definitions
|
|
3
|
-
* @module hono-ban/types/options
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ErrorStatusCode } from "./error";
|
|
7
|
-
import type { Headers, AllowedMethods } from "./common";
|
|
8
|
-
import type { ErrorFormatter } from "./formatter";
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Options for creating errors
|
|
12
|
-
*/
|
|
13
|
-
export interface BanOptions<T = unknown> {
|
|
14
|
-
/** HTTP status code */
|
|
15
|
-
statusCode?: ErrorStatusCode;
|
|
16
|
-
|
|
17
|
-
/** Error message */
|
|
18
|
-
message?: string;
|
|
19
|
-
|
|
20
|
-
/** Additional error data */
|
|
21
|
-
data?: T;
|
|
22
|
-
|
|
23
|
-
/** HTTP response headers (not included in response body) */
|
|
24
|
-
headers?: Headers;
|
|
25
|
-
|
|
26
|
-
/** Allowed HTTP methods */
|
|
27
|
-
allow?: AllowedMethods;
|
|
28
|
-
|
|
29
|
-
/** Original error cause */
|
|
30
|
-
cause?: Error | unknown;
|
|
31
|
-
|
|
32
|
-
/** Custom error formatter */
|
|
33
|
-
formatter?: ErrorFormatter;
|
|
34
|
-
|
|
35
|
-
/** Fields to remove from output */
|
|
36
|
-
sanitize?: readonly string[];
|
|
37
|
-
|
|
38
|
-
/** Whether to include stack trace */
|
|
39
|
-
includeStackTrace?: boolean;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Options for formatting errors
|
|
44
|
-
*/
|
|
45
|
-
export interface FormatOptions {
|
|
46
|
-
/** Additional headers for HTTP response (not included in response body) */
|
|
47
|
-
headers?: Headers;
|
|
48
|
-
|
|
49
|
-
/** Fields to remove from output */
|
|
50
|
-
sanitize?: readonly string[];
|
|
51
|
-
|
|
52
|
-
/** Whether to include stack trace */
|
|
53
|
-
includeStackTrace?: boolean;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Middleware configuration options
|
|
58
|
-
*/
|
|
59
|
-
export interface BanMiddlewareOptions {
|
|
60
|
-
/** Custom error formatter or formatter name */
|
|
61
|
-
formatter?: ErrorFormatter;
|
|
62
|
-
|
|
63
|
-
/** Fields to remove from output */
|
|
64
|
-
sanitize?: readonly string[];
|
|
65
|
-
|
|
66
|
-
/** Whether to include stack trace */
|
|
67
|
-
includeStackTrace?: boolean;
|
|
68
|
-
|
|
69
|
-
/** Default headers for HTTP response (not included in response body) */
|
|
70
|
-
headers?: Headers;
|
|
71
|
-
}
|
package/src/types/rfc7807.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type definitions for RFC 7807 Problem Details format.
|
|
3
|
-
*
|
|
4
|
-
* This module provides type definitions for the RFC 7807 Problem Details format,
|
|
5
|
-
* including interfaces and types for validation parameters, constraint violations,
|
|
6
|
-
* and the complete problem details structure.
|
|
7
|
-
*
|
|
8
|
-
* @module hono-ban/types/rfc7807
|
|
9
|
-
* @see {@link https://datatracker.ietf.org/doc/html/rfc7807} RFC 7807 Problem Details
|
|
10
|
-
*/
|
|
11
|
-
|
|
12
|
-
import { z } from "@hono/zod-openapi";
|
|
13
|
-
import {
|
|
14
|
-
RFC7807ValidationParamSchema,
|
|
15
|
-
RFC7807ConstraintViolationSchema,
|
|
16
|
-
RFC7807ErrorDataSchema,
|
|
17
|
-
RFC7807DetailsSchema,
|
|
18
|
-
} from "../formatters/rfc7807/schemas";
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Represents a validation error parameter in an RFC 7807 problem details object.
|
|
22
|
-
* Used to indicate specific validation failures in request parameters.
|
|
23
|
-
*
|
|
24
|
-
* @type RFC7807ValidationParam
|
|
25
|
-
* @property {string} name - The name of the parameter that failed validation
|
|
26
|
-
* @property {string} reason - The reason why the parameter failed validation
|
|
27
|
-
*/
|
|
28
|
-
export type RFC7807ValidationParam = z.infer<
|
|
29
|
-
typeof RFC7807ValidationParamSchema
|
|
30
|
-
>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Represents a constraint violation in an RFC 7807 problem details object.
|
|
34
|
-
* Used to indicate violations of business rules or data constraints.
|
|
35
|
-
*
|
|
36
|
-
* @type RFC7807ConstraintViolation
|
|
37
|
-
* @property {string} name - The name of the violated constraint
|
|
38
|
-
* @property {string} reason - The reason why the constraint was violated
|
|
39
|
-
* @property {string} resource - The resource or entity where the violation occurred
|
|
40
|
-
* @property {string} constraint - The specific constraint that was violated
|
|
41
|
-
*/
|
|
42
|
-
export type RFC7807ConstraintViolation = z.infer<
|
|
43
|
-
typeof RFC7807ConstraintViolationSchema
|
|
44
|
-
>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Additional error data that can be included in an RFC 7807 problem details object.
|
|
48
|
-
* This interface extends the standard problem details with validation and constraint information.
|
|
49
|
-
*
|
|
50
|
-
* @type RFC7807ErrorData
|
|
51
|
-
* @property {RFC7807ValidationParam[]} [invalid-params] - Array of validation errors
|
|
52
|
-
* @property {RFC7807ConstraintViolation[]} [violations] - Array of constraint violations
|
|
53
|
-
*/
|
|
54
|
-
export type RFC7807ErrorData = z.infer<typeof RFC7807ErrorDataSchema>;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Complete RFC 7807 problem details object structure.
|
|
58
|
-
* This interface represents the full problem details format as defined in RFC 7807,
|
|
59
|
-
* with additional properties for validation and constraint violation data.
|
|
60
|
-
*
|
|
61
|
-
* @type RFC7807Details
|
|
62
|
-
* @property {string} type - URI reference that identifies the problem type
|
|
63
|
-
* @property {string} title - Short, human-readable summary of the problem
|
|
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
|
|
68
|
-
*/
|
|
69
|
-
export type RFC7807Details = z.infer<typeof RFC7807DetailsSchema>;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Hook function for customizing RFC 7807 problem details before formatting.
|
|
73
|
-
* This type represents a function that can modify the problem details object
|
|
74
|
-
* before it is serialized into the final response.
|
|
75
|
-
*
|
|
76
|
-
* @callback RFC7807DetailsHook
|
|
77
|
-
* @param {RFC7807Details} details - The problem details object to modify
|
|
78
|
-
* @returns {RFC7807Details} The modified problem details object
|
|
79
|
-
*/
|
|
80
|
-
export type RFC7807DetailsHook = (details: RFC7807Details) => RFC7807Details;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Configuration options for the RFC 7807 formatter.
|
|
84
|
-
* These options control how problem details are generated and formatted.
|
|
85
|
-
*
|
|
86
|
-
* @interface RFC7807FormatterOptions
|
|
87
|
-
* @property {string} [baseUrl="https://api.example.com/problems"] - Base URL for problem type URIs
|
|
88
|
-
* @property {RFC7807DetailsHook[]} [hooks=[]] - Array of hooks for customizing problem details
|
|
89
|
-
*/
|
|
90
|
-
export interface RFC7807FormatterOptions {
|
|
91
|
-
baseUrl?: string;
|
|
92
|
-
hooks?: RFC7807DetailsHook[];
|
|
93
|
-
}
|
package/src/utils/index.ts
DELETED
package/src/utils/sanitize.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Sanitization utilities
|
|
3
|
-
* @module hono-ban/utils/sanitize
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Recursively sanitize an object by removing specified keys
|
|
8
|
-
* @param obj - Object to sanitize
|
|
9
|
-
* @param keysToRemove - Keys to remove from object
|
|
10
|
-
*/
|
|
11
|
-
export function sanitizeObject(
|
|
12
|
-
obj: unknown,
|
|
13
|
-
keysToRemove: readonly string[]
|
|
14
|
-
): unknown {
|
|
15
|
-
if (!keysToRemove.length) {
|
|
16
|
-
return obj;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (Array.isArray(obj)) {
|
|
20
|
-
return obj.map((item) => sanitizeObject(item, keysToRemove));
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (obj !== null && typeof obj === "object") {
|
|
24
|
-
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
25
|
-
if (keysToRemove.includes(key)) {
|
|
26
|
-
return acc;
|
|
27
|
-
}
|
|
28
|
-
acc[key] = sanitizeObject(value, keysToRemove);
|
|
29
|
-
return acc;
|
|
30
|
-
}, {} as Record<string, unknown>);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return obj;
|
|
34
|
-
}
|
package/src/utils/validate.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Validation utilities
|
|
3
|
-
* @module hono-ban/utils/validate
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ErrorStatusCode } from "../types/error";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Validate and normalize HTTP error status code
|
|
10
|
-
* @param code - Status code to validate
|
|
11
|
-
*/
|
|
12
|
-
export function validateStatusCode(code: number): ErrorStatusCode {
|
|
13
|
-
// Handle invalid inputs
|
|
14
|
-
if (typeof code !== "number" || isNaN(code)) {
|
|
15
|
-
return 500;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
// Ensure code is in error range
|
|
19
|
-
if (code < 400 || code >= 600) {
|
|
20
|
-
return 500;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return code as ErrorStatusCode;
|
|
24
|
-
}
|