Skip to content

Commit 70113b6

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent f95e70b commit 70113b6

File tree

2 files changed

+614
-44
lines changed

2 files changed

+614
-44
lines changed

packages/runtime/server/src/metadata.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,24 +55,27 @@ export function createMetadataHandler(app: IObjectQL) {
5555
// 1. List Entries (GET /api/metadata/:type)
5656
// ---------------------------------------------------------
5757

58-
// Legacy/Alias: /api/metadata or /api/metadata/objects -> list objects
59-
if (method === 'GET' && (url === '/api/metadata' || url === '/api/metadata/objects')) {
60-
const configs = app.getConfigs();
61-
const objects = Object.values(configs).map(obj => ({
62-
name: obj.name,
63-
label: obj.label || obj.name,
64-
icon: obj.icon,
65-
description: obj.description,
66-
fields: obj.fields || {}
67-
}));
68-
return sendJson({ objects });
69-
}
70-
7158
// Generic List: /api/metadata/:type
59+
// Also handles legacy /api/metadata (defaults to objects)
7260
const listMatch = url.match(/^\/api\/metadata\/([^\/]+)$/);
73-
if (method === 'GET' && listMatch) {
74-
let [, type] = listMatch;
75-
if (type === 'objects') type = 'object'; // Should not hit due to order, but safe to keep.
61+
const isRootMetadata = url === '/api/metadata';
62+
63+
if (method === 'GET' && (listMatch || isRootMetadata)) {
64+
let type = isRootMetadata ? 'object' : listMatch![1];
65+
if (type === 'objects') type = 'object'; // Alias behavior
66+
67+
if (type === 'object') {
68+
const configs = app.getConfigs();
69+
const objects = Object.values(configs).map(obj => ({
70+
name: obj.name,
71+
label: obj.label || obj.name,
72+
icon: obj.icon,
73+
description: obj.description,
74+
fields: obj.fields || {}
75+
}));
76+
// Return both keys for compatibility during migration
77+
return sendJson({ objects, object: objects });
78+
}
7679

7780
const entries = app.metadata.list(type);
7881
// Return simple list
@@ -89,9 +92,10 @@ export function createMetadataHandler(app: IObjectQL) {
8992

9093
if (method === 'GET' && detailMatch) {
9194
let [, type, id] = detailMatch;
95+
if (type === 'objects') type = 'object';
9296

9397
// Handle Object Special Logic (Field Formatting)
94-
if (type === 'objects' || type === 'object') {
98+
if (type === 'object') {
9599
const metadata = app.getObject(id);
96100
if (!metadata) {
97101
return sendError(ErrorCode.NOT_FOUND, `Object '${id}' not found`, 404);
@@ -150,7 +154,7 @@ export function createMetadataHandler(app: IObjectQL) {
150154

151155
// GET /api/metadata/objects/:name/fields/:field
152156
// Legacy path support.
153-
const fieldMatch = url.match(/^\/api\/metadata\/objects\/([^\/]+)\/fields\/([^\/\?]+)$/);
157+
const fieldMatch = url.match(/^\/api\/metadata\/(?:objects|object)\/([^\/]+)\/fields\/([^\/\?]+)$/);
154158
if (method === 'GET' && fieldMatch) {
155159
const [, objectName, fieldName] = fieldMatch;
156160
const metadata = app.getObject(objectName);
@@ -177,7 +181,7 @@ export function createMetadataHandler(app: IObjectQL) {
177181
}
178182

179183
// GET /api/metadata/objects/:name/actions
180-
const actionsMatch = url.match(/^\/api\/metadata\/objects\/([^\/]+)\/actions$/);
184+
const actionsMatch = url.match(/^\/api\/metadata\/(?:objects|object)\/([^\/]+)\/actions$/);
181185
if (method === 'GET' && actionsMatch) {
182186
const [, objectName] = actionsMatch;
183187
const metadata = app.getObject(objectName);

0 commit comments

Comments
 (0)