@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.
Files changed (95) hide show
  1. package/README.md +457 -367
  2. package/handler/handler.d.ts +3 -3
  3. package/handler/handler.es6.js +2 -2
  4. package/handler/handler.js +2 -2
  5. package/hooks/index.d.ts +2 -1
  6. package/hooks/index.es6.js +2 -1
  7. package/hooks/index.js +2 -1
  8. package/hooks/useAction/useAction.es6.js +1 -1
  9. package/hooks/useAction/useAction.js +1 -1
  10. package/hooks/useApi/useApi.d.ts +1 -3
  11. package/hooks/useEndpoint/useEndpoint.es6.js +3 -1
  12. package/hooks/useEndpoint/useEndpoint.js +3 -1
  13. package/hooks/useRequest/index.es6.js +1 -1
  14. package/hooks/useRequest/index.js +0 -1
  15. package/hooks/useRequest/useRequest.d.ts +1 -4
  16. package/hooks/useRequest/useRequest.es6.js +4 -4
  17. package/hooks/useRequest/useRequest.js +3 -4
  18. package/hooks/useResponse/index.es6.js +1 -1
  19. package/hooks/useResponse/index.js +0 -1
  20. package/hooks/useResponse/useResponse.d.ts +1 -4
  21. package/hooks/useResponse/useResponse.es6.js +4 -4
  22. package/hooks/useResponse/useResponse.js +3 -4
  23. package/hooks/useServer/useServer.d.ts +1 -0
  24. package/hooks/useServer/useServer.es6.js +3 -1
  25. package/hooks/useServer/useServer.js +3 -1
  26. package/hooks/useServerPlugin/index.d.ts +1 -0
  27. package/hooks/useServerPlugin/index.es6.js +1 -0
  28. package/hooks/useServerPlugin/index.js +9 -0
  29. package/hooks/useServerPlugin/useServerPlugin.d.ts +2 -0
  30. package/hooks/useServerPlugin/useServerPlugin.es6.js +13 -0
  31. package/hooks/useServerPlugin/useServerPlugin.js +17 -0
  32. package/hooks/useServerPlugins/index.d.ts +1 -0
  33. package/hooks/useServerPlugins/index.es6.js +1 -0
  34. package/hooks/useServerPlugins/index.js +10 -0
  35. package/hooks/useServerPlugins/useServerPlugins.d.ts +4 -0
  36. package/hooks/useServerPlugins/useServerPlugins.es6.js +14 -0
  37. package/hooks/useServerPlugins/useServerPlugins.js +19 -0
  38. package/index.es6.js +5 -4
  39. package/index.js +7 -6
  40. package/package.json +1 -1
  41. package/plugins/main/api/api.d.ts +2 -0
  42. package/plugins/main/api/api.es6.js +44 -52
  43. package/plugins/main/api/api.js +43 -51
  44. package/plugins/main/endpoint/endpoint.es6.js +4 -1
  45. package/plugins/main/endpoint/endpoint.js +4 -1
  46. package/plugins/main/index.d.ts +1 -1
  47. package/plugins/main/index.es6.js +1 -1
  48. package/plugins/main/index.js +1 -1
  49. package/plugins/main/preset/index.d.ts +1 -0
  50. package/plugins/main/preset/index.es6.js +1 -0
  51. package/plugins/main/{fallback → preset}/index.js +2 -2
  52. package/plugins/main/preset/preset.d.ts +3 -0
  53. package/plugins/main/preset/preset.es6.js +13 -0
  54. package/plugins/main/preset/preset.js +21 -0
  55. package/plugins/main/request/request.es6.js +2 -13
  56. package/plugins/main/request/request.js +2 -13
  57. package/plugins/main/response/response.d.ts +1 -0
  58. package/plugins/main/response/response.es6.js +7 -7
  59. package/plugins/main/response/response.js +7 -7
  60. package/plugins/main/server/server.d.ts +3 -3
  61. package/plugins/main/server/server.es6.js +28 -6
  62. package/plugins/main/server/server.js +28 -6
  63. package/plugins/request/success/success.d.ts +1 -1
  64. package/plugins/request/success/success.es6.js +4 -4
  65. package/plugins/request/success/success.js +4 -4
  66. package/plugins/utils/blacklist/blacklist.es6.js +4 -3
  67. package/plugins/utils/blacklist/blacklist.js +4 -3
  68. package/plugins/utils/dts/dts.d.ts +2 -1
  69. package/plugins/utils/dts/dts.es6.js +2 -2
  70. package/plugins/utils/dts/dts.js +2 -2
  71. package/plugins/utils/protection/protection.es6.js +4 -2
  72. package/plugins/utils/protection/protection.js +4 -2
  73. package/plugins/utils/swagger/swagger.es6.js +5 -3
  74. package/plugins/utils/swagger/swagger.js +5 -3
  75. package/plugins/utils/whitelist/whitelist.es6.js +4 -2
  76. package/plugins/utils/whitelist/whitelist.js +4 -2
  77. package/types.d.ts +2 -9
  78. package/utils/action/Action.es6.js +12 -10
  79. package/utils/action/Action.js +12 -10
  80. package/utils/generateTypes/generateTypes.d.ts +2 -2
  81. package/utils/generateTypes/generateTypes.es6.js +40 -15
  82. package/utils/generateTypes/generateTypes.js +40 -15
  83. package/utils/getEndpoint/getEndpoint.es6.js +2 -2
  84. package/utils/getEndpoint/getEndpoint.js +2 -2
  85. package/hooks/useRequestPlugin/index.d.ts +0 -1
  86. package/hooks/useRequestPlugin/index.es6.js +0 -1
  87. package/hooks/useRequestPlugin/index.js +0 -9
  88. package/hooks/useRequestPlugin/useRequestPlugin.d.ts +0 -2
  89. package/hooks/useRequestPlugin/useRequestPlugin.es6.js +0 -13
  90. package/hooks/useRequestPlugin/useRequestPlugin.js +0 -17
  91. package/plugins/main/fallback/fallback.d.ts +0 -4
  92. package/plugins/main/fallback/fallback.es6.js +0 -19
  93. package/plugins/main/fallback/fallback.js +0 -23
  94. package/plugins/main/fallback/index.d.ts +0 -1
  95. package/plugins/main/fallback/index.es6.js +0 -1
