@modern-js/plugin-koa 1.4.8-canary.0 → 1.4.9

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.
Files changed (69) hide show
  1. package/CHANGELOG.md +26 -0
  2. package/dist/js/modern/cli/index.js +19 -12
  3. package/dist/js/node/cli/index.js +18 -11
  4. package/package.json +14 -15
  5. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/.wireit/6275696c64/stdout +0 -9
  6. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/cli/index.js +0 -60
  7. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/context.js +0 -6
  8. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/index.js +0 -3
  9. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/plugin.js +0 -129
  10. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/registerRoutes.js +0 -82
  11. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/modern/runtime.js +0 -1
  12. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/cli/index.js +0 -76
  13. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/context.js +0 -15
  14. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/index.js +0 -28
  15. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/plugin.js +0 -151
  16. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/registerRoutes.js +0 -97
  17. package/.wireit/6275696c64/cache/5a7fd1190b57f273bbfc05fc04fa99ef67c54e4eac07712cccd97346c9903534/dist/js/node/runtime.js +0 -13
  18. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/cli/index.js +0 -60
  19. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/context.js +0 -6
  20. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/index.js +0 -3
  21. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/plugin.js +0 -129
  22. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/registerRoutes.js +0 -82
  23. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/modern/runtime.js +0 -1
  24. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/cli/index.js +0 -76
  25. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/context.js +0 -15
  26. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/index.js +0 -28
  27. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/plugin.js +0 -151
  28. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/registerRoutes.js +0 -97
  29. package/.wireit/6275696c64/cache/a48cd57db6d78fbc9aca74c669a12325d8d6adee3602be60b39b7f0aee775e0e/dist/js/node/runtime.js +0 -13
  30. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/cli/index.js +0 -60
  31. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/context.js +0 -6
  32. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/index.js +0 -3
  33. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/plugin.js +0 -129
  34. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/registerRoutes.js +0 -82
  35. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/modern/runtime.js +0 -1
  36. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/cli/index.js +0 -76
  37. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/context.js +0 -15
  38. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/index.js +0 -28
  39. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/plugin.js +0 -151
  40. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/registerRoutes.js +0 -97
  41. package/.wireit/6275696c64/cache/c2bcc9f69d6e88cab5556c7e21d2d96caf864704311f42923d5aac9d76903c09/dist/js/node/runtime.js +0 -13
  42. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/.wireit/6275696c64/stdout +0 -9
  43. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/cli/index.js +0 -62
  44. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/context.js +0 -6
  45. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/index.js +0 -3
  46. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/plugin.js +0 -129
  47. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/registerRoutes.js +0 -82
  48. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/modern/runtime.js +0 -1
  49. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/cli/index.js +0 -78
  50. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/context.js +0 -15
  51. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/index.js +0 -28
  52. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/plugin.js +0 -151
  53. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/registerRoutes.js +0 -97
  54. package/.wireit/6275696c64/cache/c702551e9ac5f99417b3d7904cde2a76ea272e9a04c27fe6afd33e73b7c25856/dist/js/node/runtime.js +0 -13
  55. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/cli/index.js +0 -60
  56. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/context.js +0 -6
  57. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/index.js +0 -3
  58. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/plugin.js +0 -129
  59. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/registerRoutes.js +0 -82
  60. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/modern/runtime.js +0 -1
  61. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/cli/index.js +0 -76
  62. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/context.js +0 -15
  63. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/index.js +0 -28
  64. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/plugin.js +0 -151
  65. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/registerRoutes.js +0 -97
  66. package/.wireit/6275696c64/cache/fceab7422ff46c3bc400017a31f179fd20fb88539c1968876d37bec188823945/dist/js/node/runtime.js +0 -13
  67. package/.wireit/6275696c64/lock +0 -0
  68. package/.wireit/6275696c64/state +0 -1
  69. package/.wireit/6275696c64/stdout +0 -9
package/CHANGELOG.md CHANGED
@@ -1,5 +1,31 @@
1
1
  # @modern-js/plugin-koa
