@modern-js/plugin-koa 2.14.0 → 2.16.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.
- package/CHANGELOG.md +25 -0
- package/dist/cjs/cli/index.js +57 -50
- package/dist/cjs/context.js +13 -26
- package/dist/cjs/index.js +28 -37
- package/dist/cjs/plugin.js +158 -134
- package/dist/cjs/registerRoutes.js +10 -26
- package/dist/cjs/runtime.js +27 -28
- package/dist/cjs/utils.js +36 -54
- package/dist/esm/cli/index.js +47 -47
- package/dist/esm/index.js +1 -2
- package/dist/esm/plugin.js +374 -365
- package/dist/esm/registerRoutes.js +7 -8
- package/dist/esm/runtime.js +3 -4
- package/dist/esm/utils.js +391 -369
- package/dist/esm-node/cli/index.js +7 -17
- package/dist/esm-node/context.js +1 -4
- package/dist/esm-node/index.js +1 -4
- package/dist/esm-node/plugin.js +94 -92
- package/dist/esm-node/registerRoutes.js +1 -4
- package/dist/esm-node/runtime.js +2 -6
- package/dist/esm-node/utils.js +14 -23
- package/package.json +15 -11
package/dist/cjs/runtime.js
CHANGED
|
@@ -1,32 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
6
6
|
for (var name in all)
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var runtime_exports = {};
|
|
20
|
-
__export(runtime_exports, {
|
|
7
|
+
Object.defineProperty(target, name, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: all[name]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
_export(exports, {
|
|
21
13
|
hook: () => hook,
|
|
22
|
-
useContext: () =>
|
|
14
|
+
useContext: () => _context.useContext
|
|
23
15
|
});
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
16
|
+
const _context = require("./context");
|
|
17
|
+
_export_star(require("@modern-js/bff-core"), exports);
|
|
18
|
+
function _export_star(from, to) {
|
|
19
|
+
Object.keys(from).forEach(function(k) {
|
|
20
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
21
|
+
Object.defineProperty(to, k, {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
get: function() {
|
|
24
|
+
return from[k];
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
return from;
|
|
30
|
+
}
|
|
27
31
|
const hook = (attacher) => attacher;
|
|
28
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
-
0 && (module.exports = {
|
|
30
|
-
hook,
|
|
31
|
-
useContext
|
|
32
|
-
});
|
package/dist/cjs/utils.js
CHANGED
|
@@ -1,62 +1,46 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
function _export(target, all) {
|
|
8
6
|
for (var name in all)
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
21
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
22
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
23
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
24
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
25
|
-
mod
|
|
26
|
-
));
|
|
27
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
28
|
-
var utils_exports = {};
|
|
29
|
-
__export(utils_exports, {
|
|
7
|
+
Object.defineProperty(target, name, {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: all[name]
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
_export(exports, {
|
|
30
13
|
createRouteHandler: () => createRouteHandler,
|
|
31
14
|
isNormalMethod: () => isNormalMethod
|
|
32
15
|
});
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
16
|
+
const _bffcore = require("@modern-js/bff-core");
|
|
17
|
+
const _bffruntime = require("@modern-js/bff-runtime");
|
|
18
|
+
const _typeis = /* @__PURE__ */ _interop_require_default(require("type-is"));
|
|
19
|
+
function _interop_require_default(obj) {
|
|
20
|
+
return obj && obj.__esModule ? obj : {
|
|
21
|
+
default: obj
|
|
22
|
+
};
|
|
23
|
+
}
|
|
37
24
|
const handleResponseMeta = (ctx, handler) => {
|
|
38
|
-
const responseMeta = Reflect.getMetadata(
|
|
39
|
-
import_bff_core.HttpMetadata.Response,
|
|
40
|
-
handler
|
|
41
|
-
);
|
|
25
|
+
const responseMeta = Reflect.getMetadata(_bffcore.HttpMetadata.Response, handler);
|
|
42
26
|
if (Array.isArray(responseMeta)) {
|
|
43
27
|
for (const meta of responseMeta) {
|
|
44
28
|
const metaType = meta.type;
|
|
45
29
|
const metaValue = meta.value;
|
|
46
30
|
switch (metaType) {
|
|
47
|
-
case
|
|
31
|
+
case _bffcore.ResponseMetaType.Headers:
|
|
48
32
|
for (const [key, value] of Object.entries(metaValue)) {
|
|
49
33
|
if (typeof value === "string") {
|
|
50
34
|
ctx.append(key, value);
|
|
51
35
|
}
|
|
52
36
|
}
|
|
53
37
|
break;
|
|
54
|
-
case
|
|
38
|
+
case _bffcore.ResponseMetaType.Redirect:
|
|
55
39
|
if (typeof metaValue === "string") {
|
|
56
40
|
ctx.redirect(metaValue);
|
|
57
41
|
}
|
|
58
42
|
break;
|
|
59
|
-
case
|
|
43
|
+
case _bffcore.ResponseMetaType.StatusCode:
|
|
60
44
|
if (typeof metaValue === "number") {
|
|
61
45
|
ctx.status = metaValue;
|
|
62
46
|
}
|
|
@@ -70,7 +54,7 @@ const handleResponseMeta = (ctx, handler) => {
|
|
|
70
54
|
const createRouteHandler = (handler) => {
|
|
71
55
|
const apiHandler = async (ctx) => {
|
|
72
56
|
const input = await getInputFromRequest(ctx);
|
|
73
|
-
if ((0,
|
|
57
|
+
if ((0, _bffcore.isWithMetaHandler)(handler)) {
|
|
74
58
|
try {
|
|
75
59
|
handleResponseMeta(ctx, handler);
|
|
76
60
|
const body = await handler(input);
|
|
@@ -90,7 +74,7 @@ const createRouteHandler = (handler) => {
|
|
|
90
74
|
};
|
|
91
75
|
}
|
|
92
76
|
}
|
|
93
|
-
} else if ((0,
|
|
77
|
+
} else if ((0, _bffruntime.isSchemaHandler)(handler)) {
|
|
94
78
|
const result = await handler(input);
|
|
95
79
|
if (result.type !== "HandleSuccess") {
|
|
96
80
|
if (result.type === "InputValidationError") {
|
|
@@ -110,13 +94,10 @@ const createRouteHandler = (handler) => {
|
|
|
110
94
|
}
|
|
111
95
|
}
|
|
112
96
|
};
|
|
113
|
-
Object.defineProperties(
|
|
114
|
-
apiHandler,
|
|
115
|
-
Object.getOwnPropertyDescriptors(handler)
|
|
116
|
-
);
|
|
97
|
+
Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
|
|
117
98
|
return apiHandler;
|
|
118
99
|
};
|
|
119
|
-
const isNormalMethod = (httpMethod) =>
|
|
100
|
+
const isNormalMethod = (httpMethod) => _bffcore.httpMethods.includes(httpMethod);
|
|
120
101
|
const getInputFromRequest = async (ctx) => {
|
|
121
102
|
const draft = {
|
|
122
103
|
params: ctx.params,
|
|
@@ -124,19 +105,20 @@ const getInputFromRequest = async (ctx) => {
|
|
|
124
105
|
headers: ctx.headers,
|
|
125
106
|
cookies: ctx.headers.cookie
|
|
126
107
|
};
|
|
127
|
-
if (
|
|
108
|
+
if (_typeis.default.is(ctx.request.type, [
|
|
109
|
+
"application/json"
|
|
110
|
+
])) {
|
|
128
111
|
draft.data = ctx.request.body;
|
|
129
|
-
} else if (
|
|
112
|
+
} else if (_typeis.default.is(ctx.request.type, [
|
|
113
|
+
"multipart/form-data"
|
|
114
|
+
])) {
|
|
130
115
|
draft.formData = ctx.request.files;
|
|
131
|
-
} else if (
|
|
116
|
+
} else if (_typeis.default.is(ctx.request.type, [
|
|
117
|
+
"application/x-www-form-urlencoded"
|
|
118
|
+
])) {
|
|
132
119
|
draft.formUrlencoded = ctx.request.body;
|
|
133
120
|
} else {
|
|
134
121
|
draft.body = ctx.request.body;
|
|
135
122
|
}
|
|
136
123
|
return draft;
|
|
137
124
|
};
|
|
138
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
139
|
-
0 && (module.exports = {
|
|
140
|
-
createRouteHandler,
|
|
141
|
-
isNormalMethod
|
|
142
|
-
});
|
package/dist/esm/cli/index.js
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
1
|
import * as path from "path";
|
|
2
2
|
import { createRuntimeExportsUtils } from "@modern-js/utils";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
};
|
|
3
|
+
export default function() {
|
|
4
|
+
return {
|
|
5
|
+
name: "@modern-js/plugin-koa",
|
|
6
|
+
setup: function(api) {
|
|
7
|
+
var bffExportsUtils;
|
|
8
|
+
var useAppContext = api.useAppContext;
|
|
9
|
+
var runtimeModulePath = path.resolve(__dirname, "../runtime");
|
|
10
|
+
return {
|
|
11
|
+
config: function config() {
|
|
12
|
+
var appContext = useAppContext();
|
|
13
|
+
bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
|
|
14
|
+
var runtimePath = "@modern-js/plugin-koa/runtime";
|
|
15
|
+
var alias = process.env.NODE_ENV === "production" ? runtimePath : require.resolve(runtimePath);
|
|
16
|
+
return {
|
|
17
|
+
output: {
|
|
18
|
+
externals: {
|
|
19
|
+
"@modern-js/runtime/koa": runtimePath
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
source: {
|
|
23
|
+
alias: {
|
|
24
|
+
"@modern-js/runtime/server": alias,
|
|
25
|
+
"@modern-js/runtime/koa": alias
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
collectServerPlugins: function collectServerPlugins(param) {
|
|
31
|
+
var plugins = param.plugins;
|
|
32
|
+
plugins.push({
|
|
33
|
+
"@modern-js/plugin-koa": "@modern-js/plugin-koa/server"
|
|
34
|
+
});
|
|
35
|
+
return {
|
|
36
|
+
plugins
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
addRuntimeExports: function addRuntimeExports(input) {
|
|
40
|
+
var currentFile = bffExportsUtils.getPath();
|
|
41
|
+
var relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
|
|
42
|
+
var relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("koa"));
|
|
43
|
+
bffExportsUtils.addExport("const pluginRuntime = require('".concat(relativeRuntimeModulePath, "');\n const Koa = require('").concat(relativeFramePath, "')\n module.exports = {\n Koa: Koa,\n ...pluginRuntime\n }\n "));
|
|
44
|
+
return input;
|
|
47
45
|
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
;
|
package/dist/esm/index.js
CHANGED