@modern-js/plugin-bff 2.67.1 → 2.67.3
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/dist/cjs/cli.js +12 -1
- package/dist/cjs/runtime/hono/adapter.js +120 -0
- package/dist/cjs/runtime/hono/index.js +33 -0
- package/dist/cjs/runtime/hono/operators.js +71 -0
- package/dist/cjs/server.js +10 -1
- package/dist/cjs/utils/createHonoRoutes.js +148 -0
- package/dist/esm/cli.js +11 -1
- package/dist/esm/runtime/hono/adapter.js +184 -0
- package/dist/esm/runtime/hono/index.js +6 -0
- package/dist/esm/runtime/hono/operators.js +79 -0
- package/dist/esm/server.js +22 -3
- package/dist/esm/utils/createHonoRoutes.js +330 -0
- package/dist/esm-node/cli.js +12 -1
- package/dist/esm-node/runtime/hono/adapter.js +86 -0
- package/dist/esm-node/runtime/hono/index.js +6 -0
- package/dist/esm-node/runtime/hono/operators.js +46 -0
- package/dist/esm-node/server.js +10 -1
- package/dist/esm-node/utils/createHonoRoutes.js +114 -0
- package/dist/types/runtime/hono/adapter.d.ts +18 -0
- package/dist/types/runtime/hono/index.d.ts +3 -0
- package/dist/types/runtime/hono/operators.d.ts +10 -0
- package/dist/types/utils/createHonoRoutes.d.ts +10 -0
- package/package.json +25 -14
package/dist/cjs/cli.js
CHANGED
|
@@ -42,6 +42,7 @@ var import_runtimeGenerator = __toESM(require("./utils/runtimeGenerator"));
|
|
|
42
42
|
const DEFAULT_API_PREFIX = "/api";
|
|
43
43
|
const TS_CONFIG_FILENAME = "tsconfig.json";
|
|
44
44
|
const RUNTIME_CREATE_REQUEST = "@modern-js/plugin-bff/runtime/create-request";
|
|
45
|
+
const RUNTIME_HONO = "@modern-js/plugin-bff/hono";
|
|
45
46
|
const bffPlugin = () => ({
|
|
46
47
|
name: "@modern-js/plugin-bff",
|
|
47
48
|
setup: (api) => {
|
|
@@ -130,8 +131,15 @@ const bffPlugin = () => ({
|
|
|
130
131
|
await generator();
|
|
131
132
|
}
|
|
132
133
|
};
|
|
134
|
+
const isHono = () => {
|
|
135
|
+
const { bffRuntimeFramework } = api.useAppContext();
|
|
136
|
+
return bffRuntimeFramework === "hono";
|
|
137
|
+
};
|
|
133
138
|
return {
|
|
134
139
|
config() {
|
|
140
|
+
const honoRuntimePath = isHono() ? {
|
|
141
|
+
[RUNTIME_HONO]: RUNTIME_HONO
|
|
142
|
+
} : void 0;
|
|
135
143
|
return {
|
|
136
144
|
tools: {
|
|
137
145
|
bundlerChain: (chain, { CHAIN_ID, isServer }) => {
|
|
@@ -174,6 +182,9 @@ const bffPlugin = () => ({
|
|
|
174
182
|
`./${import_utils.API_DIR}`,
|
|
175
183
|
/create-request/
|
|
176
184
|
]
|
|
185
|
+
},
|
|
186
|
+
output: {
|
|
187
|
+
externals: honoRuntimePath
|
|
177
188
|
}
|
|
178
189
|
};
|
|
179
190
|
},
|
|
@@ -194,7 +205,7 @@ const bffPlugin = () => ({
|
|
|
194
205
|
isSPA: false,
|
|
195
206
|
isSSR: false
|
|
196
207
|
}));
|
|
197
|
-
if (bff === null || bff === void 0 ? void 0 : bff.enableHandleWeb) {
|
|
208
|
+
if (!isHono() && (bff === null || bff === void 0 ? void 0 : bff.enableHandleWeb)) {
|
|
198
209
|
return {
|
|
199
210
|
routes: routes.map((route) => {
|
|
200
211
|
return {
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var adapter_exports = {};
|
|
30
|
+
__export(adapter_exports, {
|
|
31
|
+
HonoAdapter: () => HonoAdapter
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(adapter_exports);
|
|
34
|
+
var import_server_core = require("@modern-js/server-core");
|
|
35
|
+
var import_utils = require("@modern-js/utils");
|
|
36
|
+
var import_createHonoRoutes = __toESM(require("../../utils/createHonoRoutes"));
|
|
37
|
+
const before = [
|
|
38
|
+
"custom-server-hook",
|
|
39
|
+
"custom-server-middleware",
|
|
40
|
+
"render"
|
|
41
|
+
];
|
|
42
|
+
class HonoAdapter {
|
|
43
|
+
wrapInArray(handler) {
|
|
44
|
+
if (Array.isArray(handler)) {
|
|
45
|
+
return handler;
|
|
46
|
+
} else {
|
|
47
|
+
return [
|
|
48
|
+
handler
|
|
49
|
+
];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
constructor(api) {
|
|
53
|
+
this.apiMiddleware = [];
|
|
54
|
+
this.apiServer = null;
|
|
55
|
+
this.isHono = true;
|
|
56
|
+
this.setHandlers = async () => {
|
|
57
|
+
if (!this.isHono) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const { apiHandlerInfos } = this.api.useAppContext();
|
|
61
|
+
const honoHandlers = (0, import_createHonoRoutes.default)(apiHandlerInfos);
|
|
62
|
+
this.apiMiddleware = honoHandlers.map(({ path, method, handler }) => ({
|
|
63
|
+
name: "hono-bff-api",
|
|
64
|
+
path,
|
|
65
|
+
method,
|
|
66
|
+
handler,
|
|
67
|
+
order: "post",
|
|
68
|
+
before
|
|
69
|
+
}));
|
|
70
|
+
};
|
|
71
|
+
this.registerApiRoutes = async () => {
|
|
72
|
+
if (!this.isHono) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
this.apiServer = new import_server_core.Hono();
|
|
76
|
+
this.apiMiddleware.forEach(({ path = "*", method = "all", handler }) => {
|
|
77
|
+
var _this_apiServer;
|
|
78
|
+
const handlers = this.wrapInArray(handler);
|
|
79
|
+
(_this_apiServer = this.apiServer) === null || _this_apiServer === void 0 ? void 0 : _this_apiServer[method](path, ...handlers);
|
|
80
|
+
});
|
|
81
|
+
};
|
|
82
|
+
this.registerMiddleware = async (options) => {
|
|
83
|
+
const { prefix } = options;
|
|
84
|
+
const { bffRuntimeFramework } = this.api.useAppContext();
|
|
85
|
+
if (bffRuntimeFramework !== "hono") {
|
|
86
|
+
this.isHono = false;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const { middlewares: globalMiddlewares } = this.api.useAppContext();
|
|
90
|
+
await this.setHandlers();
|
|
91
|
+
if ((0, import_utils.isProd)()) {
|
|
92
|
+
globalMiddlewares.push(...this.apiMiddleware);
|
|
93
|
+
} else {
|
|
94
|
+
await this.registerApiRoutes();
|
|
95
|
+
const dynamicApiMiddleware = {
|
|
96
|
+
name: "dynamic-bff-handler",
|
|
97
|
+
path: `${prefix}/*`,
|
|
98
|
+
method: "all",
|
|
99
|
+
order: "post",
|
|
100
|
+
before,
|
|
101
|
+
handler: async (c, next) => {
|
|
102
|
+
if (this.apiServer) {
|
|
103
|
+
const response = await this.apiServer.fetch(c.req, c.env);
|
|
104
|
+
if (response.status !== 404) {
|
|
105
|
+
return new Response(response.body, response);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
await next();
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
globalMiddlewares.push(dynamicApiMiddleware);
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
this.api = api;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
118
|
+
0 && (module.exports = {
|
|
119
|
+
HonoAdapter
|
|
120
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
|
+
var hono_exports = {};
|
|
21
|
+
__export(hono_exports, {
|
|
22
|
+
useHonoContext: () => import_server_core.useHonoContext
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(hono_exports);
|
|
25
|
+
__reExport(hono_exports, require("@modern-js/bff-core"), module.exports);
|
|
26
|
+
var import_server_core = require("@modern-js/server-core");
|
|
27
|
+
__reExport(hono_exports, require("./operators"), module.exports);
|
|
28
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
+
0 && (module.exports = {
|
|
30
|
+
useHonoContext,
|
|
31
|
+
...require("@modern-js/bff-core"),
|
|
32
|
+
...require("./operators")
|
|
33
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var operators_exports = {};
|
|
20
|
+
__export(operators_exports, {
|
|
21
|
+
Middleware: () => Middleware,
|
|
22
|
+
Pipe: () => Pipe
|
|
23
|
+
});
|
|
24
|
+
module.exports = __toCommonJS(operators_exports);
|
|
25
|
+
var import_server_core = require("@modern-js/server-core");
|
|
26
|
+
const Pipe = (func) => {
|
|
27
|
+
return {
|
|
28
|
+
name: "pipe",
|
|
29
|
+
async execute(executeHelper, next) {
|
|
30
|
+
const { inputs } = executeHelper;
|
|
31
|
+
const ctx = (0, import_server_core.useHonoContext)();
|
|
32
|
+
const { res } = ctx;
|
|
33
|
+
if (typeof func === "function") {
|
|
34
|
+
let isPiped = true;
|
|
35
|
+
const end = (value) => {
|
|
36
|
+
isPiped = false;
|
|
37
|
+
if (typeof value === "function") {
|
|
38
|
+
value(res);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
const output = await func(inputs, end);
|
|
44
|
+
if (!isPiped) {
|
|
45
|
+
if (output) {
|
|
46
|
+
return executeHelper.result = output;
|
|
47
|
+
} else {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
executeHelper.inputs = output;
|
|
52
|
+
await next();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
const Middleware = (middleware) => {
|
|
58
|
+
return {
|
|
59
|
+
name: "middleware",
|
|
60
|
+
metadata(helper) {
|
|
61
|
+
const middlewares = helper.getMetadata("pipe") || [];
|
|
62
|
+
middlewares.push(middleware);
|
|
63
|
+
helper.setMetadata("middleware", middlewares);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
68
|
+
0 && (module.exports = {
|
|
69
|
+
Middleware,
|
|
70
|
+
Pipe
|
|
71
|
+
});
|
package/dist/cjs/server.js
CHANGED
|
@@ -34,7 +34,9 @@ module.exports = __toCommonJS(server_exports);
|
|
|
34
34
|
var import_path = __toESM(require("path"));
|
|
35
35
|
var import_bff_core = require("@modern-js/bff-core");
|
|
36
36
|
var import_utils = require("@modern-js/utils");
|
|
37
|
+
var import_utils2 = require("@modern-js/utils");
|
|
37
38
|
var import_constants = require("./constants");
|
|
39
|
+
var import_adapter = require("./runtime/hono/adapter");
|
|
38
40
|
class Storage {
|
|
39
41
|
reset() {
|
|
40
42
|
this.middlewares = [];
|
|
@@ -55,6 +57,7 @@ var server_default = () => ({
|
|
|
55
57
|
const transformAPI = createTransformAPI(storage);
|
|
56
58
|
let apiAppPath = "";
|
|
57
59
|
let apiRouter;
|
|
60
|
+
const honoAdapter = new import_adapter.HonoAdapter(api);
|
|
58
61
|
return {
|
|
59
62
|
async prepare() {
|
|
60
63
|
var _config_bff, _config_bff1, _config_bff2;
|
|
@@ -96,7 +99,7 @@ var server_default = () => ({
|
|
|
96
99
|
onLast: () => null
|
|
97
100
|
});
|
|
98
101
|
}
|
|
99
|
-
if (handler) {
|
|
102
|
+
if (handler && (0, import_utils2.isFunction)(handler)) {
|
|
100
103
|
globalMiddlewares.push({
|
|
101
104
|
name: "bind-bff",
|
|
102
105
|
handler: (c, next) => {
|
|
@@ -114,6 +117,10 @@ var server_default = () => ({
|
|
|
114
117
|
]
|
|
115
118
|
});
|
|
116
119
|
}
|
|
120
|
+
honoAdapter.registerMiddleware({
|
|
121
|
+
prefix,
|
|
122
|
+
enableHandleWeb
|
|
123
|
+
});
|
|
117
124
|
},
|
|
118
125
|
async reset({ event }) {
|
|
119
126
|
storage.reset();
|
|
@@ -134,6 +141,8 @@ var server_default = () => ({
|
|
|
134
141
|
...appContext2,
|
|
135
142
|
apiHandlerInfos
|
|
136
143
|
});
|
|
144
|
+
await honoAdapter.setHandlers();
|
|
145
|
+
await honoAdapter.registerApiRoutes();
|
|
137
146
|
}
|
|
138
147
|
},
|
|
139
148
|
async prepareApiServer(props, next) {
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var createHonoRoutes_exports = {};
|
|
30
|
+
__export(createHonoRoutes_exports, {
|
|
31
|
+
createHonoHandler: () => createHonoHandler,
|
|
32
|
+
default: () => createHonoRoutes_default
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(createHonoRoutes_exports);
|
|
35
|
+
var import_bff_core = require("@modern-js/bff-core");
|
|
36
|
+
var import_type_is = __toESM(require("type-is"));
|
|
37
|
+
const createHonoRoutes = (handlerInfos) => {
|
|
38
|
+
return handlerInfos.map(({ routePath, handler, httpMethod }) => {
|
|
39
|
+
const routeMiddlwares = Reflect.getMetadata("middleware", handler) || [];
|
|
40
|
+
const honoHandler = createHonoHandler(handler);
|
|
41
|
+
return {
|
|
42
|
+
method: httpMethod.toLowerCase(),
|
|
43
|
+
path: routePath,
|
|
44
|
+
handler: routeMiddlwares.length > 0 ? [
|
|
45
|
+
...routeMiddlwares,
|
|
46
|
+
honoHandler
|
|
47
|
+
] : honoHandler
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
};
|
|
51
|
+
const handleResponseMeta = (c, handler) => {
|
|
52
|
+
const responseMeta = Reflect.getMetadata(import_bff_core.HttpMetadata.Response, handler);
|
|
53
|
+
if (Array.isArray(responseMeta)) {
|
|
54
|
+
for (const meta of responseMeta) {
|
|
55
|
+
switch (meta.type) {
|
|
56
|
+
case import_bff_core.ResponseMetaType.Headers:
|
|
57
|
+
for (const [key, value] of Object.entries(meta.value)) {
|
|
58
|
+
c.header(key, value);
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
case import_bff_core.ResponseMetaType.Redirect:
|
|
62
|
+
return c.redirect(meta.value);
|
|
63
|
+
case import_bff_core.ResponseMetaType.StatusCode:
|
|
64
|
+
c.status(meta.value);
|
|
65
|
+
break;
|
|
66
|
+
default:
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return null;
|
|
72
|
+
};
|
|
73
|
+
const createHonoHandler = (handler) => {
|
|
74
|
+
return async (c, next) => {
|
|
75
|
+
try {
|
|
76
|
+
const input = await getHonoInput(c);
|
|
77
|
+
if ((0, import_bff_core.isWithMetaHandler)(handler)) {
|
|
78
|
+
try {
|
|
79
|
+
const response = handleResponseMeta(c, handler);
|
|
80
|
+
if (response) {
|
|
81
|
+
return response;
|
|
82
|
+
}
|
|
83
|
+
if (c.finalized)
|
|
84
|
+
return;
|
|
85
|
+
const result = await handler(input);
|
|
86
|
+
return result && typeof result === "object" ? c.json(result) : c.body(result);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (error instanceof import_bff_core.ValidationError) {
|
|
89
|
+
c.status(error.status);
|
|
90
|
+
return c.json({
|
|
91
|
+
message: error.message
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
throw error;
|
|
95
|
+
}
|
|
96
|
+
} else {
|
|
97
|
+
const args = Object.values(input.params).concat(input);
|
|
98
|
+
try {
|
|
99
|
+
const body = await handler(...args);
|
|
100
|
+
if (c.finalized) {
|
|
101
|
+
return await Promise.resolve();
|
|
102
|
+
}
|
|
103
|
+
if (typeof body !== "undefined") {
|
|
104
|
+
return c.json(body);
|
|
105
|
+
}
|
|
106
|
+
} catch {
|
|
107
|
+
return next();
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
} catch (error) {
|
|
111
|
+
next();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
};
|
|
115
|
+
const getHonoInput = async (c) => {
|
|
116
|
+
const draft = {
|
|
117
|
+
params: c.req.param(),
|
|
118
|
+
query: c.req.query(),
|
|
119
|
+
headers: c.req.header(),
|
|
120
|
+
cookies: c.req.header("cookie")
|
|
121
|
+
};
|
|
122
|
+
try {
|
|
123
|
+
const contentType = c.req.header("content-type") || "";
|
|
124
|
+
if (import_type_is.default.is(contentType, [
|
|
125
|
+
"application/json"
|
|
126
|
+
])) {
|
|
127
|
+
draft.data = await c.req.json();
|
|
128
|
+
} else if (import_type_is.default.is(contentType, [
|
|
129
|
+
"multipart/form-data"
|
|
130
|
+
])) {
|
|
131
|
+
draft.formData = await c.req.parseBody();
|
|
132
|
+
} else if (import_type_is.default.is(contentType, [
|
|
133
|
+
"application/x-www-form-urlencoded"
|
|
134
|
+
])) {
|
|
135
|
+
draft.formUrlencoded = await c.req.parseBody();
|
|
136
|
+
} else {
|
|
137
|
+
draft.body = await c.req.json();
|
|
138
|
+
}
|
|
139
|
+
} catch (error) {
|
|
140
|
+
draft.body = null;
|
|
141
|
+
}
|
|
142
|
+
return draft;
|
|
143
|
+
};
|
|
144
|
+
var createHonoRoutes_default = createHonoRoutes;
|
|
145
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
146
|
+
0 && (module.exports = {
|
|
147
|
+
createHonoHandler
|
|
148
|
+
});
|
package/dist/esm/cli.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
|
|
2
3
|
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
|
|
3
4
|
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
|
|
4
5
|
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
@@ -13,6 +14,7 @@ import runtimeGenerator from "./utils/runtimeGenerator";
|
|
|
13
14
|
var DEFAULT_API_PREFIX = "/api";
|
|
14
15
|
var TS_CONFIG_FILENAME = "tsconfig.json";
|
|
15
16
|
var RUNTIME_CREATE_REQUEST = "@modern-js/plugin-bff/runtime/create-request";
|
|
17
|
+
var RUNTIME_HONO = "@modern-js/plugin-bff/hono";
|
|
16
18
|
var bffPlugin = function() {
|
|
17
19
|
return {
|
|
18
20
|
name: "@modern-js/plugin-bff",
|
|
@@ -200,8 +202,13 @@ var bffPlugin = function() {
|
|
|
200
202
|
return _ref.apply(this, arguments);
|
|
201
203
|
};
|
|
202
204
|
}();
|
|
205
|
+
var isHono = function() {
|
|
206
|
+
var bffRuntimeFramework = api.useAppContext().bffRuntimeFramework;
|
|
207
|
+
return bffRuntimeFramework === "hono";
|
|
208
|
+
};
|
|
203
209
|
return {
|
|
204
210
|
config: function config() {
|
|
211
|
+
var honoRuntimePath = isHono() ? _define_property({}, RUNTIME_HONO, RUNTIME_HONO) : void 0;
|
|
205
212
|
return {
|
|
206
213
|
tools: {
|
|
207
214
|
bundlerChain: function(chain, param) {
|
|
@@ -245,6 +252,9 @@ var bffPlugin = function() {
|
|
|
245
252
|
"./".concat(API_DIR),
|
|
246
253
|
/create-request/
|
|
247
254
|
]
|
|
255
|
+
},
|
|
256
|
+
output: {
|
|
257
|
+
externals: honoRuntimePath
|
|
248
258
|
}
|
|
249
259
|
};
|
|
250
260
|
},
|
|
@@ -269,7 +279,7 @@ var bffPlugin = function() {
|
|
|
269
279
|
isSSR: false
|
|
270
280
|
};
|
|
271
281
|
});
|
|
272
|
-
if (bff === null || bff === void 0 ? void 0 : bff.enableHandleWeb) {
|
|
282
|
+
if (!isHono() && (bff === null || bff === void 0 ? void 0 : bff.enableHandleWeb)) {
|
|
273
283
|
return {
|
|
274
284
|
routes: routes.map(function(route) {
|
|
275
285
|
return _object_spread_props(_object_spread({}, route), {
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { _ as _async_to_generator } from "@swc/helpers/_/_async_to_generator";
|
|
2
|
+
import { _ as _class_call_check } from "@swc/helpers/_/_class_call_check";
|
|
3
|
+
import { _ as _to_consumable_array } from "@swc/helpers/_/_to_consumable_array";
|
|
4
|
+
import { _ as _ts_generator } from "@swc/helpers/_/_ts_generator";
|
|
5
|
+
import { Hono } from "@modern-js/server-core";
|
|
6
|
+
import { isProd } from "@modern-js/utils";
|
|
7
|
+
import createHonoRoutes from "../../utils/createHonoRoutes";
|
|
8
|
+
var before = [
|
|
9
|
+
"custom-server-hook",
|
|
10
|
+
"custom-server-middleware",
|
|
11
|
+
"render"
|
|
12
|
+
];
|
|
13
|
+
var HonoAdapter = /* @__PURE__ */ function() {
|
|
14
|
+
"use strict";
|
|
15
|
+
function HonoAdapter2(api) {
|
|
16
|
+
_class_call_check(this, HonoAdapter2);
|
|
17
|
+
var _this = this;
|
|
18
|
+
this.apiMiddleware = [];
|
|
19
|
+
this.apiServer = null;
|
|
20
|
+
this.isHono = true;
|
|
21
|
+
this.setHandlers = /* @__PURE__ */ _async_to_generator(function() {
|
|
22
|
+
var apiHandlerInfos, honoHandlers;
|
|
23
|
+
return _ts_generator(this, function(_state) {
|
|
24
|
+
if (!_this.isHono) {
|
|
25
|
+
return [
|
|
26
|
+
2
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
apiHandlerInfos = _this.api.useAppContext().apiHandlerInfos;
|
|
30
|
+
honoHandlers = createHonoRoutes(apiHandlerInfos);
|
|
31
|
+
_this.apiMiddleware = honoHandlers.map(function(param) {
|
|
32
|
+
var path = param.path, method = param.method, handler = param.handler;
|
|
33
|
+
return {
|
|
34
|
+
name: "hono-bff-api",
|
|
35
|
+
path,
|
|
36
|
+
method,
|
|
37
|
+
handler,
|
|
38
|
+
order: "post",
|
|
39
|
+
before
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
return [
|
|
43
|
+
2
|
|
44
|
+
];
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
this.registerApiRoutes = /* @__PURE__ */ _async_to_generator(function() {
|
|
48
|
+
return _ts_generator(this, function(_state) {
|
|
49
|
+
if (!_this.isHono) {
|
|
50
|
+
return [
|
|
51
|
+
2
|
|
52
|
+
];
|
|
53
|
+
}
|
|
54
|
+
_this.apiServer = new Hono();
|
|
55
|
+
_this.apiMiddleware.forEach(function(param) {
|
|
56
|
+
var _param_path = param.path, path = _param_path === void 0 ? "*" : _param_path, _param_method = param.method, method = _param_method === void 0 ? "all" : _param_method, handler = param.handler;
|
|
57
|
+
var _this_apiServer;
|
|
58
|
+
var _this_apiServer1;
|
|
59
|
+
var handlers = _this.wrapInArray(handler);
|
|
60
|
+
(_this_apiServer1 = _this.apiServer) === null || _this_apiServer1 === void 0 ? void 0 : (_this_apiServer = _this_apiServer1)[method].apply(_this_apiServer, [
|
|
61
|
+
path
|
|
62
|
+
].concat(_to_consumable_array(handlers)));
|
|
63
|
+
});
|
|
64
|
+
return [
|
|
65
|
+
2
|
|
66
|
+
];
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
this.registerMiddleware = function() {
|
|
70
|
+
var _ref = _async_to_generator(function(options) {
|
|
71
|
+
var prefix, bffRuntimeFramework, _this_api_useAppContext, globalMiddlewares, _globalMiddlewares, dynamicApiMiddleware;
|
|
72
|
+
return _ts_generator(this, function(_state) {
|
|
73
|
+
switch (_state.label) {
|
|
74
|
+
case 0:
|
|
75
|
+
prefix = options.prefix;
|
|
76
|
+
bffRuntimeFramework = _this.api.useAppContext().bffRuntimeFramework;
|
|
77
|
+
if (bffRuntimeFramework !== "hono") {
|
|
78
|
+
_this.isHono = false;
|
|
79
|
+
return [
|
|
80
|
+
2
|
|
81
|
+
];
|
|
82
|
+
}
|
|
83
|
+
_this_api_useAppContext = _this.api.useAppContext(), globalMiddlewares = _this_api_useAppContext.middlewares;
|
|
84
|
+
return [
|
|
85
|
+
4,
|
|
86
|
+
_this.setHandlers()
|
|
87
|
+
];
|
|
88
|
+
case 1:
|
|
89
|
+
_state.sent();
|
|
90
|
+
if (!isProd())
|
|
91
|
+
return [
|
|
92
|
+
3,
|
|
93
|
+
2
|
|
94
|
+
];
|
|
95
|
+
(_globalMiddlewares = globalMiddlewares).push.apply(_globalMiddlewares, _to_consumable_array(_this.apiMiddleware));
|
|
96
|
+
return [
|
|
97
|
+
3,
|
|
98
|
+
4
|
|
99
|
+
];
|
|
100
|
+
case 2:
|
|
101
|
+
return [
|
|
102
|
+
4,
|
|
103
|
+
_this.registerApiRoutes()
|
|
104
|
+
];
|
|
105
|
+
case 3:
|
|
106
|
+
_state.sent();
|
|
107
|
+
dynamicApiMiddleware = {
|
|
108
|
+
name: "dynamic-bff-handler",
|
|
109
|
+
path: "".concat(prefix, "/*"),
|
|
110
|
+
method: "all",
|
|
111
|
+
order: "post",
|
|
112
|
+
before,
|
|
113
|
+
handler: function() {
|
|
114
|
+
var _ref2 = _async_to_generator(function(c, next) {
|
|
115
|
+
var response;
|
|
116
|
+
return _ts_generator(this, function(_state2) {
|
|
117
|
+
switch (_state2.label) {
|
|
118
|
+
case 0:
|
|
119
|
+
if (!_this.apiServer)
|
|
120
|
+
return [
|
|
121
|
+
3,
|
|
122
|
+
2
|
|
123
|
+
];
|
|
124
|
+
return [
|
|
125
|
+
4,
|
|
126
|
+
_this.apiServer.fetch(c.req, c.env)
|
|
127
|
+
];
|
|
128
|
+
case 1:
|
|
129
|
+
response = _state2.sent();
|
|
130
|
+
if (response.status !== 404) {
|
|
131
|
+
return [
|
|
132
|
+
2,
|
|
133
|
+
new Response(response.body, response)
|
|
134
|
+
];
|
|
135
|
+
}
|
|
136
|
+
_state2.label = 2;
|
|
137
|
+
case 2:
|
|
138
|
+
return [
|
|
139
|
+
4,
|
|
140
|
+
next()
|
|
141
|
+
];
|
|
142
|
+
case 3:
|
|
143
|
+
_state2.sent();
|
|
144
|
+
return [
|
|
145
|
+
2
|
|
146
|
+
];
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
return function(c, next) {
|
|
151
|
+
return _ref2.apply(this, arguments);
|
|
152
|
+
};
|
|
153
|
+
}()
|
|
154
|
+
};
|
|
155
|
+
globalMiddlewares.push(dynamicApiMiddleware);
|
|
156
|
+
_state.label = 4;
|
|
157
|
+
case 4:
|
|
158
|
+
return [
|
|
159
|
+
2
|
|
160
|
+
];
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
return function(options) {
|
|
165
|
+
return _ref.apply(this, arguments);
|
|
166
|
+
};
|
|
167
|
+
}();
|
|
168
|
+
this.api = api;
|
|
169
|
+
}
|
|
170
|
+
var _proto = HonoAdapter2.prototype;
|
|
171
|
+
_proto.wrapInArray = function wrapInArray(handler) {
|
|
172
|
+
if (Array.isArray(handler)) {
|
|
173
|
+
return handler;
|
|
174
|
+
} else {
|
|
175
|
+
return [
|
|
176
|
+
handler
|
|
177
|
+
];
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
return HonoAdapter2;
|
|
181
|
+
}();
|
|
182
|
+
export {
|
|
183
|
+
HonoAdapter
|
|
184
|
+
};
|