|
| 1 | +/** |
| 2 | + * IHttpServer - Standard HTTP Server Interface |
| 3 | + * |
| 4 | + * Abstract interface for HTTP server capabilities. |
| 5 | + * This allows plugins to interact with HTTP servers without knowing |
| 6 | + * the underlying implementation (Express, Fastify, Hono, etc.). |
| 7 | + * |
| 8 | + * Follows Dependency Inversion Principle - plugins depend on this interface, |
| 9 | + * not on concrete HTTP framework implementations. |
| 10 | + */ |
| 11 | + |
| 12 | +/** |
| 13 | + * Generic HTTP Request type |
| 14 | + * Abstraction over framework-specific request objects |
| 15 | + */ |
| 16 | +export interface IHttpRequest { |
| 17 | + /** Request path parameters */ |
| 18 | + params: Record<string, string>; |
| 19 | + /** Request query parameters */ |
| 20 | + query: Record<string, string | string[]>; |
| 21 | + /** Request body */ |
| 22 | + body?: any; |
| 23 | + /** Request headers */ |
| 24 | + headers: Record<string, string | string[]>; |
| 25 | + /** HTTP method */ |
| 26 | + method: string; |
| 27 | + /** Request path */ |
| 28 | + path: string; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * Generic HTTP Response type |
| 33 | + * Abstraction over framework-specific response objects |
| 34 | + */ |
| 35 | +export interface IHttpResponse { |
| 36 | + /** |
| 37 | + * Send a JSON response |
| 38 | + * @param data - Data to send |
| 39 | + */ |
| 40 | + json(data: any): void | Promise<void>; |
| 41 | + |
| 42 | + /** |
| 43 | + * Send a text/html response |
| 44 | + * @param data - Data to send |
| 45 | + */ |
| 46 | + send(data: string): void | Promise<void>; |
| 47 | + |
| 48 | + /** |
| 49 | + * Set HTTP status code |
| 50 | + * @param code - HTTP status code |
| 51 | + */ |
| 52 | + status(code: number): IHttpResponse; |
| 53 | + |
| 54 | + /** |
| 55 | + * Set response header |
| 56 | + * @param name - Header name |
| 57 | + * @param value - Header value (string or array of strings for multi-value headers) |
| 58 | + */ |
| 59 | + header(name: string, value: string | string[]): IHttpResponse; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Route handler function |
| 64 | + */ |
| 65 | +export type RouteHandler = ( |
| 66 | + req: IHttpRequest, |
| 67 | + res: IHttpResponse |
| 68 | +) => void | Promise<void>; |
| 69 | + |
| 70 | +/** |
| 71 | + * Middleware function |
| 72 | + */ |
| 73 | +export type Middleware = ( |
| 74 | + req: IHttpRequest, |
| 75 | + res: IHttpResponse, |
| 76 | + next: () => void | Promise<void> |
| 77 | +) => void | Promise<void>; |
| 78 | + |
| 79 | +/** |
| 80 | + * IHttpServer - HTTP Server capability interface |
| 81 | + * |
| 82 | + * Defines the contract for HTTP server implementations. |
| 83 | + * Concrete implementations (Express, Fastify, Hono) should implement this interface. |
| 84 | + */ |
| 85 | +export interface IHttpServer { |
| 86 | + /** |
| 87 | + * Register a GET route handler |
| 88 | + * @param path - Route path (e.g., '/api/users/:id') |
| 89 | + * @param handler - Route handler function |
| 90 | + */ |
| 91 | + get(path: string, handler: RouteHandler): void; |
| 92 | + |
| 93 | + /** |
| 94 | + * Register a POST route handler |
| 95 | + * @param path - Route path |
| 96 | + * @param handler - Route handler function |
| 97 | + */ |
| 98 | + post(path: string, handler: RouteHandler): void; |
| 99 | + |
| 100 | + /** |
| 101 | + * Register a PUT route handler |
| 102 | + * @param path - Route path |
| 103 | + * @param handler - Route handler function |
| 104 | + */ |
| 105 | + put(path: string, handler: RouteHandler): void; |
| 106 | + |
| 107 | + /** |
| 108 | + * Register a DELETE route handler |
| 109 | + * @param path - Route path |
| 110 | + * @param handler - Route handler function |
| 111 | + */ |
| 112 | + delete(path: string, handler: RouteHandler): void; |
| 113 | + |
| 114 | + /** |
| 115 | + * Register a PATCH route handler |
| 116 | + * @param path - Route path |
| 117 | + * @param handler - Route handler function |
| 118 | + */ |
| 119 | + patch(path: string, handler: RouteHandler): void; |
| 120 | + |
| 121 | + /** |
| 122 | + * Register middleware |
| 123 | + * @param path - Optional path to apply middleware to (if omitted, applies globally) |
| 124 | + * @param handler - Middleware function |
| 125 | + */ |
| 126 | + use(path: string | Middleware, handler?: Middleware): void; |
| 127 | + |
| 128 | + /** |
| 129 | + * Start the HTTP server |
| 130 | + * @param port - Port number to listen on |
| 131 | + * @returns Promise that resolves when server is ready |
| 132 | + */ |
| 133 | + listen(port: number): Promise<void>; |
| 134 | + |
| 135 | + /** |
| 136 | + * Stop the HTTP server |
| 137 | + * @returns Promise that resolves when server is stopped |
| 138 | + */ |
| 139 | + close?(): Promise<void>; |
| 140 | +} |
0 commit comments