@modern-js/plugin-express 2.0.0-beta.2 → 2.0.0-beta.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/CHANGELOG.md CHANGED
@@ -1,5 +1,91 @@
1
1
  # @modern-js/plugin-express
2
2
 
3
+ ## 2.0.0-beta.4
4
+
5
+ ### Major Changes
6
+
7
+ - dda38c9c3e: chore: v2
8
+
9
+ ### Patch Changes
10
+
11
+ - 8d24bed25b: fix: compat Hook API in /server namespace
12
+ fix: 在 @modern-js/runtime/server 命名空间下兼容 Hook API
13
+ - 3bbea92b2a: feat: support Hook、Middleware new API
14
+ feat: 支持 Hook、Middleware 的新 API
15
+ - Updated dependencies [7879e8f]
16
+ - Updated dependencies [6aca875]
17
+ - Updated dependencies [2e6031955e]
18
+ - Updated dependencies [2344eb26ed]
19
+ - Updated dependencies [a2509bfbdb]
20
+ - Updated dependencies [7b7d12c]
21
+ - Updated dependencies [92f0eade39]
22
+ - Updated dependencies [edd1cfb1af]
23
+ - Updated dependencies [cc971eabfc]
24
+ - Updated dependencies [5b9049f2e9]
25
+ - Updated dependencies [6bda14ed71]
26
+ - Updated dependencies [92004d1906]
27
+ - Updated dependencies [b8bbe036c7]
28
+ - Updated dependencies [40ed5874c6]
29
+ - Updated dependencies [87c1ff86b9]
30
+ - Updated dependencies [d5a31df781]
31
+ - Updated dependencies [dda38c9c3e]
32
+ - Updated dependencies [102d32e4ba]
33
+ - Updated dependencies [8b8e1bb571]
34
+ - Updated dependencies [3bbea92b2a]
35
+ - Updated dependencies [b710adb843]
36
+ - Updated dependencies [ea7cf06]
37
+ - Updated dependencies [bbe4c4a]
38
+ - Updated dependencies [e4558a0]
39
+ - Updated dependencies [abf3421a75]
40
+ - Updated dependencies [543be9558e]
41
+ - Updated dependencies [14b712da84]
42
+ - @modern-js/types@2.0.0-beta.4
43
+ - @modern-js/utils@2.0.0-beta.4
44
+ - @modern-js/bff-core@2.0.0-beta.4
45
+ - @modern-js/bff-runtime@2.0.0-beta.4
46
+
47
+ ## 2.0.0-beta.3
48
+
49
+ ### Major Changes
50
+
51
+ - dda38c9c3e: chore: v2
52
+
53
+ ### Patch Changes
54
+
55
+ - 8d24bed25b: fix: compat Hook API in /server namespace
56
+ fix: 在 @modern-js/runtime/server 命名空间下兼容 Hook API
57
+ - 3bbea92b2a: feat: support Hook、Middleware new API
58
+ feat: 支持 Hook、Middleware 的新 API
59
+ - Updated dependencies [6aca875]
60
+ - Updated dependencies [2e60319]
61
+ - Updated dependencies [2344eb26ed]
62
+ - Updated dependencies [a2509bfbdb]
63
+ - Updated dependencies [92f0eade39]
64
+ - Updated dependencies [edd1cfb1af]
65
+ - Updated dependencies [cc971eabfc]
66
+ - Updated dependencies [5b9049f2e9]
67
+ - Updated dependencies [6bda14ed71]
68
+ - Updated dependencies [92004d1906]
69
+ - Updated dependencies [b8bbe036c7]
70
+ - Updated dependencies [40ed5874c6]
71
+ - Updated dependencies [87c1ff86b9]
72
+ - Updated dependencies [d5a31df781]
73
+ - Updated dependencies [dda38c9c3e]
74
+ - Updated dependencies [102d32e4ba]
75
+ - Updated dependencies [8b8e1bb571]
76
+ - Updated dependencies [3bbea92b2a]
77
+ - Updated dependencies [b710adb]
78
+ - Updated dependencies [ea7cf06]
79
+ - Updated dependencies [bbe4c4a]
80
+ - Updated dependencies [e4558a0]
81
+ - Updated dependencies [abf3421a75]
82
+ - Updated dependencies [543be9558e]
83
+ - Updated dependencies [14b712da84]
84
+ - @modern-js/utils@2.0.0-beta.3
85
+ - @modern-js/types@2.0.0-beta.3
86
+ - @modern-js/bff-core@2.0.0-beta.3
87
+ - @modern-js/bff-runtime@2.0.0-beta.3
88
+
3
89
  ## 2.0.0-beta.2
