@innet/server 2.0.0-alpha.1 → 2.0.0-alpha.11
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 +417 -29
- package/example/requests/index.d.ts +1 -1
- package/example/requests/todo/AddTodo/AddTodo.d.ts +1 -0
- package/example/requests/todo/AddTodo/index.d.ts +1 -0
- package/example/requests/todo/DeleteTodo/DeleteTodo.d.ts +1 -0
- package/example/requests/todo/DeleteTodo/index.d.ts +1 -0
- package/example/requests/todo/EditTodo/EditTodo.d.ts +1 -0
- package/example/requests/todo/EditTodo/index.d.ts +1 -0
- package/example/requests/todo/GetTodo/GetTodo.d.ts +1 -0
- package/example/requests/todo/GetTodo/index.d.ts +1 -0
- package/example/requests/todo/GetTodos/GetTodos.d.ts +1 -0
- package/example/requests/todo/GetTodos/index.d.ts +1 -0
- package/example/requests/todo/index.d.ts +5 -0
- package/example/requests/todo/todos.d.ts +1 -0
- package/example/schemas/app/ListQueryParams/ListQueryParams.d.ts +1 -0
- package/example/schemas/app/ListQueryParams/index.d.ts +1 -0
- package/example/schemas/app/ListSchema/ListSchema.d.ts +4 -1
- package/example/schemas/app/index.d.ts +1 -0
- package/example/schemas/index.d.ts +1 -3
- package/example/schemas/todo/TodoSchema/TodoSchema.d.ts +4 -0
- package/example/schemas/todo/TodoSchema/index.d.ts +1 -0
- package/example/schemas/todo/index.d.ts +1 -0
- package/example/tags/Todo/Todo.d.ts +1 -0
- package/example/tags/Todo/index.d.ts +1 -0
- package/example/tags/index.d.ts +1 -1
- package/handler/handler.d.ts +3 -1
- package/handler/handler.es6.js +2 -0
- package/handler/handler.js +2 -0
- package/hooks/index.d.ts +2 -0
- package/hooks/index.es6.js +2 -0
- package/hooks/index.js +2 -0
- package/hooks/useClientIp/index.d.ts +1 -0
- package/hooks/useClientIp/index.es6.js +1 -0
- package/hooks/useClientIp/index.js +9 -0
- package/hooks/useClientIp/useClientIp.d.ts +1 -0
- package/hooks/useClientIp/useClientIp.es6.js +9 -0
- package/hooks/useClientIp/useClientIp.js +13 -0
- package/hooks/useComponentName/useComponentName.d.ts +1 -1
- package/hooks/useRequestPlugin/index.d.ts +1 -0
- package/hooks/useRequestPlugin/index.es6.js +1 -0
- package/hooks/useRequestPlugin/index.js +9 -0
- package/hooks/useRequestPlugin/useRequestPlugin.d.ts +2 -0
- package/hooks/useRequestPlugin/useRequestPlugin.es6.js +13 -0
- package/hooks/useRequestPlugin/useRequestPlugin.js +17 -0
- package/index.es6.js +5 -1
- package/index.js +10 -0
- package/package.json +2 -2
- package/plugins/main/api/api.es6.js +18 -11
- package/plugins/main/api/api.js +18 -11
- package/plugins/main/response/index.es6.js +1 -1
- package/plugins/main/response/index.js +1 -0
- package/plugins/main/response/response.d.ts +5 -2
- package/plugins/main/response/response.es6.js +10 -2
- package/plugins/main/response/response.js +10 -1
- package/plugins/main/server/server.es6.js +4 -1
- package/plugins/main/server/server.js +4 -1
- package/plugins/request/cookie/cookie.es6.js +3 -19
- package/plugins/request/cookie/cookie.js +3 -23
- package/plugins/request/redirect/redirect.d.ts +2 -1
- package/plugins/request/success/success.d.ts +1 -0
- package/plugins/request/success/success.es6.js +13 -4
- package/plugins/request/success/success.js +13 -4
- package/plugins/schema/boolean/boolean.es6.js +2 -4
- package/plugins/schema/boolean/boolean.js +1 -3
- package/plugins/utils/dts/dts.d.ts +1 -2
- package/plugins/utils/dts/dts.es6.js +6 -25
- package/plugins/utils/dts/dts.js +5 -29
- package/plugins/utils/index.d.ts +1 -0
- package/plugins/utils/index.es6.js +1 -0
- package/plugins/utils/index.js +1 -0
- package/plugins/utils/protection/index.d.ts +1 -0
- package/plugins/utils/protection/index.es6.js +1 -0
- package/plugins/utils/protection/index.js +9 -0
- package/plugins/utils/protection/protection.d.ts +8 -0
- package/plugins/utils/protection/protection.es6.js +34 -0
- package/plugins/utils/protection/protection.js +38 -0
- package/plugins/utils/swagger/swagger.es6.js +7 -11
- package/plugins/utils/swagger/swagger.js +7 -11
- package/types.d.ts +2 -3
- package/utils/action/Action.d.ts +7 -4
- package/utils/action/Action.es6.js +25 -3
- package/utils/action/Action.js +26 -4
- package/utils/generateTypes/generateTypes.d.ts +3 -0
- package/utils/generateTypes/generateTypes.es6.js +102 -0
- package/utils/generateTypes/generateTypes.js +107 -0
- package/utils/generateTypes/generateTypes.test.d.ts +1 -0
- package/utils/generateTypes/index.d.ts +1 -0
- package/utils/generateTypes/index.es6.js +1 -0
- package/utils/generateTypes/index.js +10 -0
- package/utils/index.d.ts +1 -0
- package/utils/index.es6.js +1 -0
- package/utils/index.js +1 -0
- package/utils/rules/objectOf/objectOf.es6.js +4 -1
- package/utils/rules/objectOf/objectOf.js +4 -1
- package/example/requests/partners/EditPartner/EditPartner.d.ts +0 -1
- package/example/requests/partners/EditPartner/index.d.ts +0 -1
- package/example/requests/partners/GetPartner/GetPartner.d.ts +0 -1
- package/example/requests/partners/GetPartner/index.d.ts +0 -1
- package/example/requests/partners/GetPartners/GetPartners.d.ts +0 -1
- package/example/requests/partners/GetPartners/index.d.ts +0 -1
- package/example/requests/partners/index.d.ts +0 -3
- package/example/schemas/address/AddressSchema/AddressSchema.d.ts +0 -1
- package/example/schemas/address/AddressSchema/index.d.ts +0 -1
- package/example/schemas/address/index.d.ts +0 -1
- package/example/schemas/location/LocationSchema/LocationSchema.d.ts +0 -1
- package/example/schemas/location/LocationSchema/index.d.ts +0 -1
- package/example/schemas/location/index.d.ts +0 -1
- package/example/schemas/partner/EditPartnerSchema/EditPartnerSchema.d.ts +0 -1
- package/example/schemas/partner/EditPartnerSchema/index.d.ts +0 -1
- package/example/schemas/partner/PartnerSchema/PartnerSchema.d.ts +0 -1
- package/example/schemas/partner/PartnerSchema/index.d.ts +0 -1
- package/example/schemas/partner/index.d.ts +0 -2
- package/example/tags/Partner/Partner.d.ts +0 -1
- package/example/tags/Partner/index.d.ts +0 -1
package/hooks/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useClientIp';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useClientIp } from './useClientIp.es6.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useClientIp(): string | null;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
require('../useAction/index.js');
|
|
6
|
+
var useAction = require('../useAction/useAction.js');
|
|
7
|
+
|
|
8
|
+
function useClientIp() {
|
|
9
|
+
const action = useAction.useAction();
|
|
10
|
+
return action.clientIp;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
exports.useClientIp = useClientIp;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function useComponentName():
|
|
1
|
+
export declare function useComponentName(): string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './useRequestPlugin';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useRequestPlugin } from './useRequestPlugin.es6.js';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { onDestroy } from 'watch-state';
|
|
2
|
+
import '../useApi/index.es6.js';
|
|
3
|
+
import { useApi } from '../useApi/useApi.es6.js';
|
|
4
|
+
|
|
5
|
+
function useRequestPlugin(listener) {
|
|
6
|
+
const { requestPlugins } = useApi();
|
|
7
|
+
requestPlugins.add(listener);
|
|
8
|
+
onDestroy(() => {
|
|
9
|
+
requestPlugins.delete(listener);
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { useRequestPlugin };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var watchState = require('watch-state');
|
|
6
|
+
require('../useApi/index.js');
|
|
7
|
+
var useApi = require('../useApi/useApi.js');
|
|
8
|
+
|
|
9
|
+
function useRequestPlugin(listener) {
|
|
10
|
+
const { requestPlugins } = useApi.useApi();
|
|
11
|
+
requestPlugins.add(listener);
|
|
12
|
+
watchState.onDestroy(() => {
|
|
13
|
+
requestPlugins.delete(listener);
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.useRequestPlugin = useRequestPlugin;
|
package/index.es6.js
CHANGED
|
@@ -12,7 +12,7 @@ export { host } from './plugins/main/host/host.es6.js';
|
|
|
12
12
|
export { variable } from './plugins/main/variable/variable.es6.js';
|
|
13
13
|
export { tag } from './plugins/main/tag/tag.es6.js';
|
|
14
14
|
export { endpoint } from './plugins/main/endpoint/endpoint.es6.js';
|
|
15
|
-
export { response } from './plugins/main/response/response.es6.js';
|
|
15
|
+
export { response, statuses } from './plugins/main/response/response.es6.js';
|
|
16
16
|
export { request } from './plugins/main/request/request.es6.js';
|
|
17
17
|
export { param } from './plugins/main/param/param.es6.js';
|
|
18
18
|
export { body } from './plugins/main/body/body.es6.js';
|
|
@@ -41,6 +41,7 @@ export { swagger } from './plugins/utils/swagger/swagger.es6.js';
|
|
|
41
41
|
export { dts } from './plugins/utils/dts/dts.es6.js';
|
|
42
42
|
export { dev } from './plugins/utils/dev/dev.es6.js';
|
|
43
43
|
export { prod } from './plugins/utils/prod/prod.es6.js';
|
|
44
|
+
export { protection } from './plugins/utils/protection/protection.es6.js';
|
|
44
45
|
export { serverFn } from './plugins/handler/serverFn/serverFn.es6.js';
|
|
45
46
|
export { EMPTY_SEARCH, parseSearch } from './utils/parseSearch/parseSearch.es6.js';
|
|
46
47
|
export { stringifySearch } from './utils/stringifySearch/stringifySearch.es6.js';
|
|
@@ -81,6 +82,7 @@ export { maxBin } from './utils/rules/maxBin/maxBin.es6.js';
|
|
|
81
82
|
export { binaryAccept } from './utils/rules/binaryAccept/binaryAccept.es6.js';
|
|
82
83
|
export { JSONString } from './utils/JSONString/JSONString.es6.js';
|
|
83
84
|
export { Bin } from './utils/FileData/Bin.es6.js';
|
|
85
|
+
export { generateSchemaTypes, generateTypes } from './utils/generateTypes/generateTypes.es6.js';
|
|
84
86
|
export { serverContext, useServer } from './hooks/useServer/useServer.es6.js';
|
|
85
87
|
export { apiContext, useApi } from './hooks/useApi/useApi.es6.js';
|
|
86
88
|
export { hostContext, useHost } from './hooks/useHost/useHost.es6.js';
|
|
@@ -106,3 +108,5 @@ export { ruleContext, useRule, useSetRule } from './hooks/useRule/useRule.es6.js
|
|
|
106
108
|
export { objectRuleContext, useObjectRule } from './hooks/useObjectRule/useObjectRule.es6.js';
|
|
107
109
|
export { bodyFileContext, useBodyFile } from './hooks/useBodyFile/useBodyFile.es6.js';
|
|
108
110
|
export { usePath } from './hooks/usePath/usePath.es6.js';
|
|
111
|
+
export { useRequestPlugin } from './hooks/useRequestPlugin/useRequestPlugin.es6.js';
|
|
112
|
+
export { useClientIp } from './hooks/useClientIp/useClientIp.es6.js';
|
package/index.js
CHANGED
|
@@ -45,6 +45,7 @@ var swagger = require('./plugins/utils/swagger/swagger.js');
|
|
|
45
45
|
var dts = require('./plugins/utils/dts/dts.js');
|
|
46
46
|
var dev = require('./plugins/utils/dev/dev.js');
|
|
47
47
|
var prod = require('./plugins/utils/prod/prod.js');
|
|
48
|
+
var protection = require('./plugins/utils/protection/protection.js');
|
|
48
49
|
var serverFn = require('./plugins/handler/serverFn/serverFn.js');
|
|
49
50
|
var parseSearch = require('./utils/parseSearch/parseSearch.js');
|
|
50
51
|
var stringifySearch = require('./utils/stringifySearch/stringifySearch.js');
|
|
@@ -85,6 +86,7 @@ var maxBin = require('./utils/rules/maxBin/maxBin.js');
|
|
|
85
86
|
var binaryAccept = require('./utils/rules/binaryAccept/binaryAccept.js');
|
|
86
87
|
var JSONString = require('./utils/JSONString/JSONString.js');
|
|
87
88
|
var Bin = require('./utils/FileData/Bin.js');
|
|
89
|
+
var generateTypes = require('./utils/generateTypes/generateTypes.js');
|
|
88
90
|
var useServer = require('./hooks/useServer/useServer.js');
|
|
89
91
|
var useApi = require('./hooks/useApi/useApi.js');
|
|
90
92
|
var useHost = require('./hooks/useHost/useHost.js');
|
|
@@ -110,6 +112,8 @@ var useRule = require('./hooks/useRule/useRule.js');
|
|
|
110
112
|
var useObjectRule = require('./hooks/useObjectRule/useObjectRule.js');
|
|
111
113
|
var useBodyFile = require('./hooks/useBodyFile/useBodyFile.js');
|
|
112
114
|
var usePath = require('./hooks/usePath/usePath.js');
|
|
115
|
+
var useRequestPlugin = require('./hooks/useRequestPlugin/useRequestPlugin.js');
|
|
116
|
+
var useClientIp = require('./hooks/useClientIp/useClientIp.js');
|
|
113
117
|
|
|
114
118
|
|
|
115
119
|
|
|
@@ -129,6 +133,7 @@ exports.variable = variable.variable;
|
|
|
129
133
|
exports.tag = tag.tag;
|
|
130
134
|
exports.endpoint = endpoint.endpoint;
|
|
131
135
|
exports.response = response.response;
|
|
136
|
+
exports.statuses = response.statuses;
|
|
132
137
|
exports.request = request.request;
|
|
133
138
|
exports.param = param.param;
|
|
134
139
|
exports.body = body.body;
|
|
@@ -160,6 +165,7 @@ exports.swagger = swagger.swagger;
|
|
|
160
165
|
exports.dts = dts.dts;
|
|
161
166
|
exports.dev = dev.dev;
|
|
162
167
|
exports.prod = prod.prod;
|
|
168
|
+
exports.protection = protection.protection;
|
|
163
169
|
exports.serverFn = serverFn.serverFn;
|
|
164
170
|
exports.EMPTY_SEARCH = parseSearch.EMPTY_SEARCH;
|
|
165
171
|
exports.parseSearch = parseSearch.parseSearch;
|
|
@@ -203,6 +209,8 @@ exports.maxBin = maxBin.maxBin;
|
|
|
203
209
|
exports.binaryAccept = binaryAccept.binaryAccept;
|
|
204
210
|
exports.JSONString = JSONString.JSONString;
|
|
205
211
|
exports.Bin = Bin.Bin;
|
|
212
|
+
exports.generateSchemaTypes = generateTypes.generateSchemaTypes;
|
|
213
|
+
exports.generateTypes = generateTypes.generateTypes;
|
|
206
214
|
exports.serverContext = useServer.serverContext;
|
|
207
215
|
exports.useServer = useServer.useServer;
|
|
208
216
|
exports.apiContext = useApi.apiContext;
|
|
@@ -243,3 +251,5 @@ exports.useObjectRule = useObjectRule.useObjectRule;
|
|
|
243
251
|
exports.bodyFileContext = useBodyFile.bodyFileContext;
|
|
244
252
|
exports.useBodyFile = useBodyFile.useBodyFile;
|
|
245
253
|
exports.usePath = usePath.usePath;
|
|
254
|
+
exports.useRequestPlugin = useRequestPlugin.useRequestPlugin;
|
|
255
|
+
exports.useClientIp = useClientIp.useClientIp;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@innet/server",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.11",
|
|
4
4
|
"description": "Create server-side application with innet",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"module": "index.es6.js",
|
|
@@ -42,7 +42,6 @@
|
|
|
42
42
|
"@types/multiparty": "^0.0.33",
|
|
43
43
|
"@types/qs": "^6.9.7",
|
|
44
44
|
"cookie": "^0.5.0",
|
|
45
|
-
"dtsgenerator": "^3.18.0",
|
|
46
45
|
"http-proxy": "^1.18.1",
|
|
47
46
|
"innet": "^2.0.0-alpha.3",
|
|
48
47
|
"is-invalid-path": "^1.0.2",
|
|
@@ -50,6 +49,7 @@
|
|
|
50
49
|
"multiparty": "^4.2.3",
|
|
51
50
|
"openapi-types": "^12.1.3",
|
|
52
51
|
"qs": "^6.11.2",
|
|
52
|
+
"request-ip": "^3.3.0",
|
|
53
53
|
"string_decoder": "^1.3.0",
|
|
54
54
|
"tslib": "^2.6.1",
|
|
55
55
|
"uuid": "^9.0.0",
|
|
@@ -7,12 +7,12 @@ import '../../../utils/rules/index.es6.js';
|
|
|
7
7
|
import { useServer } from '../../../hooks/useServer/useServer.es6.js';
|
|
8
8
|
import { apiContext } from '../../../hooks/useApi/useApi.es6.js';
|
|
9
9
|
import { Action } from '../../../utils/action/Action.es6.js';
|
|
10
|
-
import { JSONString } from '../../../utils/JSONString/JSONString.es6.js';
|
|
11
|
-
import { RulesError } from '../../../utils/rules/helpers.es6.js';
|
|
12
10
|
import { responseContext } from '../../../hooks/useResponse/useResponse.es6.js';
|
|
13
11
|
import { requestContext } from '../../../hooks/useRequest/useRequest.es6.js';
|
|
14
|
-
import { paramsContext } from '../../../hooks/useParams/useParams.es6.js';
|
|
15
12
|
import { actionContext } from '../../../hooks/useAction/useAction.es6.js';
|
|
13
|
+
import { JSONString } from '../../../utils/JSONString/JSONString.es6.js';
|
|
14
|
+
import { RulesError } from '../../../utils/rules/helpers.es6.js';
|
|
15
|
+
import { paramsContext } from '../../../hooks/useParams/useParams.es6.js';
|
|
16
16
|
|
|
17
17
|
const api = () => {
|
|
18
18
|
var _a;
|
|
@@ -34,18 +34,29 @@ const api = () => {
|
|
|
34
34
|
var _b, _c, _d, _e, _f, _g;
|
|
35
35
|
if (res.writableEnded)
|
|
36
36
|
return;
|
|
37
|
-
const action = new Action(req);
|
|
37
|
+
const action = new Action(req, res);
|
|
38
38
|
const path = action.parsedUrl.path;
|
|
39
39
|
const url = path.endsWith('/') ? path.slice(0, -1) : path;
|
|
40
|
+
if (!url.startsWith(prefix)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
for (const requestPlugin of requestPlugins) {
|
|
44
|
+
const result = requestPlugin(action);
|
|
45
|
+
if (!result)
|
|
46
|
+
continue;
|
|
47
|
+
const newHandler = Object.create(handler);
|
|
48
|
+
responseContext.set(newHandler, res);
|
|
49
|
+
requestContext.set(newHandler, req);
|
|
50
|
+
actionContext.set(newHandler, action);
|
|
51
|
+
innet(result, newHandler);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
40
54
|
if (url === (prefix || '')) {
|
|
41
55
|
res.setHeader('Content-Type', 'application/json');
|
|
42
56
|
res.write(JSONString(docs));
|
|
43
57
|
res.end();
|
|
44
58
|
return;
|
|
45
59
|
}
|
|
46
|
-
if (!url.startsWith(prefix)) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
60
|
const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
|
|
50
61
|
const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
|
|
51
62
|
const splitPath = rawSplitPath.at(-1) ? rawSplitPath : rawSplitPath.slice(0, -1);
|
|
@@ -154,10 +165,6 @@ const api = () => {
|
|
|
154
165
|
}
|
|
155
166
|
}
|
|
156
167
|
}
|
|
157
|
-
for (const requestPlugin of requestPlugins) {
|
|
158
|
-
if (requestPlugin(req, res))
|
|
159
|
-
return;
|
|
160
|
-
}
|
|
161
168
|
if (context.fallback) {
|
|
162
169
|
const newHandler = Object.create(context.fallback.handler);
|
|
163
170
|
responseContext.set(newHandler, res);
|
package/plugins/main/api/api.js
CHANGED
|
@@ -11,12 +11,12 @@ require('../../../utils/rules/index.js');
|
|
|
11
11
|
var useServer = require('../../../hooks/useServer/useServer.js');
|
|
12
12
|
var useApi = require('../../../hooks/useApi/useApi.js');
|
|
13
13
|
var Action = require('../../../utils/action/Action.js');
|
|
14
|
-
var JSONString = require('../../../utils/JSONString/JSONString.js');
|
|
15
|
-
var helpers = require('../../../utils/rules/helpers.js');
|
|
16
14
|
var useResponse = require('../../../hooks/useResponse/useResponse.js');
|
|
17
15
|
var useRequest = require('../../../hooks/useRequest/useRequest.js');
|
|
18
|
-
var useParams = require('../../../hooks/useParams/useParams.js');
|
|
19
16
|
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
17
|
+
var JSONString = require('../../../utils/JSONString/JSONString.js');
|
|
18
|
+
var helpers = require('../../../utils/rules/helpers.js');
|
|
19
|
+
var useParams = require('../../../hooks/useParams/useParams.js');
|
|
20
20
|
|
|
21
21
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
22
22
|
|
|
@@ -42,18 +42,29 @@ const api = () => {
|
|
|
42
42
|
var _b, _c, _d, _e, _f, _g;
|
|
43
43
|
if (res.writableEnded)
|
|
44
44
|
return;
|
|
45
|
-
const action = new Action.Action(req);
|
|
45
|
+
const action = new Action.Action(req, res);
|
|
46
46
|
const path = action.parsedUrl.path;
|
|
47
47
|
const url = path.endsWith('/') ? path.slice(0, -1) : path;
|
|
48
|
+
if (!url.startsWith(prefix)) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
for (const requestPlugin of requestPlugins) {
|
|
52
|
+
const result = requestPlugin(action);
|
|
53
|
+
if (!result)
|
|
54
|
+
continue;
|
|
55
|
+
const newHandler = Object.create(handler);
|
|
56
|
+
useResponse.responseContext.set(newHandler, res);
|
|
57
|
+
useRequest.requestContext.set(newHandler, req);
|
|
58
|
+
useAction.actionContext.set(newHandler, action);
|
|
59
|
+
innet__default["default"](result, newHandler);
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
48
62
|
if (url === (prefix || '')) {
|
|
49
63
|
res.setHeader('Content-Type', 'application/json');
|
|
50
64
|
res.write(JSONString.JSONString(docs));
|
|
51
65
|
res.end();
|
|
52
66
|
return;
|
|
53
67
|
}
|
|
54
|
-
if (!url.startsWith(prefix)) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
68
|
const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
|
|
58
69
|
const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
|
|
59
70
|
const splitPath = rawSplitPath.at(-1) ? rawSplitPath : rawSplitPath.slice(0, -1);
|
|
@@ -162,10 +173,6 @@ const api = () => {
|
|
|
162
173
|
}
|
|
163
174
|
}
|
|
164
175
|
}
|
|
165
|
-
for (const requestPlugin of requestPlugins) {
|
|
166
|
-
if (requestPlugin(req, res))
|
|
167
|
-
return;
|
|
168
|
-
}
|
|
169
176
|
if (context.fallback) {
|
|
170
177
|
const newHandler = Object.create(context.fallback.handler);
|
|
171
178
|
useResponse.responseContext.set(newHandler, res);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export { response } from './response.es6.js';
|
|
1
|
+
export { response, statuses } from './response.es6.js';
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { type HandlerPlugin } from 'innet';
|
|
2
|
+
import { type ErrorStatuses, type RedirectStatuses, type SuccessStatuses } from '../../request';
|
|
3
|
+
export type StatusKey = ErrorStatuses | RedirectStatuses | SuccessStatuses;
|
|
2
4
|
export interface ResponseProps {
|
|
3
5
|
/**
|
|
4
6
|
* A description of the response.
|
|
5
7
|
* [CommonMark syntax](https://spec.commonmark.org) MAY be used for rich text representation.
|
|
6
8
|
* */
|
|
7
|
-
description
|
|
9
|
+
description?: string;
|
|
8
10
|
/**
|
|
9
11
|
* Any [HTTP status code](https://swagger.io/specification/#http-codes) can be used as the property.
|
|
10
12
|
* To define a range of response codes, this field MAY contain the uppercase wildcard character X.
|
|
11
13
|
* For example, 2XX represents all response codes between [200-299].
|
|
12
14
|
* Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX.
|
|
13
15
|
* */
|
|
14
|
-
status?: number | 'default';
|
|
16
|
+
status?: number | `${1 | 2 | 3 | 4 | 5}XX` | 'default' | StatusKey;
|
|
15
17
|
}
|
|
18
|
+
export declare const statuses: Record<StatusKey, number>;
|
|
16
19
|
export declare const response: HandlerPlugin;
|
|
@@ -2,18 +2,26 @@ import innet, { useNewHandler } from 'innet';
|
|
|
2
2
|
import { useProps, useChildren, useContext } from '@innet/jsx';
|
|
3
3
|
import '../../../hooks/index.es6.js';
|
|
4
4
|
import '../../../utils/index.es6.js';
|
|
5
|
+
import '../../request/index.es6.js';
|
|
6
|
+
import { errorStatuses } from '../../request/error/error.es6.js';
|
|
7
|
+
import { redirectStatuses } from '../../request/redirect/redirect.es6.js';
|
|
8
|
+
import { successStatuses } from '../../request/success/success.es6.js';
|
|
5
9
|
import { useEndpoint, endpointContext } from '../../../hooks/useEndpoint/useEndpoint.es6.js';
|
|
6
10
|
import { useThrow } from '../../../hooks/useThrow/useThrow.es6.js';
|
|
7
11
|
import { schemaContext } from '../../../hooks/useSchemaContext/useSchemaContext.es6.js';
|
|
8
12
|
import { getOrAdd } from '../../../utils/getOrAdd/getOrAdd.es6.js';
|
|
9
13
|
import { ruleContext } from '../../../hooks/useRule/useRule.es6.js';
|
|
10
14
|
|
|
15
|
+
const statuses = Object.assign(Object.assign(Object.assign({}, errorStatuses), redirectStatuses), successStatuses);
|
|
11
16
|
const response = () => {
|
|
12
|
-
|
|
17
|
+
let { description = '', status = 'default' } = useProps() || {};
|
|
13
18
|
const { operation, props: { path } } = useEndpoint();
|
|
14
19
|
const children = useChildren();
|
|
15
20
|
const handler = useNewHandler();
|
|
16
21
|
const endpoint = useContext(endpointContext);
|
|
22
|
+
if (status in statuses) {
|
|
23
|
+
status = statuses[status];
|
|
24
|
+
}
|
|
17
25
|
if (!endpoint) {
|
|
18
26
|
useThrow('<{type}> MUST be placed in <endpoint> element');
|
|
19
27
|
}
|
|
@@ -41,4 +49,4 @@ const response = () => {
|
|
|
41
49
|
innet(children, handler);
|
|
42
50
|
};
|
|
43
51
|
|
|
44
|
-
export { response };
|
|
52
|
+
export { response, statuses };
|
|
@@ -6,6 +6,10 @@ var innet = require('innet');
|
|
|
6
6
|
var jsx = require('@innet/jsx');
|
|
7
7
|
require('../../../hooks/index.js');
|
|
8
8
|
require('../../../utils/index.js');
|
|
9
|
+
require('../../request/index.js');
|
|
10
|
+
var error = require('../../request/error/error.js');
|
|
11
|
+
var redirect = require('../../request/redirect/redirect.js');
|
|
12
|
+
var success = require('../../request/success/success.js');
|
|
9
13
|
var useEndpoint = require('../../../hooks/useEndpoint/useEndpoint.js');
|
|
10
14
|
var useThrow = require('../../../hooks/useThrow/useThrow.js');
|
|
11
15
|
var useSchemaContext = require('../../../hooks/useSchemaContext/useSchemaContext.js');
|
|
@@ -16,12 +20,16 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
16
20
|
|
|
17
21
|
var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
|
|
18
22
|
|
|
23
|
+
const statuses = Object.assign(Object.assign(Object.assign({}, error.errorStatuses), redirect.redirectStatuses), success.successStatuses);
|
|
19
24
|
const response = () => {
|
|
20
|
-
|
|
25
|
+
let { description = '', status = 'default' } = jsx.useProps() || {};
|
|
21
26
|
const { operation, props: { path } } = useEndpoint.useEndpoint();
|
|
22
27
|
const children = jsx.useChildren();
|
|
23
28
|
const handler = innet.useNewHandler();
|
|
24
29
|
const endpoint = jsx.useContext(useEndpoint.endpointContext);
|
|
30
|
+
if (status in statuses) {
|
|
31
|
+
status = statuses[status];
|
|
32
|
+
}
|
|
25
33
|
if (!endpoint) {
|
|
26
34
|
useThrow.useThrow('<{type}> MUST be placed in <endpoint> element');
|
|
27
35
|
}
|
|
@@ -50,3 +58,4 @@ const response = () => {
|
|
|
50
58
|
};
|
|
51
59
|
|
|
52
60
|
exports.response = response;
|
|
61
|
+
exports.statuses = statuses;
|
|
@@ -22,7 +22,10 @@ const server = () => {
|
|
|
22
22
|
cert = fs.readFileSync(cert).toString();
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
catch (_d) {
|
|
25
|
+
catch (_d) {
|
|
26
|
+
key = '';
|
|
27
|
+
cert = '';
|
|
28
|
+
}
|
|
26
29
|
const https = Boolean(key && cert);
|
|
27
30
|
const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, } = props;
|
|
28
31
|
const server = https ? http2.createServer({ key, cert }) : http.createServer();
|
|
@@ -33,7 +33,10 @@ const server = () => {
|
|
|
33
33
|
cert = fs__default["default"].readFileSync(cert).toString();
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
|
-
catch (_d) {
|
|
36
|
+
catch (_d) {
|
|
37
|
+
key = '';
|
|
38
|
+
cert = '';
|
|
39
|
+
}
|
|
37
40
|
const https = Boolean(key && cert);
|
|
38
41
|
const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, } = props;
|
|
39
42
|
const server = https ? http2__default["default"].createServer({ key, cert }) : http__default["default"].createServer();
|
|
@@ -1,28 +1,12 @@
|
|
|
1
1
|
import { __rest } from 'tslib';
|
|
2
2
|
import { useProps } from '@innet/jsx';
|
|
3
|
-
import cookie$1 from 'cookie';
|
|
4
3
|
import '../../../hooks/index.es6.js';
|
|
5
|
-
import {
|
|
6
|
-
import { useThrow } from '../../../hooks/useThrow/useThrow.es6.js';
|
|
4
|
+
import { useAction } from '../../../hooks/useAction/useAction.es6.js';
|
|
7
5
|
|
|
8
6
|
const cookie = () => {
|
|
9
|
-
const
|
|
10
|
-
if (!res) {
|
|
11
|
-
useThrow('<{type}> MUST be in <request> or <fallback>');
|
|
12
|
-
}
|
|
7
|
+
const action = useAction();
|
|
13
8
|
const _a = useProps(), { key, value } = _a, opt = __rest(_a, ["key", "value"]);
|
|
14
|
-
|
|
15
|
-
if (typeof cookies === 'string') {
|
|
16
|
-
cookies = [cookies];
|
|
17
|
-
}
|
|
18
|
-
const normValue = typeof value === 'string' ? cookie$1.serialize(key, value, opt) : `${key}=; max-age=0`;
|
|
19
|
-
if (cookies) {
|
|
20
|
-
cookies.push(normValue);
|
|
21
|
-
}
|
|
22
|
-
else {
|
|
23
|
-
cookies = normValue;
|
|
24
|
-
}
|
|
25
|
-
res.setHeader('Set-Cookie', cookies);
|
|
9
|
+
action.setCookie(key, value, opt);
|
|
26
10
|
};
|
|
27
11
|
|
|
28
12
|
export { cookie };
|
|
@@ -4,33 +4,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
var tslib = require('tslib');
|
|
6
6
|
var jsx = require('@innet/jsx');
|
|
7
|
-
var cookie$1 = require('cookie');
|
|
8
7
|
require('../../../hooks/index.js');
|
|
9
|
-
var
|
|
10
|
-
var useThrow = require('../../../hooks/useThrow/useThrow.js');
|
|
11
|
-
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
|
-
|
|
14
|
-
var cookie__default = /*#__PURE__*/_interopDefaultLegacy(cookie$1);
|
|
8
|
+
var useAction = require('../../../hooks/useAction/useAction.js');
|
|
15
9
|
|
|
16
10
|
const cookie = () => {
|
|
17
|
-
const
|
|
18
|
-
if (!res) {
|
|
19
|
-
useThrow.useThrow('<{type}> MUST be in <request> or <fallback>');
|
|
20
|
-
}
|
|
11
|
+
const action = useAction.useAction();
|
|
21
12
|
const _a = jsx.useProps(), { key, value } = _a, opt = tslib.__rest(_a, ["key", "value"]);
|
|
22
|
-
|
|
23
|
-
if (typeof cookies === 'string') {
|
|
24
|
-
cookies = [cookies];
|
|
25
|
-
}
|
|
26
|
-
const normValue = typeof value === 'string' ? cookie__default["default"].serialize(key, value, opt) : `${key}=; max-age=0`;
|
|
27
|
-
if (cookies) {
|
|
28
|
-
cookies.push(normValue);
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
cookies = normValue;
|
|
32
|
-
}
|
|
33
|
-
res.setHeader('Set-Cookie', cookies);
|
|
13
|
+
action.setCookie(key, value, opt);
|
|
34
14
|
};
|
|
35
15
|
|
|
36
16
|
exports.cookie = cookie;
|
|
@@ -9,9 +9,10 @@ export declare const redirectStatuses: {
|
|
|
9
9
|
temporaryRedirect: number;
|
|
10
10
|
permanentRedirect: number;
|
|
11
11
|
};
|
|
12
|
+
export type RedirectStatuses = keyof typeof redirectStatuses;
|
|
12
13
|
export interface RedirectProps {
|
|
13
14
|
to: string;
|
|
14
15
|
encode?: boolean;
|
|
15
|
-
status?: number |
|
|
16
|
+
status?: number | RedirectStatuses;
|
|
16
17
|
}
|
|
17
18
|
export declare const redirect: HandlerPlugin;
|
|
@@ -17,15 +17,24 @@ const successStatuses = {
|
|
|
17
17
|
};
|
|
18
18
|
const success = () => {
|
|
19
19
|
const children = useChildren();
|
|
20
|
-
const
|
|
20
|
+
const { status, contentType } = useProps() || {};
|
|
21
21
|
const res = useResponse();
|
|
22
|
-
const status = props === null || props === void 0 ? void 0 : props.status;
|
|
23
22
|
if (!res) {
|
|
24
23
|
throw Error('<success> MUST be in <request>');
|
|
25
24
|
}
|
|
26
25
|
res.statusCode = typeof status === 'string' ? successStatuses[status] : status !== null && status !== void 0 ? status : ((children) ? 200 : 204);
|
|
27
|
-
if (children) {
|
|
28
|
-
|
|
26
|
+
if (children === null || children === void 0 ? void 0 : children[0]) {
|
|
27
|
+
const child = children[0];
|
|
28
|
+
const type = contentType || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
|
|
29
|
+
? 'text/plain'
|
|
30
|
+
: 'application/json');
|
|
31
|
+
res.setHeader('Content-Type', type);
|
|
32
|
+
if (type === 'application/json') {
|
|
33
|
+
res.write(JSONString(child));
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
res.write(String(child));
|
|
37
|
+
}
|
|
29
38
|
}
|
|
30
39
|
res.end();
|
|
31
40
|
};
|
|
@@ -21,15 +21,24 @@ const successStatuses = {
|
|
|
21
21
|
};
|
|
22
22
|
const success = () => {
|
|
23
23
|
const children = jsx.useChildren();
|
|
24
|
-
const
|
|
24
|
+
const { status, contentType } = jsx.useProps() || {};
|
|
25
25
|
const res = useResponse.useResponse();
|
|
26
|
-
const status = props === null || props === void 0 ? void 0 : props.status;
|
|
27
26
|
if (!res) {
|
|
28
27
|
throw Error('<success> MUST be in <request>');
|
|
29
28
|
}
|
|
30
29
|
res.statusCode = typeof status === 'string' ? successStatuses[status] : status !== null && status !== void 0 ? status : ((children) ? 200 : 204);
|
|
31
|
-
if (children) {
|
|
32
|
-
|
|
30
|
+
if (children === null || children === void 0 ? void 0 : children[0]) {
|
|
31
|
+
const child = children[0];
|
|
32
|
+
const type = contentType || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
|
|
33
|
+
? 'text/plain'
|
|
34
|
+
: 'application/json');
|
|
35
|
+
res.setHeader('Content-Type', type);
|
|
36
|
+
if (type === 'application/json') {
|
|
37
|
+
res.write(JSONString.JSONString(child));
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
res.write(String(child));
|
|
41
|
+
}
|
|
33
42
|
}
|
|
34
43
|
res.end();
|
|
35
44
|
};
|