@modern-js/plugin-express 2.15.0 → 2.17.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.
@@ -1,7 +1,6 @@
1
1
  import * as path from "path";
2
2
  import { createRuntimeExportsUtils } from "@modern-js/utils";
3
- import { getRelativeRuntimePath } from "@modern-js/bff-core";
4
- var cli_default = () => ({
3
+ export default () => ({
5
4
  name: "@modern-js/plugin-express",
6
5
  setup: (api) => {
7
6
  let bffExportsUtils;
@@ -10,29 +9,16 @@ var cli_default = () => ({
10
9
  return {
11
10
  config() {
12
11
  const appContext = useAppContext();
13
- const { appDirectory } = appContext;
14
12
  bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
15
- const serverRuntimePath = bffExportsUtils.getPath();
16
- const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
17
- if (process.env.NODE_ENV === "production") {
18
- return {
19
- source: {
20
- alias: {
21
- "@modern-js/runtime/server": relativeRuntimePath,
22
- "@modern-js/runtime/express": relativeRuntimePath
23
- }
24
- }
25
- };
26
- } else {
27
- return {
28
- source: {
29
- alias: {
30
- "@modern-js/runtime/server": serverRuntimePath,
31
- "@modern-js/runtime/express": serverRuntimePath
32
- }
13
+ const runtimePath = process.env.NODE_ENV === "development" ? require.resolve("@modern-js/plugin-express/runtime") : "@modern-js/plugin-express/runtime";
14
+ return {
15
+ source: {
16
+ alias: {
17
+ "@modern-js/runtime/server": runtimePath,
18
+ "@modern-js/runtime/express": runtimePath
33
19
  }
34
- };
35
- }
20
+ }
21
+ };
36
22
  },
37
23
  collectServerPlugins({ plugins }) {
38
24
  plugins.push({
@@ -58,6 +44,3 @@ var cli_default = () => ({
58
44
  };
59
45
  }
60
46
  });
61
- export {
62
- cli_default as default
63
- };
@@ -1,6 +1,3 @@
1
1
  import { createStorage } from "@modern-js/bff-core";
2
2
  const { run, useContext } = createStorage();
3
- export {
4
- run,
5
- useContext
6
- };
3
+ export { run, useContext };
@@ -1,6 +1,3 @@
1
1
  import plugin from "./plugin";
2
2
  export * from "./context";
3
- var src_default = plugin;
4
- export {
5
- src_default as default
6
- };
3
+ export default plugin;
@@ -46,116 +46,117 @@ const initApp = (app) => {
46
46
  app.use(express.json());
47
47
  return app;
48
48
  };
49
- var plugin_default = () => ({
50
- name: "@modern-js/plugin-express",
51
- pre: [
52
- "@modern-js/plugin-bff"
53
- ],
54
- post: [
55
- "@modern-js/plugin-server"
56
- ],
57
- setup: (api) => ({
58
- async prepareApiServer({ pwd, config, render }) {
59
- var _a;
60
- let app;
61
- const appContext = api.useAppContext();
62
- const apiHandlerInfos = appContext.apiHandlerInfos;
63
- const apiDirectory = appContext.apiDirectory;
64
- const apiDir = apiDirectory || path.join(pwd, "./api");
65
- const mode = appContext.apiMode;
66
- const userConfig = api.useConfigContext();
67
- if (mode === "framework") {
68
- const appModule = await findAppModule(apiDir);
69
- app = appModule[0];
70
- const hooks = appModule[1];
71
- if (!app || !app.use) {
72
- app = express();
73
- }
74
- initApp(app);
75
- if (config) {
76
- const { middleware } = config;
77
- initMiddlewares(middleware, app);
78
- }
79
- useRun(app);
80
- registerRoutes(app, apiHandlerInfos);
81
- if (hooks) {
82
- const { afterLambdaRegisted } = hooks;
83
- if (afterLambdaRegisted) {
84
- afterLambdaRegisted(app);
85
- }
86
- }
87
- } else if (mode === "function") {
88
- app = express();
89
- initApp(app);
90
- if (config) {
91
- const { middleware } = config;
92
- initMiddlewares(middleware, app);
93
- }
94
- useRun(app);
95
- registerRoutes(app, apiHandlerInfos);
96
- } else {
97
- throw new Error(`mode must be function or framework`);
98
- }
99
- if (((_a = userConfig.bff) == null ? void 0 : _a.enableHandleWeb) && render) {
100
- app.use(async (req, res, next) => {
101
- const html = await render(req, res);
102
- if (html) {
103
- res.end(html);
104
- }
105
- next();
106
- });
107
- }
108
- return (req, res) => new Promise((resolve, reject) => {
109
- const handler = (err) => {
110
- if (err) {
111
- return reject(err);
49
+ export default () => {
50
+ return {
51
+ name: "@modern-js/plugin-express",
52
+ pre: [
53
+ "@modern-js/plugin-bff"
54
+ ],
55
+ post: [
56
+ "@modern-js/plugin-server"
57
+ ],
58
+ setup: (api) => {
59
+ return {
60
+ async prepareApiServer({ pwd, config, render }) {
61
+ var _userConfig_bff;
62
+ let app;
63
+ const appContext = api.useAppContext();
64
+ const apiHandlerInfos = appContext.apiHandlerInfos;
65
+ const apiDirectory = appContext.apiDirectory;
66
+ const apiDir = apiDirectory || path.join(pwd, "./api");
67
+ const mode = appContext.apiMode;
68
+ const userConfig = api.useConfigContext();
69
+ if (mode === "framework") {
70
+ const appModule = await findAppModule(apiDir);
71
+ app = appModule[0];
72
+ const hooks = appModule[1];
73
+ if (!app || !app.use) {
74
+ app = express();
75
+ }
76
+ initApp(app);
77
+ if (config) {
78
+ const { middleware } = config;
79
+ initMiddlewares(middleware, app);
80
+ }
81
+ useRun(app);
82
+ registerRoutes(app, apiHandlerInfos);
83
+ if (hooks) {
84
+ const { afterLambdaRegisted } = hooks;
85
+ if (afterLambdaRegisted) {
86
+ afterLambdaRegisted(app);
87
+ }
88
+ }
89
+ } else if (mode === "function") {
90
+ app = express();
91
+ initApp(app);
92
+ if (config) {
93
+ const { middleware } = config;
94
+ initMiddlewares(middleware, app);
95
+ }
96
+ useRun(app);
97
+ registerRoutes(app, apiHandlerInfos);
98
+ } else {
99
+ throw new Error(`mode must be function or framework`);
112
100
  }
113
- return finalhandler(req, res, {})(null);
114
- };
115
- res.on("finish", (err) => {
116
- if (err) {
117
- return reject(err);
101
+ if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
102
+ app.use(async (req, res, next) => {
103
+ const html = await render(req, res);
104
+ if (html) {
105
+ res.end(html);
106
+ }
107
+ next();
108
+ });
118
109
  }
119
- return resolve();
120
- });
121
- return app(req, res, handler);
122
- });
123
- },
124
- prepareWebServer({ config }, next) {
125
- var _a;
126
- const userConfig = api.useConfigContext();
127
- if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
128
- return next();
129
- }
130
- const app = express();
131
- initApp(app);
132
- if (config) {
133
- const { middleware } = config;
134
- debug("web middleware", middleware);
135
- initMiddlewares(middleware, app);
136
- }
137
- return (ctx) => new Promise((resolve, reject) => {
138
- const { source: { req, res } } = ctx;
139
- const handler = (err) => {
140
- if (err) {
141
- return reject(err);
110
+ return (req, res) => new Promise((resolve, reject) => {
111
+ const handler = (err) => {
112
+ if (err) {
113
+ return reject(err);
114
+ }
115
+ return finalhandler(req, res, {})(null);
116
+ };
117
+ res.on("finish", (err) => {
118
+ if (err) {
119
+ return reject(err);
120
+ }
121
+ return resolve();
122
+ });
123
+ return app(req, res, handler);
124
+ });
125
+ },
126
+ prepareWebServer({ config }, next) {
127
+ var _userConfig_server;
128
+ const userConfig = api.useConfigContext();
129
+ if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
130
+ return next();
142
131
  }
143
- if (res.headersSent && res.statusCode !== 200) {
144
- finalhandler(req, res, {})(null);
132
+ const app = express();
133
+ initApp(app);
134
+ if (config) {
135
+ const { middleware } = config;
136
+ debug("web middleware", middleware);
137
+ initMiddlewares(middleware, app);
145
138
  }
146
- return resolve();
147
- };
148
- res.on("finish", (err) => {
149
- if (err) {
150
- return reject(err);
151
- }
152
- return resolve();
153
- });
154
- return app(req, res, handler);
155
- });
139
+ return (ctx) => new Promise((resolve, reject) => {
140
+ const { source: { req, res } } = ctx;
141
+ const handler = (err) => {
142
+ if (err) {
143
+ return reject(err);
144
+ }
145
+ if (res.headersSent && res.statusCode !== 200) {
146
+ finalhandler(req, res, {})(null);
147
+ }
148
+ return resolve();
149
+ };
150
+ res.on("finish", (err) => {
151
+ if (err) {
152
+ return reject(err);
153
+ }
154
+ return resolve();
155
+ });
156
+ return app(req, res, handler);
157
+ });
158
+ }
159
+ };
156
160
  }
157
- })
158
- });
159
- export {
160
- plugin_default as default
161
+ };
161
162
  };
@@ -12,7 +12,4 @@ const registerRoutes = (app, handlerInfos) => {
12
12
  }
13
13
  });
14
14
  };
15
- var registerRoutes_default = registerRoutes;
16
- export {
17
- registerRoutes_default as default
18
- };
15
+ export default registerRoutes;
@@ -1,4 +1 @@
1
- const hook = (attacher) => attacher;
2
- export {
3
- hook
4
- };
1
+ export const hook = (attacher) => attacher;
@@ -1,8 +1,4 @@
1
1
  export * from "@modern-js/bff-core";
2
- import { useContext } from "../context";
3
- import { hook } from "./hook";
2
+ export { useContext } from "../context";
3
+ export { hook } from "./hook";
4
4
  export * from "./operators";
5
- export {
6
- hook,
7
- useContext
8
- };
@@ -1,5 +1,5 @@
1
1
  import { useContext } from "../context";
2
- const Pipe = (func) => {
2
+ export const Pipe = (func) => {
3
3
  return {
4
4
  name: "pipe",
5
5
  // eslint-disable-next-line consistent-return
@@ -31,7 +31,7 @@ const Pipe = (func) => {
31
31
  }
32
32
  };
33
33
  };
34
- const Middleware = (middleware) => {
34
+ export const Middleware = (middleware) => {
35
35
  return {
36
36
  name: "middleware",
37
37
  metadata(helper) {
@@ -41,7 +41,3 @@ const Middleware = (middleware) => {
41
41
  }
42
42
  };
43
43
  };
44
- export {
45
- Middleware,
46
- Pipe
47
- };
@@ -27,9 +27,8 @@ const handleResponseMeta = (res, handler) => {
27
27
  }
28
28
  }
29
29
  };
30
- const createRouteHandler = (handler) => {
30
+ export const createRouteHandler = (handler) => {
31
31
  const apiHandler = async (req, res, next) => {
32
- var _a;
33
32
  const input = await getInputFromRequest(req);
34
33
  if (isWithMetaHandler(handler)) {
35
34
  try {
@@ -65,7 +64,8 @@ const createRouteHandler = (handler) => {
65
64
  }
66
65
  } else if (isInputParamsDeciderHandler(handler)) {
67
66
  try {
68
- const args = ((_a = input == null ? void 0 : input.data) == null ? void 0 : _a.args) || [];
67
+ var _input_data;
68
+ const args = (input === null || input === void 0 ? void 0 : (_input_data = input.data) === null || _input_data === void 0 ? void 0 : _input_data.args) || [];
69
69
  const body = await handler(...args);
70
70
  if (typeof body !== "undefined") {
71
71
  if (typeof body === "object") {
@@ -94,7 +94,7 @@ const createRouteHandler = (handler) => {
94
94
  Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
95
95
  return apiHandler;
96
96
  };
97
- const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
97
+ export const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
98
98
  const getInputFromRequest = async (request) => {
99
99
  const draft = {
100
100
  params: request.params,
@@ -135,7 +135,3 @@ const resolveFormData = (request) => {
135
135
  });
136
136
  });
137
137
  };
138
- export {
139
- createRouteHandler,
140
- isNormalMethod
141
- };
@@ -6,6 +6,6 @@ type MaybeAsync<T> = T | Promise<T>;
6
6
  type PipeFunction<T> = (value: T, end: EndFunction) => MaybeAsync<void> | MaybeAsync<T>;
7
7
  export declare const Pipe: <T>(func: PipeFunction<T>) => Operator;
8
8
  export type Pipe = typeof Pipe;
9
- export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator;
9
+ export declare const Middleware: (middleware: (req: Request, res: Response, next: NextFunction) => void) => Operator<void>;
10
10
  export type Middleware = typeof Middleware;
11
11
  export {};
package/package.json CHANGED
@@ -3,7 +3,11 @@
3
3
  "description": "A Progressive React Framework for modern web development.",
4
4
  "homepage": "https://modernjs.dev",
5
5
  "bugs": "https://github.com/web-infra-dev/modern.js/issues",
6
- "repository": "web-infra-dev/modern.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/modern.js",
9
+ "directory": "packages/server/plugin-express"
10
+ },
7
11
  "license": "MIT",
8
12
  "keywords": [
9
13
  "react",
@@ -11,7 +15,7 @@
11
15
  "modern",
12
16
  "modern.js"
13
17
  ],
14
- "version": "2.15.0",
18
+ "version": "2.17.0",
15
19
  "jsnext:source": "./src/index.ts",
16
20
  "types": "./dist/types/cli/index.d.ts",
17
21
  "main": "./dist/cjs/cli/index.js",
@@ -33,6 +37,9 @@
33
37
  "require": "./dist/cjs/index.js"
34
38
  },
35
39
  "default": "./dist/esm/index.js"
40
+ },
41
+ "./runtime": {
42
+ "default": "./dist/cjs/runtime/index.js"
36
43
  }
37
44
  },
38
45
  "dependencies": {
@@ -42,10 +49,10 @@
42
49
  "formidable": "^1.2.2",
43
50
  "reflect-metadata": "^0.1.13",
44
51
  "type-is": "^1.6.18",
45
- "@modern-js/bff-core": "2.15.0",
46
- "@modern-js/bff-runtime": "2.15.0",
47
- "@modern-js/types": "2.15.0",
48
- "@modern-js/utils": "2.15.0"
52
+ "@modern-js/bff-core": "2.17.0",
53
+ "@modern-js/bff-runtime": "2.17.0",
54
+ "@modern-js/types": "2.17.0",
55
+ "@modern-js/utils": "2.17.0"
49
56
  },
50
57
  "devDependencies": {
51
58
  "@types/cookie-parser": "^1.4.2",
@@ -61,11 +68,11 @@
61
68
  "supertest": "^6.1.6",
62
69
  "typescript": "^4",
63
70
  "zod": "^3.17.3",
64
- "@modern-js/core": "2.15.0",
65
- "@modern-js/app-tools": "2.15.0",
66
- "@modern-js/server-core": "2.15.0",
67
- "@scripts/build": "2.15.0",
68
- "@scripts/jest-config": "2.15.0"
71
+ "@modern-js/core": "2.17.0",
72
+ "@modern-js/app-tools": "2.17.0",
73
+ "@modern-js/server-core": "2.17.0",
74
+ "@scripts/build": "2.17.0",
75
+ "@scripts/jest-config": "2.17.0"
69
76
  },
70
77
  "peerDependencies": {
71
78
  "express": "^4.17.1"