@modern-js/plugin-koa 1.2.0 → 1.3.1

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 CHANGED
@@ -1,5 +1,52 @@
1
1
  # @modern-js/plugin-koa
2
2
 
3
+ ## 1.3.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 83166714: change .npmignore
8
+ - Updated dependencies [83166714]
9
+ - Updated dependencies [c3de9882]
10
+ - Updated dependencies [33ff48af]
11
+ - @modern-js/core@1.3.2
12
+ - @modern-js/adapter-helpers@1.2.1
13
+ - @modern-js/bff-runtime@1.2.1
14
+ - @modern-js/bff-utils@1.2.2
15
+ - @modern-js/server-plugin@1.2.1
16
+ - @modern-js/utils@1.2.2
17
+
18
+ ## 1.3.0
19
+
20
+ ### Minor Changes
21
+
22
+ - cfe11628: Make Modern.js self bootstraping
23
+
24
+ ### Patch Changes
25
+
26
+ - 146dcd85: modify server framework plugin hook types and hook context
27
+ - 146dcd85: modify server framework plugin hook types
28
+ - 146dcd85: fix test case in babel compiler
29
+ - Updated dependencies [2da09c69]
30
+ - Updated dependencies [fc71e36f]
31
+ - Updated dependencies [146dcd85]
32
+ - Updated dependencies [c3d46ee4]
33
+ - Updated dependencies [cfe11628]
34
+ - Updated dependencies [146dcd85]
35
+ - Updated dependencies [8e7603ee]
36
+ - Updated dependencies [146dcd85]
37
+ - @modern-js/utils@1.2.0
38
+ - @modern-js/core@1.3.0
39
+ - @modern-js/bff-runtime@1.2.0
40
+ - @modern-js/adapter-helpers@1.2.0
41
+ - @modern-js/bff-utils@1.2.0
42
+ - @modern-js/server-plugin@1.2.0
43
+
44
+ ## 1.2.1
45
+
46
+ ### Patch Changes
47
+
48
+ - ad107726: feat: new action support apppend type define
49
+
3
50
  ## 1.2.0
4
51
 
5
52
  ### Minor Changes
package/jest.config.js ADDED
@@ -0,0 +1,8 @@
1
+ const sharedConfig = require('@scripts/jest-config');
2
+
3
+ /** @type {import('@jest/types').Config.InitialOptions} */
4
+ module.exports = {
5
+ // eslint-disable-next-line node/no-unsupported-features/es-syntax
6
+ ...sharedConfig,
7
+ rootDir: __dirname,
8
+ };
package/modern.config.js CHANGED
@@ -1,10 +1,2 @@
1
1
  /** @type {import('@modern-js/module-tools').UserConfig} */
2
- module.exports = {
3
- testing: {
4
- jest: {
5
- collectCoverage: true,
6
- collectCoverageFrom: ['src/**/*.ts'],
7
- coveragePathIgnorePatterns: ['/node_modules/'],
8
- },
9
- },
10
- };
2
+ module.exports = {};
package/package.json CHANGED
@@ -11,26 +11,31 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.2.0",
14
+ "version": "1.3.1",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
18
18
  "module": "./dist/js/treeshaking/index.js",
19
19
  "jsnext:modern": "./dist/js/modern/index.js",
20
20
  "exports": {
21
+ "./types": "./types.d.ts",
21
22
  ".": {
22
23
  "node": {
24
+ "jsnext:source": "./src/index.ts",
23
25
  "import": "./dist/js/modern/index.js",
24
26
  "require": "./dist/js/node/index.js"
25
27
  },
26
28
  "default": "./dist/js/treeshaking/index.js"
27
29
  },
28
- "./cli": "./dist/js/node/cli/index.js"
30
+ "./cli": {
31
+ "jsnext:source": "./src/cli/index.ts",
32
+ "default": "./dist/js/node/cli/index.js"
33
+ }
29
34
  },