@@ -1,15 +1,12 @@
1
1
  import { __rest, __awaiter } from 'tslib';
2
2
  import innet, { useNewHandler, useApp } from 'innet';
3
- import { onDestroy } from 'watch-state';
4
3
  import '../../../hooks/index.es6.js';
5
4
  import '../../../utils/index.es6.js';
6
5
  import '../../../utils/rules/index.es6.js';
7
- import { useServer } from '../../../hooks/useServer/useServer.es6.js';
6
+ import { serverPlugins } from '../../../hooks/useServerPlugins/useServerPlugins.es6.js';
8
7
  import { apiContext } from '../../../hooks/useApi/useApi.es6.js';
9
- import { Action } from '../../../utils/action/Action.es6.js';
10
- import { responseContext } from '../../../hooks/useResponse/useResponse.es6.js';
11
- import { requestContext } from '../../../hooks/useRequest/useRequest.es6.js';
12
- import { actionContext } from '../../../hooks/useAction/useAction.es6.js';
8
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
9
+ import { useAction, actionContext } from '../../../hooks/useAction/useAction.es6.js';
13
10
  import { JSONString } from '../../../utils/JSONString/JSONString.es6.js';
14
11
  import { RulesError } from '../../../utils/rules/helpers.es6.js';
15
12
  import { paramsContext } from '../../../hooks/useParams/useParams.es6.js';