4
90
 
5
91
  ### Major Changes
@@ -1,29 +1,31 @@
1
- import * as path from 'path';
2
- import { createRuntimeExportsUtils } from '@modern-js/utils';
3
- import { getRelativeRuntimePath } from '@modern-js/bff-core';
4
- export default (() => ({
5
- name: '@modern-js/plugin-express',
6
- setup: api => {
1
+ import * as path from "path";
2
+ import { createRuntimeExportsUtils } from "@modern-js/utils";
3
+ import { getRelativeRuntimePath } from "@modern-js/bff-core";
4
+ var cli_default = () => ({
5
+ name: "@modern-js/plugin-express",
6
+ setup: (api) => {
7
7
  let bffExportsUtils;
8
- const {
9
- useAppContext
10
- } = api;
11
- const runtimeModulePath = path.resolve(__dirname, '../runtime');
8
+ const { useAppContext } = api;
9
+ const runtimeModulePath = path.resolve(__dirname, "../runtime");
12
10
  return {
13
11
  config() {
14
12
  const appContext = useAppContext();
15
- const {
16
- appDirectory
17
- } = appContext;
18
- bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
13
+ const { appDirectory } = appContext;
14
+ bffExportsUtils = createRuntimeExportsUtils(
15
+ appContext.internalDirectory,
16
+ "server"
17
+ );
19
18
  const serverRuntimePath = bffExportsUtils.getPath();
20
- const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
21
- if (process.env.NODE_ENV === 'production') {
19
+ const relativeRuntimePath = getRelativeRuntimePath(
20
+ appDirectory,
21
+ serverRuntimePath
22
+ );
23
+ if (process.env.NODE_ENV === "production") {
22
24
  return {
23
25
  source: {
24
26
  alias: {
25
- '@modern-js/runtime/server': relativeRuntimePath,
26
- '@modern-js/runtime/express': relativeRuntimePath
27
+ "@modern-js/runtime/server": relativeRuntimePath,
28
+ "@modern-js/runtime/express": relativeRuntimePath
27
29
  }
28
30
  }
29
31
  };
@@ -31,8 +33,8 @@ export default (() => ({
31
33
  return {
32
34
  source: {
33
35
  alias: {
34
- '@modern-js/runtime/server': serverRuntimePath,
35
- '@modern-js/runtime/express': serverRuntimePath
36
+ "@modern-js/runtime/server": serverRuntimePath,
37
+ "@modern-js/runtime/express": serverRuntimePath
36
38
  }
37
39
  }
38
40
  };
@@ -40,8 +42,14 @@ export default (() => ({
40
42
  },
41
43
  addRuntimeExports(input) {
42
44
  const currentFile = bffExportsUtils.getPath();
43
- const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
44
- const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('express'));
45
+ const relativeRuntimeModulePath = path.relative(
46
+ path.dirname(currentFile),
47
+ runtimeModulePath
48
+ );
49
+ const relativeFramePath = path.relative(
50
+ path.dirname(currentFile),
51
+ require.resolve("express")
52
+ );
45
53
  bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
46
54
  const express = require('${relativeFramePath}')
47
55
  module.exports = {
@@ -53,4 +61,7 @@ export default (() => ({
53
61
  }
54
62
  };
55
63
  }
56
- }));
64
+ });
65
+ export {
66
+ cli_default as default
67
+ };
@@ -1,6 +1,6 @@
1
- import { createStorage } from '@modern-js/bff-core';
2
- const {
1
+ import { createStorage } from "@modern-js/bff-core";
2
+ const { run, useContext } = createStorage();
3
+ export {
3
4
  run,
4
5
  useContext
5
- } = createStorage();
6
- export { run, useContext };
6
+ };
@@ -1,3 +1,6 @@
1
1
  import plugin from "./plugin";
2
2
  export * from "./context";
3
- export default plugin;
3
+ var src_default = plugin;
4
+ export {
5
+ src_default as default
6
+ };
@@ -1,143 +1,139 @@
1
- import * as path from 'path';
2
- import express from 'express';
3
- import cookieParser from 'cookie-parser';
4
- import { fs, createDebugger, compatRequire } from '@modern-js/utils';
5
- import finalhandler from 'finalhandler';
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
21
+ import * as path from "path";
22
+ import express from "express";
23
+ import cookieParser from "cookie-parser";
24
+ import { fs, createDebugger, compatRequire } from "@modern-js/utils";
25
+ import finalhandler from "finalhandler";
6
26
  import { run } from "./context";
7
27
  import registerRoutes from "./registerRoutes";
8
- const debug = createDebugger('express');
9
- const findAppModule = async apiDir => {
10
- const exts = ['.ts', '.js'];
11
- const paths = exts.map(ext => path.resolve(apiDir, `app${ext}`));
28
+ const debug = createDebugger("express");
29
+ const findAppModule = (apiDir) => __async(void 0, null, function* () {
30
+ const exts = [".ts", ".js"];
31
+ const paths = exts.map((ext) => path.resolve(apiDir, `app${ext}`));
12
32
  for (const filename of paths) {
13
- if (await fs.pathExists(filename)) {
14
- // 每次获取 app.ts 的时候,避免使用缓存的 app.ts
33
+ if (yield fs.pathExists(filename)) {
15
34
  delete require.cache[filename];
16
35
  return [compatRequire(filename), require(filename)];
17
36
  }
18
37
  }
19
38
  return [];
20
- };
39
+ });
21
40
  const initMiddlewares = (middleware, app) => {
22
- middleware.forEach(middlewareItem => {
23
- const middlewareFunc = typeof middlewareItem === 'string' ? compatRequire(middlewareItem) : middlewareItem;
41
+ middleware.forEach((middlewareItem) => {
42
+ const middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
24
43
  app.use(middlewareFunc);
25
44
  });
26
45
  };
27
- const useRun = app => {
46
+ const useRun = (app) => {
28
47
  app.use((req, res, next) => {
29
- run({
30
- req,
31
- res
32
- }, next);
48
+ run({ req, res }, next);
33
49
  });
34
50
  };
35
- const initApp = app => {
51
+ const initApp = (app) => {
36
52
  app.use(cookieParser());
37
53
  app.use(express.text());
38
- app.use(express.urlencoded({
39
- extended: true
40
- }));
54
+ app.use(express.urlencoded({ extended: true }));
41
55
  app.use(express.json());
42
56
  return app;
43
57
  };
44
- export default (() => ({
45
- name: '@modern-js/plugin-express',
46
- pre: ['@modern-js/plugin-bff'],
47
- post: ['@modern-js/plugin-server'],
48
- setup: api => ({
49
- async prepareApiServer({
50
- pwd,
51
- config
52
- }) {
53
- let app;
54
- const apiDir = path.join(pwd, './api');
55
- const appContext = api.useAppContext();
56
- const apiHandlerInfos = appContext.apiHandlerInfos;
57
- const mode = appContext.apiMode;
58
- if (mode === 'framework') {
59
- const appModule = await findAppModule(apiDir);
60
- app = appModule[0];
61
- const hooks = appModule[1];
62
- if (!app || !app.use) {
63
- // console.warn('There is not api/app.ts.');
64
- app = express();
65
- }
66
- initApp(app);
67
- if (config) {
68
- const {
69
- middleware
70
- } = config;
71
- initMiddlewares(middleware, app);
72
- }
73
- useRun(app);
74
- registerRoutes(app, apiHandlerInfos);
75
- if (hooks) {
76
- const {
77
- afterLambdaRegisted
78
- } = hooks;
79
- if (afterLambdaRegisted) {
80
- afterLambdaRegisted(app);
58
+ var plugin_default = () => ({
59
+ name: "@modern-js/plugin-express",
60
+ pre: ["@modern-js/plugin-bff"],
61
+ post: ["@modern-js/plugin-server"],
62
+ setup: (api) => ({
63
+ prepareApiServer(_0) {
64
+ return __async(this, arguments, function* ({ pwd, config }) {
65
+ let app;
66
+ const apiDir = path.join(pwd, "./api");
67
+ const appContext = api.useAppContext();
68
+ const apiHandlerInfos = appContext.apiHandlerInfos;
69
+ const mode = appContext.apiMode;
70
+ if (mode === "framework") {
71
+ const appModule = yield findAppModule(apiDir);
72
+ app = appModule[0];
73
+ const hooks = appModule[1];
74
+ if (!app || !app.use) {
75
+ app = express();
81
76
  }
82
- }
83
- } else if (mode === 'function') {
84
- app = express();
85
- initApp(app);
86
- if (config) {
87
- const {
88
- middleware
89
- } = config;
90
- initMiddlewares(middleware, app);
91
- }
92
- useRun(app);
93
- registerRoutes(app, apiHandlerInfos);
94
- } else {
95
- throw new Error(`mode must be function or framework`);
96
- }
97
- return (req, res) => new Promise((resolve, reject) => {
98
- const handler = err => {
99
- if (err) {
100
- return reject(err);
77
+ initApp(app);
78
+ if (config) {
79
+ const { middleware } = config;
80
+ initMiddlewares(middleware, app);
101
81
  }
102
- // finalhanlder will trigger 'finish' event
103
- return finalhandler(req, res, {})(null);
104
- // return resolve();
105
- };
106
-
107
- res.on('finish', err => {
108
- if (err) {
109
- return reject(err);
82
+ useRun(app);
83
+ registerRoutes(app, apiHandlerInfos);
84
+ if (hooks) {
85
+ const { afterLambdaRegisted } = hooks;
86
+ if (afterLambdaRegisted) {
87
+ afterLambdaRegisted(app);
88
+ }
110
89
  }
111
- return resolve();
90
+ } else if (mode === "function") {
91
+ app = express();
92
+ initApp(app);
93
+ if (config) {
94
+ const { middleware } = config;
95
+ initMiddlewares(middleware, app);
96
+ }
97
+ useRun(app);
98
+ registerRoutes(app, apiHandlerInfos);
99
+ } else {
100
+ throw new Error(`mode must be function or framework`);
101
+ }
102
+ return (req, res) => new Promise((resolve, reject) => {
103
+ const handler = (err) => {
104
+ if (err) {
105
+ return reject(err);
106
+ }
107
+ return finalhandler(req, res, {})(null);
108
+ };
109
+ res.on("finish", (err) => {
110
+ if (err) {
111
+ return reject(err);
112
+ }
113
+ return resolve();
114
+ });
115
+ return app(req, res, handler);
112
116
  });
113
- return app(req, res, handler);
114
117
  });
115
118
  },
116
- prepareWebServer({
117
- config
118
- }, next) {
119
- var _userConfig$server;
119
+ prepareWebServer({ config }, next) {
120
+ var _a2;
120
121
  const userConfig = api.useConfigContext();
121
- if (userConfig !== null && userConfig !== void 0 && (_userConfig$server = userConfig.server) !== null && _userConfig$server !== void 0 && _userConfig$server.disableFrameworkExt) {
122
+ if ((_a2 = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a2.disableFrameworkExt) {
122
123
  return next();
123
124
  }
124
125
  const app = express();
125
126
  initApp(app);
126
127
  if (config) {
127
- const {
128
- middleware
129
- } = config;
130
- debug('web middleware', middleware);
128
+ const { middleware } = config;
129
+ debug("web middleware", middleware);
131
130
  initMiddlewares(middleware, app);
132
131
  }
133
- return ctx => new Promise((resolve, reject) => {
132
+ return (ctx) => new Promise((resolve, reject) => {
134
133
  const {
135
- source: {
136
- req,
137
- res
138
- }
134
+ source: { req, res }
139
135
  } = ctx;
140
- const handler = err => {
136
+ const handler = (err) => {
141
137
  if (err) {
142
138
  return reject(err);
143
139
  }
@@ -146,9 +142,7 @@ export default (() => ({
146
142
  }
147
143
  return resolve();
148
144
  };
149
-
150
- // when user call res.send
151
- res.on('finish', err => {
145
+ res.on("finish", (err) => {
152
146
  if (err) {
153
147
  return reject(err);
154
148
  }
@@ -158,4 +152,7 @@ export default (() => ({
158
152
  });
159
153
  }
160
154
  })
161
- }));
155
+ });
156
+ export {
157
+ plugin_default as default
158
+ };
@@ -1,14 +1,10 @@
1
- import 'reflect-metadata';
1
+ import "reflect-metadata";
2
2
  import { createRouteHandler } from "./utils";
3
3
  const registerRoutes = (app, handlerInfos) => {
4
- handlerInfos.forEach(({
5
- routePath,
6
- handler,
7
- httpMethod
8
- }) => {
4
+ handlerInfos.forEach(({ routePath, handler, httpMethod }) => {
9
5
  const routeHandler = createRouteHandler(handler);
10
6
  const method = httpMethod.toLowerCase();
11
- const routeMiddlwares = Reflect.getMetadata('middleware', handler) || [];
7
+ const routeMiddlwares = Reflect.getMetadata("middleware", handler) || [];
12
8
  if (routeMiddlwares.length > 0) {
13
9
  app[method](routePath, routeMiddlwares, routeHandler);
14
10
  } else {
@@ -16,4 +12,7 @@ const registerRoutes = (app, handlerInfos) => {
16
12
  }
17
13
  });
18
14
  };
19
- export default registerRoutes;
15
+ var registerRoutes_default = registerRoutes;
16
+ export {
17
+ registerRoutes_default as default
18
+ };
@@ -1 +1,4 @@
1
- export const hook = attacher => attacher;
1
+ const hook = (attacher) => attacher;
2
+ export {
3
+ hook
4
+ };
@@ -1,4 +1,8 @@
1
- export * from '@modern-js/bff-core';
2
- export { useContext } from "../context";
3
- export { hook } from "./hook";
4
- export * from "./operators";
1
+ export * from "@modern-js/bff-core";
2
+ import { useContext } from "../context";
3
+ import { hook } from "./hook";
4
+ export * from "./operators";
5
+ export {
6
+ hook,
7
+ useContext
8
+ };
@@ -1,54 +1,68 @@
1
+ var __async = (__this, __arguments, generator) => {
2
+ return new Promise((resolve, reject) => {
3
+ var fulfilled = (value) => {
4
+ try {
5
+ step(generator.next(value));
6
+ } catch (e) {
7
+ reject(e);
8
+ }
9
+ };
10
+ var rejected = (value) => {
11
+ try {
12
+ step(generator.throw(value));
13
+ } catch (e) {
14
+ reject(e);
15
+ }
16
+ };
17
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
18
+ step((generator = generator.apply(__this, __arguments)).next());
19
+ });
20
+ };
1
21
  import { useContext } from "../context";
2
- export const Pipe = func => {
22
+ const Pipe = (func) => {
3
23
  return {
4
- name: 'pipe',
5
- // eslint-disable-next-line consistent-return
6
- async execute(executeHelper, next) {
7
- const {
8
- inputs
9
- } = executeHelper;
10
- const ctx = useContext();
11
- const {
12
- res
13
- } = ctx;
14
- if (typeof func === 'function') {
15
- let isPiped = true;
16
- const end = value => {
17
- isPiped = false;
18
- if (typeof value === 'function') {
19
- value(res);
20
- return;
21
- }
22
- // eslint-disable-next-line consistent-return
23
- return value;
24
- };
25
- const output = await func(inputs, end);
26
- if (!isPiped) {
27
- if (output) {
28
- return executeHelper.result = output;
29
- } else {
30
- // eslint-disable-next-line consistent-return
31
- return;
24
+ name: "pipe",
25
+ execute(executeHelper, next) {
26
+ return __async(this, null, function* () {
27
+ const { inputs } = executeHelper;
28
+ const ctx = useContext();
29
+ const { res } = ctx;
30
+ if (typeof func === "function") {
31
+ let isPiped = true;
32
+ const end = (value) => {
33
+ isPiped = false;
34
+ if (typeof value === "function") {
35
+ value(res);
36
+ return;
37
+ }
38
+ return value;
39
+ };
40
+ const output = yield func(inputs, end);
41
+ if (!isPiped) {
42
+ if (output) {
43
+ return executeHelper.result = output;
44
+ } else {
45
+ return;
46
+ }
32
47
  }
48
+ executeHelper.inputs = output;
49
+ yield next();
33
50
  }
34
- executeHelper.inputs = output;
35
- await next();
36
- }
51
+ });
37
52
  }
38
53
  };
39
54
  };
40
-
41
- // eslint-disable-next-line @typescript-eslint/no-redeclare
42
-
43
- export const Middleware = middleware => {
55
+ const Middleware = (middleware) => {
44
56
  return {
45
- name: 'middleware',
57
+ name: "middleware",
46
58
  metadata(helper) {
47
- const middlewares = helper.getMetadata('pipe') || [];
59
+ const middlewares = helper.getMetadata("pipe") || [];
48
60
  middlewares.push(middleware);
49
- helper.setMetadata('middleware', middlewares);
61
+ helper.setMetadata("middleware", middlewares);
50
62
  }
51
63
  };
52
64
  };
53
-
54
- // eslint-disable-next-line @typescript-eslint/no-redeclare
65
+ export {
66
+ Middleware,
67
+ Pipe
68
+ };