2
2
 
3
+ ## 1.4.9
4
+
5
+ ### Patch Changes
6
+
7
+ - bfccb4c8: fix: bff runtime path not found in development
8
+ - Updated dependencies [d57e7622]
9
+ - Updated dependencies [6451a098]
10
+ - Updated dependencies [d5a2cfd8]
11
+ - Updated dependencies [437367c6]
12
+ - @modern-js/bff-utils@1.2.8
13
+ - @modern-js/utils@1.7.6
14
+
15
+ ## 1.4.8
16
+
17
+ ### Patch Changes
18
+
19
+ - d32f35134: chore: add modern/jest/eslint/ts config files to .npmignore
20
+ - Updated dependencies [d32f35134]
21
+ - Updated dependencies [6ae4a34ae]
22
+ - Updated dependencies [b80229c79]
23
+ - Updated dependencies [948cc4436]
24
+ - @modern-js/adapter-helpers@1.2.4
25
+ - @modern-js/bff-runtime@1.2.3
26
+ - @modern-js/bff-utils@1.2.6
27
+ - @modern-js/utils@1.7.3
28
+
3
29
  ## 1.4.7
4
30
 
5
31
  ### Patch Changes
@@ -1,5 +1,5 @@
1
1
  import * as path from 'path';
2
- import { createRuntimeExportsUtils, TEST_ENV } from '@modern-js/utils';
2
+ import { createRuntimeExportsUtils } from '@modern-js/utils';
3
3
  import { getRelativeRuntimePath } from '@modern-js/adapter-helpers';