@@ -18,8 +15,7 @@ const api = () => {
18
15
  var _a;
19
16
  const handler = useNewHandler();
20
17
  const { props = {}, children } = useApp();
21
- const { server } = useServer();
22
- const { prefix = '', title = '' } = props, rest = __rest(props, ["prefix", "title"]);
18
+ const { prefix = '', title = '', include, exclude } = props, rest = __rest(props, ["prefix", "title", "include", "exclude"]);
23
19
  const info = Object.assign(Object.assign({}, rest), { version: (_a = rest.version) !== null && _a !== void 0 ? _a : '0.0.0', title });
24
20
  const endpoints = {};
25
21
  const docs = {
@@ -27,35 +23,35 @@ const api = () => {
27
23
  info,
28
24
  paths: {},
29
25
  };
30
- const requestPlugins = new Set();
31
- const context = { docs, endpoints, prefix, requestPlugins, refRules: {} };
26
+ const plugins = new Set();
27
+ const context = { docs, endpoints, prefix, refRules: {} };
28
+ const condition = (action) => {
29
+ const path = action.parsedUrl.path;
30
+ const url = path.endsWith('/') ? path.slice(0, -1) : path;
31
+ if (!url.startsWith(prefix) || (exclude === null || exclude === void 0 ? void 0 : exclude.test(url))) {
32
+ return false;
33
+ }
34
+ if (include && !include.test(url)) {
35
+ return false;
36
+ }
37
+ return true;
38
+ };
39
+ serverPlugins.set(handler, plugins);
32
40
  apiContext.set(handler, context);
33
- const listener = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
41
+ useServerPlugin(() => __awaiter(void 0, void 0, void 0, function* () {
34
42
  var _b, _c, _d, _e, _f, _g;
35
- if (res.writableEnded)
43
+ const action = useAction();
44
+ if (!condition(action))
36
45
  return;
37
- const action = new Action(req, res);
46
+ const actionHandler = useNewHandler();
38
47
  const path = action.parsedUrl.path;
39
48
  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
- }
49
+ const { req, res } = action;
54
50
  if (url === (prefix || '')) {
55
51
  res.setHeader('Content-Type', 'application/json');
56
52
  res.write(JSONString(docs));
57
53
  res.end();
58
- return;
54
+ return null;
59
55
  }
60
56
  const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
61
57
  const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
@@ -131,26 +127,28 @@ const api = () => {
131
127
  if (checkActionRules(bodyRules, 'body'))
132
128
  return true;
133
129
  }
134
- const newHandler = Object.create(runEndpoint.handler);
135
- responseContext.set(newHandler, res);
136
- requestContext.set(newHandler, req);
137
- paramsContext.set(newHandler, params);
138
- actionContext.set(newHandler, action);
139
- innet(runEndpoint.content, newHandler);
130
+ paramsContext.set(actionHandler, params);
131
+ for (const plugin of runEndpoint.plugins) {
132
+ const result = yield plugin();
133
+ if (result === undefined)
134
+ continue;
135
+ innet(result, actionHandler);
136
+ return true;
137
+ }
140
138
  return true;
141
139
  });
142
140
  }
143
- if ((_e = (_d = currentEndpoint.static) === null || _d === void 0 ? void 0 : _d[key]) === null || _e === void 0 ? void 0 : _e.content) {
141
+ if ((_e = (_d = currentEndpoint.static) === null || _d === void 0 ? void 0 : _d[key]) === null || _e === void 0 ? void 0 : _e.plugins) {
144
142
  if (!(yield run((_f = currentEndpoint.static) === null || _f === void 0 ? void 0 : _f[key], params)))
145
143
  continue;
146
- return;
144
+ return null;
147
145
  }
148
146
  if (currentEndpoint.dynamic) {
149
147
  for (const dynamicEndpoint of currentEndpoint.dynamic) {
150
- if (dynamicEndpoint.content) {
148
+ if (dynamicEndpoint.plugins) {
151
149
  if (!(yield run(dynamicEndpoint, Object.assign(Object.assign({}, params), { [dynamicEndpoint.key.slice(1, -1)]: key }))))
152
150
  continue;
153
- return;
151
+ return null;
154
152
  }
155
153
  }
156
154
  }
@@ -165,22 +163,16 @@ const api = () => {
165
163
  }
166
164
  }
167
165
  }
168
- if (context.fallback) {
169
- const newHandler = Object.create(context.fallback.handler);
170
- responseContext.set(newHandler, res);
171
- requestContext.set(newHandler, req);
166
+ for (const plugin of plugins) {
167
+ const result = yield plugin();
168
+ if (result === undefined)
169
+ continue;
170
+ const newHandler = Object.create(handler);
172
171
  actionContext.set(newHandler, action);
173
- innet(context.fallback.children, newHandler);
174
- }
175
- else {
176
- res.statusCode = 404;
177
- res.end();
172
+ innet(result, newHandler);
173
+ return null;
178
174
  }
179
- });
180
- server.on('request', listener);
181
- onDestroy(() => {
182
- server.off('request', listener);
183
- });
175
+ }));
184
176
  innet(children, handler);
