@unifiedcommerce/core 0.4.2 → 0.4.3
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/package.json +1 -1
- package/src/interfaces/rest/index.ts +4 -18
- package/src/runtime/server.ts +33 -0
package/package.json
CHANGED
|
@@ -17,7 +17,8 @@ import { searchRoutes } from "./routes/search.js";
|
|
|
17
17
|
import { auditRoutes } from "./routes/audit.js";
|
|
18
18
|
import { adminJobRoutes } from "./routes/admin-jobs.js";
|
|
19
19
|
import { customerRoutes } from "./routes/customers.js";
|
|
20
|
-
|
|
20
|
+
// Entity aliases are registered in server.ts on the top-level app (not this sub-router)
|
|
21
|
+
// because internal re-dispatch requires app.fetch(), not router.fetch().
|
|
21
22
|
|
|
22
23
|
export function createRestRoutes(kernel: Kernel) {
|
|
23
24
|
const router = new OpenAPIHono<AppEnv>({
|
|
@@ -67,23 +68,8 @@ export function createRestRoutes(kernel: Kernel) {
|
|
|
67
68
|
router.route("/audit", auditRoutes(kernel));
|
|
68
69
|
router.route("/admin", adminJobRoutes(kernel));
|
|
69
70
|
|
|
70
|
-
//
|
|
71
|
-
//
|
|
72
|
-
registerEntityAliases(router, kernel);
|
|
73
|
-
|
|
74
|
-
// Static aliases: shorter paths for catalog sub-resources
|
|
75
|
-
for (const [shortcut, target] of [["categories", "catalog/categories"], ["brands", "catalog/brands"]] as const) {
|
|
76
|
-
router.all(`/${shortcut}`, (c) => {
|
|
77
|
-
const url = new URL(c.req.url);
|
|
78
|
-
url.pathname = `/api/${target}`;
|
|
79
|
-
return router.fetch(new Request(url.toString(), c.req.raw));
|
|
80
|
-
});
|
|
81
|
-
router.all(`/${shortcut}/*`, (c) => {
|
|
82
|
-
const url = new URL(c.req.url);
|
|
83
|
-
url.pathname = url.pathname.replace(`/api/${shortcut}`, `/api/${target}`);
|
|
84
|
-
return router.fetch(new Request(url.toString(), c.req.raw));
|
|
85
|
-
});
|
|
86
|
-
}
|
|
71
|
+
// Aliases (entity + categories/brands) are registered in server.ts on the
|
|
72
|
+
// top-level app, not here, because re-dispatch requires app.fetch().
|
|
87
73
|
|
|
88
74
|
// API Reference (Scalar) — disabled in production unless config.exposeOpenApiSpec is true
|
|
89
75
|
const exposeSpec = kernel.config.exposeOpenApiSpec ?? (process.env.NODE_ENV !== "production");
|
package/src/runtime/server.ts
CHANGED
|
@@ -253,6 +253,39 @@ export async function createServer(config: CommerceConfig) {
|
|
|
253
253
|
app.route("/mcp", createMCPHandler(kernel));
|
|
254
254
|
app.route("/api/me", createCustomerPortalRoutes(kernel));
|
|
255
255
|
|
|
256
|
+
// ─── Entity aliases (e.g., /api/products → /api/catalog/entities?type=product) ──
|
|
257
|
+
for (const [entityType, entityConfig] of Object.entries(config.entities ?? {})) {
|
|
258
|
+
if (!entityConfig.alias) continue;
|
|
259
|
+
const alias = entityConfig.alias;
|
|
260
|
+
// List: inject type filter
|
|
261
|
+
app.all(`/api/${alias}`, async (c) => {
|
|
262
|
+
const url = new URL(c.req.url);
|
|
263
|
+
url.pathname = "/api/catalog/entities";
|
|
264
|
+
if (c.req.method === "GET") url.searchParams.set("type", entityType);
|
|
265
|
+
return app.fetch(new Request(url.toString(), c.req.raw));
|
|
266
|
+
});
|
|
267
|
+
// Sub-paths: rewrite /api/{alias}/... → /api/catalog/entities/...
|
|
268
|
+
app.all(`/api/${alias}/:rest{.+}`, async (c) => {
|
|
269
|
+
const url = new URL(c.req.url);
|
|
270
|
+
url.pathname = url.pathname.replace(`/api/${alias}`, "/api/catalog/entities");
|
|
271
|
+
return app.fetch(new Request(url.toString(), c.req.raw));
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Static aliases
|
|
276
|
+
for (const [shortcut, target] of [["categories", "catalog/categories"], ["brands", "catalog/brands"]] as const) {
|
|
277
|
+
app.all(`/api/${shortcut}`, (c) => {
|
|
278
|
+
const url = new URL(c.req.url);
|
|
279
|
+
url.pathname = `/api/${target}`;
|
|
280
|
+
return app.fetch(new Request(url.toString(), c.req.raw));
|
|
281
|
+
});
|
|
282
|
+
app.all(`/api/${shortcut}/:rest{.+}`, (c) => {
|
|
283
|
+
const url = new URL(c.req.url);
|
|
284
|
+
url.pathname = url.pathname.replace(`/api/${shortcut}`, `/api/${target}`);
|
|
285
|
+
return app.fetch(new Request(url.toString(), c.req.raw));
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
|
|
256
289
|
if (config.routes) {
|
|
257
290
|
config.routes(app, kernel);
|
|
258
291
|
}
|