feat(protocols): refactor protocol utilities into centralized protocols module
Some checks failed
Default (tags) / security (push) Successful in 55s
Default (tags) / test (push) Failing after 30m45s
Default (tags) / release (push) Has been skipped
Default (tags) / metadata (push) Has been skipped

This commit is contained in:
Juergen Kunz
2025-07-21 22:37:45 +00:00
parent d47b048517
commit 36068a6d92
32 changed files with 1155 additions and 394 deletions

View File

@@ -1,4 +1,6 @@
import * as plugins from '../../../plugins.js';
// Import from protocols for consistent status codes
import { HttpStatus as ProtocolHttpStatus, getStatusText as getProtocolStatusText } from '../../../protocols/http/index.js';
/**
* HTTP-specific event types
@@ -10,34 +12,33 @@ export enum HttpEvents {
REQUEST_ERROR = 'request-error',
}
/**
* HTTP status codes as an enum for better type safety
*/
export enum HttpStatus {
OK = 200,
MOVED_PERMANENTLY = 301,
FOUND = 302,
TEMPORARY_REDIRECT = 307,
PERMANENT_REDIRECT = 308,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
METHOD_NOT_ALLOWED = 405,
REQUEST_TIMEOUT = 408,
TOO_MANY_REQUESTS = 429,
INTERNAL_SERVER_ERROR = 500,
NOT_IMPLEMENTED = 501,
BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
GATEWAY_TIMEOUT = 504,
}
// Re-export for backward compatibility with subset of commonly used codes
export const HttpStatus = {
OK: ProtocolHttpStatus.OK,
MOVED_PERMANENTLY: ProtocolHttpStatus.MOVED_PERMANENTLY,
FOUND: ProtocolHttpStatus.FOUND,
TEMPORARY_REDIRECT: ProtocolHttpStatus.TEMPORARY_REDIRECT,
PERMANENT_REDIRECT: ProtocolHttpStatus.PERMANENT_REDIRECT,
BAD_REQUEST: ProtocolHttpStatus.BAD_REQUEST,
UNAUTHORIZED: ProtocolHttpStatus.UNAUTHORIZED,
FORBIDDEN: ProtocolHttpStatus.FORBIDDEN,
NOT_FOUND: ProtocolHttpStatus.NOT_FOUND,
METHOD_NOT_ALLOWED: ProtocolHttpStatus.METHOD_NOT_ALLOWED,
REQUEST_TIMEOUT: ProtocolHttpStatus.REQUEST_TIMEOUT,
TOO_MANY_REQUESTS: ProtocolHttpStatus.TOO_MANY_REQUESTS,
INTERNAL_SERVER_ERROR: ProtocolHttpStatus.INTERNAL_SERVER_ERROR,
NOT_IMPLEMENTED: ProtocolHttpStatus.NOT_IMPLEMENTED,
BAD_GATEWAY: ProtocolHttpStatus.BAD_GATEWAY,
SERVICE_UNAVAILABLE: ProtocolHttpStatus.SERVICE_UNAVAILABLE,
GATEWAY_TIMEOUT: ProtocolHttpStatus.GATEWAY_TIMEOUT,
} as const;
/**
* Base error class for HTTP-related errors
*/
export class HttpError extends Error {
constructor(message: string, public readonly statusCode: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR) {
constructor(message: string, public readonly statusCode: number = HttpStatus.INTERNAL_SERVER_ERROR) {
super(message);
this.name = 'HttpError';
}
@@ -61,7 +62,7 @@ export class CertificateError extends HttpError {
* Error related to server operations
*/
export class ServerError extends HttpError {
constructor(message: string, public readonly code?: string, statusCode: HttpStatus = HttpStatus.INTERNAL_SERVER_ERROR) {
constructor(message: string, public readonly code?: string, statusCode: number = HttpStatus.INTERNAL_SERVER_ERROR) {
super(message, statusCode);
this.name = 'ServerError';
}
@@ -93,7 +94,7 @@ export class NotFoundError extends HttpError {
export interface IRedirectConfig {
source: string; // Source path or pattern
destination: string; // Destination URL
type: HttpStatus; // Redirect status code
type: number; // Redirect status code
preserveQuery?: boolean; // Whether to preserve query parameters
}
@@ -115,30 +116,12 @@ export interface IRouterConfig {
*/
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS' | 'CONNECT' | 'TRACE';
/**
* Helper function to get HTTP status text
*/
export function getStatusText(status: HttpStatus): string {
const statusTexts: Record<HttpStatus, string> = {
[HttpStatus.OK]: 'OK',
[HttpStatus.MOVED_PERMANENTLY]: 'Moved Permanently',
[HttpStatus.FOUND]: 'Found',
[HttpStatus.TEMPORARY_REDIRECT]: 'Temporary Redirect',
[HttpStatus.PERMANENT_REDIRECT]: 'Permanent Redirect',
[HttpStatus.BAD_REQUEST]: 'Bad Request',
[HttpStatus.UNAUTHORIZED]: 'Unauthorized',
[HttpStatus.FORBIDDEN]: 'Forbidden',
[HttpStatus.NOT_FOUND]: 'Not Found',
[HttpStatus.METHOD_NOT_ALLOWED]: 'Method Not Allowed',
[HttpStatus.REQUEST_TIMEOUT]: 'Request Timeout',
[HttpStatus.TOO_MANY_REQUESTS]: 'Too Many Requests',
[HttpStatus.INTERNAL_SERVER_ERROR]: 'Internal Server Error',
[HttpStatus.NOT_IMPLEMENTED]: 'Not Implemented',
[HttpStatus.BAD_GATEWAY]: 'Bad Gateway',
[HttpStatus.SERVICE_UNAVAILABLE]: 'Service Unavailable',
[HttpStatus.GATEWAY_TIMEOUT]: 'Gateway Timeout',
};
return statusTexts[status] || 'Unknown';
export function getStatusText(status: number): string {
return getProtocolStatusText(status as ProtocolHttpStatus);
}
// Legacy interfaces for backward compatibility