@modern-js/plugin-koa 1.3.0 → 1.4.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 +46 -0
- package/dist/js/modern/cli/index.js +1 -1
- package/dist/js/modern/plugin.js +2 -2
- package/dist/js/node/cli/index.js +1 -1
- package/dist/js/node/plugin.js +3 -3
- package/dist/types/cli/index.d.ts +2 -0
- package/package.json +12 -13
- package/tests/functionMode.test.ts +1 -1
- package/tests/helpers.ts +1 -1
- package/tests/lambdaMode.test.ts +1 -1
- package/tests/webServer.test.ts +1 -1
- package/src/cli/index.ts +0 -85
- package/src/context.ts +0 -9
- package/src/index.ts +0 -5
- package/src/plugin.ts +0 -125
- package/src/registerRoutes.ts +0 -109
- package/src/runtime.ts +0 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,51 @@
|
|
|
1
1
|
# @modern-js/plugin-koa
|
|
2
2
|
|
|
3
|
+
## 1.4.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 9594df3f: fix: allow user to set statusCode 404
|
|
8
|
+
- Updated dependencies [deeaa602]
|
|
9
|
+
- Updated dependencies [54786e58]
|
|
10
|
+
- @modern-js/utils@1.3.2
|
|
11
|
+
- @modern-js/core@1.4.3
|
|
12
|
+
|
|
13
|
+
## 1.4.0
|
|
14
|
+
|
|
15
|
+
### Minor Changes
|
|
16
|
+
|
|
17
|
+
- ec4dbffb: feat: support as a pure api service
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- 272cab15: refactor server plugin manager
|
|
22
|
+
- Updated dependencies [d9cc5ea9]
|
|
23
|
+
- Updated dependencies [bd819a8d]
|
|
24
|
+
- Updated dependencies [ec4dbffb]
|
|
25
|
+
- Updated dependencies [d099e5c5]
|
|
26
|
+
- Updated dependencies [bada2879]
|
|
27
|
+
- Updated dependencies [24f616ca]
|
|
28
|
+
- Updated dependencies [bd819a8d]
|
|
29
|
+
- Updated dependencies [272cab15]
|
|
30
|
+
- @modern-js/core@1.4.0
|
|
31
|
+
- @modern-js/utils@1.3.0
|
|
32
|
+
- @modern-js/server-core@1.2.2
|
|
33
|
+
|
|
34
|
+
## 1.3.1
|
|
35
|
+
|
|
36
|
+
### Patch Changes
|
|
37
|
+
|
|
38
|
+
- 83166714: change .npmignore
|
|
39
|
+
- Updated dependencies [83166714]
|
|
40
|
+
- Updated dependencies [c3de9882]
|
|
41
|
+
- Updated dependencies [33ff48af]
|
|
42
|
+
- @modern-js/core@1.3.2
|
|
43
|
+
- @modern-js/adapter-helpers@1.2.1
|
|
44
|
+
- @modern-js/bff-runtime@1.2.1
|
|
45
|
+
- @modern-js/bff-utils@1.2.2
|
|
46
|
+
- @modern-js/server-plugin@1.2.1
|
|
47
|
+
- @modern-js/utils@1.2.2
|
|
48
|
+
|
|
3
49
|
## 1.3.0
|
|
4
50
|
|
|
5
51
|
### Minor Changes
|
package/dist/js/modern/plugin.js
CHANGED
|
@@ -2,7 +2,7 @@ import * as path from 'path';
|
|
|
2
2
|
import Koa from 'koa';
|
|
3
3
|
import Router from 'koa-router';
|
|
4
4
|
import koaBody from 'koa-body';
|
|
5
|
-
import { createPlugin } from '@modern-js/server-
|
|
5
|
+
import { createPlugin } from '@modern-js/server-core';
|
|
6
6
|
import { requireModule } from '@modern-js/bff-utils';
|
|
7
7
|
import { fs } from '@modern-js/utils';
|
|
8
8
|
import { run } from "./context";
|
|
@@ -94,7 +94,7 @@ export default createPlugin(() => ({
|
|
|
94
94
|
|
|
95
95
|
if (!ctx.body) {
|
|
96
96
|
// restore statusCode
|
|
97
|
-
if (ctx.res.statusCode === 404) {
|
|
97
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
98
98
|
ctx.res.statusCode = 200;
|
|
99
99
|
}
|
|
100
100
|
|
package/dist/js/node/plugin.js
CHANGED
|
@@ -13,7 +13,7 @@ var _koaRouter = _interopRequireDefault(require("koa-router"));
|
|
|
13
13
|
|
|
14
14
|
var _koaBody = _interopRequireDefault(require("koa-body"));
|
|
15
15
|
|
|
16
|
-
var
|
|
16
|
+
var _serverCore = require("@modern-js/server-core");
|
|
17
17
|
|
|
18
18
|
var _bffUtils = require("@modern-js/bff-utils");
|
|
19
19
|
|
|
@@ -49,7 +49,7 @@ const initMiddlewares = (middleware, app) => {
|
|
|
49
49
|
});
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
-
var _default = (0,
|
|
52
|
+
var _default = (0, _serverCore.createPlugin)(() => ({
|
|
53
53
|
// eslint-disable-next-line max-statements
|
|
54
54
|
async prepareApiServer({
|
|
55
55
|
pwd,
|
|
@@ -115,7 +115,7 @@ var _default = (0, _serverPlugin.createPlugin)(() => ({
|
|
|
115
115
|
|
|
116
116
|
if (!ctx.body) {
|
|
117
117
|
// restore statusCode
|
|
118
|
-
if (ctx.res.statusCode === 404) {
|
|
118
|
+
if (ctx.res.statusCode === 404 && !ctx.response._explicitStatus) {
|
|
119
119
|
ctx.res.statusCode = 200;
|
|
120
120
|
}
|
|
121
121
|
|
|
@@ -11,8 +11,10 @@ declare const _default: import("@modern-js/core").AsyncPlugin<Partial<import("@m
|
|
|
11
11
|
watchFiles: import("@modern-js/core").ParallelWorkflow<void, unknown>;
|
|
12
12
|
fileChange: import("@modern-js/core").AsyncWorkflow<{
|
|
13
13
|
filename: string;
|
|
14
|
+
eventType: "add" | "unlink" | "change";
|
|
14
15
|
}, void>;
|
|
15
16
|
beforeExit: import("@modern-js/core").AsyncWorkflow<void, void>;
|
|
17
|
+
beforeRestart: import("@modern-js/core").AsyncWorkflow<void, void>;
|
|
16
18
|
} & import("@modern-js/core").ClearDraftProgress<import("@modern-js/core").Hooks>>>>;
|
|
17
19
|
|
|
18
20
|
export default _default;
|
package/package.json
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"modern",
|
|
12
12
|
"modern.js"
|
|
13
13
|
],
|
|
14
|
-
"version": "1.
|
|
14
|
+
"version": "1.4.1",
|
|
15
15
|
"jsnext:source": "./src/index.ts",
|
|
16
16
|
"types": "./dist/types/index.d.ts",
|
|
17
17
|
"main": "./dist/js/node/index.js",
|
|
@@ -34,8 +34,8 @@
|
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
36
|
"@babel/runtime": "^7.15.3",
|
|
37
|
-
"@modern-js/adapter-helpers": "^1.2.
|
|
38
|
-
"@modern-js/utils": "^1.2
|
|
37
|
+
"@modern-js/adapter-helpers": "^1.2.1",
|
|
38
|
+
"@modern-js/utils": "^1.3.2",
|
|
39
39
|
"formidable": "^1.2.2",
|
|
40
40
|
"koa-body": "^4.2.0",
|
|
41
41
|
"koa-router": "^10.0.0",
|
|
@@ -52,10 +52,10 @@
|
|
|
52
52
|
"@types/type-is": "^1.6.3",
|
|
53
53
|
"supertest": "^6.1.6",
|
|
54
54
|
"typescript": "^4",
|
|
55
|
-
"@modern-js/bff-runtime": "^1.2.
|
|
56
|
-
"@modern-js/bff-utils": "^1.2.
|
|
57
|
-
"@modern-js/core": "^1.3
|
|
58
|
-
"@modern-js/server-
|
|
55
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
56
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
57
|
+
"@modern-js/core": "^1.4.3",
|
|
58
|
+
"@modern-js/server-core": "^1.2.2",
|
|
59
59
|
"@scripts/build": "0.0.0",
|
|
60
60
|
"jest": "^27",
|
|
61
61
|
"@scripts/jest-config": "0.0.0"
|
|
@@ -66,16 +66,15 @@
|
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
|
-
"@modern-js/bff-utils": "^1.2.
|
|
70
|
-
"@modern-js/core": "^1.3
|
|
71
|
-
"@modern-js/server-
|
|
72
|
-
"@modern-js/bff-runtime": "^1.2.
|
|
69
|
+
"@modern-js/bff-utils": "^1.2.2",
|
|
70
|
+
"@modern-js/core": "^1.4.3",
|
|
71
|
+
"@modern-js/server-core": "^1.2.2",
|
|
72
|
+
"@modern-js/bff-runtime": "^1.2.1",
|
|
73
73
|
"koa": "^2.13.3"
|
|
74
74
|
},
|
|
75
75
|
"publishConfig": {
|
|
76
76
|
"registry": "https://registry.npmjs.org/",
|
|
77
|
-
"access": "public"
|
|
78
|
-
"types": "./dist/types/index.d.ts"
|
|
77
|
+
"access": "public"
|
|
79
78
|
},
|
|
80
79
|
"scripts": {
|
|
81
80
|
"new": "modern new",
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import request from 'supertest';
|
|
3
|
-
import { serverManager } from '@modern-js/server-
|
|
3
|
+
import { serverManager } from '@modern-js/server-core';
|
|
4
4
|
import plugin from '../src/plugin';
|
|
5
5
|
import { APIPlugin } from './helpers';
|
|
6
6
|
import './common';
|
package/tests/helpers.ts
CHANGED
package/tests/lambdaMode.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import request from 'supertest';
|
|
3
3
|
import Koa from 'koa';
|
|
4
|
-
import { serverManager } from '@modern-js/server-
|
|
4
|
+
import { serverManager } from '@modern-js/server-core';
|
|
5
5
|
import Router from 'koa-router';
|
|
6
6
|
import koaBody from 'koa-body';
|
|
7
7
|
import plugin from '../src/plugin';
|
package/tests/webServer.test.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as path from 'path';
|
|
2
2
|
import fs from 'fs';
|
|
3
3
|
import request from 'supertest';
|
|
4
|
-
import { serverManager } from '@modern-js/server-
|
|
4
|
+
import { serverManager } from '@modern-js/server-core';
|
|
5
5
|
import plugin from '../src/plugin';
|
|
6
6
|
import { APIPlugin } from './helpers';
|
|
7
7
|
import './common';
|
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
package/src/index.ts
DELETED
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;
|
package/src/registerRoutes.ts
DELETED
|
@@ -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';
|