185
177
  };
186
178
 
@@ -4,15 +4,12 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var tslib = require('tslib');
6
6
  var innet = require('innet');
7
- var watchState = require('watch-state');
8
7
  require('../../../hooks/index.js');
9
8
  require('../../../utils/index.js');
10
9
  require('../../../utils/rules/index.js');
11
- var useServer = require('../../../hooks/useServer/useServer.js');
10
+ var useServerPlugins = require('../../../hooks/useServerPlugins/useServerPlugins.js');
12
11
  var useApi = require('../../../hooks/useApi/useApi.js');
13
- var Action = require('../../../utils/action/Action.js');
14
- var useResponse = require('../../../hooks/useResponse/useResponse.js');
15
- var useRequest = require('../../../hooks/useRequest/useRequest.js');
12
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
16
13
  var useAction = require('../../../hooks/useAction/useAction.js');
17
14
  var JSONString = require('../../../utils/JSONString/JSONString.js');
18
15
  var helpers = require('../../../utils/rules/helpers.js');
@@ -26,8 +23,7 @@ const api = () => {
26
23
  var _a;
27
24
  const handler = innet.useNewHandler();
28
25
  const { props = {}, children } = innet.useApp();
29
- const { server } = useServer.useServer();
30
- const { prefix = '', title = '' } = props, rest = tslib.__rest(props, ["prefix", "title"]);
26
+ const { prefix = '', title = '', include, exclude } = props, rest = tslib.__rest(props, ["prefix", "title", "include", "exclude"]);
31
27
  const info = Object.assign(Object.assign({}, rest), { version: (_a = rest.version) !== null && _a !== void 0 ? _a : '0.0.0', title });
32
28
  const endpoints = {};
33
29
  const docs = {
@@ -35,35 +31,35 @@ const api = () => {
35
31
  info,
36
32
  paths: {},
37
33
  };
38
- const requestPlugins = new Set();
39
- const context = { docs, endpoints, prefix, requestPlugins, refRules: {} };
34
+ const plugins = new Set();
35
+ const context = { docs, endpoints, prefix, refRules: {} };
36
+ const condition = (action) => {
37
+ const path = action.parsedUrl.path;
38
+ const url = path.endsWith('/') ? path.slice(0, -1) : path;
39
+ if (!url.startsWith(prefix) || (exclude === null || exclude === void 0 ? void 0 : exclude.test(url))) {
40
+ return false;
41
+ }
42
+ if (include && !include.test(url)) {
43
+ return false;
44
+ }
45
+ return true;
46
+ };
47
+ useServerPlugins.serverPlugins.set(handler, plugins);
40
48
  useApi.apiContext.set(handler, context);
41
- const listener = (req, res) => tslib.__awaiter(void 0, void 0, void 0, function* () {
49
+ useServerPlugin.useServerPlugin(() => tslib.__awaiter(void 0, void 0, void 0, function* () {
42
50
  var _b, _c, _d, _e, _f, _g;
43
- if (res.writableEnded)
51
+ const action = useAction.useAction();
52
+ if (!condition(action))
44
53
  return;
45
- const action = new Action.Action(req, res);
54
+ const actionHandler = innet.useNewHandler();
46
55
  const path = action.parsedUrl.path;
47
56
  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
- }
57
+ const { req, res } = action;
62
58
  if (url === (prefix || '')) {
63
59
  res.setHeader('Content-Type', 'application/json');
64
60
  res.write(JSONString.JSONString(docs));
65
61
  res.end();
66
- return;
62
+ return null;
67
63
  }
68
64
  const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
69
65
  const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
@@ -139,26 +135,28 @@ const api = () => {
139
135
  if (checkActionRules(bodyRules, 'body'))
140
136
  return true;
141
137
  }
142
- const newHandler = Object.create(runEndpoint.handler);
143
- useResponse.responseContext.set(newHandler, res);
144
- useRequest.requestContext.set(newHandler, req);
145
- useParams.paramsContext.set(newHandler, params);
146
- useAction.actionContext.set(newHandler, action);
147
- innet__default["default"](runEndpoint.content, newHandler);
138
+ useParams.paramsContext.set(actionHandler, params);
139
+ for (const plugin of runEndpoint.plugins) {
140
+ const result = yield plugin();
141
+ if (result === undefined)
142
+ continue;
143
+ innet__default["default"](result, actionHandler);
144
+ return true;
145
+ }
148
146
  return true;
149
147
  });
150
148
  }
151
- if ((_e = (_d = currentEndpoint.static) === null || _d === void 0 ? void 0 : _d[key]) === null || _e === void 0 ? void 0 : _e.content) {
149
+ if ((_e = (_d = currentEndpoint.static) === null || _d === void 0 ? void 0 : _d[key]) === null || _e === void 0 ? void 0 : _e.plugins) {
152
150
  if (!(yield run((_f = currentEndpoint.static) === null || _f === void 0 ? void 0 : _f[key], params)))
153
151
  continue;
154
- return;
152
+ return null;
155
153
  }
156
154
  if (currentEndpoint.dynamic) {
157
155
  for (const dynamicEndpoint of currentEndpoint.dynamic) {
158
- if (dynamicEndpoint.content) {
156
+ if (dynamicEndpoint.plugins) {
159
157
  if (!(yield run(dynamicEndpoint, Object.assign(Object.assign({}, params), { [dynamicEndpoint.key.slice(1, -1)]: key }))))
160
158
  continue;
161
- return;
159
+ return null;
162
160
  }
163
161
  }
164
162
  }
@@ -173,22 +171,16 @@ const api = () => {
173
171
  }
174
172
  }
175
173
  }
176
- if (context.fallback) {
177
- const newHandler = Object.create(context.fallback.handler);
178
- useResponse.responseContext.set(newHandler, res);
179
- useRequest.requestContext.set(newHandler, req);
174
+ for (const plugin of plugins) {
175
+ const result = yield plugin();
176
+ if (result === undefined)
177
+ continue;
178
+ const newHandler = Object.create(handler);
180
179
  useAction.actionContext.set(newHandler, action);
181
- innet__default["default"](context.fallback.children, newHandler);
182
- }
183
- else {
184
- res.statusCode = 404;
185
- res.end();
180
+ innet__default["default"](result, newHandler);
181
+ return null;
186
182
  }
187
- });
188
- server.on('request', listener);
189
- watchState.onDestroy(() => {
190
- server.off('request', listener);
191
- });
183
+ }));
192
184
  innet__default["default"](children, handler);
