@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.
@@ -1,6 +1,6 @@
1
1
  import * as path from "path";
2
2
  import { createRuntimeExportsUtils } from "@modern-js/utils";
3
- var cli_default = () => ({
3
+ export default () => ({
4
4
  name: "@modern-js/plugin-koa",
5
5
  setup: (api) => {
6
6
  let bffExportsUtils;
@@ -9,10 +9,7 @@ var cli_default = () => ({
9
9
  return {
10
10
  config() {
11
11
  const appContext = useAppContext();
12
- bffExportsUtils = createRuntimeExportsUtils(
13
- appContext.internalDirectory,
14
- "server"
15
- );
12
+ bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, "server");
16
13
  const runtimePath = "@modern-js/plugin-koa/runtime";
17
14
  const alias = process.env.NODE_ENV === "production" ? runtimePath : require.resolve(runtimePath);
18
15
  return {
@@ -33,18 +30,14 @@ var cli_default = () => ({
33
30
  plugins.push({
34
31
  "@modern-js/plugin-koa": "@modern-js/plugin-koa/server"
35
32
  });
36
- return { plugins };
33
+ return {
34
+ plugins
35
+ };
37
36
  },
38
37
  addRuntimeExports(input) {
39
38
  const currentFile = bffExportsUtils.getPath();
40
- const relativeRuntimeModulePath = path.relative(
41
- path.dirname(currentFile),
42
- runtimeModulePath
43
- );
44
- const relativeFramePath = path.relative(
45
- path.dirname(currentFile),
46
- require.resolve("koa")
47
- );
39
+ const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
40
+ const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve("koa"));
48
41
  bffExportsUtils.addExport(`const pluginRuntime = require('${relativeRuntimeModulePath}');
49
42
  const Koa = require('${relativeFramePath}')
50
43
  module.exports = {
@@ -57,6 +50,3 @@ var cli_default = () => ({
57
50
  };
58
51
  }
59
52
  });
60
- export {
61
- cli_default as default
62
- };
@@ -1,6 +1,3 @@
1
1
  import { createStorage } from "@modern-js/bff-core";
2
2
  const { run, useContext } = createStorage();
3
- export {
4
- run,
5
- useContext
6
- };
3
+ export { run, useContext };
@@ -1,6 +1,3 @@
1
1
  import plugin from "./plugin";
2
2
  export * from "./context";
3
- var src_default = plugin;
4
- export {
5
- src_default as default
6
- };
3
+ export default plugin;
@@ -6,7 +6,10 @@ import { fs, compatRequire } from "@modern-js/utils";
6
6
  import { run } from "./context";
7
7
  import registerRoutes from "./registerRoutes";
8
8
  const findAppModule = async (apiDir) => {
9
- const exts = [".ts", ".js"];
9
+ const exts = [
10
+ ".ts",
11
+ ".js"
12
+ ];
10
13
  const paths = exts.map((ext) => path.join(apiDir, `app${ext}`));
11
14
  for (const filename of paths) {
12
15
  if (await fs.pathExists(filename)) {
@@ -22,101 +25,100 @@ const initMiddlewares = (middleware, app) => {
22
25
  app.use(middlewareFunc);
23
26
  });
24
27
  };
25
- var plugin_default = () => ({
26
- name: "@modern-js/plugin-koa",
27
- pre: ["@modern-js/plugin-bff"],
28
- post: ["@modern-js/plugin-server"],
29
- setup: (api) => ({
30
- async prepareApiServer({ pwd, config, render }) {
31
- var _a;
32
- let app;
33
- const router = new Router();
34
- const apiDir = path.join(pwd, "./api");
35
- const appContext = api.useAppContext();
36
- const apiHandlerInfos = appContext.apiHandlerInfos;
37
- const mode = appContext.apiMode;
38
- const userConfig = api.useConfigContext();
39
- if (mode === "framework") {
40
- app = await findAppModule(apiDir);
41
- if (!(app instanceof Koa)) {
42
- app = new Koa();
43
- app.use(
44
- koaBody({
28
+ export default () => {
29
+ return {
30
+ name: "@modern-js/plugin-koa",
31
+ pre: [
32
+ "@modern-js/plugin-bff"
33
+ ],
34
+ post: [
35
+ "@modern-js/plugin-server"
36
+ ],
37
+ setup: (api) => {
38
+ return {
39
+ async prepareApiServer({ pwd, config, render }) {
40
+ var _userConfig_bff;
41
+ let app;
42
+ const router = new Router();
43
+ const apiDir = path.join(pwd, "./api");
44
+ const appContext = api.useAppContext();
45
+ const apiHandlerInfos = appContext.apiHandlerInfos;
46
+ const mode = appContext.apiMode;
47
+ const userConfig = api.useConfigContext();
48
+ if (mode === "framework") {
49
+ app = await findAppModule(apiDir);
50
+ if (!(app instanceof Koa)) {
51
+ app = new Koa();
52
+ app.use(koaBody({
53
+ multipart: true
54
+ }));
55
+ }
56
+ if (config) {
57
+ const { middleware } = config;
58
+ initMiddlewares(middleware, app);
59
+ }
60
+ app.use(run);
61
+ registerRoutes(router, apiHandlerInfos);
62
+ } else if (mode === "function") {
63
+ app = new Koa();
64
+ app.use(koaBody({
45
65
  multipart: true
46
- })
47
- );
48
- }
49
- if (config) {
50
- const { middleware } = config;
51
- initMiddlewares(middleware, app);
52
- }
53
- app.use(run);
54
- registerRoutes(router, apiHandlerInfos);
55
- } else if (mode === "function") {
56
- app = new Koa();
57
- app.use(
58
- koaBody({
59
- multipart: true
60
- })
61
- );
62
- if (config) {
63
- const { middleware } = config;
64
- initMiddlewares(middleware, app);
65
- }
66
- app.use(run);
67
- registerRoutes(router, apiHandlerInfos);
68
- } else {
69
- throw new Error(`mode must be function or framework`);
70
- }
71
- app.use(router.routes());
72
- if (((_a = userConfig.bff) == null ? void 0 : _a.enableHandleWeb) && render) {
73
- app.use(async (ctx, next) => {
74
- const html = await render(ctx.req, ctx.res);
75
- if (html) {
76
- ctx.body = html;
66
+ }));
67
+ if (config) {
68
+ const { middleware } = config;
69
+ initMiddlewares(middleware, app);
70
+ }
71
+ app.use(run);
72
+ registerRoutes(router, apiHandlerInfos);
73
+ } else {
74
+ throw new Error(`mode must be function or framework`);
77
75
  }
78
- await next();
79
- });
80
- }
81
- return (req, res) => {
82
- return Promise.resolve(app.callback()(req, res));
83
- };
84
- },
85
- prepareWebServer({ config }, next) {
86
- var _a;
87
- const userConfig = api.useConfigContext();
88
- if (!((_a = userConfig == null ? void 0 : userConfig.server) == null ? void 0 : _a.enableFrameworkExt)) {
89
- return next();
90
- }
91
- const app = new Koa();
92
- app.use(async (ctx, next2) => {
93
- await next2();
94
- if (!ctx.body) {
95
- if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
96
- ctx.res.statusCode = 200;
76
+ app.use(router.routes());
77
+ if (((_userConfig_bff = userConfig.bff) === null || _userConfig_bff === void 0 ? void 0 : _userConfig_bff.enableHandleWeb) && render) {
78
+ app.use(async (ctx, next) => {
79
+ const html = await render(ctx.req, ctx.res);
80
+ if (html) {
81
+ ctx.body = html;
82
+ }
83
+ await next();
84
+ });
97
85
  }
98
- ctx.respond = false;
99
- }
100
- });
101
- app.use(koaBody());
102
- if (config) {
103
- const { middleware } = config;
104
- initMiddlewares(middleware, app);
105
- }
106
- return (ctx) => {
107
- const {
108
- source: { req, res }
109
- } = ctx;
110
- app.on("error", (err) => {
111
- if (err) {
112
- throw err;
86
+ return (req, res) => {
87
+ return Promise.resolve(app.callback()(req, res));
88
+ };
89
+ },
90
+ prepareWebServer({ config }, next) {
91
+ var _userConfig_server;
92
+ const userConfig = api.useConfigContext();
93
+ if (!(userConfig === null || userConfig === void 0 ? void 0 : (_userConfig_server = userConfig.server) === null || _userConfig_server === void 0 ? void 0 : _userConfig_server.enableFrameworkExt)) {
94
+ return next();
95
+ }
96
+ const app = new Koa();
97
+ app.use(async (ctx, next2) => {
98
+ await next2();
99
+ if (!ctx.body) {
100
+ if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
101
+ ctx.res.statusCode = 200;
102
+ }
103
+ ctx.respond = false;
104
+ }
105
+ });
106
+ app.use(koaBody());
107
+ if (config) {
108
+ const { middleware } = config;
109
+ initMiddlewares(middleware, app);
113
110
  }
114
- });
115
- return Promise.resolve(app.callback()(req, res));
111
+ return (ctx) => {
112
+ const { source: { req, res } } = ctx;
113
+ app.on("error", (err) => {
114
+ if (err) {
115
+ throw err;
116
+ }
117
+ });
118
+ return Promise.resolve(app.callback()(req, res));
119
+ };
120
+ }
116
121
  };
117
122
  }
118
- })
119
- });
120
- export {
121
- plugin_default as default
123
+ };
122
124
  };
@@ -6,7 +6,4 @@ const registerRoutes = (router, handlerInfos) => {
6
6
  router[method](routePath, routeHandler);
7
7
  });
8
8
  };
9
- var registerRoutes_default = registerRoutes;
10
- export {
11
- registerRoutes_default as default
12
- };
9
+ export default registerRoutes;
@@ -1,7 +1,3 @@
1
- const hook = (attacher) => attacher;
2
- import { useContext } from "./context";
1
+ export const hook = (attacher) => attacher;
2
+ export { useContext } from "./context";
3
3
  export * from "@modern-js/bff-core";
4
- export {
5
- hook,
6
- useContext
7
- };
@@ -1,16 +1,8 @@
1
- import {
2
- httpMethods,
3
- isWithMetaHandler,
4
- ResponseMetaType,
5
- HttpMetadata
6
- } from "@modern-js/bff-core";
1
+ import { httpMethods, isWithMetaHandler, ResponseMetaType, HttpMetadata } from "@modern-js/bff-core";
7
2
  import { isSchemaHandler } from "@modern-js/bff-runtime";
8
3
  import typeIs from "type-is";
9
4
  const handleResponseMeta = (ctx, handler) => {
10
- const responseMeta = Reflect.getMetadata(
11
- HttpMetadata.Response,
12
- handler
13
- );
5
+ const responseMeta = Reflect.getMetadata(HttpMetadata.Response, handler);
14
6
  if (Array.isArray(responseMeta)) {
15
7
  for (const meta of responseMeta) {
16
8
  const metaType = meta.type;
@@ -39,7 +31,7 @@ const handleResponseMeta = (ctx, handler) => {
39
31
  }
40
32
  }
41
33
  };
42
- const createRouteHandler = (handler) => {
34
+ export const createRouteHandler = (handler) => {
43
35
  const apiHandler = async (ctx) => {
44
36
  const input = await getInputFromRequest(ctx);
45
37
  if (isWithMetaHandler(handler)) {
@@ -82,13 +74,10 @@ const createRouteHandler = (handler) => {
82
74
  }
83
75
  }
84
76
  };
85
- Object.defineProperties(
86
- apiHandler,
87
- Object.getOwnPropertyDescriptors(handler)
88
- );
77
+ Object.defineProperties(apiHandler, Object.getOwnPropertyDescriptors(handler));
89
78
  return apiHandler;
90
79
  };
91
- const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
80
+ export const isNormalMethod = (httpMethod) => httpMethods.includes(httpMethod);
92
81
  const getInputFromRequest = async (ctx) => {
93
82
  const draft = {
94
83
  params: ctx.params,
@@ -96,18 +85,20 @@ const getInputFromRequest = async (ctx) => {
96
85
  headers: ctx.headers,
97
86
  cookies: ctx.headers.cookie
98
87
  };
99
- if (typeIs.is(ctx.request.type, ["application/json"])) {
88
+ if (typeIs.is(ctx.request.type, [
89
+ "application/json"
90
+ ])) {
100
91
  draft.data = ctx.request.body;
101
- } else if (typeIs.is(ctx.request.type, ["multipart/form-data"])) {
92
+ } else if (typeIs.is(ctx.request.type, [
93
+ "multipart/form-data"
94
+ ])) {
102
95
  draft.formData = ctx.request.files;
103
- } else if (typeIs.is(ctx.request.type, ["application/x-www-form-urlencoded"])) {
96
+ } else if (typeIs.is(ctx.request.type, [
97
+ "application/x-www-form-urlencoded"
98
+ ])) {
104
99
  draft.formUrlencoded = ctx.request.body;
105
100
  } else {
106
101
  draft.body = ctx.request.body;
107
102
  }
108
103
  return draft;
109
104
  };
110
- export {
111
- createRouteHandler,
112
- isNormalMethod
113
- };
package/package.json CHANGED
@@ -3,7 +3,11 @@
3
3
  "description": "A Progressive React Framework for modern web development.",
4
4
  "homepage": "https://modernjs.dev",
5
5
  "bugs": "https://github.com/web-infra-dev/modern.js/issues",
6
- "repository": "web-infra-dev/modern.js",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/web-infra-dev/modern.js",
9
+ "directory": "packages/server/plugin-koa"
10
+ },
7
11
  "license": "MIT",
8
12
  "keywords": [
9
13
  "react",
@@ -11,7 +15,7 @@
11
15
  "modern",
12
16
  "modern.js"
13
17
  ],
14
- "version": "2.14.0",
18
+ "version": "2.16.0",
15
19
  "jsnext:source": "./src/index.ts",
16
20
  "types": "./dist/types/cli/index.d.ts",
17
21
  "main": "./dist/cjs/cli/index.js",
@@ -63,10 +67,10 @@
63
67
  "koa-body": "^4.2.0",
64
68
  "koa-router": "^10.0.0",
65
69
  "type-is": "^1.6.18",
66
- "@modern-js/bff-core": "2.14.0",
67
- "@modern-js/bff-runtime": "2.14.0",
68
- "@modern-js/utils": "2.14.0",
69
- "@modern-js/types": "2.14.0"
70
+ "@modern-js/bff-core": "2.16.0",
71
+ "@modern-js/bff-runtime": "2.16.0",
72
+ "@modern-js/utils": "2.16.0",
73
+ "@modern-js/types": "2.16.0"
70
74
  },
71
75
  "devDependencies": {
72
76
  "@types/jest": "^29",
@@ -80,11 +84,11 @@
80
84
  "supertest": "^6.1.6",
81
85
  "typescript": "^4",
82
86
  "zod": "^3.17.3",
83
- "@modern-js/core": "2.14.0",
84
- "@modern-js/server-core": "2.14.0",
85
- "@modern-js/app-tools": "2.14.0",
86
- "@scripts/build": "2.14.0",
87
- "@scripts/jest-config": "2.14.0"
87
+ "@modern-js/core": "2.16.0",
88
+ "@modern-js/server-core": "2.16.0",
89
+ "@modern-js/app-tools": "2.16.0",
90
+ "@scripts/build": "2.16.0",
91
+ "@scripts/jest-config": "2.16.0"
88
92
  },
89
93
  "peerDependencies": {
90
94
  "koa": "^2.13.3"