Skip to content

Commit ec05449

Browse files
Copilothotlong
andcommitted
Add capability contract interfaces (IHttpServer and IDataEngine)
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 6d02a1f commit ec05449

File tree

4 files changed

+433
-0
lines changed

4 files changed

+433
-0
lines changed

packages/runtime/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ export { ObjectStackRuntimeProtocol } from './protocol.js';
1515
// Export Types
1616
export * from './types.js';
1717

18+
// Export Interfaces (Capability Contracts)
19+
export * from './interfaces/http-server.js';
20+
export * from './interfaces/data-engine.js';
21+
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/**
2+
* IDataEngine - Standard Data Engine Interface
3+
*
4+
* Abstract interface for data persistence capabilities.
5+
* This allows plugins to interact with data engines without knowing
6+
* the underlying implementation (SQL, MongoDB, Memory, etc.).
7+
*
8+
* Follows Dependency Inversion Principle - plugins depend on this interface,
9+
* not on concrete database implementations.
10+
*/
11+
12+
/**
13+
* Query filter conditions
14+
*/
15+
export interface QueryFilter {
16+
[field: string]: any;
17+
}
18+
19+
/**
20+
* Query options for find operations
21+
*/
22+
export interface QueryOptions {
23+
/** Filter conditions */
24+
filter?: QueryFilter;
25+
/** Fields to select */
26+
select?: string[];
27+
/** Sort order */
28+
sort?: Record<string, 1 | -1 | 'asc' | 'desc'>;
29+
/** Limit number of results */
30+
limit?: number;
31+
/** Skip number of results (for pagination) */
32+
skip?: number;
33+
/** Maximum number of results (alternative to limit) */
34+
top?: number;
35+
}
36+
37+
/**
38+
* IDataEngine - Data persistence capability interface
39+
*
40+
* Defines the contract for data engine implementations.
41+
* Concrete implementations (ObjectQL, Prisma, TypeORM) should implement this interface.
42+
*/
43+
export interface IDataEngine {
44+
/**
45+
* Insert a new record
46+
*
47+
* @param objectName - Name of the object/table (e.g., 'user', 'order')
48+
* @param data - Data to insert
49+
* @returns Promise resolving to the created record (including generated ID)
50+
*
51+
* @example
52+
* ```ts
53+
* const user = await engine.insert('user', {
54+
* name: 'John Doe',
55+
* email: 'john@example.com'
56+
* });
57+
* console.log(user.id); // Auto-generated ID
58+
* ```
59+
*/
60+
insert(objectName: string, data: any): Promise<any>;
61+
62+
/**
63+
* Find records matching a query
64+
*
65+
* @param objectName - Name of the object/table
66+
* @param query - Query conditions (optional)
67+
* @returns Promise resolving to an array of matching records
68+
*
69+
* @example
70+
* ```ts
71+
* // Find all users
72+
* const allUsers = await engine.find('user');
73+
*
74+
* // Find with filter
75+
* const activeUsers = await engine.find('user', {
76+
* filter: { status: 'active' }
77+
* });
78+
*
79+
* // Find with limit and sort
80+
* const recentUsers = await engine.find('user', {
81+
* sort: { createdAt: -1 },
82+
* limit: 10
83+
* });
84+
* ```
85+
*/
86+
find(objectName: string, query?: QueryOptions): Promise<any[]>;
87+
88+
/**
89+
* Update a record by ID
90+
*
91+
* @param objectName - Name of the object/table
92+
* @param id - Record ID
93+
* @param data - Updated data (partial update)
94+
* @returns Promise resolving to the updated record
95+
*
96+
* @example
97+
* ```ts
98+
* const updatedUser = await engine.update('user', '123', {
99+
* name: 'Jane Doe',
100+
* email: 'jane@example.com'
101+
* });
102+
* ```
103+
*/
104+
update(objectName: string, id: any, data: any): Promise<any>;
105+
106+
/**
107+
* Delete a record by ID
108+
*
109+
* @param objectName - Name of the object/table
110+
* @param id - Record ID
111+
* @returns Promise resolving to true if deleted, false otherwise
112+
*
113+
* @example
114+
* ```ts
115+
* const deleted = await engine.delete('user', '123');
116+
* if (deleted) {
117+
* console.log('User deleted successfully');
118+
* }
119+
* ```
120+
*/
121+
delete(objectName: string, id: any): Promise<boolean>;
122+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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
58+
*/
59+
header(name: string, value: 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

Comments
 (0)