@modern-js/plugin-koa 2.0.0-beta.3 → 2.0.0-beta.6
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 +89 -0
- package/dist/js/modern/cli/index.js +40 -23
- package/dist/js/modern/context.js +4 -4
- package/dist/js/modern/index.js +4 -1
- package/dist/js/modern/plugin.js +94 -81
- package/dist/js/modern/registerRoutes.js +5 -6
- package/dist/js/modern/runtime.js +7 -3
- package/dist/js/modern/utils.js +60 -27
- package/dist/js/node/cli/index.js +65 -30
- package/dist/js/node/context.js +27 -10
- package/dist/js/node/index.js +33 -22
- package/dist/js/node/plugin.js +125 -95
- package/dist/js/node/registerRoutes.js +27 -14
- package/dist/js/node/runtime.js +28 -27
- package/dist/js/node/utils.js +89 -40
- package/dist/js/treeshaking/cli/index.js +58 -0
- package/dist/js/treeshaking/context.js +3 -0
- package/dist/js/treeshaking/index.js +4 -0
- package/dist/js/treeshaking/plugin.js +363 -0
- package/dist/js/treeshaking/registerRoutes.js +11 -0
- package/dist/js/treeshaking/runtime.js +6 -0
- package/dist/js/treeshaking/utils.js +410 -0
- package/dist/types/cli/index.d.ts +4 -1
- package/dist/types/plugin.d.ts +2 -0
- package/package.json +35 -22
package/dist/js/node/utils.js
CHANGED
|
@@ -1,35 +1,79 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.
|
|
4
|
-
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all)
|
|
9
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
+
};
|
|
11
|
+
var __copyProps = (to, from, except, desc) => {
|
|
12
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
+
for (let key of __getOwnPropNames(from))
|
|
14
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
+
mod
|
|
22
|
+
));
|
|
23
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
+
var __async = (__this, __arguments, generator) => {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
var fulfilled = (value) => {
|
|
27
|
+
try {
|
|
28
|
+
step(generator.next(value));
|
|
29
|
+
} catch (e) {
|
|
30
|
+
reject(e);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
var rejected = (value) => {
|
|
34
|
+
try {
|
|
35
|
+
step(generator.throw(value));
|
|
36
|
+
} catch (e) {
|
|
37
|
+
reject(e);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var utils_exports = {};
|
|
45
|
+
__export(utils_exports, {
|
|
46
|
+
createRouteHandler: () => createRouteHandler,
|
|
47
|
+
isNormalMethod: () => isNormalMethod
|
|
5
48
|
});
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
49
|
+
module.exports = __toCommonJS(utils_exports);
|
|
50
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
51
|
+
var import_bff_runtime = require("@modern-js/bff-runtime");
|
|
52
|
+
var import_type_is = __toESM(require("type-is"));
|
|
11
53
|
const handleResponseMeta = (ctx, handler) => {
|
|
12
|
-
const responseMeta = Reflect.getMetadata(
|
|
54
|
+
const responseMeta = Reflect.getMetadata(
|
|
55
|
+
import_bff_core.HttpMetadata.Response,
|
|
56
|
+
handler
|
|
57
|
+
);
|
|
13
58
|
if (Array.isArray(responseMeta)) {
|
|
14
59
|
for (const meta of responseMeta) {
|
|
15
60
|
const metaType = meta.type;
|
|
16
61
|
const metaValue = meta.value;
|
|
17
62
|
switch (metaType) {
|
|
18
|
-
case
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
63
|
+
case import_bff_core.ResponseMetaType.Headers:
|
|
20
64
|
for (const [key, value] of Object.entries(metaValue)) {
|
|
21
|
-
if (typeof value ===
|
|
65
|
+
if (typeof value === "string") {
|
|
22
66
|
ctx.append(key, value);
|
|
23
67
|
}
|
|
24
68
|
}
|
|
25
69
|
break;
|
|
26
|
-
case
|
|
27
|
-
if (typeof metaValue ===
|
|
70
|
+
case import_bff_core.ResponseMetaType.Redirect:
|
|
71
|
+
if (typeof metaValue === "string") {
|
|
28
72
|
ctx.redirect(metaValue);
|
|
29
73
|
}
|
|
30
74
|
break;
|
|
31
|
-
case
|
|
32
|
-
if (typeof metaValue ===
|
|
75
|
+
case import_bff_core.ResponseMetaType.StatusCode:
|
|
76
|
+
if (typeof metaValue === "number") {
|
|
33
77
|
ctx.status = metaValue;
|
|
34
78
|
}
|
|
35
79
|
break;
|
|
@@ -39,15 +83,14 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
39
83
|
}
|
|
40
84
|
}
|
|
41
85
|
};
|
|
42
|
-
const createRouteHandler = handler => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if ((0, _bffCore.isWithMetaHandler)(handler)) {
|
|
86
|
+
const createRouteHandler = (handler) => {
|
|
87
|
+
const apiHandler = (ctx) => __async(void 0, null, function* () {
|
|
88
|
+
const input = yield getInputFromRequest(ctx);
|
|
89
|
+
if ((0, import_bff_core.isWithMetaHandler)(handler)) {
|
|
47
90
|
try {
|
|
48
91
|
handleResponseMeta(ctx, handler);
|
|
49
|
-
const body =
|
|
50
|
-
if (typeof body !==
|
|
92
|
+
const body = yield handler(input);
|
|
93
|
+
if (typeof body !== "undefined") {
|
|
51
94
|
ctx.body = body;
|
|
52
95
|
}
|
|
53
96
|
} catch (error) {
|
|
@@ -63,10 +106,10 @@ const createRouteHandler = handler => {
|
|
|
63
106
|
};
|
|
64
107
|
}
|
|
65
108
|
}
|
|
66
|
-
} else if ((0,
|
|
67
|
-
const result =
|
|
68
|
-
if (result.type !==
|
|
69
|
-
if (result.type ===
|
|
109
|
+
} else if ((0, import_bff_runtime.isSchemaHandler)(handler)) {
|
|
110
|
+
const result = yield handler(input);
|
|
111
|
+
if (result.type !== "HandleSuccess") {
|
|
112
|
+
if (result.type === "InputValidationError") {
|
|
70
113
|
ctx.status = 400;
|
|
71
114
|
} else {
|
|
72
115
|
ctx.status = 500;
|
|
@@ -77,33 +120,39 @@ const createRouteHandler = handler => {
|
|
|
77
120
|
}
|
|
78
121
|
} else {
|
|
79
122
|
const args = Object.values(input.params).concat(input);
|
|
80
|
-
const body =
|
|
81
|
-
if (typeof body !==
|
|
123
|
+
const body = yield handler(...args);
|
|
124
|
+
if (typeof body !== "undefined") {
|
|
82
125
|
ctx.body = body;
|
|
83
126
|
}
|
|
84
127
|
}
|
|
85
|
-
};
|
|
86
|
-
Object.defineProperties(
|
|
128
|
+
});
|
|
129
|
+
Object.defineProperties(
|
|
130
|
+
apiHandler,
|
|
131
|
+
Object.getOwnPropertyDescriptors(handler)
|
|
132
|
+
);
|
|
87
133
|
return apiHandler;
|
|
88
134
|
};
|
|
89
|
-
|
|
90
|
-
const
|
|
91
|
-
exports.isNormalMethod = isNormalMethod;
|
|
92
|
-
const getInputFromRequest = async ctx => {
|
|
135
|
+
const isNormalMethod = (httpMethod) => import_bff_core.httpMethods.includes(httpMethod);
|
|
136
|
+
const getInputFromRequest = (ctx) => __async(void 0, null, function* () {
|
|
93
137
|
const draft = {
|
|
94
138
|
params: ctx.params,
|
|
95
139
|
query: ctx.query,
|
|
96
140
|
headers: ctx.headers,
|
|
97
141
|
cookies: ctx.headers.cookie
|
|
98
142
|
};
|
|
99
|
-
if (
|
|
143
|
+
if (import_type_is.default.is(ctx.request.type, ["application/json"])) {
|
|
100
144
|
draft.data = ctx.request.body;
|
|
101
|
-
} else if (
|
|
145
|
+
} else if (import_type_is.default.is(ctx.request.type, ["multipart/form-data"])) {
|
|
102
146
|
draft.formData = ctx.request.files;
|
|
103
|
-
} else if (
|
|
147
|
+
} else if (import_type_is.default.is(ctx.request.type, ["application/x-www-form-urlencoded"])) {
|
|
104
148
|
draft.formUrlencoded = ctx.request.body;
|
|
105
149
|
} else {
|
|
106
150
|
draft.body = ctx.request.body;
|
|
107
151
|
}
|
|
108
152
|
return draft;
|
|
109
|
-
};
|
|
153
|
+
});
|
|
154
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
155
|
+
0 && (module.exports = {
|
|
156
|
+
createRouteHandler,
|
|
157
|
+
isNormalMethod
|
|
158
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
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 = function() {
|
|
5
|
+
return {
|
|
6
|
+
name: "@modern-js/plugin-koa",
|
|
7
|
+
setup: function(api) {
|
|
8
|
+
var bffExportsUtils;
|
|
9
|
+
var useAppContext = api.useAppContext;
|
|
10
|
+
var runtimeModulePath = path.resolve(__dirname, "../runtime");
|
|
11
|
+
return {
|
|
12
|
+
config: function config() {
|
|
13
|
+
var appContext = useAppContext();
|
|
14
|
+
var appDirectory = appContext.appDirectory;
|
|
15
|
+
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
|
|
16
|
+
var serverRuntimePath = bffExportsUtils.getPath();
|
|
17
|
+
var relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
|
|
18
|
+
if (process.env.NODE_ENV === "production") {
|
|
19
|
+
return {
|
|
20
|
+
source: {
|
|
21
|
+
alias: {
|
|
22
|
+
"@modern-js/runtime/server": relativeRuntimePath,
|
|
23
|
+
"@modern-js/runtime/koa": relativeRuntimePath
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
} else {
|
|
28
|
+
return {
|
|
29
|
+
source: {
|
|
30
|
+
alias: {
|
|
31
|
+
"@modern-js/runtime/server": serverRuntimePath,
|
|
32
|
+
"@modern-js/runtime/koa": serverRuntimePath
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
collectServerPlugins: function collectServerPlugins(param) {
|
|
39
|
+
var plugins = param.plugins;
|
|
40
|
+
plugins.push({
|
|
41
|
+
"@modern-js/plugin-koa": "@modern-js/plugin-koa/server"
|
|
42
|
+
});
|
|
43
|
+
return {
|
|
44
|
+
plugins: plugins
|
|
45
|
+
};
|
|
46
|
+
},
|
|
47
|
+
addRuntimeExports: function addRuntimeExports(input) {
|
|
48
|
+
var currentFile = bffExportsUtils.getPath();
|
|
49
|
+
var relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
50
|
+
var relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("koa"));
|
|
51
|
+
bffExportsUtils.addExport("const pluginRuntime = require('".concat(relativeRuntimeModulePath, "');\n const Koa = require('").concat(relativeFramePath, "')\n module.exports = {\n Koa: Koa,\n ...pluginRuntime\n }\n "));
|
|
52
|
+
return input;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
export { cli_default as default };
|
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
+
try {
|
|
3
|
+
var info = gen[key](arg);
|
|
4
|
+
var value = info.value;
|
|
5
|
+
} catch (error) {
|
|
6
|
+
reject(error);
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
if (info.done) {
|
|
10
|
+
resolve(value);
|
|
11
|
+
} else {
|
|
12
|
+
Promise.resolve(value).then(_next, _throw);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function _asyncToGenerator(fn) {
|
|
16
|
+
return function() {
|
|
17
|
+
var self = this, args = arguments;
|
|
18
|
+
return new Promise(function(resolve, reject) {
|
|
19
|
+
var gen = fn.apply(self, args);
|
|
20
|
+
function _next(value) {
|
|
21
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
+
}
|
|
23
|
+
function _throw(err) {
|
|
24
|
+
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
+
}
|
|
26
|
+
_next(undefined);
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
function _instanceof(left, right) {
|
|
31
|
+
if (right != null && typeof Symbol !== "undefined" && right[Symbol.hasInstance]) {
|
|
32
|
+
return !!right[Symbol.hasInstance](left);
|
|
33
|
+
} else {
|
|
34
|
+
return left instanceof right;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
var __generator = this && this.__generator || function(thisArg, body) {
|
|
38
|
+
var f, y, t, g, _ = {
|
|
39
|
+
label: 0,
|
|
40
|
+
sent: function() {
|
|
41
|
+
if (t[0] & 1) throw t[1];
|
|
42
|
+
return t[1];
|
|
43
|
+
},
|
|
44
|
+
trys: [],
|
|
45
|
+
ops: []
|
|
46
|
+
};
|
|
47
|
+
return(g = {
|
|
48
|
+
next: verb(0),
|
|
49
|
+
"throw": verb(1),
|
|
50
|
+
"return": verb(2)
|
|
51
|
+
}, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
52
|
+
return this;
|
|
53
|
+
}), g);
|
|
54
|
+
function verb(n) {
|
|
55
|
+
return function(v) {
|
|
56
|
+
return step([
|
|
57
|
+
n,
|
|
58
|
+
v
|
|
59
|
+
]);
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function step(op) {
|
|
63
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
64
|
+
while(_)try {
|
|
65
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
66
|
+
if (y = 0, t) op = [
|
|
67
|
+
op[0] & 2,
|
|
68
|
+
t.value
|
|
69
|
+
];
|
|
70
|
+
switch(op[0]){
|
|
71
|
+
case 0:
|
|
72
|
+
case 1:
|
|
73
|
+
t = op;
|
|
74
|
+
break;
|
|
75
|
+
case 4:
|
|
76
|
+
_.label++;
|
|
77
|
+
return {
|
|
78
|
+
value: op[1],
|
|
79
|
+
done: false
|
|
80
|
+
};
|
|
81
|
+
case 5:
|
|
82
|
+
_.label++;
|
|
83
|
+
y = op[1];
|
|
84
|
+
op = [
|
|
85
|
+
0
|
|
86
|
+
];
|
|
87
|
+
continue;
|
|
88
|
+
case 7:
|
|
89
|
+
op = _.ops.pop();
|
|
90
|
+
_.trys.pop();
|
|
91
|
+
continue;
|
|
92
|
+
default:
|
|
93
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
94
|
+
_ = 0;
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
98
|
+
_.label = op[1];
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
if (op[0] === 6 && _.label < t[1]) {
|
|
102
|
+
_.label = t[1];
|
|
103
|
+
t = op;
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
if (t && _.label < t[2]) {
|
|
107
|
+
_.label = t[2];
|
|
108
|
+
_.ops.push(op);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
if (t[2]) _.ops.pop();
|
|
112
|
+
_.trys.pop();
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
op = body.call(thisArg, _);
|
|
116
|
+
} catch (e) {
|
|
117
|
+
op = [
|
|
118
|
+
6,
|
|
119
|
+
e
|
|
120
|
+
];
|
|
121
|
+
y = 0;
|
|
122
|
+
} finally{
|
|
123
|
+
f = t = 0;
|
|
124
|
+
}
|
|
125
|
+
if (op[0] & 5) throw op[1];
|
|
126
|
+
return {
|
|
127
|
+
value: op[0] ? op[1] : void 0,
|
|
128
|
+
done: true
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
import * as path from "path";
|
|
133
|
+
import Koa from "koa";
|
|
134
|
+
import Router from "koa-router";
|
|
135
|
+
import koaBody from "koa-body";
|
|
136
|
+
import { fs, compatRequire } from "@modern-js/utils";
|
|
137
|
+
import { run } from "./context";
|
|
138
|
+
import registerRoutes from "./registerRoutes";
|
|
139
|
+
var findAppModule = function() {
|
|
140
|
+
var _ref = _asyncToGenerator(function(apiDir) {
|
|
141
|
+
var exts, paths, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, filename, err;
|
|
142
|
+
return __generator(this, function(_state) {
|
|
143
|
+
switch(_state.label){
|
|
144
|
+
case 0:
|
|
145
|
+
exts = [
|
|
146
|
+
".ts",
|
|
147
|
+
".js"
|
|
148
|
+
];
|
|
149
|
+
paths = exts.map(function(ext) {
|
|
150
|
+
return path.join(apiDir, "app".concat(ext));
|
|
151
|
+
});
|
|
152
|
+
_iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
153
|
+
_state.label = 1;
|
|
154
|
+
case 1:
|
|
155
|
+
_state.trys.push([
|
|
156
|
+
1,
|
|
157
|
+
6,
|
|
158
|
+
7,
|
|
159
|
+
8
|
|
160
|
+
]);
|
|
161
|
+
_iterator = paths[Symbol.iterator]();
|
|
162
|
+
_state.label = 2;
|
|
163
|
+
case 2:
|
|
164
|
+
if (!!(_iteratorNormalCompletion = (_step = _iterator.next()).done)) return [
|
|
165
|
+
3,
|
|
166
|
+
5
|
|
167
|
+
];
|
|
168
|
+
filename = _step.value;
|
|
169
|
+
return [
|
|
170
|
+
4,
|
|
171
|
+
fs.pathExists(filename)
|
|
172
|
+
];
|
|
173
|
+
case 3:
|
|
174
|
+
if (_state.sent()) {
|
|
175
|
+
delete require.cache[filename];
|
|
176
|
+
return [
|
|
177
|
+
2,
|
|
178
|
+
compatRequire(filename)
|
|
179
|
+
];
|
|
180
|
+
}
|
|
181
|
+
_state.label = 4;
|
|
182
|
+
case 4:
|
|
183
|
+
_iteratorNormalCompletion = true;
|
|
184
|
+
return [
|
|
185
|
+
3,
|
|
186
|
+
2
|
|
187
|
+
];
|
|
188
|
+
case 5:
|
|
189
|
+
return [
|
|
190
|
+
3,
|
|
191
|
+
8
|
|
192
|
+
];
|
|
193
|
+
case 6:
|
|
194
|
+
err = _state.sent();
|
|
195
|
+
_didIteratorError = true;
|
|
196
|
+
_iteratorError = err;
|
|
197
|
+
return [
|
|
198
|
+
3,
|
|
199
|
+
8
|
|
200
|
+
];
|
|
201
|
+
case 7:
|
|
202
|
+
try {
|
|
203
|
+
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
204
|
+
_iterator.return();
|
|
205
|
+
}
|
|
206
|
+
} finally{
|
|
207
|
+
if (_didIteratorError) {
|
|
208
|
+
throw _iteratorError;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return [
|
|
212
|
+
7
|
|
213
|
+
];
|
|
214
|
+
case 8:
|
|
215
|
+
return [
|
|
216
|
+
2,
|
|
217
|
+
null
|
|
218
|
+
];
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
return function findAppModule(apiDir) {
|
|
223
|
+
return _ref.apply(this, arguments);
|
|
224
|
+
};
|
|
225
|
+
}();
|
|
226
|
+
var initMiddlewares = function(middleware, app) {
|
|
227
|
+
middleware.forEach(function(middlewareItem) {
|
|
228
|
+
var middlewareFunc = typeof middlewareItem === "string" ? compatRequire(middlewareItem) : middlewareItem;
|
|
229
|
+
app.use(middlewareFunc);
|
|
230
|
+
});
|
|
231
|
+
};
|
|
232
|
+
var plugin_default = function() {
|
|
233
|
+
return {
|
|
234
|
+
name: "@modern-js/plugin-koa",
|
|
235
|
+
pre: [
|
|
236
|
+
"@modern-js/plugin-bff"
|
|
237
|
+
],
|
|
238
|
+
post: [
|
|
239
|
+
"@modern-js/plugin-server"
|
|
240
|
+
],
|
|
241
|
+
setup: function(api) {
|
|
242
|
+
return {
|
|
243
|
+
prepareApiServer: function prepareApiServer(param) {
|
|
244
|
+
var pwd = param.pwd, config = param.config;
|
|
245
|
+
return _asyncToGenerator(function() {
|
|
246
|
+
var app, router, apiDir, appContext, apiHandlerInfos, mode, middleware, middleware1;
|
|
247
|
+
return __generator(this, function(_state) {
|
|
248
|
+
switch(_state.label){
|
|
249
|
+
case 0:
|
|
250
|
+
router = new Router();
|
|
251
|
+
apiDir = path.join(pwd, "./api");
|
|
252
|
+
appContext = api.useAppContext();
|
|
253
|
+
apiHandlerInfos = appContext.apiHandlerInfos;
|
|
254
|
+
mode = appContext.apiMode;
|
|
255
|
+
if (!(mode === "framework")) return [
|
|
256
|
+
3,
|
|
257
|
+
2
|
|
258
|
+
];
|
|
259
|
+
return [
|
|
260
|
+
4,
|
|
261
|
+
findAppModule(apiDir)
|
|
262
|
+
];
|
|
263
|
+
case 1:
|
|
264
|
+
app = _state.sent();
|
|
265
|
+
if (!_instanceof(app, Koa)) {
|
|
266
|
+
app = new Koa();
|
|
267
|
+
app.use(koaBody({
|
|
268
|
+
multipart: true
|
|
269
|
+
}));
|
|
270
|
+
}
|
|
271
|
+
if (config) {
|
|
272
|
+
middleware = config.middleware;
|
|
273
|
+
initMiddlewares(middleware, app);
|
|
274
|
+
}
|
|
275
|
+
app.use(run);
|
|
276
|
+
registerRoutes(router, apiHandlerInfos);
|
|
277
|
+
return [
|
|
278
|
+
3,
|
|
279
|
+
3
|
|
280
|
+
];
|
|
281
|
+
case 2:
|
|
282
|
+
if (mode === "function") {
|
|
283
|
+
app = new Koa();
|
|
284
|
+
app.use(koaBody({
|
|
285
|
+
multipart: true
|
|
286
|
+
}));
|
|
287
|
+
if (config) {
|
|
288
|
+
middleware1 = config.middleware;
|
|
289
|
+
initMiddlewares(middleware1, app);
|
|
290
|
+
}
|
|
291
|
+
app.use(run);
|
|
292
|
+
registerRoutes(router, apiHandlerInfos);
|
|
293
|
+
} else {
|
|
294
|
+
throw new Error("mode must be function or framework");
|
|
295
|
+
}
|
|
296
|
+
_state.label = 3;
|
|
297
|
+
case 3:
|
|
298
|
+
app.use(router.routes());
|
|
299
|
+
return [
|
|
300
|
+
2,
|
|
301
|
+
function(req, res) {
|
|
302
|
+
return Promise.resolve(app.callback()(req, res));
|
|
303
|
+
}
|
|
304
|
+
];
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
})();
|
|
308
|
+
},
|
|
309
|
+
prepareWebServer: function prepareWebServer(param, next) {
|
|
310
|
+
var config = param.config;
|
|
311
|
+
var ref;
|
|
312
|
+
var userConfig = api.useConfigContext();
|
|
313
|
+
if (userConfig === null || userConfig === void 0 ? void 0 : (ref = userConfig.server) === null || ref === void 0 ? void 0 : ref.disableFrameworkExt) {
|
|
314
|
+
return next();
|
|
315
|
+
}
|
|
316
|
+
var app = new Koa();
|
|
317
|
+
app.use(function() {
|
|
318
|
+
var _ref = _asyncToGenerator(function(ctx, next2) {
|
|
319
|
+
return __generator(this, function(_state) {
|
|
320
|
+
switch(_state.label){
|
|
321
|
+
case 0:
|
|
322
|
+
return [
|
|
323
|
+
4,
|
|
324
|
+
next2()
|
|
325
|
+
];
|
|
326
|
+
case 1:
|
|
327
|
+
_state.sent();
|
|
328
|
+
if (!ctx.body) {
|
|
329
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
330
|
+
ctx.res.statusCode = 200;
|
|
331
|
+
}
|
|
332
|
+
ctx.respond = false;
|
|
333
|
+
}
|
|
334
|
+
return [
|
|
335
|
+
2
|
|
336
|
+
];
|
|
337
|
+
}
|
|
338
|
+
});
|
|
339
|
+
});
|
|
340
|
+
return function(ctx, next2) {
|
|
341
|
+
return _ref.apply(this, arguments);
|
|
342
|
+
};
|
|
343
|
+
}());
|
|
344
|
+
app.use(koaBody());
|
|
345
|
+
if (config) {
|
|
346
|
+
var middleware = config.middleware;
|
|
347
|
+
initMiddlewares(middleware, app);
|
|
348
|
+
}
|
|
349
|
+
return function(ctx) {
|
|
350
|
+
var _source = ctx.source, req = _source.req, res = _source.res;
|
|
351
|
+
app.on("error", function(err) {
|
|
352
|
+
if (err) {
|
|
353
|
+
throw err;
|
|
354
|
+
}
|
|
355
|
+
});
|
|
356
|
+
return Promise.resolve(app.callback()(req, res));
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
};
|
|
363
|
+
export { plugin_default as default };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { createRouteHandler } from "./utils";
|
|
2
|
+
var registerRoutes = function(router, handlerInfos) {
|
|
3
|
+
handlerInfos.forEach(function(param) {
|
|
4
|
+
var routePath = param.routePath, handler = param.handler, httpMethod = param.httpMethod;
|
|
5
|
+
var routeHandler = createRouteHandler(handler);
|
|
6
|
+
var method = httpMethod.toLowerCase();
|
|
7
|
+
router[method](routePath, routeHandler);
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var registerRoutes_default = registerRoutes;
|
|
11
|
+
export { registerRoutes_default as default };
|