193
185
  };
194
186
 
@@ -6,6 +6,7 @@ import { useTag } from '../../../hooks/useTag/useTag.es6.js';
6
6
  import { useApi } from '../../../hooks/useApi/useApi.es6.js';
7
7
  import { getEndpoint } from '../../../utils/getEndpoint/getEndpoint.es6.js';
8
8
  import { endpointContext } from '../../../hooks/useEndpoint/useEndpoint.es6.js';
9
+ import { serverPlugins } from '../../../hooks/useServerPlugins/useServerPlugins.es6.js';
9
10
 
10
11
  const endpoint = () => {
11
12
  const handler = useNewHandler();
@@ -42,12 +43,14 @@ const endpoint = () => {
42
43
  paths[path][method] = operation;
43
44
  }
44
45
  if (!endpoints[method]) {
45
- endpoints[method] = { key: '' };
46
+ endpoints[method] = { key: '', plugins: new Set() };
46
47
  }
47
48
  // @ts-expect-error: it's always an object
48
49
  const endpoint = getEndpoint(path, endpoints[method]);
49
50
  // @ts-expect-error: it's always an object
50
51
  endpointContext.set(handler, { operation, props, endpoint });
52
+ // @ts-expect-error: it's always an object
53
+ serverPlugins.set(handler, endpoint.plugins);
51
54
  innet(children, handler);
52
55
  };
53
56
 
