@modern-js/plugin-express 1.3.0 → 1.3.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 +15 -0
- package/package.json +12 -12
- package/src/cli/index.ts +0 -85
- package/src/context.ts +0 -8
- package/src/index.ts +0 -5
- package/src/plugin.ts +0 -157
- package/src/registerRoutes.ts +0 -119
- package/src/runtime.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# @modern-js/plugin-express
|
|
2
2
|
|
|
3
|
+
## 1.3.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 83166714: change .npmignore
|
|
8
|
+
- Updated dependencies [83166714]
|
|
9
|
+
- Updated dependencies [c3de9882]
|
|
10
|
+
- Updated dependencies [33ff48af]
|
|
11
|
+
- @modern-js/core@1.3.2
|
|
12
|
+
- @modern-js/adapter-helpers@1.2.1
|
|
13
|
+
- @modern-js/bff-runtime@1.2.1
|
|
14
|
+
- @modern-js/bff-utils@1.2.2
|
|
15
|
+
- @modern-js/server-plugin@1.2.1
|
|
16
|
+
- @modern-js/utils@1.2.2
|
|
17
|
+
|
|
3
18
|
## 1.3.0
|
|
4
19
|
|
|
5
20
|
### Minor Changes
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.3.
|
|
14
|
+
"version": "1.3.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,8 +34,8 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@babel/runtime": "^7.15.3",
|
|
37
|
-
"@modern-js/adapter-helpers": "^1.2.
|
|
38
|
-
"@modern-js/utils": "^1.2.
|
|
37
|
+
"@modern-js/adapter-helpers": "^1.2.1",
|
|
38
|
+
"@modern-js/utils": "^1.2.2",
|
|
39
39
|
"cookie-parser": "^1.4.5",
|
|
40
40
|
"finalhandler": "^1.1.2",
|
|
41
41
|
"formidable": "^1.2.2",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"express": "^4.17.1",
|
|
46
|
-
"@modern-js/server-utils": "^1.2.
|
|
47
|
-
"@modern-js/bff-runtime": "^1.2.
|
|
46
|
+
"@modern-js/server-utils": "^1.2.1",
|
|
47
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
48
48
|
"@types/cookie-parser": "^1.4.2",
|
|
49
49
|
"@types/express": "^4.17.13",
|
|
50
50
|
"@types/finalhandler": "^1.1.1",
|
|
@@ -55,9 +55,9 @@
|
|
|
55
55
|
"@types/type-is": "^1.6.3",
|
|
56
56
|
"supertest": "^6.1.6",
|
|
57
57
|
"typescript": "^4",
|
|
58
|
-
"@modern-js/bff-utils": "^1.2.
|
|
59
|
-
"@modern-js/core": "^1.3.
|
|
60
|
-
"@modern-js/server-plugin": "^1.2.
|
|
58
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
59
|
+
"@modern-js/core": "^1.3.2",
|
|
60
|
+
"@modern-js/server-plugin": "^1.2.1",
|
|
61
61
|
"@scripts/build": "0.0.0",
|
|
62
62
|
"jest": "^27",
|
|
63
63
|
"@scripts/jest-config": "0.0.0"
|
|
@@ -68,10 +68,10 @@
|
|
|
68
68
|
}
|
|
69
69
|
},
|
|
70
70
|
"peerDependencies": {
|
|
71
|
-
"@modern-js/bff-utils": "^1.2.
|
|
72
|
-
"@modern-js/core": "^1.3.
|
|
73
|
-
"@modern-js/server-plugin": "^1.2.
|
|
74
|
-
"@modern-js/bff-runtime": "^1.2.
|
|
71
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
72
|
+
"@modern-js/core": "^1.3.2",
|
|
73
|
+
"@modern-js/server-plugin": "^1.2.1",
|
|
74
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
75
75
|
"express": "^4.17.1"
|
|
76
76
|
},
|
|
77
77
|
"publishConfig": {
|
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
package/src/index.ts
DELETED
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;
|
package/src/registerRoutes.ts
DELETED
|
@@ -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';
|