Skip to content

Commit 26ec96b

Browse files
Copilothotlong
andcommitted
Phase 1: Move contracts to spec package and add new interface abstractions
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 354bbae commit 26ec96b

File tree

14 files changed

+884
-6
lines changed

14 files changed

+884
-6
lines changed

packages/core/src/index.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
/**
2+
* @objectstack/core
3+
*
4+
* Core runtime for ObjectStack microkernel architecture.
5+
* Provides plugin system, dependency injection, and lifecycle management.
6+
*/
7+
18
export * from './kernel.js';
29
export * from './types.js';
3-
export * from './contracts/http-server.js';
4-
export * from './contracts/data-engine.js';
5-
export * from './contracts/logger.js';
610
export * from './logger.js';
711
export * from './plugin-loader.js';
812
export * from './enhanced-kernel.js';
13+
14+
// Re-export contracts from @objectstack/spec for backward compatibility
15+
export type {
16+
Logger,
17+
IHttpServer,
18+
IHttpRequest,
19+
IHttpResponse,
20+
RouteHandler,
21+
Middleware,
22+
IDataEngine,
23+
DriverInterface
24+
} from '@objectstack/spec/contracts';

packages/core/src/logger.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { LoggerConfig, LogLevel } from '@objectstack/spec/system';
2-
import type { Logger } from './contracts/logger.js';
2+
import type { Logger } from '@objectstack/spec/contracts';
33