@@ -10,6 +10,7 @@ var useTag = require('../../../hooks/useTag/useTag.js');
10
10
  var useApi = require('../../../hooks/useApi/useApi.js');
11
11
  var getEndpoint = require('../../../utils/getEndpoint/getEndpoint.js');
12
12
  var useEndpoint = require('../../../hooks/useEndpoint/useEndpoint.js');
13
+ var useServerPlugins = require('../../../hooks/useServerPlugins/useServerPlugins.js');
13
14
 
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
16
 
@@ -50,12 +51,14 @@ const endpoint = () => {
50
51
  paths[path][method] = operation;
51
52
  }
52
53
  if (!endpoints[method]) {
53
- endpoints[method] = { key: '' };
54
+ endpoints[method] = { key: '', plugins: new Set() };
54
55
  }
55
56
  // @ts-expect-error: it's always an object
56
57
  const endpoint = getEndpoint.getEndpoint(path, endpoints[method]);
57
58
  // @ts-expect-error: it's always an object
58
59
  useEndpoint.endpointContext.set(handler, { operation, props, endpoint });
60
+ // @ts-expect-error: it's always an object
61
+ useServerPlugins.serverPlugins.set(handler, endpoint.plugins);
59
62
  innet__default["default"](children, handler);
60
63
  };
61
64
 
@@ -10,4 +10,4 @@ export * from './response';
10
10
  export * from './request';
11
11
  export * from './param';
12
12
  export * from './body';
13
- export * from './fallback';
13
+ export * from './preset';
@@ -10,4 +10,4 @@ import './response/index.es6.js';
10
10
  import './request/index.es6.js';
11
11
  import './param/index.es6.js';
12
12
  import './body/index.es6.js';
13
- import './fallback/index.es6.js';
13
+ import './preset/index.es6.js';
@@ -12,5 +12,5 @@ require('./response/index.js');
12
12
  require('./request/index.js');
13
13
  require('./param/index.js');
14
14
  require('./body/index.js');
15
- require('./fallback/index.js');
15
+ require('./preset/index.js');
16
16
 
@@ -0,0 +1 @@
1
+ export * from './preset';
@@ -0,0 +1 @@
1
+ export { preset } from './preset.es6.js';
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var fallback = require('./fallback.js');
5
+ var preset = require('./preset.js');
6
6
 
7
7
 
8
8
 
9
- exports.fallback = fallback.fallback;
9
+ exports.preset = preset.preset;
@@ -0,0 +1,3 @@
1
+ export interface PresetProps {
2
+ }
3
+ export declare function preset(): void;
@@ -0,0 +1,13 @@
1
+ import innet, { useHandler } from 'innet';
2
+ import { useChildren } from '@innet/jsx';
3
+ import '../../../hooks/index.es6.js';
4
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
5
+
6
+ function preset() {
7
+ const children = useChildren();
8
+ useServerPlugin(() => {
9
+ innet(children, useHandler());
10
+ });
11
+ }
12
+
13
+ export { preset };
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var innet = require('innet');
6
+ var jsx = require('@innet/jsx');
7
+ require('../../../hooks/index.js');
8
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
9
+
10
+ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
11
+
12
+ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
13
+
14
+ function preset() {
15
+ const children = jsx.useChildren();
16
+ useServerPlugin.useServerPlugin(() => {
17
+ innet__default["default"](children, innet.useHandler());
18
+ });
19
+ }
20
+
21
+ exports.preset = preset;
@@ -1,21 +1,10 @@
1
- import { useHandler } from 'innet';
2
1
  import { useChildren } from '@innet/jsx';
3
2
  import '../../../hooks/index.es6.js';
4
- import { useEndpoint } from '../../../hooks/useEndpoint/useEndpoint.es6.js';
3
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
5
4
 
6
5
  const request = () => {
7
- const endpointContext = useEndpoint();
8
- if (!endpointContext) {
9
- throw Error('Use <request> inside <endpoint>');
10
- }
11
6
  const children = useChildren();
12
- const handler = useHandler();
13
- const { endpoint, props } = endpointContext;
14
- if (endpoint.content) {
15
- throw Error(`You cannot use the same endpoints ${props.method}:${props.path}`);
16
- }
17
- endpoint.content = children;
18
- endpoint.handler = handler;
7
+ useServerPlugin(() => children);
19
8
  };