4
4
  export default (() => ({
5
5
  name: '@modern-js/plugin-koa',
@@ -16,19 +16,26 @@ export default (() => ({
16
16
  appDirectory
17
17
  } = appContext;
18
18
  bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
19
- let serverRuntimePath = bffExportsUtils.getPath(); // Basedir is not a fixed path when the test is executed,so server runtime path must be absolute path.
19
+ const serverRuntimePath = bffExportsUtils.getPath();
20
+ const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
20
21
 
21
- if (process.env.NODE_ENV !== TEST_ENV) {
22
- serverRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
23
- }
24
-
25
- return {
26
- source: {
27
- alias: {
28
- '@modern-js/runtime/server': serverRuntimePath
22
+ if (process.env.NODE_ENV === 'production') {
23
+ return {
24
+ source: {
25
+ alias: {
26
+ '@modern-js/runtime/server': relativeRuntimePath
27
+ }
29
28
  }
30
- }
31
- };
29
+ };
30
+ } else {
31
+ return {
32
+ source: {
33
+ alias: {
34
+ '@modern-js/runtime/server': serverRuntimePath
35
+ }
36
+ }
37
+ };
38
+ }
32
39
  },
33
40
 
34
41
  addRuntimeExports(input) {
@@ -30,19 +30,26 @@ var _default = () => ({
30
30
  appDirectory
31
31
  } = appContext;
32
32
  bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
33
- let serverRuntimePath = bffExportsUtils.getPath(); // Basedir is not a fixed path when the test is executed,so server runtime path must be absolute path.
33
+ const serverRuntimePath = bffExportsUtils.getPath();
34
+ const relativeRuntimePath = (0, _adapterHelpers.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
34
35
 
35
- if (process.env.NODE_ENV !== _utils.TEST_ENV) {
36
- serverRuntimePath = (0, _adapterHelpers.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
37
- }
38
-
39
- return {
40
- source: {
41
- alias: {
42
- '@modern-js/runtime/server': serverRuntimePath
36
+ if (process.env.NODE_ENV === 'production') {
37
+ return {
38
+ source: {
39
+ alias: {
40
+ '@modern-js/runtime/server': relativeRuntimePath
41
+ }
43
42
  }
44
- }
45
- };
43
+ };
44
+ } else {
45
+ return {
46
+ source: {
47
+ alias: {
48
+ '@modern-js/runtime/server': serverRuntimePath
49
+ }
50
+ }
51
+ };
52
+ }
46
53
  },
47
54
 
48
55
  addRuntimeExports(input) {
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.8-canary.0",
14
+ "version": "1.4.9",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -34,10 +34,10 @@
34
34
  },
35
35
  "dependencies": {
36
36
  "@babel/runtime": "^7.15.3",
37
- "@modern-js/adapter-helpers": "^1.2.3",
38
- "@modern-js/bff-runtime": "^1.2.2",
39
- "@modern-js/bff-utils": "^1.2.5",
40
- "@modern-js/utils": "^1.7.2",
37
+ "@modern-js/adapter-helpers": "^1.2.4",
38
+ "@modern-js/bff-runtime": "^1.2.3",
39
+ "@modern-js/bff-utils": "^1.2.8",
40
+ "@modern-js/utils": "^1.7.6",
41
41
  "koa-body": "^4.2.0",
42
42
  "koa-router": "^10.0.0",
43
43
  "type-is": "^1.6.18"
@@ -45,15 +45,15 @@
45
45
  "devDependencies": {
46
46
  "@scripts/build": "0.0.0",
47
47
  "@scripts/jest-config": "0.0.0",
48
- "@types/jest": "^27.0.1",
48
+ "@types/jest": "^27",
49
49
  "@types/koa": "^2.13.4",
50
50
  "@types/koa-router": "^7.4.4",
51
51
  "@types/node": "^14",
52
52
  "@types/supertest": "^2.0.11",
53
53
  "@types/type-is": "^1.6.3",
54
54
  "jest": "^27",
55
- "@modern-js/server-core": "1.3.4",
56
- "@modern-js/core": "1.10.1",
55
+ "@modern-js/server-core": "1.3.5",
56
+ "@modern-js/core": "1.11.1",
57
57
  "supertest": "^6.1.6",
58
58
  "koa": "^2.13.3",
59
59
  "typescript": "^4"
@@ -68,34 +68,33 @@
68
68
  },
69
69
  "publishConfig": {
70
70
  "registry": "https://registry.npmjs.org/",
71
- "access": "public",
72
- "types": "./dist/types/index.d.ts"
71
+ "access": "public"
73
72
  },
74
73
  "wireit": {
75
74
  "build": {
76
75
  "command": "modern build",
77
76
  "files": [
78
- "src/**/*.ts",
77
+ "src/**/*",
79
78
  "tsconfig.json",
80
79
  "package.json"
81
80
  ],
82
81
  "output": [
83
- "dist/**/*.js"
82
+ "dist/**/*"
84
83
  ]
85
84
  },
86
85
  "test": {
87
86
  "command": "jest --passWithNoTests",
88
87
  "files": [
89
- "src/**/*.ts",
88
+ "src/**/*",
90
89
  "tsconfig.json",
91
- "package.json"
90
+ "package.json",
91
+ "tests/**/*"
92
92
  ],
93
93
  "output": []
94
94
  }
95
95
  },
96
96
  "scripts": {
97
97
  "new": "modern new",
98
- "dev": "modern build --watch",
99
98
  "build": "wireit",
100
99
  "test": "wireit"
101
100
  },
@@ -1,9 +0,0 @@
1
- Compiling in progress...
2
- Run `tsc` log:
3
- [TSC Compiler]: Successfully
4
-
5
-
6
- Run babel compiler code log:
7
- [Babel Compiler]: Successfully
8
-
9
-
@@ -1,60 +0,0 @@
1
- import * as path from 'path';
2
- import { createRuntimeExportsUtils } from '@modern-js/utils';
3
- import { getRelativeRuntimePath } from '@modern-js/adapter-helpers';
4
- export default (() => ({
5
- name: '@modern-js/plugin-koa',
6
- setup: api => {
7
- let bffExportsUtils;
8
- const {
9
- useAppContext
10
- } = api;
11
- const runtimeModulePath = path.resolve(__dirname, '../runtime');
12
- return {
13
- config() {
14
- // eslint-disable-next-line react-hooks/rules-of-hooks
15
- const appContext = useAppContext();
16
- const {
17
- appDirectory
18
- } = appContext;
19
- bffExportsUtils = createRuntimeExportsUtils(appContext.internalDirectory, 'server');
20
- const serverRuntimePath = bffExportsUtils.getPath();
21
- const relativeRuntimePath = getRelativeRuntimePath(appDirectory, serverRuntimePath);
22
- return {
23
- source: {
24
- alias: {
25
- '@modern-js/runtime/server': relativeRuntimePath
26
- }
27
- }
28
- };
29
- },
30
-
31
- addRuntimeExports(input) {
32
- // eslint-disable-next-line react-hooks/rules-of-hooks
33
- const {
34
- appDirectory
35
- } = useAppContext();
36
-
37
- const runtimePath = require.resolve(`@modern-js/runtime`, {
38
- paths: [appDirectory]
39
- });
40
-
41
- const currentFile = bffExportsUtils.getPath();
42
- const runtimeDir = path.dirname(runtimePath);
43
- const relativeBffPath = path.relative(path.dirname(currentFile), path.join(runtimeDir, './exports/server'));
44
- const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
45
- const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('koa'));
46
- bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
47
- const pluginRuntime = require('${relativeRuntimeModulePath}');
48
- const Koa = require('${relativeFramePath}')
49
- module.exports = {
50
- Koa: Koa,
51
- ...bffRuntime,
52
- ...pluginRuntime
53
- }
54
- `);
55
- return input;
56
- }
57
-
58
- };
59
- }
60
- }));
@@ -1,6 +0,0 @@
1
- import { createStorage } from '@modern-js/adapter-helpers';
2
- const {
3
- run,
4
- useContext
5
- } = createStorage();
6
- export { run, useContext };
@@ -1,3 +0,0 @@
1
- import plugin from "./plugin";
2
- export * from "./context";
3
- export default plugin;
@@ -1,129 +0,0 @@
1
- import * as path from 'path';
2
- import Koa from 'koa';
3
- import Router from 'koa-router';
4
- import koaBody from 'koa-body';
5
- import { requireModule } from '@modern-js/bff-utils';
6
- import { fs } from '@modern-js/utils';
7
- import { run } from "./context";
8
- import registerRoutes from "./registerRoutes";
9
-
10
- const findAppModule = async apiDir => {
11
- const exts = ['.ts', '.js'];
12
- const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
13
-
14
- for (const filename of paths) {
15
- if (await fs.pathExists(filename)) {
16
- return requireModule(filename);
17
- }
18
- }
19
-
20
- return null;
21
- };
22
-
23
- const initMiddlewares = (middleware, app) => {
24
- middleware.forEach(middlewareItem => {
25
- const middlewareFunc = typeof middlewareItem === 'string' ? requireModule(middlewareItem) : middlewareItem;
26
- app.use(middlewareFunc);
27
- });
28
- };
29
-
30
- export default (() => ({
31
- name: '@modern-js/plugin-koa',
32
- pre: ['@modern-js/plugin-bff'],
33
- setup: () => ({
34
- async prepareApiServer({
35
- pwd,
36
- mode,
37
- config,
38
- prefix
39
- }) {
40
- let app;
41
- const router = new Router();
42
- const apiDir = path.join(pwd, './api');
43
-
44
- if (mode === 'framework') {
45
- app = await findAppModule(apiDir);
46
-
47
- if (!(app instanceof Koa)) {
48
- app = new Koa();
49
- app.use(koaBody({
50
- multipart: true
51
- }));
52
- }
53
-
54
- if (config) {
55
- const {
56
- middleware
57
- } = config;
58
- initMiddlewares(middleware, app);
59
- }
60
-
61
- app.use(run);
62
- registerRoutes(router, prefix);
63
- } else if (mode === 'function') {
64
- app = new Koa();
65
- app.use(koaBody({
66
- multipart: true
67
- }));
68
-
69
- if (config) {
70
- const {
71
- middleware
72
- } = config;
73
- initMiddlewares(middleware, app);
74
- }
75
-
76
- app.use(run);
77
- registerRoutes(router, prefix);
78
- } else {
79
- throw new Error(`mode must be function or framework`);
80
- }
81
-
82
- app.use(router.routes());
83
- return (req, res) => {
84
- app.on('error', err => {
85
- if (err) {
86
- throw err;
87
- }
88
- });
89
- return Promise.resolve(app.callback()(req, res));
90
- };
91
- },
92
-
93
- prepareWebServer({
94
- config
95
- }) {
96
- const app = new Koa();
97
- app.use(async (ctx, next) => {
98
- await next();
99
-
100
- if (!ctx.body) {
101
- // restore statusCode
102
- if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
103
- ctx.res.statusCode = 200;
104
- }
105
-
106
- ctx.respond = false;
107
- }
108
- });
109
- app.use(koaBody());
110
-
111
- if (config) {
112
- const {
113
- middleware
114
- } = config;
115
- initMiddlewares(middleware, app);
116
- }
117
-
118
- return (req, res) => {
119
- app.on('error', err => {
120
- if (err) {
121
- throw err;
122
- }
123
- });
124
- return Promise.resolve(app.callback()(req, res));
125
- };
126
- }
127
-
128
- })
129
- }));
@@ -1,82 +0,0 @@
1
- import { HttpMethod, useAPIHandlerInfos } from '@modern-js/bff-utils';
2
- import { isSchemaHandler } from '@modern-js/bff-runtime';
3
- import typeIs from 'type-is';
4
- import { sortDynamicRoutes } from '@modern-js/adapter-helpers';
5
- import { createDebugger } from '@modern-js/utils';
6
- const debug = createDebugger('koa');
7
-
8
- const registerRoutes = (router, prefix) => {
9
- // eslint-disable-next-line react-hooks/rules-of-hooks
10
- const handlerInfos = useAPIHandlerInfos();
11
- sortDynamicRoutes(handlerInfos);
12
- debug('handlerInfos', handlerInfos);
13
-
14
- if (prefix) {
15
- router.prefix(prefix);
16
- }
17
-
18
- handlerInfos.forEach(({
19
- handler,
20
- method,
21
- name
22
- }) => {
23
- const wrappedHandler = async ctx => {
24
- const input = await getInputFromRequest(ctx);
25
-
26
- if (isSchemaHandler(handler)) {
27
- const result = await handler(input);
28
-
29
- if (result.type !== 'HandleSuccess') {
30
- if (result.type === 'InputValidationError') {
31
- ctx.status = 400;
32
- } else {
33
- ctx.status = 500;
34
- }
35
-
36
- ctx.body = result.message;
37
- } else {
38
- ctx.body = result.value;
39
- }
40
- } else {
41
- const args = Object.values(input.params).concat(input); // eslint-disable-next-line @typescript-eslint/ban-ts-comment
42
- // @ts-expect-error
43
-
44
- ctx.body = await handler(...args);
45
- }
46
- };
47
-
48
- Object.defineProperties(wrappedHandler, Object.getOwnPropertyDescriptors(handler));
49
-
50
- if (isNormalMethod(method)) {
51
- const routeName = method.toLowerCase();
52
- router[routeName](name, wrappedHandler);
53
- } else {
54
- throw new Error(`Unknown HTTP Method: ${method}`);
55
- }
56
- });
57
- };
58
-
59
- const isNormalMethod = method => Object.keys(HttpMethod).includes(method);
60
-
61
- export default registerRoutes;
62
-
63
- const getInputFromRequest = async ctx => {
64
- const draft = {
65
- params: ctx.params,
66
- query: ctx.query,
67
- headers: ctx.headers,
68
- cookies: ctx.headers.cookie
69
- };
70
-
71
- if (typeIs.is(ctx.request.type, ['application/json'])) {
72
- draft.data = ctx.request.body;
73
- } else if (typeIs.is(ctx.request.type, ['multipart/form-data'])) {
74
- draft.formData = ctx.request.files;
75
- } else if (typeIs.is(ctx.request.type, ['application/x-www-form-urlencoded'])) {
76
- draft.formUrlencoded = ctx.request.body;
77
- } else {
78
- draft.body = ctx.request.body;
79
- }
80
-
81
- return draft;
82
- };
@@ -1,76 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
-
8
- var path = _interopRequireWildcard(require("path"));
9
-
10
- var _utils = require("@modern-js/utils");
11
-
12
- var _adapterHelpers = require("@modern-js/adapter-helpers");
13
-
14
- function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
15
-
16
- function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
17
-
18
- var _default = () => ({
19
- name: '@modern-js/plugin-koa',
20
- setup: api => {
21
- let bffExportsUtils;
22
- const {
23
- useAppContext
24
- } = api;
25
- const runtimeModulePath = path.resolve(__dirname, '../runtime');
26
- return {
27
- config() {
28
- // eslint-disable-next-line react-hooks/rules-of-hooks
29
- const appContext = useAppContext();
30
- const {
31
- appDirectory
32
- } = appContext;
33
- bffExportsUtils = (0, _utils.createRuntimeExportsUtils)(appContext.internalDirectory, 'server');
34
- const serverRuntimePath = bffExportsUtils.getPath();
35
- const relativeRuntimePath = (0, _adapterHelpers.getRelativeRuntimePath)(appDirectory, serverRuntimePath);
36
- return {
37
- source: {
38
- alias: {
39
- '@modern-js/runtime/server': relativeRuntimePath
40
- }
41
- }
42
- };
43
- },
44
-
45
- addRuntimeExports(input) {
46
- // eslint-disable-next-line react-hooks/rules-of-hooks
47
- const {
48
- appDirectory
49
- } = useAppContext();
50
-
51
- const runtimePath = require.resolve(`@modern-js/runtime`, {
52
- paths: [appDirectory]
53
- });
54
-
55
- const currentFile = bffExportsUtils.getPath();
56
- const runtimeDir = path.dirname(runtimePath);
57
- const relativeBffPath = path.relative(path.dirname(currentFile), path.join(runtimeDir, './exports/server'));
58
- const relativeRuntimeModulePath = path.relative(path.dirname(currentFile), runtimeModulePath);
59
- const relativeFramePath = path.relative(path.dirname(currentFile), require.resolve('koa'));
60
- bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
61
- const pluginRuntime = require('${relativeRuntimeModulePath}');
62
- const Koa = require('${relativeFramePath}')
63
- module.exports = {
64
- Koa: Koa,
65
- ...bffRuntime,
66
- ...pluginRuntime
67
- }
68
- `);
69
- return input;
70
- }
71
-
72
- };
73
- }
74
- });
75
-
76
- exports.default = _default;
@@ -1,15 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.useContext = exports.run = void 0;
7
-
8
- var _adapterHelpers = require("@modern-js/adapter-helpers");
9
-
10
- const {
11
- run,
12
- useContext
13
- } = (0, _adapterHelpers.createStorage)();
14
- exports.useContext = useContext;
15
- exports.run = run;
@@ -1,28 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- var _exportNames = {};
7
- exports.default = void 0;
8
-
9
- var _plugin = _interopRequireDefault(require("./plugin"));
10
-
11
- var _context = require("./context");
12
-
13
- Object.keys(_context).forEach(function (key) {
14
- if (key === "default" || key === "__esModule") return;
15
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
16
- if (key in exports && exports[key] === _context[key]) return;
17
- Object.defineProperty(exports, key, {
18
- enumerable: true,
19
- get: function () {
20
- return _context[key];
21
- }
22
- });
23
- });
24
-
25
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
26
-
27
- var _default = _plugin.default;
28
- exports.default = _default;