@modern-js/plugin-express 1.3.0 → 1.4.1

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,54 @@
1
1
  # @modern-js/plugin-express
2
2
 
3
+ ## 1.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 3eee457b: fix: move some peerDependencies to dependecies
8
+ - Updated dependencies [cc5e8001]
9
+ - Updated dependencies [2520ea86]
10
+ - Updated dependencies [db43dce6]
11
+ - Updated dependencies [e81fd9b7]
12
+ - Updated dependencies [1c411e71]
13
+ - @modern-js/core@1.4.6
14
+ - @modern-js/utils@1.3.4
15
+
16
+ ## 1.4.0
17
+
18
+ ### Minor Changes
19
+
20
+ - ec4dbffb: feat: support as a pure api service
21
+
22
+ ### Patch Changes
23
+
24
+ - 272cab15: refactor server plugin manager
25
+ - Updated dependencies [d9cc5ea9]
26
+ - Updated dependencies [bd819a8d]
27
+ - Updated dependencies [ec4dbffb]
28
+ - Updated dependencies [d099e5c5]
29
+ - Updated dependencies [bada2879]
30
+ - Updated dependencies [24f616ca]
31
+ - Updated dependencies [bd819a8d]
32
+ - Updated dependencies [272cab15]
33
+ - @modern-js/core@1.4.0
34
+ - @modern-js/utils@1.3.0
35
+ - @modern-js/server-core@1.2.2
36
+
37
+ ## 1.3.1
38
+
39
+ ### Patch Changes
40
+
41
+ - 83166714: change .npmignore
42
+ - Updated dependencies [83166714]
43
+ - Updated dependencies [c3de9882]
44
+ - Updated dependencies [33ff48af]
45
+ - @modern-js/core@1.3.2
46
+ - @modern-js/adapter-helpers@1.2.1
47
+ - @modern-js/bff-runtime@1.2.1
48
+ - @modern-js/bff-utils@1.2.2
49
+ - @modern-js/server-plugin@1.2.1
50
+ - @modern-js/utils@1.2.2
51
+
3
52
  ## 1.3.0
4
53
 
5
54
  ### Minor Changes
@@ -29,7 +29,7 @@ export default createPlugin(() => {
29
29
  };
30
30
  },
31
31
 
