@objectstack/plugin-hono-server 0.8.1 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +175 -0
- package/dist/hono-plugin.d.ts +43 -0
- package/dist/hono-plugin.js +790 -210
- package/package.json +8 -6
- package/src/hono-plugin.test.ts +190 -0
- package/src/hono-plugin.ts +877 -233
- package/tsconfig.json +2 -1
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -77,9 +77,130 @@ interface HonoPluginOptions {
|
|
|
77
77
|
* Path to static files directory (optional)
|
|
78
78
|
*/
|
|
79
79
|
staticRoot?: string;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* REST server configuration
|
|
83
|
+
* Controls automatic endpoint generation and API behavior
|
|
84
|
+
*/
|
|
85
|
+
restConfig?: RestServerConfig;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Whether to register standard ObjectStack CRUD endpoints
|
|
89
|
+
* @default true
|
|
90
|
+
*/
|
|
91
|
+
registerStandardEndpoints?: boolean;
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Whether to load endpoints from API Registry
|
|
95
|
+
* When enabled, routes are loaded dynamically from the API Registry
|
|
96
|
+
* When disabled, uses legacy static route registration
|
|
97
|
+
* @default true
|
|
98
|
+
*/
|
|
99
|
+
useApiRegistry?: boolean;
|
|
80
100
|
}
|
|
81
101
|
```
|
|
82
102
|
|
|
103
|
+
### Using API Registry (New in v0.9.0)
|
|
104
|
+
|
|
105
|
+
The plugin now integrates with the ObjectStack API Registry for centralized endpoint management:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { createApiRegistryPlugin } from '@objectstack/core';
|
|
109
|
+
import { HonoServerPlugin } from '@objectstack/plugin-hono-server';
|
|
110
|
+
|
|
111
|
+
const kernel = new ObjectKernel();
|
|
112
|
+
|
|
113
|
+
// 1. Register API Registry Plugin first
|
|
114
|
+
kernel.use(createApiRegistryPlugin({
|
|
115
|
+
conflictResolution: 'priority' // Handle route conflicts by priority
|
|
116
|
+
}));
|
|
117
|
+
|
|
118
|
+
// 2. Register Hono Server Plugin
|
|
119
|
+
kernel.use(new HonoServerPlugin({
|
|
120
|
+
port: 3000,
|
|
121
|
+
useApiRegistry: true,
|
|
122
|
+
registerStandardEndpoints: true,
|
|
123
|
+
restConfig: {
|
|
124
|
+
api: {
|
|
125
|
+
version: 'v1',
|
|
126
|
+
basePath: '/api',
|
|
127
|
+
enableCrud: true,
|
|
128
|
+
enableMetadata: true,
|
|
129
|
+
enableBatch: true
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}));
|
|
133
|
+
|
|
134
|
+
await kernel.bootstrap();
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
**Benefits of API Registry Integration:**
|
|
138
|
+
- 📋 Centralized endpoint registration and discovery
|
|
139
|
+
- 🔀 Priority-based route conflict resolution
|
|
140
|
+
- 🧩 Support for plugin-registered custom endpoints
|
|
141
|
+
- ⚙️ Configurable endpoint generation via `RestServerConfig`
|
|
142
|
+
- 🔍 API introspection and documentation generation
|
|
143
|
+
|
|
144
|
+
### Configuring REST Server Behavior
|
|
145
|
+
|
|
146
|
+
Use `restConfig` to control which endpoints are automatically generated:
|
|
147
|
+
|
|
148
|
+
```typescript
|
|
149
|
+
new HonoServerPlugin({
|
|
150
|
+
restConfig: {
|
|
151
|
+
api: {
|
|
152
|
+
version: 'v2',
|
|
153
|
+
basePath: '/api',
|
|
154
|
+
enableCrud: true,
|
|
155
|
+
enableMetadata: true,
|
|
156
|
+
enableBatch: true,
|
|
157
|
+
enableDiscovery: true
|
|
158
|
+
},
|
|
159
|
+
crud: {
|
|
160
|
+
dataPrefix: '/data',
|
|
161
|
+
operations: {
|
|
162
|
+
create: true,
|
|
163
|
+
read: true,
|
|
164
|
+
update: true,
|
|
165
|
+
delete: true,
|
|
166
|
+
list: true
|
|
167
|
+
}
|
|
168
|
+
},
|
|
169
|
+
metadata: {
|
|
170
|
+
prefix: '/meta',
|
|
171
|
+
enableCache: true,
|
|
172
|
+
cacheTtl: 3600
|
|
173
|
+
},
|
|
174
|
+
batch: {
|
|
175
|
+
maxBatchSize: 200,
|
|
176
|
+
operations: {
|
|
177
|
+
createMany: true,
|
|
178
|
+
updateMany: true,
|
|
179
|
+
deleteMany: true,
|
|
180
|
+
upsertMany: true
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Legacy Mode (Without API Registry)
|
|
188
|
+
|
|
189
|
+
If the API Registry plugin is not registered, the server automatically falls back to legacy mode:
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
// No API Registry needed for simple setups
|
|
193
|
+
const kernel = new ObjectKernel();
|
|
194
|
+
|
|
195
|
+
kernel.use(new HonoServerPlugin({
|
|
196
|
+
port: 3000,
|
|
197
|
+
useApiRegistry: false // Explicitly disable API Registry
|
|
198
|
+
}));
|
|
199
|
+
|
|
200
|
+
await kernel.bootstrap();
|
|
201
|
+
// All standard routes registered statically
|
|
202
|
+
```
|
|
203
|
+
|
|
83
204
|
## API Endpoints
|
|
84
205
|
|
|
85
206
|
The plugin automatically exposes the following ObjectStack REST API endpoints:
|
|
@@ -164,6 +285,60 @@ export class MyPlugin implements Plugin {
|
|
|
164
285
|
}
|
|
165
286
|
```
|
|
166
287
|
|
|
288
|
+
### Registering Custom Endpoints via API Registry
|
|
289
|
+
|
|
290
|
+
Plugins can register their own endpoints through the API Registry:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
export class MyApiPlugin implements Plugin {
|
|
294
|
+
name = 'my-api-plugin';
|
|
295
|
+
version = '1.0.0';
|
|
296
|
+
|
|
297
|
+
async init(ctx: PluginContext) {
|
|
298
|
+
const apiRegistry = ctx.getService<ApiRegistry>('api-registry');
|
|
299
|
+
|
|
300
|
+
apiRegistry.registerApi({
|
|
301
|
+
id: 'my_custom_api',
|
|
302
|
+
name: 'My Custom API',
|
|
303
|
+
type: 'rest',
|
|
304
|
+
version: 'v1',
|
|
305
|
+
basePath: '/api/v1/custom',
|
|
306
|
+
endpoints: [
|
|
307
|
+
{
|
|
308
|
+
id: 'get_custom_data',
|
|
309
|
+
method: 'GET',
|
|
310
|
+
path: '/api/v1/custom/data',
|
|
311
|
+
summary: 'Get custom data',
|
|
312
|
+
priority: 500, // Lower than core endpoints (950)
|
|
313
|
+
responses: [{
|
|
314
|
+
statusCode: 200,
|
|
315
|
+
description: 'Custom data retrieved'
|
|
316
|
+
}]
|
|
317
|
+
}
|
|
318
|
+
],
|
|
319
|
+
metadata: {
|
|
320
|
+
pluginSource: 'my-api-plugin',
|
|
321
|
+
status: 'active',
|
|
322
|
+
tags: ['custom']
|
|
323
|
+
}
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
ctx.logger.info('Custom API endpoints registered');
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async start(ctx: PluginContext) {
|
|
330
|
+
// Bind the actual handler implementation
|
|
331
|
+
const httpServer = ctx.getService<IHttpServer>('http-server');
|
|
332
|
+
|
|
333
|
+
httpServer.get('/api/v1/custom/data', async (req, res) => {
|
|
334
|
+
res.json({ data: 'my custom data' });
|
|
335
|
+
});
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
**Note:** The Hono Server Plugin loads routes from the API Registry sorted by priority (highest first), ensuring core endpoints take precedence over plugin endpoints.
|
|
341
|
+
|
|
167
342
|
### Extending with Middleware
|
|
168
343
|
|
|
169
344
|
The plugin provides extension points for adding custom middleware:
|
package/dist/hono-plugin.d.ts
CHANGED
|
@@ -1,7 +1,23 @@
|
|
|
1
1
|
import { Plugin, PluginContext } from '@objectstack/core';
|
|
2
|
+
import { RestServerConfig } from '@objectstack/spec/api';
|
|
2
3
|
export interface HonoPluginOptions {
|
|
3
4
|
port?: number;
|
|
4
5
|
staticRoot?: string;
|
|
6
|
+
/**
|
|
7
|
+
* REST server configuration
|
|
8
|
+
* Controls automatic endpoint generation and API behavior
|
|
9
|
+
*/
|
|
10
|
+
restConfig?: RestServerConfig;
|
|
11
|
+
/**
|
|
12
|
+
* Whether to register standard ObjectStack CRUD endpoints
|
|
13
|
+
* @default true
|
|
14
|
+
*/
|
|
15
|
+
registerStandardEndpoints?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Whether to load endpoints from API Registry
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
useApiRegistry?: boolean;
|
|
5
21
|
}
|
|
6
22
|
/**
|
|
7
23
|
* Hono Server Plugin
|
|
@@ -12,6 +28,9 @@ export interface HonoPluginOptions {
|
|
|
12
28
|
export declare class HonoServerPlugin implements Plugin {
|
|
13
29
|
name: string;
|
|
14
30
|
version: string;
|
|
31
|
+
private static readonly DEFAULT_ENDPOINT_PRIORITY;
|
|
32
|
+
private static readonly CORE_ENDPOINT_PRIORITY;
|
|
33
|
+
private static readonly DISCOVERY_ENDPOINT_PRIORITY;
|
|
15
34
|
private options;
|
|
16
35
|
private server;
|
|
17
36
|
constructor(options?: HonoPluginOptions);
|
|
@@ -19,10 +38,34 @@ export declare class HonoServerPlugin implements Plugin {
|
|
|
19
38
|
* Init phase - Setup HTTP server and register as service
|
|
20
39
|
*/
|
|
21
40
|
init(ctx: PluginContext): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Helper to create cache request object from HTTP headers
|
|
43
|
+
*/
|
|
44
|
+
private createCacheRequest;
|
|
22
45
|
/**
|
|
23
46
|
* Start phase - Bind routes and start listening
|
|
24
47
|
*/
|
|
25
48
|
start(ctx: PluginContext): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Register standard ObjectStack API endpoints to the API Registry
|
|
51
|
+
*/
|
|
52
|
+
private registerStandardEndpointsToRegistry;
|
|
53
|
+
/**
|
|
54
|
+
* Bind HTTP routes from API Registry
|
|
55
|
+
*/
|
|
56
|
+
private bindRoutesFromRegistry;
|
|
57
|
+
/**
|
|
58
|
+
* Bind a single endpoint to the HTTP server
|
|
59
|
+
*/
|
|
60
|
+
private bindEndpoint;
|
|
61
|
+
/**
|
|
62
|
+
* Create a route handler for an endpoint
|
|
63
|
+
*/
|
|
64
|
+
private createHandlerForEndpoint;
|
|
65
|
+
/**
|
|
66
|
+
* Legacy route registration (fallback when API Registry is not available)
|
|
67
|
+
*/
|
|
68
|
+
private bindLegacyRoutes;
|
|
26
69
|
/**
|
|
27
70
|
* Destroy phase - Stop server
|
|
28
71
|
*/
|