30
35
  "dependencies": {
31
36
  "@babel/runtime": "^7.15.3",
32
- "@modern-js/adapter-helpers": "^1.1.1",
33
- "@modern-js/utils": "^1.1.5",
37
+ "@modern-js/adapter-helpers": "^1.2.1",
38
+ "@modern-js/utils": "^1.2.2",
34
39
  "formidable": "^1.2.2",
35
40
  "koa-body": "^4.2.0",
36
41
  "koa-router": "^10.0.0",
@@ -47,12 +52,13 @@
47
52
  "@types/type-is": "^1.6.3",
48
53
  "supertest": "^6.1.6",
49
54
  "typescript": "^4",
50
- "@modern-js/bff-runtime": "^1.1.1",
51
- "@modern-js/bff-utils": "^1.1.1",
52
- "@modern-js/core": "^1.2.0",
53
- "@modern-js/server-plugin": "^1.1.3",
54
- "@modern-js/plugin-testing": "^1.1.1",
55
- "@modern-js/module-tools": "^1.1.2"
55
+ "@modern-js/bff-runtime": "^1.2.1",
56
+ "@modern-js/bff-utils": "^1.2.2",
57
+ "@modern-js/core": "^1.3.2",
58
+ "@modern-js/server-plugin": "^1.2.1",
59
+ "@scripts/build": "0.0.0",
60
+ "jest": "^27",
61
+ "@scripts/jest-config": "0.0.0"
56
62
  },
57
63
  "modernConfig": {
58
64
  "output": {
@@ -60,20 +66,21 @@
60
66
  }
61
67
  },
62
68
  "peerDependencies": {
63
- "@modern-js/bff-utils": "^1.1.1",
64
- "@modern-js/core": "^1.2.0",
65
- "@modern-js/server-plugin": "^1.1.3",
66
- "@modern-js/bff-runtime": "^1.1.1",
69
+ "@modern-js/bff-utils": "^1.2.2",
70
+ "@modern-js/core": "^1.3.2",
71
+ "@modern-js/server-plugin": "^1.2.1",
72
+ "@modern-js/bff-runtime": "^1.2.1",
67
73
  "koa": "^2.13.3"
68
74
  },
69
75
  "publishConfig": {
70
76
  "registry": "https://registry.npmjs.org/",
71
- "access": "public"
77
+ "access": "public",
78
+ "types": "./dist/types/index.d.ts"
72
79
  },
73
80
  "scripts": {
74
81
  "new": "modern new",
75
82
  "build": "modern build",
76
- "test": "modern test"
83
+ "test": "jest --passWithNoTests"
77
84
  },
78
85
  "readme": "\n<p align=\"center\">\n <a href=\"https://modernjs.dev\" target=\"blank\"><img src=\"https://lf3-static.bytednsdoc.com/obj/eden-cn/ylaelkeh7nuhfnuhf/modernjs-cover.png\" width=\"300\" alt=\"Modern.js Logo\" /></a>\n</p>\n<p align=\"center\">\n现代 Web 工程体系\n <br/>\n <a href=\"https://modernjs.dev\" target=\"blank\">\n modernjs.dev\n </a>\n</p>\n<p align=\"center\">\n The meta-framework suite designed from scratch for frontend-focused modern web development\n</p>\n\n# Introduction\n\n> The doc site ([modernjs.dev](https://modernjs.dev)) and articles are only available in Chinese for now, we are planning to add English versions soon.\n\n- [Modern.js: Hello, World!](https://zhuanlan.zhihu.com/p/426707646)\n\n## Getting Started\n\n- [Quick Start](https://modernjs.dev/docs/start)\n- [Guides](https://modernjs.dev/docs/guides)\n- [API References](https://modernjs.dev/docs/apis)\n\n## Contributing\n\n- [Contributing Guide](https://github.com/modern-js-dev/modern.js/blob/main/CONTRIBUTING.md)\n"
79
86
  }
@@ -1,5 +1,5 @@
1
1
  import { match } from '@modern-js/bff-runtime';
2
- import { useContext } from '@/context';
2
+ import { useContext } from '../../../../../src/context';
3
3
 
