@micro-cms/express-adapter 1.0.2 → 1.0.4
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/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -6
- package/dist/index.mjs +6 -6
- package/package.json +1 -1
- package/src/index.ts +7 -6
package/dist/index.d.mts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -24,29 +24,29 @@ __export(index_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
26
|
function bindExpressRoutes(options) {
|
|
27
|
-
const { app, cms, middlewareMap = {} } = options;
|
|
27
|
+
const { app, cms, middlewareMap = {}, routePrefix = "" } = options;
|
|
28
28
|
const routes = cms.runtime.getRoutes();
|
|
29
29
|
console.log(`Binding ${routes.length} CMS routes to Express...`);
|
|
30
30
|
routes.forEach((route) => {
|
|
31
31
|
const method = route.method.toLowerCase();
|
|
32
|
-
const
|
|
32
|
+
const prefixedPath = `${routePrefix}${route.path}`;
|
|
33
33
|
const middlewares = (route.middleware || []).map((mKey) => {
|
|
34
34
|
const mw = middlewareMap[mKey];
|
|
35
35
|
if (!mw) {
|
|
36
|
-
console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${
|
|
36
|
+
console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${prefixedPath}`);
|
|
37
37
|
return (req, res, next) => next();
|
|
38
38
|
}
|
|
39
39
|
return mw;
|
|
40
40
|
});
|
|
41
|
-
app[method](
|
|
41
|
+
app[method](prefixedPath, ...middlewares, async (req, res) => {
|
|
42
42
|
try {
|
|
43
43
|
await route.handler(req, res);
|
|
44
44
|
} catch (error) {
|
|
45
|
-
console.error(`Error in CMS route ${method.toUpperCase()} ${
|
|
45
|
+
console.error(`Error in CMS route ${method.toUpperCase()} ${prefixedPath}:`, error);
|
|
46
46
|
res.status(500).json({ error: error.message || "Internal Server Error" });
|
|
47
47
|
}
|
|
48
48
|
});
|
|
49
|
-
console.log(` - [${route.method}] ${
|
|
49
|
+
console.log(` - [${route.method}] ${prefixedPath}`);
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
52
|
// Annotate the CommonJS export names for ESM import in node:
|
package/dist/index.mjs
CHANGED
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
function bindExpressRoutes(options) {
|
|
3
|
-
const { app, cms, middlewareMap = {} } = options;
|
|
3
|
+
const { app, cms, middlewareMap = {}, routePrefix = "" } = options;
|
|
4
4
|
const routes = cms.runtime.getRoutes();
|
|
5
5
|
console.log(`Binding ${routes.length} CMS routes to Express...`);
|
|
6
6
|
routes.forEach((route) => {
|
|
7
7
|
const method = route.method.toLowerCase();
|
|
8
|
-
const
|
|
8
|
+
const prefixedPath = `${routePrefix}${route.path}`;
|
|
9
9
|
const middlewares = (route.middleware || []).map((mKey) => {
|
|
10
10
|
const mw = middlewareMap[mKey];
|
|
11
11
|
if (!mw) {
|
|
12
|
-
console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${
|
|
12
|
+
console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${prefixedPath}`);
|
|
13
13
|
return (req, res, next) => next();
|
|
14
14
|
}
|
|
15
15
|
return mw;
|
|
16
16
|
});
|
|
17
|
-
app[method](
|
|
17
|
+
app[method](prefixedPath, ...middlewares, async (req, res) => {
|
|
18
18
|
try {
|
|
19
19
|
await route.handler(req, res);
|
|
20
20
|
} catch (error) {
|
|
21
|
-
console.error(`Error in CMS route ${method.toUpperCase()} ${
|
|
21
|
+
console.error(`Error in CMS route ${method.toUpperCase()} ${prefixedPath}:`, error);
|
|
22
22
|
res.status(500).json({ error: error.message || "Internal Server Error" });
|
|
23
23
|
}
|
|
24
24
|
});
|
|
25
|
-
console.log(` - [${route.method}] ${
|
|
25
|
+
console.log(` - [${route.method}] ${prefixedPath}`);
|
|
26
26
|
});
|
|
27
27
|
}
|
|
28
28
|
export {
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -6,10 +6,11 @@ export interface ExpressAdapterOptions {
|
|
|
6
6
|
app: Express;
|
|
7
7
|
cms: App;
|
|
8
8
|
middlewareMap?: Record<string, any>; // Map middleware keys to actual Express middleware functions
|
|
9
|
+
routePrefix?: string;
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
export function bindExpressRoutes(options: ExpressAdapterOptions) {
|
|
12
|
-
const { app, cms, middlewareMap = {} } = options;
|
|
13
|
+
const { app, cms, middlewareMap = {}, routePrefix = '' } = options;
|
|
13
14
|
|
|
14
15
|
// Cast to any to access the runtime.getRoutes() we added to App
|
|
15
16
|
const routes: RouteDefinition[] = (cms.runtime as any).getRoutes();
|
|
@@ -18,28 +19,28 @@ export function bindExpressRoutes(options: ExpressAdapterOptions) {
|
|
|
18
19
|
|
|
19
20
|
routes.forEach(route => {
|
|
20
21
|
const method = route.method.toLowerCase();
|
|
21
|
-
const
|
|
22
|
+
const prefixedPath = `${routePrefix}${route.path}`;
|
|
22
23
|
|
|
23
24
|
// Resolve middleware
|
|
24
25
|
const middlewares = (route.middleware || []).map(mKey => {
|
|
25
26
|
const mw = middlewareMap[mKey];
|
|
26
27
|
if (!mw) {
|
|
27
|
-
console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${
|
|
28
|
+
console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${prefixedPath}`);
|
|
28
29
|
return (req: any, res: any, next: any) => next();
|
|
29
30
|
}
|
|
30
31
|
return mw;
|
|
31
32
|
});
|
|
32
33
|
|
|
33
34
|
// Register with Express
|
|
34
|
-
(app as any)[method](
|
|
35
|
+
(app as any)[method](prefixedPath, ...middlewares, async (req: any, res: any) => {
|
|
35
36
|
try {
|
|
36
37
|
await route.handler(req, res);
|
|
37
38
|
} catch (error: any) {
|
|
38
|
-
console.error(`Error in CMS route ${method.toUpperCase()} ${
|
|
39
|
+
console.error(`Error in CMS route ${method.toUpperCase()} ${prefixedPath}:`, error);
|
|
39
40
|
res.status(500).json({ error: error.message || 'Internal Server Error' });
|
|
40
41
|
}
|
|
41
42
|
});
|
|
42
43
|
|
|
43
|
-
console.log(` - [${route.method}] ${
|
|
44
|
+
console.log(` - [${route.method}] ${prefixedPath}`);
|
|
44
45
|
});
|
|
45
46
|
}
|