@innet/server 2.0.0-alpha.12 → 2.0.0-alpha.14
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/README.md +457 -367
- package/handler/handler.d.ts +3 -3
- package/handler/handler.es6.js +2 -2
- package/handler/handler.js +2 -2
- package/hooks/index.d.ts +2 -1
- package/hooks/index.es6.js +2 -1
- package/hooks/index.js +2 -1
- package/hooks/useAction/useAction.es6.js +1 -1
- package/hooks/useAction/useAction.js +1 -1
- package/hooks/useApi/useApi.d.ts +1 -3
- package/hooks/useEndpoint/useEndpoint.es6.js +3 -1
- package/hooks/useEndpoint/useEndpoint.js +3 -1
- package/hooks/useRequest/index.es6.js +1 -1
- package/hooks/useRequest/index.js +0 -1
- package/hooks/useRequest/useRequest.d.ts +1 -4
- package/hooks/useRequest/useRequest.es6.js +4 -4
- package/hooks/useRequest/useRequest.js +3 -4
- package/hooks/useResponse/index.es6.js +1 -1
- package/hooks/useResponse/index.js +0 -1
- package/hooks/useResponse/useResponse.d.ts +1 -4
- package/hooks/useResponse/useResponse.es6.js +4 -4
- package/hooks/useResponse/useResponse.js +3 -4
- package/hooks/useServer/useServer.d.ts +1 -0
- package/hooks/useServer/useServer.es6.js +3 -1
- package/hooks/useServer/useServer.js +3 -1
- package/hooks/useServerPlugin/index.d.ts +1 -0
- package/hooks/useServerPlugin/index.es6.js +1 -0
- package/hooks/useServerPlugin/index.js +9 -0
- package/hooks/useServerPlugin/useServerPlugin.d.ts +2 -0
- package/hooks/useServerPlugin/useServerPlugin.es6.js +13 -0
- package/hooks/useServerPlugin/useServerPlugin.js +17 -0
- package/hooks/useServerPlugins/index.d.ts +1 -0
- package/hooks/useServerPlugins/index.es6.js +1 -0
- package/hooks/useServerPlugins/index.js +10 -0
- package/hooks/useServerPlugins/useServerPlugins.d.ts +4 -0
- package/hooks/useServerPlugins/useServerPlugins.es6.js +14 -0
- package/hooks/useServerPlugins/useServerPlugins.js +19 -0
- package/index.es6.js +5 -4
- package/index.js +7 -6
- package/package.json +1 -1
- package/plugins/main/api/api.d.ts +2 -0
- package/plugins/main/api/api.es6.js +44 -52
- package/plugins/main/api/api.js +43 -51
- package/plugins/main/endpoint/endpoint.es6.js +4 -1
- package/plugins/main/endpoint/endpoint.js +4 -1
- package/plugins/main/index.d.ts +1 -1
- package/plugins/main/index.es6.js +1 -1
- package/plugins/main/index.js +1 -1
- package/plugins/main/preset/index.d.ts +1 -0
- package/plugins/main/preset/index.es6.js +1 -0
- package/plugins/main/{fallback → preset}/index.js +2 -2
- package/plugins/main/preset/preset.d.ts +3 -0
- package/plugins/main/preset/preset.es6.js +13 -0
- package/plugins/main/preset/preset.js +21 -0
- package/plugins/main/request/request.es6.js +2 -13
- package/plugins/main/request/request.js +2 -13
- package/plugins/main/response/response.d.ts +1 -0
- package/plugins/main/response/response.es6.js +7 -7
- package/plugins/main/response/response.js +7 -7
- package/plugins/main/server/server.d.ts +3 -3
- package/plugins/main/server/server.es6.js +28 -6
- package/plugins/main/server/server.js +28 -6
- package/plugins/request/success/success.d.ts +1 -1
- package/plugins/request/success/success.es6.js +4 -4
- package/plugins/request/success/success.js +4 -4
- package/plugins/utils/blacklist/blacklist.es6.js +4 -3
- package/plugins/utils/blacklist/blacklist.js +4 -3
- package/plugins/utils/dts/dts.d.ts +2 -1
- package/plugins/utils/dts/dts.es6.js +2 -2
- package/plugins/utils/dts/dts.js +2 -2
- package/plugins/utils/protection/protection.es6.js +4 -2
- package/plugins/utils/protection/protection.js +4 -2
- package/plugins/utils/swagger/swagger.es6.js +5 -3
- package/plugins/utils/swagger/swagger.js +5 -3
- package/plugins/utils/whitelist/whitelist.es6.js +4 -2
- package/plugins/utils/whitelist/whitelist.js +4 -2
- package/types.d.ts +2 -9
- package/utils/action/Action.es6.js +12 -10
- package/utils/action/Action.js +12 -10
- package/utils/generateTypes/generateTypes.d.ts +2 -2
- package/utils/generateTypes/generateTypes.es6.js +40 -15
- package/utils/generateTypes/generateTypes.js +40 -15
- package/utils/getEndpoint/getEndpoint.es6.js +2 -2
- package/utils/getEndpoint/getEndpoint.js +2 -2
- package/hooks/useRequestPlugin/index.d.ts +0 -1
- package/hooks/useRequestPlugin/index.es6.js +0 -1
- package/hooks/useRequestPlugin/index.js +0 -9
- package/hooks/useRequestPlugin/useRequestPlugin.d.ts +0 -2
- package/hooks/useRequestPlugin/useRequestPlugin.es6.js +0 -13
- package/hooks/useRequestPlugin/useRequestPlugin.js +0 -17
- package/plugins/main/fallback/fallback.d.ts +0 -4
- package/plugins/main/fallback/fallback.es6.js +0 -19
- package/plugins/main/fallback/fallback.js +0 -23
- package/plugins/main/fallback/index.d.ts +0 -1
- package/plugins/main/fallback/index.es6.js +0 -1
|
@@ -2,13 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var tslib = require('tslib');
|
|
5
6
|
var innet = require('innet');
|
|
6
7
|
var fs = require('node:fs');
|
|
7
8
|
var http = require('node:http');
|
|
8
9
|
var http2 = require('node:https');
|
|
9
10
|
var watchState = require('watch-state');
|
|
10
11
|
require('../../../hooks/index.js');
|
|
12
|
+
require('../../../utils/index.js');
|
|
11
13
|
var useServer = require('../../../hooks/useServer/useServer.js');
|
|
14
|
+
var useServerPlugins = require('../../../hooks/useServerPlugins/useServerPlugins.js');
|
|
15
|
+
var Action = require('../../../utils/action/Action.js');
|
|
16
|
+
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
12
17
|
|
|
13
18
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
14
19
|
|
|
@@ -38,20 +43,37 @@ const server = () => {
|
|
|
38
43
|
cert = '';
|
|
39
44
|
}
|
|
40
45
|
const https = Boolean(key && cert);
|
|
41
|
-
const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, } = props;
|
|
46
|
+
const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, onClose, } = props;
|
|
47
|
+
const plugins = new Set();
|
|
42
48
|
const server = https ? http2__default["default"].createServer({ key, cert }) : http__default["default"].createServer();
|
|
43
|
-
|
|
49
|
+
useServer.serverContext.set(handler, { server, port });
|
|
50
|
+
useServerPlugins.serverPlugins.set(handler, plugins);
|
|
44
51
|
watchState.onDestroy(() => {
|
|
45
|
-
var _a;
|
|
46
|
-
(_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
|
|
47
52
|
server.close();
|
|
48
53
|
});
|
|
49
54
|
if (onError) {
|
|
50
55
|
server.on('error', onError);
|
|
51
56
|
}
|
|
52
|
-
if (
|
|
53
|
-
server.
|
|
57
|
+
if (onClose) {
|
|
58
|
+
server.addListener('close', onClose);
|
|
54
59
|
}
|
|
60
|
+
server.on('request', (req, res) => {
|
|
61
|
+
onRequest === null || onRequest === void 0 ? void 0 : onRequest(req, res);
|
|
62
|
+
const action = new Action.Action(req, res);
|
|
63
|
+
const requestHandler = Object.create(handler);
|
|
64
|
+
useAction.actionContext.set(requestHandler, action);
|
|
65
|
+
function server() {
|
|
66
|
+
return tslib.__awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
for (const plugin of plugins) {
|
|
68
|
+
const result = yield plugin();
|
|
69
|
+
if (result !== undefined) {
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
innet__default["default"]({ type: server, props }, requestHandler);
|
|
76
|
+
});
|
|
55
77
|
innet__default["default"](children, handler);
|
|
56
78
|
server.listen(port, () => {
|
|
57
79
|
onStart === null || onStart === void 0 ? void 0 : onStart({ port, https });
|
|
@@ -13,6 +13,6 @@ export declare const successStatuses: {
|
|
|
13
13
|
export type SuccessStatuses = keyof typeof successStatuses;
|
|
14
14
|
export interface SuccessProps {
|
|
15
15
|
status?: SuccessStatuses | number;
|
|
16
|
-
|
|
16
|
+
type?: string;
|
|
17
17
|
}
|
|
18
18
|
export declare const success: HandlerPlugin;
|
|
@@ -17,7 +17,7 @@ const successStatuses = {
|
|
|
17
17
|
};
|
|
18
18
|
const success = () => {
|
|
19
19
|
const children = useChildren();
|
|
20
|
-
const { status,
|
|
20
|
+
const { status, type } = useProps() || {};
|
|
21
21
|
const res = useResponse();
|
|
22
22
|
if (!res) {
|
|
23
23
|
throw Error('<success> MUST be in <request>');
|
|
@@ -25,11 +25,11 @@ const success = () => {
|
|
|
25
25
|
res.statusCode = typeof status === 'string' ? successStatuses[status] : status !== null && status !== void 0 ? status : ((children) ? 200 : 204);
|
|
26
26
|
if (children === null || children === void 0 ? void 0 : children[0]) {
|
|
27
27
|
const child = children[0];
|
|
28
|
-
const
|
|
28
|
+
const contentType = type || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
|
|
29
29
|
? 'text/plain'
|
|
30
30
|
: 'application/json');
|
|
31
|
-
res.setHeader('Content-Type',
|
|
32
|
-
if (
|
|
31
|
+
res.setHeader('Content-Type', contentType);
|
|
32
|
+
if (contentType === 'application/json') {
|
|
33
33
|
res.write(JSONString(child));
|
|
34
34
|
}
|
|
35
35
|
else {
|
|
@@ -21,7 +21,7 @@ const successStatuses = {
|
|
|
21
21
|
};
|
|
22
22
|
const success = () => {
|
|
23
23
|
const children = jsx.useChildren();
|
|
24
|
-
const { status,
|
|
24
|
+
const { status, type } = jsx.useProps() || {};
|
|
25
25
|
const res = useResponse.useResponse();
|
|
26
26
|
if (!res) {
|
|
27
27
|
throw Error('<success> MUST be in <request>');
|
|
@@ -29,11 +29,11 @@ const success = () => {
|
|
|
29
29
|
res.statusCode = typeof status === 'string' ? successStatuses[status] : status !== null && status !== void 0 ? status : ((children) ? 200 : 204);
|
|
30
30
|
if (children === null || children === void 0 ? void 0 : children[0]) {
|
|
31
31
|
const child = children[0];
|
|
32
|
-
const
|
|
32
|
+
const contentType = type || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
|
|
33
33
|
? 'text/plain'
|
|
34
34
|
: 'application/json');
|
|
35
|
-
res.setHeader('Content-Type',
|
|
36
|
-
if (
|
|
35
|
+
res.setHeader('Content-Type', contentType);
|
|
36
|
+
if (contentType === 'application/json') {
|
|
37
37
|
res.write(JSONString.JSONString(child));
|
|
38
38
|
}
|
|
39
39
|
else {
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import { useProps, useChildren } from '@innet/jsx';
|
|
2
2
|
import '../../../hooks/index.es6.js';
|
|
3
|
-
import {
|
|
3
|
+
import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
|
|
4
|
+
import { useAction } from '../../../hooks/useAction/useAction.es6.js';
|
|
4
5
|
|
|
5
6
|
function blacklist() {
|
|
6
7
|
const { ip = process.env.BLACKLIST_IP, } = useProps() || {};
|
|
7
8
|
const children = useChildren();
|
|
8
9
|
const ips = typeof ip === 'string' ? ip.split(',') : ip;
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
useServerPlugin(() => {
|
|
11
|
+
const action = useAction();
|
|
11
12
|
if (!action.clientIp || (ips === null || ips === void 0 ? void 0 : ips.includes(action.clientIp))) {
|
|
12
13
|
return children;
|
|
13
14
|
}
|
|
@@ -4,14 +4,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var jsx = require('@innet/jsx');
|
|
6
6
|
require('../../../hooks/index.js');
|
|
7
|
-
var
|
|
7
|
+
var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
|
|
8
|
+
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
8
9
|
|
|
9
10
|
function blacklist() {
|
|
10
11
|
const { ip = process.env.BLACKLIST_IP, } = jsx.useProps() || {};
|
|
11
12
|
const children = jsx.useChildren();
|
|
12
13
|
const ips = typeof ip === 'string' ? ip.split(',') : ip;
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
useServerPlugin.useServerPlugin(() => {
|
|
15
|
+
const action = useAction.useAction();
|
|
15
16
|
if (!action.clientIp || (ips === null || ips === void 0 ? void 0 : ips.includes(action.clientIp))) {
|
|
16
17
|
return children;
|
|
17
18
|
}
|
|
@@ -6,9 +6,9 @@ import { useApi } from '../../../hooks/useApi/useApi.es6.js';
|
|
|
6
6
|
import { generateTypes } from '../../../utils/generateTypes/generateTypes.es6.js';
|
|
7
7
|
|
|
8
8
|
const dts = () => {
|
|
9
|
-
const { path } = useProps();
|
|
9
|
+
const { path = 'src/api.d.ts', namespace } = useProps() || {};
|
|
10
10
|
const { docs } = useApi();
|
|
11
|
-
promises.writeFile(path, generateTypes(docs)).catch(e => {
|
|
11
|
+
promises.writeFile(path, generateTypes(docs, namespace)).catch(e => {
|
|
12
12
|
console.error(e);
|
|
13
13
|
});
|
|
14
14
|
};
|
package/plugins/utils/dts/dts.js
CHANGED
|
@@ -10,9 +10,9 @@ var useApi = require('../../../hooks/useApi/useApi.js');
|
|
|
10
10
|
var generateTypes = require('../../../utils/generateTypes/generateTypes.js');
|
|
11
11
|
|
|
12
12
|
const dts = () => {
|
|
13
|
-
const { path } = jsx.useProps();
|
|
13
|
+
const { path = 'src/api.d.ts', namespace } = jsx.useProps() || {};
|
|
14
14
|
const { docs } = useApi.useApi();
|
|
15
|
-
fs.promises.writeFile(path, generateTypes.generateTypes(docs)).catch(e => {
|
|
15
|
+
fs.promises.writeFile(path, generateTypes.generateTypes(docs, namespace)).catch(e => {
|
|
16
16
|
console.error(e);
|
|
17
17
|
});
|
|
18
18
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useProps, useChildren } from '@innet/jsx';
|
|
2
2
|
import '../../../hooks/index.es6.js';
|
|
3
|
-
import {
|
|
3
|
+
import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
|
|
4
|
+
import { useAction } from '../../../hooks/useAction/useAction.es6.js';
|
|
4
5
|
|
|
5
6
|
function protection() {
|
|
6
7
|
const { maxAge = Number(process.env.PROTECTION_MAX_AGE) || 365 * 24 * 60 * 60, value = process.env.PROTECTION, excludeIp = process.env.PROTECTED_IP, cookieKey = process.env.PROTECTION_COOKIE_KEY || 'protection', searchKey = process.env.PROTECTION_SEARCH_KEY || 'protection', } = useProps() || {};
|
|
@@ -8,7 +9,8 @@ function protection() {
|
|
|
8
9
|
if (!value)
|
|
9
10
|
return;
|
|
10
11
|
const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
|
|
11
|
-
|
|
12
|
+
useServerPlugin(() => {
|
|
13
|
+
const action = useAction();
|
|
12
14
|
if (!action.clientIp)
|
|
13
15
|
return children;
|
|
14
16
|
if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
|
|
@@ -4,7 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var jsx = require('@innet/jsx');
|
|
6
6
|
require('../../../hooks/index.js');
|
|
7
|
-
var
|
|
7
|
+
var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
|
|
8
|
+
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
8
9
|
|
|
9
10
|
function protection() {
|
|
10
11
|
const { maxAge = Number(process.env.PROTECTION_MAX_AGE) || 365 * 24 * 60 * 60, value = process.env.PROTECTION, excludeIp = process.env.PROTECTED_IP, cookieKey = process.env.PROTECTION_COOKIE_KEY || 'protection', searchKey = process.env.PROTECTION_SEARCH_KEY || 'protection', } = jsx.useProps() || {};
|
|
@@ -12,7 +13,8 @@ function protection() {
|
|
|
12
13
|
if (!value)
|
|
13
14
|
return;
|
|
14
15
|
const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
|
|
15
|
-
|
|
16
|
+
useServerPlugin.useServerPlugin(() => {
|
|
17
|
+
const action = useAction.useAction();
|
|
16
18
|
if (!action.clientIp)
|
|
17
19
|
return children;
|
|
18
20
|
if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
|
|
@@ -2,14 +2,16 @@ import { useProps } from '@innet/jsx';
|
|
|
2
2
|
import '../../../hooks/index.es6.js';
|
|
3
3
|
import html from './swagger.html.es6.js';
|
|
4
4
|
import { useApi } from '../../../hooks/useApi/useApi.es6.js';
|
|
5
|
-
import {
|
|
5
|
+
import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
|
|
6
|
+
import { useAction } from '../../../hooks/useAction/useAction.es6.js';
|
|
6
7
|
|
|
7
8
|
const swagger = () => {
|
|
8
9
|
const { path = '/swagger-ui' } = useProps() || {};
|
|
9
10
|
const { docs, prefix } = useApi();
|
|
10
11
|
let swaggerResponse;
|
|
11
|
-
|
|
12
|
-
|
|
12
|
+
useServerPlugin(() => {
|
|
13
|
+
const action = useAction();
|
|
14
|
+
if (action.path === prefix + path) {
|
|
13
15
|
if (!swaggerResponse) {
|
|
14
16
|
swaggerResponse = html.replace('spec: {},', `spec: ${JSON.stringify(docs)},`);
|
|
15
17
|
}
|
|
@@ -6,14 +6,16 @@ var jsx = require('@innet/jsx');
|
|
|
6
6
|
require('../../../hooks/index.js');
|
|
7
7
|
var swagger$1 = require('./swagger.html.js');
|
|
8
8
|
var useApi = require('../../../hooks/useApi/useApi.js');
|
|
9
|
-
var
|
|
9
|
+
var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
|
|
10
|
+
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
10
11
|
|
|
11
12
|
const swagger = () => {
|
|
12
13
|
const { path = '/swagger-ui' } = jsx.useProps() || {};
|
|
13
14
|
const { docs, prefix } = useApi.useApi();
|
|
14
15
|
let swaggerResponse;
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
useServerPlugin.useServerPlugin(() => {
|
|
17
|
+
const action = useAction.useAction();
|
|
18
|
+
if (action.path === prefix + path) {
|
|
17
19
|
if (!swaggerResponse) {
|
|
18
20
|
swaggerResponse = swagger$1["default"].replace('spec: {},', `spec: ${JSON.stringify(docs)},`);
|
|
19
21
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { useProps, useChildren } from '@innet/jsx';
|
|
2
2
|
import '../../../hooks/index.es6.js';
|
|
3
|
-
import {
|
|
3
|
+
import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
|
|
4
|
+
import { useAction } from '../../../hooks/useAction/useAction.es6.js';
|
|
4
5
|
|
|
5
6
|
function whitelist() {
|
|
6
7
|
const { ip = process.env.WHITELIST_IP, } = useProps() || {};
|
|
@@ -8,7 +9,8 @@ function whitelist() {
|
|
|
8
9
|
const ips = typeof ip === 'string' ? ip.split(',') : ip;
|
|
9
10
|
if (!ips)
|
|
10
11
|
return;
|
|
11
|
-
|
|
12
|
+
useServerPlugin(() => {
|
|
13
|
+
const action = useAction();
|
|
12
14
|
if (!action.clientIp || !ips.includes(action.clientIp)) {
|
|
13
15
|
return children;
|
|
14
16
|
}
|
|
@@ -4,7 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var jsx = require('@innet/jsx');
|
|
6
6
|
require('../../../hooks/index.js');
|
|
7
|
-
var
|
|
7
|
+
var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
|
|
8
|
+
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
8
9
|
|
|
9
10
|
function whitelist() {
|
|
10
11
|
const { ip = process.env.WHITELIST_IP, } = jsx.useProps() || {};
|
|
@@ -12,7 +13,8 @@ function whitelist() {
|
|
|
12
13
|
const ips = typeof ip === 'string' ? ip.split(',') : ip;
|
|
13
14
|
if (!ips)
|
|
14
15
|
return;
|
|
15
|
-
|
|
16
|
+
useServerPlugin.useServerPlugin(() => {
|
|
17
|
+
const action = useAction.useAction();
|
|
16
18
|
if (!action.clientIp || !ips.includes(action.clientIp)) {
|
|
17
19
|
return children;
|
|
18
20
|
}
|
package/types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { Handler } from 'innet';
|
|
2
1
|
import type { OpenAPIV3_1 as API } from 'openapi-types';
|
|
3
2
|
import type { ApiErrorValue } from './constants';
|
|
4
|
-
import { type
|
|
3
|
+
import { type ServerPlugin } from './hooks';
|
|
5
4
|
import { type Rule, type RulesErrors } from './utils/rules';
|
|
6
5
|
export type TagObject = API.TagObject;
|
|
7
6
|
export type Document = API.Document;
|
|
@@ -37,10 +36,9 @@ export interface EndpointRulesMaps {
|
|
|
37
36
|
}
|
|
38
37
|
export interface Endpoint<I = unknown, O = unknown, E extends ApiErrorValue = ApiErrorValue, P extends InValidationErrorParam = InValidationErrorParam, D extends ValidationErrorData = ValidationErrorData> {
|
|
39
38
|
key: string;
|
|
40
|
-
|
|
39
|
+
plugins: Set<ServerPlugin>;
|
|
41
40
|
rules?: EndpointRules;
|
|
42
41
|
rulesMaps?: EndpointRulesMaps;
|
|
43
|
-
handler?: Handler;
|
|
44
42
|
static?: Record<string, Endpoint<I, O, E, P, D>>;
|
|
45
43
|
dynamic?: Endpoint<I, O, E, P, D>[];
|
|
46
44
|
}
|
|
@@ -68,8 +66,3 @@ export interface BaseSchemaProps<T> {
|
|
|
68
66
|
export interface ValuesSchemaProps<T> extends BaseSchemaProps<T> {
|
|
69
67
|
values?: T[];
|
|
70
68
|
}
|
|
71
|
-
export interface Fallback {
|
|
72
|
-
children: any;
|
|
73
|
-
handler: Handler;
|
|
74
|
-
}
|
|
75
|
-
export type RequestPlugin = (action: Action) => any;
|
|
@@ -30,8 +30,8 @@ let Action = (() => {
|
|
|
30
30
|
_Action_cookie.set(this, {});
|
|
31
31
|
}
|
|
32
32
|
get parsedUrl() {
|
|
33
|
-
var
|
|
34
|
-
const match = (
|
|
33
|
+
var _b;
|
|
34
|
+
const match = (_b = this.req.url) === null || _b === void 0 ? void 0 : _b.match(URL_PARSER);
|
|
35
35
|
if (!match)
|
|
36
36
|
throw Error('cannot parse url');
|
|
37
37
|
const result = match.groups;
|
|
@@ -68,8 +68,8 @@ let Action = (() => {
|
|
|
68
68
|
__classPrivateFieldSet(this, _Action_headers, value, "f");
|
|
69
69
|
}
|
|
70
70
|
get originCookies() {
|
|
71
|
-
var
|
|
72
|
-
return cookieLib.parse((
|
|
71
|
+
var _b;
|
|
72
|
+
return cookieLib.parse((_b = this.req.headers.cookie) !== null && _b !== void 0 ? _b : '');
|
|
73
73
|
}
|
|
74
74
|
get cookies() {
|
|
75
75
|
if (__classPrivateFieldGet(this, _Action_cookie, "f"))
|
|
@@ -128,18 +128,20 @@ let Action = (() => {
|
|
|
128
128
|
_Action_headers = new WeakMap(),
|
|
129
129
|
_Action_cookie = new WeakMap(),
|
|
130
130
|
(() => {
|
|
131
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
131
132
|
_get_parsedUrl_decorators = [once];
|
|
132
133
|
_get_originSearch_decorators = [once];
|
|
133
134
|
_get_originCookies_decorators = [once];
|
|
134
135
|
_get_bodyType_decorators = [once];
|
|
135
136
|
_parseBody_decorators = [once];
|
|
136
137
|
_get_clientIp_decorators = [once];
|
|
137
|
-
__esDecorate(_a, null, _get_parsedUrl_decorators, { kind: "getter", name: "parsedUrl", static: false, private: false, access: { has: obj => "parsedUrl" in obj, get: obj => obj.parsedUrl } }, null, _instanceExtraInitializers);
|
|
138
|
-
__esDecorate(_a, null, _get_originSearch_decorators, { kind: "getter", name: "originSearch", static: false, private: false, access: { has: obj => "originSearch" in obj, get: obj => obj.originSearch } }, null, _instanceExtraInitializers);
|
|
139
|
-
__esDecorate(_a, null, _get_originCookies_decorators, { kind: "getter", name: "originCookies", static: false, private: false, access: { has: obj => "originCookies" in obj, get: obj => obj.originCookies } }, null, _instanceExtraInitializers);
|
|
140
|
-
__esDecorate(_a, null, _get_bodyType_decorators, { kind: "getter", name: "bodyType", static: false, private: false, access: { has: obj => "bodyType" in obj, get: obj => obj.bodyType } }, null, _instanceExtraInitializers);
|
|
141
|
-
__esDecorate(_a, null, _parseBody_decorators, { kind: "method", name: "parseBody", static: false, private: false, access: { has: obj => "parseBody" in obj, get: obj => obj.parseBody } }, null, _instanceExtraInitializers);
|
|
142
|
-
__esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp } }, null, _instanceExtraInitializers);
|
|
138
|
+
__esDecorate(_a, null, _get_parsedUrl_decorators, { kind: "getter", name: "parsedUrl", static: false, private: false, access: { has: obj => "parsedUrl" in obj, get: obj => obj.parsedUrl }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
139
|
+
__esDecorate(_a, null, _get_originSearch_decorators, { kind: "getter", name: "originSearch", static: false, private: false, access: { has: obj => "originSearch" in obj, get: obj => obj.originSearch }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
140
|
+
__esDecorate(_a, null, _get_originCookies_decorators, { kind: "getter", name: "originCookies", static: false, private: false, access: { has: obj => "originCookies" in obj, get: obj => obj.originCookies }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
141
|
+
__esDecorate(_a, null, _get_bodyType_decorators, { kind: "getter", name: "bodyType", static: false, private: false, access: { has: obj => "bodyType" in obj, get: obj => obj.bodyType }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
142
|
+
__esDecorate(_a, null, _parseBody_decorators, { kind: "method", name: "parseBody", static: false, private: false, access: { has: obj => "parseBody" in obj, get: obj => obj.parseBody }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
143
|
+
__esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
144
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
143
145
|
})(),
|
|
144
146
|
_a;
|
|
145
147
|
})();
|
package/utils/action/Action.js
CHANGED
|
@@ -38,8 +38,8 @@ let Action = (() => {
|
|
|
38
38
|
_Action_cookie.set(this, {});
|
|
39
39
|
}
|
|
40
40
|
get parsedUrl() {
|
|
41
|
-
var
|
|
42
|
-
const match = (
|
|
41
|
+
var _b;
|
|
42
|
+
const match = (_b = this.req.url) === null || _b === void 0 ? void 0 : _b.match(URL_PARSER);
|
|
43
43
|
if (!match)
|
|
44
44
|
throw Error('cannot parse url');
|
|
45
45
|
const result = match.groups;
|
|
@@ -76,8 +76,8 @@ let Action = (() => {
|
|
|
76
76
|
tslib.__classPrivateFieldSet(this, _Action_headers, value, "f");
|
|
77
77
|
}
|
|
78
78
|
get originCookies() {
|
|
79
|
-
var
|
|
80
|
-
return cookieLib__default["default"].parse((
|
|
79
|
+
var _b;
|
|
80
|
+
return cookieLib__default["default"].parse((_b = this.req.headers.cookie) !== null && _b !== void 0 ? _b : '');
|
|
81
81
|
}
|
|
82
82
|
get cookies() {
|
|
83
83
|
if (tslib.__classPrivateFieldGet(this, _Action_cookie, "f"))
|
|
@@ -136,18 +136,20 @@ let Action = (() => {
|
|
|
136
136
|
_Action_headers = new WeakMap(),
|
|
137
137
|
_Action_cookie = new WeakMap(),
|
|
138
138
|
(() => {
|
|
139
|
+
const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
|
|
139
140
|
_get_parsedUrl_decorators = [once.once];
|
|
140
141
|
_get_originSearch_decorators = [once.once];
|
|
141
142
|
_get_originCookies_decorators = [once.once];
|
|
142
143
|
_get_bodyType_decorators = [once.once];
|
|
143
144
|
_parseBody_decorators = [once.once];
|
|
144
145
|
_get_clientIp_decorators = [once.once];
|
|
145
|
-
tslib.__esDecorate(_a, null, _get_parsedUrl_decorators, { kind: "getter", name: "parsedUrl", static: false, private: false, access: { has: obj => "parsedUrl" in obj, get: obj => obj.parsedUrl } }, null, _instanceExtraInitializers);
|
|
146
|
-
tslib.__esDecorate(_a, null, _get_originSearch_decorators, { kind: "getter", name: "originSearch", static: false, private: false, access: { has: obj => "originSearch" in obj, get: obj => obj.originSearch } }, null, _instanceExtraInitializers);
|
|
147
|
-
tslib.__esDecorate(_a, null, _get_originCookies_decorators, { kind: "getter", name: "originCookies", static: false, private: false, access: { has: obj => "originCookies" in obj, get: obj => obj.originCookies } }, null, _instanceExtraInitializers);
|
|
148
|
-
tslib.__esDecorate(_a, null, _get_bodyType_decorators, { kind: "getter", name: "bodyType", static: false, private: false, access: { has: obj => "bodyType" in obj, get: obj => obj.bodyType } }, null, _instanceExtraInitializers);
|
|
149
|
-
tslib.__esDecorate(_a, null, _parseBody_decorators, { kind: "method", name: "parseBody", static: false, private: false, access: { has: obj => "parseBody" in obj, get: obj => obj.parseBody } }, null, _instanceExtraInitializers);
|
|
150
|
-
tslib.__esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp } }, null, _instanceExtraInitializers);
|
|
146
|
+
tslib.__esDecorate(_a, null, _get_parsedUrl_decorators, { kind: "getter", name: "parsedUrl", static: false, private: false, access: { has: obj => "parsedUrl" in obj, get: obj => obj.parsedUrl }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
147
|
+
tslib.__esDecorate(_a, null, _get_originSearch_decorators, { kind: "getter", name: "originSearch", static: false, private: false, access: { has: obj => "originSearch" in obj, get: obj => obj.originSearch }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
148
|
+
tslib.__esDecorate(_a, null, _get_originCookies_decorators, { kind: "getter", name: "originCookies", static: false, private: false, access: { has: obj => "originCookies" in obj, get: obj => obj.originCookies }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
149
|
+
tslib.__esDecorate(_a, null, _get_bodyType_decorators, { kind: "getter", name: "bodyType", static: false, private: false, access: { has: obj => "bodyType" in obj, get: obj => obj.bodyType }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
150
|
+
tslib.__esDecorate(_a, null, _parseBody_decorators, { kind: "method", name: "parseBody", static: false, private: false, access: { has: obj => "parseBody" in obj, get: obj => obj.parseBody }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
151
|
+
tslib.__esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp }, metadata: _metadata }, null, _instanceExtraInitializers);
|
|
152
|
+
if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
|
|
151
153
|
})(),
|
|
152
154
|
_a;
|
|
153
155
|
})();
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { type Document, type SchemaObject } from '../../types';
|
|
2
|
-
export declare function generateSchemaTypes(schema: SchemaObject, spaces?: number): string;
|
|
3
|
-
export declare function generateTypes(docs: Document): string;
|
|
2
|
+
export declare function generateSchemaTypes(schema: SchemaObject, spaces?: number, lastChar?: string): string;
|
|
3
|
+
export declare function generateTypes(docs: Document, namespace?: string): string;
|
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
function hasDefault(target) {
|
|
2
2
|
return Boolean(target && ('default' in target || 'x-default' in target));
|
|
3
3
|
}
|
|
4
|
-
function generateSchemaTypes(schema, spaces = 2) {
|
|
4
|
+
function generateSchemaTypes(schema, spaces = 2, lastChar = '\n') {
|
|
5
5
|
const space = [...new Array(spaces)].map(() => ' ').join('');
|
|
6
6
|
if ('$ref' in schema) {
|
|
7
|
-
return `Schemas.${schema.$ref.slice(21)}
|
|
7
|
+
return `Schemas.${schema.$ref.slice(21)}${lastChar}`;
|
|
8
8
|
}
|
|
9
9
|
if (schema.type === 'integer') {
|
|
10
|
-
return `${schema.format === 'int64' ? 'bigint' : 'number'}
|
|
10
|
+
return `${schema.format === 'int64' ? 'bigint' : 'number'}${lastChar}`;
|
|
11
11
|
}
|
|
12
12
|
if (schema.type === 'string') {
|
|
13
13
|
if (schema.format === 'date-time') {
|
|
14
|
-
return
|
|
14
|
+
return `Date${lastChar}`;
|
|
15
15
|
}
|
|
16
16
|
if (schema.format === 'binary') {
|
|
17
|
-
return
|
|
17
|
+
return `Bin${lastChar}`;
|
|
18
18
|
}
|
|
19
|
-
return
|
|
19
|
+
return `string${lastChar}`;
|
|
20
20
|
}
|
|
21
21
|
if (['boolean', 'number', 'null'].includes(schema.type)) {
|
|
22
|
-
return `${schema.type}
|
|
22
|
+
return `${schema.type}${lastChar}`;
|
|
23
23
|
}
|
|
24
24
|
if (schema.type === 'array') {
|
|
25
25
|
if (!schema.items)
|
|
26
|
-
return
|
|
27
|
-
return `
|
|
26
|
+
return `any[]${lastChar}`;
|
|
27
|
+
return `Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>${lastChar}`;
|
|
28
28
|
}
|
|
29
29
|
if (schema.type !== 'object') {
|
|
30
30
|
console.error('unknown type', schema);
|
|
31
|
-
return
|
|
31
|
+
return `any${lastChar}`;
|
|
32
32
|
}
|
|
33
33
|
let result = '{\n';
|
|
34
34
|
const required = schema.required || [];
|
|
@@ -39,11 +39,22 @@ function generateSchemaTypes(schema, spaces = 2) {
|
|
|
39
39
|
: '?:';
|
|
40
40
|
result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
|
|
41
41
|
}
|
|
42
|
-
return `${result}${space.slice(0, -2)}}
|
|
42
|
+
return `${result}${space.slice(0, -2)}}${lastChar}`;
|
|
43
43
|
}
|
|
44
|
-
function generateTypes(docs) {
|
|
44
|
+
function generateTypes(docs, namespace = 'Api') {
|
|
45
45
|
var _a;
|
|
46
|
-
let result =
|
|
46
|
+
let result = `declare namespace ${namespace} {
|
|
47
|
+
export interface Bin {
|
|
48
|
+
filename: string
|
|
49
|
+
fieldName: string
|
|
50
|
+
originalFilename: string
|
|
51
|
+
path: string
|
|
52
|
+
type: string
|
|
53
|
+
disposition: string
|
|
54
|
+
size: number
|
|
55
|
+
extension?: string
|
|
56
|
+
}
|
|
57
|
+
`;
|
|
47
58
|
const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
|
|
48
59
|
const paths = docs.paths;
|
|
49
60
|
if (schemas) {
|
|
@@ -90,8 +101,22 @@ function generateTypes(docs) {
|
|
|
90
101
|
if (requestBody) {
|
|
91
102
|
result += ` Body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
|
|
92
103
|
}
|
|
93
|
-
if (responses
|
|
94
|
-
result +=
|
|
104
|
+
if (responses) {
|
|
105
|
+
result += ' Response: {\n';
|
|
106
|
+
for (const key in responses) {
|
|
107
|
+
let multiple = false;
|
|
108
|
+
const response = responses[key];
|
|
109
|
+
result += ` ['${key}']: `;
|
|
110
|
+
for (const type in response.content) {
|
|
111
|
+
if (multiple) {
|
|
112
|
+
result += ' | ';
|
|
113
|
+
}
|
|
114
|
+
result += generateSchemaTypes(response.content[type].schema, 10, '');
|
|
115
|
+
multiple = true;
|
|
116
|
+
}
|
|
117
|
+
result += '\n';
|
|
118
|
+
}
|
|
119
|
+
result += ' }\n';
|
|
95
120
|
}
|
|
96
121
|
result += ' }\n';
|
|
97
122
|
}
|