20
9
 
21
10
  export { request };
@@ -2,24 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var innet = require('innet');
6
5
  var jsx = require('@innet/jsx');
7
6
  require('../../../hooks/index.js');
8
- var useEndpoint = require('../../../hooks/useEndpoint/useEndpoint.js');
7
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
9
8
 
10
9
  const request = () => {
11
- const endpointContext = useEndpoint.useEndpoint();
12
- if (!endpointContext) {
13
- throw Error('Use <request> inside <endpoint>');
14
- }
15
10
  const children = jsx.useChildren();
16
- const handler = innet.useHandler();
17
- const { endpoint, props } = endpointContext;
18
- if (endpoint.content) {
19
- throw Error(`You cannot use the same endpoints ${props.method}:${props.path}`);
20
- }
21
- endpoint.content = children;
22
- endpoint.handler = handler;
11
+ useServerPlugin.useServerPlugin(() => children);
23
12
  };
24
13
 
25
14
  exports.request = request;
@@ -14,6 +14,7 @@ export interface ResponseProps {
14
14
  * Only the following range definitions are allowed: 1XX, 2XX, 3XX, 4XX, and 5XX.
15
15
  * */
16
16
  status?: number | `${1 | 2 | 3 | 4 | 5}XX` | 'default' | StatusKey;
17
+ type?: string;
17
18
  }
18
19
  export declare const statuses: Record<StatusKey, number>;
19
20
  export declare const response: HandlerPlugin;
@@ -14,7 +14,8 @@ import { ruleContext } from '../../../hooks/useRule/useRule.es6.js';
14
14
 
15
15
  const statuses = Object.assign(Object.assign(Object.assign({}, errorStatuses), redirectStatuses), successStatuses);