4
4
  export const get = ({ query }: { query: Record<string, unknown> }) => ({
5
5
  query,
@@ -1,5 +1,5 @@
1
1
  import { match } from '@modern-js/bff-runtime';
2
- import { useContext } from '@/context';
2
+ import { useContext } from '../../../../../../src/context';
3
3
 
4
4
  export const get = ({ query }: { query: Record<string, unknown> }) => ({
5
5
  query,
@@ -1,6 +1,6 @@
1
1
  import * as path from 'path';
2
2
  import request from 'supertest';
3
- import Koa, { Context, Next } from 'koa';
3
+ import Koa from 'koa';
4
4
  import { serverManager } from '@modern-js/server-plugin';
5
5
  import Router from 'koa-router';
6
6
  import koaBody from 'koa-body';
@@ -111,11 +111,11 @@ describe('add middlewares', () => {
111
111
 
112
112
  test('should works', async () => {
113
113
  const foo = 'foo';
114
- const fakeMiddleware = jest.fn(async (ctx: Context, next: Next) => {
114
+ const fakeMiddleware = jest.fn(async (ctx: any, next: any) => {
115
115
  await next();
116
116
  ctx.body = foo;
117
117
  });
118
- const fakeMiddleware2 = jest.fn(async (ctx: Context, next: Next) => {
118
+ const fakeMiddleware2 = jest.fn(async (ctx: any, next: any) => {
119
119
  await next();
120
120
  });
121
121
 
@@ -7,8 +7,6 @@
7
7
  "isolatedModules": true,
8
8
  "sourceMap": true
9
9
  },
10
- "paths": {
11
- "@/*": ["../src/*"]
12
- },
10
+ "paths": {},
13
11
  "exclude": ["src/__test__/fixtures/**"]
14
12
  }
package/types.d.ts CHANGED
@@ -1,7 +1,17 @@
1
1
  /// <reference path="./dist/types/index.d.ts" />
2
-
3
2
  declare module '@modern-js/runtime/server' {
4
- import { Context } from 'koa';
3
+
4
+ import { Context, Middleware } from 'koa';
5
+
6
+ type KoaOptions = {
7
+ addMiddleware: (...input: Middleware[]) => void;
8
+ };
9
+
10
+ type KoaAttacher = (options: KoaOptions) => void;
5
11
 
6
12
  export function useContext(): Context;
13
+
14
+ export function hook(attacher: KoaAttacher): KoaAttacher;
15
+
16
+ export * from '@modern-js/bff-runtime';
7
17
  }
package/src/cli/index.ts DELETED
@@ -1,85 +0,0 @@
1
- import * as path from 'path';
2
- import { useAppContext, createPlugin } from '@modern-js/core';
3
- import { createRuntimeExportsUtils } from '@modern-js/utils';
4
-
5
- export default createPlugin(
6
- () => {
7
- let bffExportsUtils: any;
8
- const runtimeModulePath = path.resolve(__dirname, '../runtime');
9
-
10
- return {
11
- config() {
12
- // eslint-disable-next-line react-hooks/rules-of-hooks
13
- const appContext = useAppContext();
14
- const { appDirectory } = appContext;
15
- bffExportsUtils = createRuntimeExportsUtils(
16
- appContext.internalDirectory,
17
- 'server',
18
- );
19
-
20
- const serverRuntimePath = bffExportsUtils.getPath();
21
-
22
- // Look up one level, because the artifacts after build have dist directories
23
- let relativeRuntimePath = path.join(
24
- '../',
25
- path.relative(appDirectory, serverRuntimePath),
26
- );
27
-
28
- if (
29
- process.env.NODE_ENV === 'development' ||
30
- process.env.NODE_ENV === 'test'
31
- ) {
32
- relativeRuntimePath = `./${path.relative(
33
- appDirectory,
34
- serverRuntimePath,
35
- )}`;
36
- }
37
-
38
- return {
39
- source: {
40
- alias: {
41
- '@modern-js/runtime/server': relativeRuntimePath,
42
- },
43
- },
44
- };
45
- },
46
- modifyEntryImports(input) {
47
- // eslint-disable-next-line react-hooks/rules-of-hooks
48
- const { appDirectory } = useAppContext();
49
- const runtimePath = require.resolve(`@modern-js/runtime`, {
50
- paths: [appDirectory],
51
- });
52
-
53
- const currentFile = bffExportsUtils.getPath();
54
-
55
- const runtimeDir = path.dirname(runtimePath);
56
-
57
- const relativeBffPath = path.relative(
58
- path.dirname(currentFile),
59
- path.join(runtimeDir, './exports/server'),
60
- );
61
- const relativeRuntimeModulePath = path.relative(
62
- path.dirname(currentFile),
63
- runtimeModulePath,
64
- );
65
-
66
- const relativeFramePath = path.relative(
67
- path.dirname(currentFile),
68
- require.resolve('koa'),
69
- );
70
-
71
- bffExportsUtils.addExport(`const bffRuntime = require('${relativeBffPath}');
72
- const pluginRuntime = require('${relativeRuntimeModulePath}');
73
- const Koa = require('${relativeFramePath}')
74
- module.exports = {
75
- Koa: Koa,
76
- ...bffRuntime,
77
- ...pluginRuntime
78
- }
79
- `);
80
- return input;
81
- },
82
- };
83
- },
84
- { name: '@modern-js/plugin-koa' },
85
- );
package/src/context.ts DELETED
@@ -1,9 +0,0 @@
1
- import { createStorage } from '@modern-js/adapter-helpers';
2
-
3
- import type { Context } from 'koa';
4
-
5
- export type { Context };
6
-
7
- const { run, useContext } = createStorage<Context>();
8
-
9
- export { run, useContext };
package/src/index.ts DELETED
@@ -1,5 +0,0 @@
1
- // eslint-disable-next-line filenames/match-exported
2
- import plugin from './plugin';
3
-
4
- export * from './context';
5
- export default plugin;
package/src/plugin.ts DELETED
@@ -1,125 +0,0 @@
1
- import * as path from 'path';
2
- import Koa, { Middleware } from 'koa';
3
- import type Application from 'koa';
4
- import Router from 'koa-router';
5
- import koaBody from 'koa-body';
6
- import { createPlugin } from '@modern-js/server-plugin';
7
- import { requireModule } from '@modern-js/bff-utils';
8
- import { fs } from '@modern-js/utils';
9
- import { run } from './context';
10
- import registerRoutes from './registerRoutes';
11
-
12
- interface FrameConfig {
13
- middleware: (Middleware | string)[];
14
- }
15
-
16
- const findAppModule = async (apiDir: string) => {
17
- const exts = ['.ts', '.js'];
18
- const paths = exts.map(ext => path.join(apiDir, `app${ext}`));
19
-
20
- for (const filename of paths) {
21
- if (await fs.pathExists(filename)) {
22
- return requireModule(filename);
23
- }
24
- }
25
-
26
- return null;
27
- };
28
-
29
- const initMiddlewares = (
30
- middleware: (Middleware | string)[],
31
- app: Application,
32
- ) => {
33
- middleware.forEach(middlewareItem => {
34
- const middlewareFunc =
35
- typeof middlewareItem === 'string'
36
- ? requireModule(middlewareItem)
37
- : middlewareItem;
38
- app.use(middlewareFunc);
39
- });
40
- };
41
-
42
- export type Mode = 'function' | 'framework';
43
-
44
- export default createPlugin(
45
- () => ({
46
- // eslint-disable-next-line max-statements
47
- async prepareApiServer({ pwd, mode, config, prefix }) {
48
- let app: Application;
49
- const router = new Router();
50
- const apiDir = path.join(pwd, './api');
51
-
52
- if (mode === 'framework') {
53
- app = await findAppModule(apiDir);
54
- if (!(app instanceof Koa)) {
55
- app = new Koa();
56
- app.use(koaBody());
57
- }
58
-
59
- if (config) {
60
- const { middleware } = config as FrameConfig;
61
- initMiddlewares(middleware, app);
62
- }
63
-
64
- app.use(run);
65
- registerRoutes(router, prefix as string);
66
- } else if (mode === 'function') {
67
- app = new Koa();
68
- app.use(koaBody());
69
- if (config) {
70
- const { middleware } = config as FrameConfig;
71
- initMiddlewares(middleware, app);
72
- }
73
-
74
- app.use(run);
75
- registerRoutes(router, prefix as string);
76
- } else {
77
- throw new Error(`mode must be function or framework`);
78
- }
79
-
80
- app.use(router.routes());
81
-
82
- return (req, res) => {
83
- app.on('error', err => {
84
- if (err) {
85
- throw err;
86
- }
87
- });
88
- return Promise.resolve(app.callback()(req, res));
89
- };
90
- },
91
- prepareWebServer({ config }) {
92
- const app: Application = new Koa();
93
-
94
- app.use(async (ctx, next) => {
95
- await next();
96
- if (!ctx.body) {
97
- // restore statusCode
98
- if (ctx.res.statusCode === 404) {
99
- ctx.res.statusCode = 200;
100
- }
101
- ctx.respond = false;
102
- }
103
- });
104
-
105
- app.use(koaBody());
106
- if (config) {
107
- const { middleware } = config as FrameConfig;
108
- initMiddlewares(middleware, app);
109
- }
110
-
111
- return (req, res) => {
112
- app.on('error', err => {
113
- if (err) {
114
- throw err;
115
- }
116
- });
117
- return Promise.resolve(app.callback()(req, res));
118
- };
119
- },
120
- }),
121
- {
122
- name: '@modern-js/plugin-koa',
123
- pre: ['@modern-js/plugin-bff'],
124
- },
125
- ) as any;
@@ -1,109 +0,0 @@
1
- import { HttpMethod, useAPIHandlerInfos } from '@modern-js/bff-utils';
2
- import { isSchemaHandler, InputType } from '@modern-js/bff-runtime';
3
- import Router from 'koa-router';
4
- import { Context } from 'koa';
5
- import typeIs from 'type-is';
6
- import formidable from 'formidable';
7
- import { sortDynamicRoutes } from '@modern-js/adapter-helpers';
8
- import { createDebugger } from '@modern-js/utils';
9
-
10
- const debug = createDebugger('koa');
11
-
12
- const registerRoutes = (router: Router, prefix?: string) => {
13
- // eslint-disable-next-line react-hooks/rules-of-hooks
14
- const handlerInfos = useAPIHandlerInfos();
15
- sortDynamicRoutes(handlerInfos);
16
- debug('handlerInfos', handlerInfos);
17
-
18
- if (prefix) {
19
- router.prefix(prefix);
20
- }
21
-
22
- handlerInfos.forEach(({ handler, method, name }) => {
23
- const wrapedHandler = async (ctx: Context) => {
24
- const input = await getInputFromRequest(ctx);
25
-
26
- if (isSchemaHandler(handler)) {
27
- const result = await handler(input);
28
- if (result.type !== 'HandleSuccess') {
29
- if (result.type === 'InputValidationError') {
30
- // eslint-disable-next-line require-atomic-updates
31
- ctx.status = 400;
32
- } else {
33
- // eslint-disable-next-line require-atomic-updates
34
- ctx.status = 500;
35
- }
36
- // eslint-disable-next-line require-atomic-updates
37
- ctx.body = result.message;
38
- } else {
39
- // eslint-disable-next-line require-atomic-updates
40
- ctx.body = result.value;
41
- }
42
- } else {
43
- const args = Object.values(input.params as any).concat(input);
44
- // eslint-disable-next-line require-atomic-updates
45
- ctx.type = 'json';
46
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
47
- // @ts-expect-error
48
- // eslint-disable-next-line require-atomic-updates
49
- ctx.body = await handler(...args);
50
- }
51
- };
52
-
53
- Object.defineProperties(
54
- wrapedHandler,
55
- Object.getOwnPropertyDescriptors(handler),
56
- );
57
-
58
- if (isNormalMethod(method)) {
59
- const routeName = method.toLowerCase();
60
- (router as any)[routeName](name, wrapedHandler);
61
- } else {
62
- throw new Error(`Unknown HTTP Method: ${method}`);
63
- }
64
- });
65
- };
66
-
67
- const isNormalMethod = (method: string): method is HttpMethod =>
68
- Object.keys(HttpMethod).includes(method);
69
-
70
- export default registerRoutes;
71
-
72
- const getInputFromRequest = async (ctx: Context): Promise<InputType> => {
73
- const draft: Record<string, any> = {
74
- params: ctx.params,
75
- query: ctx.query,
76
- headers: ctx.headers,
77
- cookies: ctx.headers.cookie,
78
- };
79
-
80
- if (typeIs.is(ctx.request.type, ['application/json'])) {
81
- draft.data = ctx.request.body;
82
- } else if (typeIs.is(ctx.request.type, ['multipart/form-data'])) {
83
- draft.formData = await resvoleFormData(ctx);
84
- } else if (
85
- typeIs.is(ctx.request.type, ['application/x-www-form-urlencoded'])
86
- ) {
87
- draft.formUrlencoded = ctx.request.body;
88
- } else {
89
- draft.body = ctx.request.body;
90
- }
91
-
92
- return draft as any;
93
- };
94
-
95
- const resvoleFormData = (ctx: Context): Promise<Record<string, any>> => {
96
- const form = formidable({ multiples: true });
97
- return new Promise((resolve, reject) => {
98
- form.parse(ctx.req, (err, fields, files) => {
99
- if (err) {
100
- reject(err);
101
- }
102
-
103
- resolve({
104
- ...fields,
105
- ...files,
106
- });
107
- });
108
- });
109
- };
package/src/runtime.ts DELETED
@@ -1 +0,0 @@
1
- export { useContext } from './context';