32
- modifyEntryImports(input) {
32
+ addRuntimeExports(input) {
33
33
  // eslint-disable-next-line react-hooks/rules-of-hooks
34
34
  const {
35
35
  appDirectory
@@ -1,7 +1,7 @@
1
1
  import * as path from 'path';
2
2
  import express from 'express';
3
3
  import cookieParser from 'cookie-parser';
4
- import { createPlugin } from '@modern-js/server-plugin';
4
+ import { createPlugin } from '@modern-js/server-core';
5
5
  import { requireModule } from '@modern-js/bff-utils';
6
6
  import { fs, createDebugger } from '@modern-js/utils';
7
7
  import finalhandler from 'finalhandler';
@@ -43,7 +43,7 @@ var _default = (0, _core.createPlugin)(() => {
43
43
  };
44
44
  },
45
45
 
46
- modifyEntryImports(input) {
46
+ addRuntimeExports(input) {
47
47
  // eslint-disable-next-line react-hooks/rules-of-hooks
48
48
  const {
49
49
  appDirectory
@@ -11,7 +11,7 @@ var _express = _interopRequireDefault(require("express"));
11
11
 
12
12
  var _cookieParser = _interopRequireDefault(require("cookie-parser"));
13
13
 
14
- var _serverPlugin = require("@modern-js/server-plugin");
14
+ var _serverCore = require("@modern-js/server-core");
15
15
 
16
16
  var _bffUtils = require("@modern-js/bff-utils");
17
17
 
@@ -70,7 +70,7 @@ const initApp = app => {
70
70
  return app;
71
71
  };
72
72
 
73
- var _default = (0, _serverPlugin.createPlugin)(() => ({
73
+ var _default = (0, _serverCore.createPlugin)(() => ({
74
74
  // eslint-disable-next-line max-statements
75
75
  async prepareApiServer({
76
76
  pwd,
@@ -11,8 +11,10 @@ declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@m
11
11
  watchFiles: import("@modern-js/core").ParallelWorkflow<void, unknown>;
12
12
  fileChange: import("@modern-js/core").AsyncWorkflow<{
13
13
  filename: string;
14
+ eventType: "add" | "unlink" | "change";
14
15
  }, void>;
15
16
  beforeExit: import("@modern-js/core").AsyncWorkflow<void, void>;
17
+ beforeRestart: import("@modern-js/core").AsyncWorkflow<void, void>;
16
18
  } & import("@modern-js/core").ClearDraftProgress<import("@modern-js/core").Hooks>>>>;
17
19
 
18
20
  export default _default;
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.3.0",
14
+ "version": "1.4.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -34,17 +34,20 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@babel/runtime": "^7.15.3",
37
- "@modern-js/adapter-helpers": "^1.2.0",
38
- "@modern-js/utils": "^1.2.0",
37
+ "@modern-js/adapter-helpers": "^1.2.1",
38
+ "@modern-js/utils": "^1.3.4",
39
39
  "cookie-parser": "^1.4.5",
40
40
  "finalhandler": "^1.1.2",
41
41
  "formidable": "^1.2.2",
42
- "type-is": "^1.6.18"
42
+ "type-is": "^1.6.18",
43
+ "@modern-js/core": "^1.4.6",
44
+ "@modern-js/bff-utils": "^1.2.2",
45
+ "@modern-js/server-core": "^1.2.2",
46
+ "@modern-js/bff-runtime": "^1.2.1"
43
47
  },
44
48
  "devDependencies": {
45
49
  "express": "^4.17.1",
46
- "@modern-js/server-utils": "^1.2.0",
47
- "@modern-js/bff-runtime": "^1.2.0",
50
+ "@modern-js/server-utils": "^1.2.1",
48
51
  "@types/cookie-parser": "^1.4.2",
49
52
  "@types/express": "^4.17.13",
50
53
  "@types/finalhandler": "^1.1.1",
@@ -55,9 +58,7 @@
55
58
  "@types/type-is": "^1.6.3",
56
59
  "supertest": "^6.1.6",
57
60
  "typescript": "^4",
58
- "@modern-js/bff-utils": "^1.2.0",
59
- "@modern-js/core": "^1.3.0",
60
- "@modern-js/server-plugin": "^1.2.0",
61
+ "@modern-js/core": "^1.4.6",
61
62
  "@scripts/build": "0.0.0",
62
63
  "jest": "^27",
63
64
  "@scripts/jest-config": "0.0.0"
@@ -68,16 +69,11 @@
68
69
  }
69
70
  },
70
71
  "peerDependencies": {
71
- "@modern-js/bff-utils": "^1.2.0",
72
- "@modern-js/core": "^1.3.0",
73
- "@modern-js/server-plugin": "^1.2.0",
74
- "@modern-js/bff-runtime": "^1.2.0",
75
72
  "express": "^4.17.1"
76
73
  },
77
74
  "publishConfig": {
78
75
  "registry": "https://registry.npmjs.org/",
79
- "access": "public",
80
- "types": "./dist/types/index.d.ts"
76
+ "access": "public"
81
77
  },
82
78
  "scripts": {
83
79
  "new": "modern new",
@@ -1,6 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import request from 'supertest';
3
- import { serverManager } from '@modern-js/server-plugin';
3
+ import { serverManager } from '@modern-js/server-core';
4
4
  import { INTROSPECTION_ROUTE_PATH } from '@modern-js/bff-utils';
5
5
  import plugin from '../src/plugin';
6
6
  import { APIPlugin } from './helpers';
package/tests/helpers.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import path from 'path';
2
- import { createPlugin } from '@modern-js/server-plugin';
2
+ import { createPlugin } from '@modern-js/server-core';
3
3
  import { injectAPIHandlerInfos, API_DIR } from '@modern-js/bff-utils';
4
4
 
5
5
  export const APIPlugin = createPlugin(() => ({
@@ -1,7 +1,7 @@
1
1
  import path from 'path';
2
2
  import express from 'express';
3
3
  import request from 'supertest';
4
- import { serverManager } from '@modern-js/server-plugin';
4
+ import { serverManager } from '@modern-js/server-core';
5
5
  import { INTROSPECTION_ROUTE_PATH } from '@modern-js/bff-utils';
6
6
  import plugin from '../src/plugin';
7
7
  import { APIPlugin } from './helpers';
@@ -2,7 +2,7 @@ import * as path from 'path';
2
2
  import { Buffer } from 'buffer';
3
3
  import { Request, Response } from 'express';
4
4
  import request from 'supertest';
5
- import { serverManager } from '@modern-js/server-plugin';
5
+ import { serverManager } from '@modern-js/server-core';
6
6
  import plugin from '../src/plugin';
7
7
  import './common';
8
8
 
package/src/cli/index.ts DELETED
@@ -1,85 +0,0 @@
1
- import * as path from 'path';
2
- import { useAppContext, createPlugin } from '@modern-js/core';
3
- import { createRuntimeExportsUtils } from '@modern-js/utils';
4
-
5
- export default createPlugin(
6
- () => {
7
- let bffExportsUtils: any;
8
- const runtimeModulePath = path.resolve(__dirname, '../runtime');
9
-
10
- return {
11
- config() {
12
- // eslint-disable-next-line react-hooks/rules-of-hooks
13
- const appContext = useAppContext();
14
- const { appDirectory } = appContext;
15
- bffExportsUtils = createRuntimeExportsUtils(
16
- appContext.internalDirectory,
17
- 'server',
18
- );
19
-
20
- const serverRuntimePath = bffExportsUtils.getPath();
21
-
22
- // Look up one level, because the artifacts after build have dist directories
23
- let relativeRuntimePath = path.join(
24
- '../',
25
- path.relative(appDirectory, serverRuntimePath),
26
- );
27
-
28
- if (
29
- process.env.NODE_ENV === 'development' ||
30
- process.env.NODE_ENV === 'test'
31
- ) {
32
- relativeRuntimePath = `./${path.relative(
33
- appDirectory,
34
- serverRuntimePath,
35
- )}`;
36
- }
37
-
38
- return {
39
- source: {
40
- alias: {
41
- '@modern-js/runtime/server': relativeRuntimePath,
42
- },
43
- },
44
- };
45
- },
46
- modifyEntryImports(input) {
47
- // eslint-disable-next-line react-hooks/rules-of-hooks
48
- const { appDirectory } = useAppContext();
49
- const runtimePath = require.resolve(`@modern-js/runtime`, {
50
- paths: [appDirectory],
51
- });
52
-
53
- const currentFile = bffExportsUtils.getPath();
54
-
55
- const runtimeDir = path.dirname(runtimePath);
56
-
57
- const relativeBffPath = path.relative(
58
- path.dirname(currentFile),
59
- path.join(runtimeDir, './exports/server'),
60
- );
61
- const relativeRuntimeModulePath = path.relative(
62
- path.dirname(currentFile),
63
- runtimeModulePath,
64
- );
65
-
66
- const relativeFramePath = path.relative(
67
- path.dirname(currentFile),
68
- require.resolve('express'),
69
- );
70
-
71
- bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
72
- const pluginRuntime = require('${relativeRuntimeModulePath}');
73
- const express = require('${relativeFramePath}')
74
- module.exports = {
75
- express: express,
76
- ...bffRuntime,
77
- ...pluginRuntime
78
- }
79
- `);
80
- return input;
81
- },
82
- };
83
- },
84
- { name: '@modern-js/plugin-express' },
85
- );
package/src/context.ts DELETED
@@ -1,8 +0,0 @@
1
- import { createStorage } from '@modern-js/adapter-helpers';
2
- import { Request, Response } from 'express';
3
-
4
- export type Context = { req: Request; res: Response };
5
-
6
- const { run, useContext } = createStorage<Context>();
7
-
8
- export { run, useContext };
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- // eslint-disable-next-line filenames/match-exported
2
- import plugin from './plugin';
3
-
4
- export * from './context';
5
- export default plugin;
package/src/plugin.ts DELETED
@@ -1,157 +0,0 @@
1
- import * as path from 'path';
2
- import express, { RequestHandler, Express } from 'express';
3
- import type { Request, Response } from 'express';
4
- import cookieParser from 'cookie-parser';
5
- import { createPlugin } from '@modern-js/server-plugin';
6
- import { requireModule } from '@modern-js/bff-utils';
7
- import { fs, createDebugger } from '@modern-js/utils';
8
- import finalhandler from 'finalhandler';
9
- import { run } from './context';
10
- import registerRoutes from './registerRoutes';
11
-
12
- const debug = createDebugger('express');
13
-
14
- interface FrameConfig {
15
- middleware: (RequestHandler | string)[];
16
- }
17
-
18
- export type Mode = 'function' | 'framework';
19
-
20
- const findAppModule = async (apiDir: string) => {
21
- const exts = ['.ts', '.js'];
22
- const paths = exts.map(ext => path.resolve(apiDir, `app${ext}`));
23
-
24
- for (const filename of paths) {
25
- if (await fs.pathExists(filename)) {
26
- return requireModule(filename);
27
- }
28
- }
29
-
30
- return null;
31
- };
32
-
33
- const initMiddlewares = (
34
- middleware: (RequestHandler | string)[],
35
- app: Express,
36
- ) => {
37
- middleware.forEach(middlewareItem => {
38
- const middlewareFunc =
39
- typeof middlewareItem === 'string'
40
- ? requireModule(middlewareItem)
41
- : middlewareItem;
42
- app.use(middlewareFunc);
43
- });
44
- };
45
-
46
- const useRun = (app: Express) => {
47
- app.use((req, res, next) => {
48
- run({ req, res }, next);
49
- });
50
- };
51
-
52
- const initApp = (app: express.Express) => {
53
- app.use(cookieParser());
54
- app.use(express.text());
55
- app.use(express.urlencoded({ extended: true }));
56
- app.use(express.json());
57
- return app;
58
- };
59
-
60
- export default createPlugin(
61
- () => ({
62
- // eslint-disable-next-line max-statements
63
- async prepareApiServer({ pwd, mode, config }) {
64
- let app: Express;
65
- const apiDir = path.join(pwd, './api');
66
- if (mode === 'framework') {
67
- app = await findAppModule(apiDir);
68
-
69
- if (!app || !app.use) {
70
- // console.warn('There is not api/app.ts.');
71
- app = express();
72
- }
73
- initApp(app);
74
-
75
- if (config) {
76
- const { middleware } = config as FrameConfig;
77
- initMiddlewares(middleware, app);
78
- }
79
- // eslint-disable-next-line react-hooks/rules-of-hooks
80
- useRun(app);
81
-
82
- registerRoutes(app);
83
- } else if (mode === 'function') {
84
- app = express();
85
- initApp(app);
86
-
87
- if (config) {
88
- const { middleware } = config as FrameConfig;
89
- initMiddlewares(middleware, app);
90
- }
91
-
92
- // eslint-disable-next-line react-hooks/rules-of-hooks
93
- useRun(app);
94
-
95
- registerRoutes(app);
96
- } else {
97
- throw new Error(`mode must be function or framework`);
98
- }
99
-
100
- return (req, res) =>
101
- new Promise((resolve, reject) => {
102
- const handler = (err: any) => {
103
- if (err) {
104
- return reject(err);
105
- }
106
- // finalhanlder will trigger 'finish' event
107
- return finalhandler(req, res, {})(null);
108
- // return resolve();
109
- };
110
-
111
- res.on('finish', (err: Error) => {
112
- if (err) {
113
- return reject(err);
114
- }
115
- return resolve();
116
- });
117
- return app(req as Request, res as Response, handler);
118
- });
119
- },
120
-
121
- prepareWebServer({ config }) {
122
- const app = express();
123
- initApp(app);
124
- if (config) {
125
- const { middleware } = config as FrameConfig;
126
- debug('web middleware', middleware);
127
- initMiddlewares(middleware, app);
128
- }
129
-
130
- return (req, res) =>
131
- new Promise((resolve, reject) => {
132
- const handler = (err: string) => {
133
- if (err) {
134
- return reject(err);
135
- }
136
- if (res.headersSent && res.statusCode !== 200) {
137
- finalhandler(req, res, {})(null);
138
- }
139
- return resolve();
140
- };
141
-
142
- // when user call res.send
143
- res.on('finish', (err: Error) => {
144
- if (err) {
145
- return reject(err);
146
- }
147
- return resolve();
148
- });
149
- return app(req as Request, res as Response, handler);
150
- });
151
- },
152
- }),
153
- {
154
- name: '@modern-js/plugin-express',
155
- pre: ['@modern-js/plugin-bff'],
156
- },
157
- ) as any;
@@ -1,119 +0,0 @@
1
- import { HttpMethod, useAPIHandlerInfos } from '@modern-js/bff-utils';
2
- import { isSchemaHandler, InputType } from '@modern-js/bff-runtime';
3
- import {
4
- Express,
5
- Request,
6
- Response,
7
- RequestHandler,
8
- NextFunction,
9
- } from 'express';
10
- import typeIs from 'type-is';
11
- import formidable from 'formidable';
12
- import { sortDynamicRoutes } from '@modern-js/adapter-helpers';
13
- import { createDebugger } from '@modern-js/utils';
14
-
15
- const debug = createDebugger('express');
16
-
17
- const registerRoutes = (app: Express) => {
18
- // eslint-disable-next-line react-hooks/rules-of-hooks
19
- const handlerInfos = useAPIHandlerInfos();
20
- sortDynamicRoutes(handlerInfos);
21
- debug('handlerInfos', handlerInfos);
22
-
23
- handlerInfos.forEach(({ path, handler, method, name }) => {
24
- const wrapedHandler: RequestHandler = async (
25
- req: Request,
26
- res: Response,
27
- next: NextFunction,
28
- ) => {
29
- const input = await getInputFromRequest(req);
30
-
31
- if (isSchemaHandler(handler)) {
32
- const result = await handler(input);
33
- if (result.type !== 'HandleSuccess') {
34
- if (result.type === 'InputValidationError') {
35
- res.status(400);
36
- } else {
37
- res.status(500);
38
- }
39
- return res.json(result.message);
40
- } else {
41
- res.status(200);
42
- return res.json(result.value);
43
- }
44
- } else {
45
- const args = Object.values(input.params as any).concat(input);
46
-
47
- try {
48
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
49
- // @ts-expect-error
50
- const body = await handler(...args);
51
-
52
- // this should never happen
53
- if (res.headersSent) {
54
- return await Promise.resolve();
55
- }
56
-
57
- res.status(200);
58
- return res.json(body);
59
- } catch (e) {
60
- return next(e);
61
- }
62
- }
63
- };
64
-
65
- Object.defineProperties(
66
- wrapedHandler,
67
- Object.getOwnPropertyDescriptors(handler),
68
- );
69
-
70
- if (isNormalMethod(method)) {
71
- const routeName = method.toLowerCase();
72
- (app as any)[routeName](path || name, wrapedHandler);
73
- } else {
74
- throw new Error(`Unknown HTTP Method: ${method}`);
75
- }
76
- });
77
- };
78
-
79
- const isNormalMethod = (method: string): method is HttpMethod =>
80
- Object.keys(HttpMethod).includes(method);
81
-
82
- export default registerRoutes;
83
-
84
- const getInputFromRequest = async (request: Request): Promise<InputType> => {
85
- const draft: Record<string, any> = {
86
- params: request.params,
87
- query: request.query,
88
- headers: request.headers,
89
- cookies: request.headers.cookie,
90
- };
91
-
92
- if (typeIs(request, ['application/json'])) {
93
- draft.data = request.body;
94
- } else if (typeIs(request, ['multipart/form-data'])) {
95
- draft.formData = await resvoleFormData(request);
96
- } else if (typeIs(request, ['application/x-www-form-urlencoded'])) {
97
- draft.formUrlencoded = request.body;
98
- } else {
99
- draft.body = request.body;
100
- }
101
-
102
- return draft as any;
103
- };
104
-
105
- const resvoleFormData = (request: Request): Promise<Record<string, any>> => {
106
- const form = formidable({ multiples: true });
107
- return new Promise((resolve, reject) => {
108
- form.parse(request, (err, fields, files) => {
109
- if (err) {
110
- reject(err);
111
- }
112
-
113
- resolve({
114
- ...fields,
115
- ...files,
116
- });
117
- });
118
- });
119
- };
package/src/runtime.ts DELETED
@@ -1 +0,0 @@
1
- export { useContext } from './context';