16
16
  const response = () => {
17
- let { description = '', status = 'default' } = useProps() || {};
17
+ var _a;
18
+ let { description = '', status = 'default', type = 'application/json', } = useProps() || {};
18
19
  const { operation, props: { path } } = useEndpoint();
19
20
  const children = useChildren();
20
21
  const handler = useNewHandler();
@@ -28,17 +29,16 @@ const response = () => {
28
29
  if (!operation.responses) {
29
30
  operation.responses = {};
30
31
  }
31
- if (operation.responses[status]) {
32
- throw Error(`status ${status} for '${path}' already used`);
32
+ const defaultResponse = operation.responses[status];
33
+ if ((_a = defaultResponse === null || defaultResponse === void 0 ? void 0 : defaultResponse.content) === null || _a === void 0 ? void 0 : _a[type]) {
34
+ throw Error(`status ${status} and type ${type} for '${path}' already used`);
33
35
  }
34
36
  const schema = {};
35
37
  const response = {
36
38
  description,
37
- content: {
38
- 'application/json': {
39
+ content: Object.assign(Object.assign({}, defaultResponse === null || defaultResponse === void 0 ? void 0 : defaultResponse.content), { [type]: {
39
40
  schema,
40
- },
41
- },
41
+ } }),
42
42
  };
43
43
  operation.responses[status] = response;
44
44
  schemaContext.set(handler, schema);
@@ -22,7 +22,8 @@ var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
22
22
 
23
23
  const statuses = Object.assign(Object.assign(Object.assign({}, error.errorStatuses), redirect.redirectStatuses), success.successStatuses);
24
24
  const response = () => {
25
- let { description = '', status = 'default' } = jsx.useProps() || {};
25
+ var _a;
26
+ let { description = '', status = 'default', type = 'application/json', } = jsx.useProps() || {};
26
27
  const { operation, props: { path } } = useEndpoint.useEndpoint();
27
28
  const children = jsx.useChildren();
28
29
  const handler = innet.useNewHandler();
@@ -36,17 +37,16 @@ const response = () => {
36
37
  if (!operation.responses) {
37
38
  operation.responses = {};
38
39
  }
39
- if (operation.responses[status]) {
40
- throw Error(`status ${status} for '${path}' already used`);
40
+ const defaultResponse = operation.responses[status];
41
+ if ((_a = defaultResponse === null || defaultResponse === void 0 ? void 0 : defaultResponse.content) === null || _a === void 0 ? void 0 : _a[type]) {
42
+ throw Error(`status ${status} and type ${type} for '${path}' already used`);
41
43
  }
42
44
  const schema = {};
43
45
  const response = {
44
46
  description,
45
- content: {
46
- 'application/json': {
47
+ content: Object.assign(Object.assign({}, defaultResponse === null || defaultResponse === void 0 ? void 0 : defaultResponse.content), { [type]: {
47
48
  schema,
48
- },
49
- },
49
+ } }),
50
50
  };
51
51
  operation.responses[status] = response;
52
52
  useSchemaContext.schemaContext.set(handler, schema);
@@ -1,13 +1,13 @@
1
1
  /// <reference types="node" />
2
2
  import { type HandlerPlugin } from 'innet';
3
- import { type ServerResponse } from 'http';
3
+ import { type IncomingMessage, type ServerResponse } from 'http';
4
4
  import { type ServerStartParams, type SSL } from '../../../types';
5
5
  export interface ServerProps {
6
6
  port?: number;
7
7
  ssl?: SSL;
8
8
  onStart?: (params: ServerStartParams) => any;
9
- onRequest?: (req: Request, res: ServerResponse) => any;
9
+ onRequest?: (req: IncomingMessage, res: ServerResponse) => any;
10
10
  onError?: (e: Error) => any;
11
- onDestroy?: () => any;
11
+ onClose?: () => any;
12
12
  }
13
13
  export declare const server: HandlerPlugin;
@@ -1,10 +1,15 @@
1
+ import { __awaiter } from 'tslib';
1
2
  import innet, { useNewHandler, useApp } from 'innet';
2
3
  import fs from 'node:fs';
3
4
  import http from 'node:http';
4
5
  import http2 from 'node:https';
5
6
  import { onDestroy } from 'watch-state';
6
7
  import '../../../hooks/index.es6.js';
8
+ import '../../../utils/index.es6.js';
7
9
  import { serverContext } from '../../../hooks/useServer/useServer.es6.js';
10
+ import { serverPlugins } from '../../../hooks/useServerPlugins/useServerPlugins.es6.js';
11
+ import { Action } from '../../../utils/action/Action.es6.js';
12
+ import { actionContext } from '../../../hooks/useAction/useAction.es6.js';
8
13
 
9
14
  // eslint-disable-next-line @typescript-eslint/no-var-requires
10
15
  const isInvalidPath = require('is-invalid-path');
@@ -27,20 +32,37 @@ const server = () => {
27
32
  cert = '';
28
33
  }
29
34
  const https = Boolean(key && cert);
30
- const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, } = props;
35
+ const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, onClose, } = props;
36
+ const plugins = new Set();
31
37
  const server = https ? http2.createServer({ key, cert }) : http.createServer();
32
- handler[serverContext.key] = { server, port };
38
+ serverContext.set(handler, { server, port });
39
+ serverPlugins.set(handler, plugins);
33
40
  onDestroy(() => {
34
- var _a;
35
- (_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
36
41
  server.close();
37
42
  });
38
43
  if (onError) {
39
44
  server.on('error', onError);
40
45
  }
41
- if (onRequest) {
42
- server.on('request', onRequest);
46
+ if (onClose) {
47
+ server.addListener('close', onClose);
43
48
  }
49
+ server.on('request', (req, res) => {
50
+ onRequest === null || onRequest === void 0 ? void 0 : onRequest(req, res);
51
+ const action = new Action(req, res);
52
+ const requestHandler = Object.create(handler);
53
+ actionContext.set(requestHandler, action);
54
+ function server() {
55
+ return __awaiter(this, void 0, void 0, function* () {
56
+ for (const plugin of plugins) {
57
+ const result = yield plugin();
58
+ if (result !== undefined) {
59
+ return result;
60
+ }
61
+ }
62
+ });
63
+ }
64
+ innet({ type: server, props }, requestHandler);
65
+ });
44
66
  innet(children, handler);
45
67
  server.listen(port, () => {
46
68
  onStart === null || onStart === void 0 ? void 0 : onStart({ port, https });