-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhttp-server.ts
More file actions
149 lines (133 loc) · 4.12 KB
/
Copy pathhttp-server.ts
File metadata and controls
149 lines (133 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* IHttpServer - Standard HTTP Server Interface
*
* Abstract interface for HTTP server capabilities.
* This allows plugins to interact with HTTP servers without knowing
* the underlying implementation (Express, Fastify, Hono, etc.).
*
* Follows Dependency Inversion Principle - plugins depend on this interface,
* not on concrete HTTP framework implementations.
*/
/**
* Generic HTTP Request type
* Abstraction over framework-specific request objects
*/
export interface IHttpRequest {
/** Request path parameters */
params: Record<string, string>;
/** Request query parameters */
query: Record<string, string | string[]>;
/** Request body (parsed — JSON or form data when applicable) */
body?: any;
/** Request headers */
headers: Record<string, string | string[]>;
/** HTTP method */
method: string;
/** Request path */
path: string;
/**
* Raw body accessor for binary uploads (file streams, multipart parts).
* Implementations should consume the underlying request stream lazily so
* handlers that only need `body` do not pay the parsing cost. May be
* undefined when the underlying framework cannot expose the raw stream.
*/
rawBody?: () => Promise<Buffer>;
}
/**
* Generic HTTP Response type
* Abstraction over framework-specific response objects
*/
export interface IHttpResponse {
/**
* Send a JSON response
* @param data - Data to send
*/
json(data: any): void | Promise<void>;
/**
* Send a text/html response
* @param data - Data to send
*/
send(data: string | Uint8Array | ArrayBuffer): void | Promise<void>;
/**
* Set HTTP status code
* @param code - HTTP status code
*/
status(code: number): IHttpResponse;
/**
* Set response header
* @param name - Header name
* @param value - Header value (string or array of strings for multi-value headers)
*/
header(name: string, value: string | string[]): IHttpResponse;
}
/**
* Route handler function
*/
export type RouteHandler = (
req: IHttpRequest,
res: IHttpResponse
) => void | Promise<void>;
/**
* Middleware function
*/
export type Middleware = (
req: IHttpRequest,
res: IHttpResponse,
next: () => void | Promise<void>
) => void | Promise<void>;
/**
* IHttpServer - HTTP Server capability interface
*
* Defines the contract for HTTP server implementations.
* Concrete implementations (Express, Fastify, Hono) should implement this interface.
*/
export interface IHttpServer {
/**
* Register a GET route handler
* @param path - Route path (e.g., '/api/users/:id')
* @param handler - Route handler function
*/
get(path: string, handler: RouteHandler): void;
/**
* Register a POST route handler
* @param path - Route path
* @param handler - Route handler function
*/
post(path: string, handler: RouteHandler): void;
/**
* Register a PUT route handler
* @param path - Route path
* @param handler - Route handler function
*/
put(path: string, handler: RouteHandler): void;
/**
* Register a DELETE route handler
* @param path - Route path
* @param handler - Route handler function
*/
delete(path: string, handler: RouteHandler): void;
/**
* Register a PATCH route handler
* @param path - Route path
* @param handler - Route handler function
*/
patch(path: string, handler: RouteHandler): void;
/**
* Register middleware
* @param path - Optional path to apply middleware to (if omitted, applies globally)
* @param handler - Middleware function
*/
use(path: string | Middleware, handler?: Middleware): void;
/**
* Start the HTTP server
* @param port - Port number to listen on
* @returns Promise that resolves when server is ready
*/
listen(port: number): Promise<void>;
/**
* Stop the HTTP server
* @returns Promise that resolves when server is stopped
*/
close?(): Promise<void>;
}