44
/**
55
* Universal Logger Implementation

packages/core/src/plugin-loader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Plugin, PluginContext } from './types.js';
2-
import type { Logger } from './contracts/logger.js';
2+
import type { Logger } from '@objectstack/spec/contracts';
33
import { z } from 'zod';
44

55
/**

packages/core/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ObjectKernel } from './kernel.js';
2-
import type { Logger } from './contracts/logger.js';
2+
import type { Logger } from '@objectstack/spec/contracts';
33

44
/**
55
* PluginContext - Runtime context available to plugins

packages/spec/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
"./ui": {
4949
"types": "./dist/ui/index.d.ts",
5050
"default": "./dist/ui/index.js"
51+
},
52+
"./contracts": {
53+
"types": "./dist/contracts/index.d.ts",
54+
"default": "./dist/contracts/index.js"
5155
}
5256
},
5357
"files": [
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
DataEngineQueryOptions,
3+
DataEngineInsertOptions,
4+
DataEngineUpdateOptions,
5+
DataEngineDeleteOptions,
6+
DataEngineAggregateOptions,
7+
DataEngineCountOptions,
8+
DataEngineRequest,
9+
QueryAST,
10+
DriverOptions
11+
} from '../data/index.js';
12+
13+
/**
14+
* IDataEngine - Standard Data Engine Interface
15+
*
16+
* Abstract interface for data persistence capabilities.
17+
* Following the Dependency Inversion Principle - plugins depend on this interface,
18+
* not on concrete database implementations.
19+
*
20+
* Aligned with 'src/data/data-engine.zod.ts' in @objectstack/spec.
21+
*/
22+
23+
export interface IDataEngine {
24+
find(objectName: string, query?: DataEngineQueryOptions): Promise<any[]>;
25+
findOne(objectName: string, query?: DataEngineQueryOptions): Promise<any>;
26+
insert(objectName: string, data: any | any[], options?: DataEngineInsertOptions): Promise<any>;
27+
update(objectName: string, data: any, options?: DataEngineUpdateOptions): Promise<any>;
28+
delete(objectName: string, options?: DataEngineDeleteOptions): Promise<any>;
29+
count(objectName: string, query?: DataEngineCountOptions): Promise<number>;
30+
aggregate(objectName: string, query: DataEngineAggregateOptions): Promise<any[]>;
31+
32+
/**
33+
* Vector Search (AI/RAG)
34+
*/
35+
vectorFind?(objectName: string, vector: number[], options?: { filter?: any, limit?: number, select?: string[], threshold?: number }): Promise<any[]>;
36+
37+
/**
38+
* Batch Operations (Transactional)
39+
*/
40+
batch?(requests: DataEngineRequest[], options?: { transaction?: boolean }): Promise<any[]>;
41+
42+
/**
43+
* Execute raw command (Escape hatch)
44+
*/
45+
execute?(command: any, options?: Record<string, any>): Promise<any>;
46+
}
47+
48+
export interface DriverInterface {
49+
name: string;
50+
version: string;
51+
connect(): Promise<void>;
52+
disconnect(): Promise<void>;
53+
54+
find(object: string, query: QueryAST, options?: DriverOptions): Promise<any[]>;
55+
findOne(object: string, query: QueryAST, options?: DriverOptions): Promise<any>;
56+
create(object: string, data: any, options?: DriverOptions): Promise<any>;
57+
update(object: string, id: any, data: any, options?: DriverOptions): Promise<any>;
58+
delete(object: string, id: any, options?: DriverOptions): Promise<any>;
59+
60+
/**
61+
* Bulk & Batch Operations
62+
*/
63+
bulkCreate?(object: string, data: any[], options?: DriverOptions): Promise<any>;
64+
updateMany?(object: string, query: QueryAST, data: any, options?: DriverOptions): Promise<any>;
65+
deleteMany?(object: string, query: QueryAST, options?: DriverOptions): Promise<any>;
66+
67+
count?(object: string, query: QueryAST, options?: DriverOptions): Promise<number>;
68+
69+
/**
70+
* Raw Execution
71+
*/
72+
execute?(command: any, params?: any, options?: DriverOptions): Promise<any>;
73+
}
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 (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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* ObjectStack Contracts
3+
*
4+
* Core interface definitions following "Protocol First" principle.
5+
* All interfaces should be defined in @objectstack/spec to avoid circular dependencies.
6+
*/
7+
8+
export * from './logger.js';
9+
export * from './data-engine.js';
10+
export * from './http-server.js';
11+
export * from './service-registry.js';
12+
export * from './plugin-validator.js';
13+
export * from './startup-orchestrator.js';
14+
export * from './plugin-lifecycle-events.js';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Logger Contract
3+
*
4+
* Defines the interface for logging in ObjectStack.
5+
* Compatible with both browser console and structured logging systems.
6+
*/
7+
export interface Logger {
8+
/**
9+
* Log a debug message
10+
* @param message - The message to log
11+
* @param meta - Optional metadata to include
12+
*/
13+
debug(message: string, meta?: Record<string, any>): void;
14+
15+
/**
16+
* Log an informational message
17+
* @param message - The message to log
18+
* @param meta - Optional metadata to include
19+
*/
20+
info(message: string, meta?: Record<string, any>): void;
21+
22+
/**
23+
* Log a warning message
24+
* @param message - The message to log
25+
* @param meta - Optional metadata to include
26+
*/
27+
warn(message: string, meta?: Record<string, any>): void;
28+
29+
/**
30+
* Log an error message
31+
* @param message - The message to log
32+
* @param error - Optional error object
33+
* @param meta - Optional metadata to include
34+
*/
35+
error(message: string, error?: Error, meta?: Record<string, any>): void;
36+
37+
/**
38+
* Log a fatal error message
39+
* @param message - The message to log
40+
* @param error - Optional error object
41+
* @param meta - Optional metadata to include
42+
*/
43+
fatal?(message: string, error?: Error, meta?: Record<string, any>): void;
44+
45+
/**
46+
* Create a child logger with additional context
47+
* @param context - Context to add to all logs from this child
48+
*/
49+
child?(context: Record<string, any>): Logger;
50+
51+
/**
52+
* Set trace context for distributed tracing
53+
* @param traceId - Trace identifier
54+
* @param spanId - Span identifier
55+
*/
56+
withTrace?(traceId: string, spanId?: string): Logger;
57+
58+
/**
59+
* Compatibility method for console.log usage
60+
* @param message - The message to log
61+
* @param args - Additional arguments
62+
*/
63+
log?(message: string, ...args: any[]): void;
64+
65+
/**
66+
* Cleanup resources (close file streams, etc.)
67+
* Should be called when the logger is no longer needed
68+
*/
69+
destroy?(): Promise<void>;
70+
}

0 commit comments

Comments
 (0)