@micro-cms/express-adapter 1.0.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.
@@ -0,0 +1,11 @@
1
+ import { Express } from 'express';
2
+ import { App } from '@micro-cms/core';
3
+
4
+ interface ExpressAdapterOptions {
5
+ app: Express;
6
+ cms: App;
7
+ middlewareMap?: Record<string, any>;
8
+ }
9
+ declare function bindExpressRoutes(options: ExpressAdapterOptions): void;
10
+
11
+ export { type ExpressAdapterOptions, bindExpressRoutes };
@@ -0,0 +1,11 @@
1
+ import { Express } from 'express';
2
+ import { App } from '@micro-cms/core';
3
+
4
+ interface ExpressAdapterOptions {
5
+ app: Express;
6
+ cms: App;
7
+ middlewareMap?: Record<string, any>;
8
+ }
9
+ declare function bindExpressRoutes(options: ExpressAdapterOptions): void;
10
+
11
+ export { type ExpressAdapterOptions, bindExpressRoutes };
package/dist/index.js ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ bindExpressRoutes: () => bindExpressRoutes
24
+ });
25
+ module.exports = __toCommonJS(index_exports);
26
+ function bindExpressRoutes(options) {
27
+ const { app, cms, middlewareMap = {} } = options;
28
+ const routes = cms.runtime.getRoutes();
29
+ console.log(`Binding ${routes.length} CMS routes to Express...`);
30
+ routes.forEach((route) => {
31
+ const method = route.method.toLowerCase();
32
+ const path = route.path;
33
+ const middlewares = (route.middleware || []).map((mKey) => {
34
+ const mw = middlewareMap[mKey];
35
+ if (!mw) {
36
+ console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${path}`);
37
+ return (req, res, next) => next();
38
+ }
39
+ return mw;
40
+ });
41
+ app[method](path, ...middlewares, async (req, res) => {
42
+ try {
43
+ await route.handler(req, res);
44
+ } catch (error) {
45
+ console.error(`Error in CMS route ${method.toUpperCase()} ${path}:`, error);
46
+ res.status(500).json({ error: error.message || "Internal Server Error" });
47
+ }
48
+ });
49
+ console.log(` - [${route.method}] ${path}`);
50
+ });
51
+ }
52
+ // Annotate the CommonJS export names for ESM import in node:
53
+ 0 && (module.exports = {
54
+ bindExpressRoutes
55
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,30 @@
1
+ // src/index.ts
2
+ function bindExpressRoutes(options) {
3
+ const { app, cms, middlewareMap = {} } = options;
4
+ const routes = cms.runtime.getRoutes();
5
+ console.log(`Binding ${routes.length} CMS routes to Express...`);
6
+ routes.forEach((route) => {
7
+ const method = route.method.toLowerCase();
8
+ const path = route.path;
9
+ const middlewares = (route.middleware || []).map((mKey) => {
10
+ const mw = middlewareMap[mKey];
11
+ if (!mw) {
12
+ console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${path}`);
13
+ return (req, res, next) => next();
14
+ }
15
+ return mw;
16
+ });
17
+ app[method](path, ...middlewares, async (req, res) => {
18
+ try {
19
+ await route.handler(req, res);
20
+ } catch (error) {
21
+ console.error(`Error in CMS route ${method.toUpperCase()} ${path}:`, error);
22
+ res.status(500).json({ error: error.message || "Internal Server Error" });
23
+ }
24
+ });
25
+ console.log(` - [${route.method}] ${path}`);
26
+ });
27
+ }
28
+ export {
29
+ bindExpressRoutes
30
+ };
package/package.json ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "name": "@micro-cms/express-adapter",
3
+ "version": "1.0.0",
4
+ "description": "Express.js framework adapter for Micro-CMS",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsup src/index.ts --format cjs,esm --dts"
9
+ },
10
+ "peerDependencies": {
11
+ "express": "^4.18.0",
12
+ "@micro-cms/types": "workspace:*",
13
+ "@micro-cms/core": "workspace:*"
14
+ },
15
+ "devDependencies": {
16
+ "@types/express": "^4.17.0",
17
+ "typescript": "^5.3.3",
18
+ "tsup": "^8.0.2"
19
+ }
20
+ }
package/src/index.ts ADDED
@@ -0,0 +1,45 @@
1
+ import { Express } from 'express';
2
+ import { App } from '@micro-cms/core';
3
+ import { RouteDefinition } from '@micro-cms/types';
4
+
5
+ export interface ExpressAdapterOptions {
6
+ app: Express;
7
+ cms: App;
8
+ middlewareMap?: Record<string, any>; // Map middleware keys to actual Express middleware functions
9
+ }
10
+
11
+ export function bindExpressRoutes(options: ExpressAdapterOptions) {
12
+ const { app, cms, middlewareMap = {} } = options;
13
+
14
+ // Cast to any to access the runtime.getRoutes() we added to App
15
+ const routes: RouteDefinition[] = (cms.runtime as any).getRoutes();
16
+
17
+ console.log(`Binding ${routes.length} CMS routes to Express...`);
18
+
19
+ routes.forEach(route => {
20
+ const method = route.method.toLowerCase();
21
+ const path = route.path;
22
+
23
+ // Resolve middleware
24
+ const middlewares = (route.middleware || []).map(mKey => {
25
+ const mw = middlewareMap[mKey];
26
+ if (!mw) {
27
+ console.warn(`Warning: Middleware '${mKey}' not found in middlewareMap for route ${path}`);
28
+ return (req: any, res: any, next: any) => next();
29
+ }
30
+ return mw;
31
+ });
32
+
33
+ // Register with Express
34
+ (app as any)[method](path, ...middlewares, async (req: any, res: any) => {
35
+ try {
36
+ await route.handler(req, res);
37
+ } catch (error: any) {
38
+ console.error(`Error in CMS route ${method.toUpperCase()} ${path}:`, error);
39
+ res.status(500).json({ error: error.message || 'Internal Server Error' });
40
+ }
41
+ });
42
+
43
+ console.log(` - [${route.method}] ${path}`);
44
+ });
45
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "compilerOptions": {
4
+ "baseUrl": ".",
5
+ "outDir": "./dist",
6
+ "rootDir": "./src"
7
+ },
8
+ "include": ["src/**/*.ts"],
9
+ "exclude": ["node_modules", "dist"]
10
+ }