Skip to content

Commit 1425205

Browse files
committed
feat: 增加应用程序的注册和获取功能,更新 API 路由
1 parent f4d97b6 commit 1425205

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

examples/server/src/index.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ app.get('/api/v1/meta/objects/:name', (c) => {
4343
return c.json(schema);
4444
});
4545

46+
/**
47+
* Metadata API: Get All Apps
48+
*/
49+
app.get('/api/v1/meta/apps', (c) => {
50+
const apps = SchemaRegistry.getAllApps().map(app => ({
51+
name: app.name,
52+
label: app.label,
53+
icon: app.icon,
54+
description: app.description,
55+
path: `/api/v1/meta/apps/${app.name}`
56+
}));
57+
return c.json({ data: apps });
58+
});
59+
60+
/**
61+
* Metadata API: Get Single App
62+
*/
63+
app.get('/api/v1/meta/apps/:name', (c) => {
64+
const name = c.req.param('name');
65+
const app = SchemaRegistry.getApp(name);
66+
if (!app) return c.json({ error: 'Not found' }, 404);
67+
return c.json(app);
68+
});
69+
4670
/**
4771
* Data API: Find
4872
*/
@@ -120,7 +144,7 @@ app.delete('/api/v1/data/:object/:id', async (c) => {
120144
});
121145

122146
// 4. Start Server
123-
const port = 3001;
147+
const port = 3003;
124148
console.log(`Server is running on http://localhost:${port}`);
125149

126150
serve({

examples/server/src/kernel/registry.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
import { ServiceObject } from '@objectstack/spec';
1+
import { ServiceObject, App } from '@objectstack/spec';
22

33
/**
44
* Global Schema Registry
55
*/
66
export class SchemaRegistry {
77
private static objects = new Map<string, ServiceObject>();
8+
private static apps = new Map<string, App>();
89

10+
/**
11+
* Register a new object schema
12+
*/
913
static register(schema: ServiceObject) {
1014
if (this.objects.has(schema.name)) {
1115
console.warn(`[Registry] Overwriting object: ${schema.name}`);
@@ -21,4 +25,23 @@ export class SchemaRegistry {
2125
static getAll(): ServiceObject[] {
2226
return Array.from(this.objects.values());
2327
}
28+
29+
/**
30+
* Register a new app schema
31+
*/
32+
static registerApp(app: App) {
33+
if (this.apps.has(app.name)) {
34+
console.warn(`[Registry] Overwriting app: ${app.name}`);
35+
}
36+
this.apps.set(app.name, app);
37+
console.log(`[Registry] Registered app: ${app.name}`);
38+
}
39+
40+
static getApp(name: string): App | undefined {
41+
return this.apps.get(name);
42+
}
43+
44+
static getAllApps(): App[] {
45+
return Array.from(this.apps.values());
46+
}
2447
}

examples/server/src/loader.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SchemaRegistry } from './kernel/registry';
2+
import { AppSchema } from '@objectstack/spec';
23

34
// In a real monorepo scenario, we might use path aliases or require.resolve
45
// Here we use relative paths to demonstrate loading from the sibling packages
@@ -15,6 +16,10 @@ export function loadPlugins() {
1516

1617
console.log(`[Loader] Loading App: ${app.name} (${app.label})`);
1718

19+
// 0. Register App
20+
const parsedApp = AppSchema.parse(app);
21+
SchemaRegistry.registerApp(parsedApp);
22+
1823
// 1. Register Objects
1924
if (app.objects) {
2025
app.objects.forEach((obj: any) => {

0 commit comments

Comments
 (0)