@innet/server 2.0.0-alpha.2 → 2.0.0-alpha.20

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 (208) hide show
  1. package/README.md +1105 -403
  2. package/example/requests/index.d.ts +1 -1
  3. package/example/requests/todo/AddTodo/AddTodo.d.ts +1 -0
  4. package/example/requests/todo/AddTodo/index.d.ts +1 -0
  5. package/example/requests/todo/DeleteTodo/DeleteTodo.d.ts +1 -0
  6. package/example/requests/todo/DeleteTodo/index.d.ts +1 -0
  7. package/example/requests/todo/EditTodo/EditTodo.d.ts +1 -0
  8. package/example/requests/todo/EditTodo/index.d.ts +1 -0
  9. package/example/requests/todo/GetTodo/GetTodo.d.ts +1 -0
  10. package/example/requests/todo/GetTodo/index.d.ts +1 -0
  11. package/example/requests/todo/GetTodos/GetTodos.d.ts +1 -0
  12. package/example/requests/todo/GetTodos/index.d.ts +1 -0
  13. package/example/requests/todo/index.d.ts +5 -0
  14. package/example/requests/todo/todos.d.ts +1 -0
  15. package/example/schemas/app/ListQueryParams/ListQueryParams.d.ts +1 -0
  16. package/example/schemas/app/ListQueryParams/index.d.ts +1 -0
  17. package/example/schemas/app/ListSchema/ListSchema.d.ts +4 -1
  18. package/example/schemas/app/index.d.ts +1 -0
  19. package/example/schemas/index.d.ts +1 -3
  20. package/example/schemas/todo/TodoSchema/TodoSchema.d.ts +4 -0
  21. package/example/schemas/todo/TodoSchema/index.d.ts +1 -0
  22. package/example/schemas/todo/index.d.ts +1 -0
  23. package/example/tags/Todo/Todo.d.ts +1 -0
  24. package/example/tags/Todo/index.d.ts +1 -0
  25. package/example/tags/index.d.ts +1 -1
  26. package/handler/handler.d.ts +11 -3
  27. package/handler/handler.es6.js +10 -2
  28. package/handler/handler.js +10 -2
  29. package/hooks/index.d.ts +4 -0
  30. package/hooks/index.es6.js +4 -0
  31. package/hooks/index.js +4 -0
  32. package/hooks/useAction/useAction.es6.js +1 -1
  33. package/hooks/useAction/useAction.js +1 -1
  34. package/hooks/useApi/useApi.d.ts +1 -3
  35. package/hooks/useClientIp/index.d.ts +1 -0
  36. package/hooks/useClientIp/index.es6.js +1 -0
  37. package/hooks/useClientIp/index.js +9 -0
  38. package/hooks/useClientIp/useClientIp.d.ts +1 -0
  39. package/hooks/useClientIp/useClientIp.es6.js +9 -0
  40. package/hooks/useClientIp/useClientIp.js +13 -0
  41. package/hooks/useComponentName/useComponentName.d.ts +1 -1
  42. package/hooks/useEndpoint/useEndpoint.es6.js +3 -1
  43. package/hooks/useEndpoint/useEndpoint.js +3 -1
  44. package/hooks/useObjectSchemaContext/index.d.ts +1 -0
  45. package/hooks/useObjectSchemaContext/index.es6.js +1 -0
  46. package/hooks/useObjectSchemaContext/index.js +10 -0
  47. package/hooks/useObjectSchemaContext/useObjectSchemaContext.d.ts +3 -0
  48. package/hooks/useObjectSchemaContext/useObjectSchemaContext.es6.js +14 -0
  49. package/hooks/useObjectSchemaContext/useObjectSchemaContext.js +19 -0
  50. package/hooks/useRequest/index.es6.js +1 -1
  51. package/hooks/useRequest/index.js +0 -1
  52. package/hooks/useRequest/useRequest.d.ts +1 -4
  53. package/hooks/useRequest/useRequest.es6.js +4 -4
  54. package/hooks/useRequest/useRequest.js +3 -4
  55. package/hooks/useResponse/index.es6.js +1 -1
  56. package/hooks/useResponse/index.js +0 -1
  57. package/hooks/useResponse/useResponse.d.ts +1 -4
  58. package/hooks/useResponse/useResponse.es6.js +4 -4
  59. package/hooks/useResponse/useResponse.js +3 -4
  60. package/hooks/useSchemaType/useSchemaType.d.ts +3 -2
  61. package/hooks/useSchemaType/useSchemaType.es6.js +2 -3
  62. package/hooks/useSchemaType/useSchemaType.js +2 -3
  63. package/hooks/useServer/useServer.d.ts +1 -0
  64. package/hooks/useServer/useServer.es6.js +3 -1
  65. package/hooks/useServer/useServer.js +3 -1
  66. package/hooks/useServerPlugin/index.d.ts +1 -0
  67. package/hooks/useServerPlugin/index.es6.js +1 -0
  68. package/hooks/useServerPlugin/index.js +9 -0
  69. package/hooks/useServerPlugin/useServerPlugin.d.ts +2 -0
  70. package/hooks/useServerPlugin/useServerPlugin.es6.js +13 -0
  71. package/hooks/useServerPlugin/useServerPlugin.js +17 -0
  72. package/hooks/useServerPlugins/index.d.ts +1 -0
  73. package/hooks/useServerPlugins/index.es6.js +1 -0
  74. package/hooks/useServerPlugins/index.js +10 -0
  75. package/hooks/useServerPlugins/useServerPlugins.d.ts +4 -0
  76. package/hooks/useServerPlugins/useServerPlugins.es6.js +14 -0
  77. package/hooks/useServerPlugins/useServerPlugins.js +19 -0
  78. package/index.es6.js +13 -5
  79. package/index.js +24 -6
  80. package/package.json +2 -2
  81. package/plugins/main/api/api.d.ts +2 -0
  82. package/plugins/main/api/api.es6.js +49 -51
  83. package/plugins/main/api/api.js +49 -51
  84. package/plugins/main/endpoint/endpoint.es6.js +4 -1
  85. package/plugins/main/endpoint/endpoint.js +4 -1
  86. package/plugins/main/index.d.ts +1 -1
  87. package/plugins/main/index.es6.js +1 -1
  88. package/plugins/main/index.js +1 -1
  89. package/plugins/main/preset/index.d.ts +1 -0
  90. package/plugins/main/preset/index.es6.js +1 -0
  91. package/{utils/rules/any/any.js → plugins/main/preset/index.js} +4 -4
  92. package/plugins/main/preset/preset.d.ts +3 -0
  93. package/plugins/main/preset/preset.es6.js +13 -0
  94. package/plugins/main/preset/preset.js +21 -0
  95. package/plugins/main/request/request.es6.js +2 -13
  96. package/plugins/main/request/request.js +2 -13
  97. package/plugins/main/response/index.es6.js +1 -1
  98. package/plugins/main/response/index.js +1 -0
  99. package/plugins/main/response/response.d.ts +6 -2
  100. package/plugins/main/response/response.es6.js +16 -8
  101. package/plugins/main/response/response.js +16 -7
  102. package/plugins/main/server/server.d.ts +3 -3
  103. package/plugins/main/server/server.es6.js +29 -7
  104. package/plugins/main/server/server.js +29 -7
  105. package/plugins/main/tag/tag.es6.js +4 -1
  106. package/plugins/main/tag/tag.js +4 -1
  107. package/plugins/request/cms/cms.es6.js +1 -1
  108. package/plugins/request/cms/cms.js +1 -1
  109. package/plugins/request/cookie/cookie.es6.js +3 -19
  110. package/plugins/request/cookie/cookie.js +3 -23
  111. package/plugins/request/proxy/proxy.d.ts +3 -0
  112. package/plugins/request/proxy/proxy.es6.js +4 -1
  113. package/plugins/request/proxy/proxy.js +4 -1
  114. package/plugins/request/redirect/redirect.d.ts +2 -1
  115. package/plugins/request/success/success.d.ts +1 -0
  116. package/plugins/request/success/success.es6.js +13 -4
  117. package/plugins/request/success/success.js +13 -4
  118. package/plugins/schema/any/any.d.ts +5 -0
  119. package/plugins/schema/any/any.es6.js +27 -0
  120. package/plugins/schema/any/any.js +31 -0
  121. package/plugins/schema/boolean/boolean.es6.js +2 -4
  122. package/plugins/schema/boolean/boolean.js +1 -3
  123. package/plugins/schema/field/field.es6.js +3 -5
  124. package/plugins/schema/field/field.js +2 -4
  125. package/plugins/schema/index.d.ts +1 -0
  126. package/plugins/schema/index.es6.js +1 -0
  127. package/plugins/schema/index.js +1 -0
  128. package/plugins/schema/object/object.es6.js +17 -4
  129. package/plugins/schema/object/object.js +17 -4
  130. package/plugins/utils/blacklist/blacklist.d.ts +4 -0
  131. package/plugins/utils/blacklist/blacklist.es6.js +18 -0
  132. package/plugins/utils/blacklist/blacklist.js +22 -0
  133. package/plugins/utils/blacklist/index.d.ts +1 -0
  134. package/plugins/utils/blacklist/index.es6.js +1 -0
  135. package/plugins/utils/blacklist/index.js +9 -0
  136. package/plugins/utils/dts/dts.d.ts +3 -3
  137. package/plugins/utils/dts/dts.es6.js +6 -25
  138. package/plugins/utils/dts/dts.js +5 -29
  139. package/plugins/utils/index.d.ts +3 -0
  140. package/plugins/utils/index.es6.js +3 -0
  141. package/plugins/utils/index.js +3 -0
  142. package/plugins/utils/protection/index.d.ts +1 -0
  143. package/plugins/utils/protection/index.es6.js +1 -0
  144. package/plugins/utils/protection/index.js +9 -0
  145. package/plugins/utils/protection/protection.d.ts +8 -0
  146. package/plugins/utils/protection/protection.es6.js +36 -0
  147. package/plugins/utils/protection/protection.js +40 -0
  148. package/plugins/utils/swagger/swagger.es6.js +10 -12
  149. package/plugins/utils/swagger/swagger.js +10 -12
  150. package/plugins/utils/whitelist/index.d.ts +1 -0
  151. package/plugins/utils/whitelist/index.es6.js +1 -0
  152. package/plugins/utils/whitelist/index.js +9 -0
  153. package/plugins/utils/whitelist/whitelist.d.ts +4 -0
  154. package/plugins/utils/whitelist/whitelist.es6.js +20 -0
  155. package/plugins/utils/whitelist/whitelist.js +24 -0
  156. package/types.d.ts +2 -10
  157. package/utils/action/Action.d.ts +7 -4
  158. package/utils/action/Action.es6.js +35 -11
  159. package/utils/action/Action.js +36 -12
  160. package/utils/generateTypes/generateTypes.d.ts +3 -0
  161. package/utils/generateTypes/generateTypes.es6.js +151 -0
  162. package/utils/generateTypes/generateTypes.js +156 -0
  163. package/utils/generateTypes/generateTypes.test.d.ts +1 -0
  164. package/utils/generateTypes/index.d.ts +1 -0
  165. package/utils/generateTypes/index.es6.js +1 -0
  166. package/utils/generateTypes/index.js +10 -0
  167. package/utils/getEndpoint/getEndpoint.es6.js +2 -2
  168. package/utils/getEndpoint/getEndpoint.js +2 -2
  169. package/utils/index.d.ts +1 -0
  170. package/utils/index.es6.js +1 -0
  171. package/utils/index.js +1 -0
  172. package/utils/rules/index.d.ts +0 -1
  173. package/utils/rules/index.es6.js +0 -1
  174. package/utils/rules/index.js +0 -1
  175. package/utils/rules/objectOf/objectOf.d.ts +1 -1
  176. package/utils/rules/objectOf/objectOf.es6.js +15 -2
  177. package/utils/rules/objectOf/objectOf.js +15 -2
  178. package/example/requests/partners/EditPartner/EditPartner.d.ts +0 -1
  179. package/example/requests/partners/EditPartner/index.d.ts +0 -1
  180. package/example/requests/partners/GetPartner/GetPartner.d.ts +0 -1
  181. package/example/requests/partners/GetPartner/index.d.ts +0 -1
  182. package/example/requests/partners/GetPartners/GetPartners.d.ts +0 -1
  183. package/example/requests/partners/GetPartners/index.d.ts +0 -1
  184. package/example/requests/partners/index.d.ts +0 -3
  185. package/example/schemas/address/AddressSchema/AddressSchema.d.ts +0 -1
  186. package/example/schemas/address/AddressSchema/index.d.ts +0 -1
  187. package/example/schemas/address/index.d.ts +0 -1
  188. package/example/schemas/location/LocationSchema/LocationSchema.d.ts +0 -1
  189. package/example/schemas/location/LocationSchema/index.d.ts +0 -1
  190. package/example/schemas/location/index.d.ts +0 -1
  191. package/example/schemas/partner/EditPartnerSchema/EditPartnerSchema.d.ts +0 -1
  192. package/example/schemas/partner/EditPartnerSchema/index.d.ts +0 -1
  193. package/example/schemas/partner/PartnerSchema/PartnerSchema.d.ts +0 -1
  194. package/example/schemas/partner/PartnerSchema/index.d.ts +0 -1
  195. package/example/schemas/partner/index.d.ts +0 -2
  196. package/example/tags/Partner/Partner.d.ts +0 -1
  197. package/example/tags/Partner/index.d.ts +0 -1
  198. package/plugins/main/fallback/fallback.d.ts +0 -4
  199. package/plugins/main/fallback/fallback.es6.js +0 -19
  200. package/plugins/main/fallback/fallback.js +0 -23
  201. package/plugins/main/fallback/index.d.ts +0 -1
  202. package/plugins/main/fallback/index.es6.js +0 -1
  203. package/plugins/main/fallback/index.js +0 -9
  204. package/utils/rules/any/any.d.ts +0 -1
  205. package/utils/rules/any/any.es6.js +0 -5
  206. /package/{utils/rules → plugins/schema}/any/index.d.ts +0 -0
  207. /package/{utils/rules → plugins/schema}/any/index.es6.js +0 -0
  208. /package/{utils/rules → plugins/schema}/any/index.js +0 -0
@@ -4,57 +4,63 @@ 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');
12
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
13
+ var useAction = require('../../../hooks/useAction/useAction.js');
14
14
  var JSONString = require('../../../utils/JSONString/JSONString.js');
15
15
  var helpers = require('../../../utils/rules/helpers.js');
16
- var useResponse = require('../../../hooks/useResponse/useResponse.js');
17
- var useRequest = require('../../../hooks/useRequest/useRequest.js');
18
16
  var useParams = require('../../../hooks/useParams/useParams.js');
19
- var useAction = require('../../../hooks/useAction/useAction.js');
20
17
 
21
18
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
22
19
 
23
20
  var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
24
21
 
25
22
  const api = () => {
26
- var _a;
27
23
  const handler = innet.useNewHandler();
28
24
  const { props = {}, children } = innet.useApp();
29
- const { server } = useServer.useServer();
30
- const { prefix = '', title = '' } = props, rest = tslib.__rest(props, ["prefix", "title"]);
31
- const info = Object.assign(Object.assign({}, rest), { version: (_a = rest.version) !== null && _a !== void 0 ? _a : '0.0.0', title });
25
+ const { prefix = '', title = '', include, exclude, version = process.env.INNET_API_VERSION || '0.0.0' } = props, rest = tslib.__rest(props, ["prefix", "title", "include", "exclude", "version"]);
26
+ const info = Object.assign(Object.assign({}, rest), { version, title });
32
27
  const endpoints = {};
33
28
  const docs = {
34
29
  openapi: '3.1.0',
35
30
  info,
36
31
  paths: {},
37
32
  };
38
- const requestPlugins = new Set();
39
- const context = { docs, endpoints, prefix, requestPlugins, refRules: {} };
33
+ const plugins = new Set();
34
+ const context = { docs, endpoints, prefix, refRules: {} };
35
+ const condition = (action) => {
36
+ const path = action.parsedUrl.path;
37
+ const url = path.endsWith('/') ? path.slice(0, -1) : path;
38
+ if (!url.startsWith(prefix) || (exclude === null || exclude === void 0 ? void 0 : exclude.test(url))) {
39
+ return false;
40
+ }
41
+ if (include && !include.test(url)) {
42
+ return false;
43
+ }
44
+ return true;
45
+ };
46
+ useServerPlugins.serverPlugins.set(handler, plugins);
40
47
  useApi.apiContext.set(handler, context);
41
- const listener = (req, res) => tslib.__awaiter(void 0, void 0, void 0, function* () {
42
- var _b, _c, _d, _e, _f, _g;
43
- if (res.writableEnded)
48
+ useServerPlugin.useServerPlugin(() => tslib.__awaiter(void 0, void 0, void 0, function* () {
49
+ var _a, _b, _c, _d, _e, _f;
50
+ const action = useAction.useAction();
51
+ if (!condition(action))
44
52
  return;
45
- const action = new Action.Action(req);
53
+ const actionHandler = innet.useNewHandler();
46
54
  const path = action.parsedUrl.path;
47
55
  const url = path.endsWith('/') ? path.slice(0, -1) : path;
56
+ const { req, res } = action;
48
57
  if (url === (prefix || '')) {
49
58
  res.setHeader('Content-Type', 'application/json');
50
59
  res.write(JSONString.JSONString(docs));
51
60
  res.end();
52
- return;
61
+ return null;
53
62
  }
54
- if (!url.startsWith(prefix)) {
55
- return;
56
- }
57
- const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
63
+ const method = ((_b = (_a = req.method) === null || _a === void 0 ? void 0 : _a.toLowerCase()) !== null && _b !== void 0 ? _b : 'get');
58
64
  const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
59
65
  const splitPath = rawSplitPath.at(-1) ? rawSplitPath : rawSplitPath.slice(0, -1);
60
66
  const endpoint = endpoints[method];
@@ -128,32 +134,34 @@ const api = () => {
128
134
  if (checkActionRules(bodyRules, 'body'))
129
135
  return true;
130
136
  }
131
- const newHandler = Object.create(runEndpoint.handler);
132
- useResponse.responseContext.set(newHandler, res);
133
- useRequest.requestContext.set(newHandler, req);
134
- useParams.paramsContext.set(newHandler, params);
135
- useAction.actionContext.set(newHandler, action);
136
- innet__default["default"](runEndpoint.content, newHandler);
137
+ useParams.paramsContext.set(actionHandler, params);
138
+ for (const plugin of runEndpoint.plugins) {
139
+ const result = yield plugin();
140
+ if (result === undefined)
141
+ continue;
142
+ innet__default["default"](result, actionHandler);
143
+ return true;
144
+ }
137
145
  return true;
138
146
  });
139
147
  }
140
- if ((_e = (_d = currentEndpoint.static) === null || _d === void 0 ? void 0 : _d[key]) === null || _e === void 0 ? void 0 : _e.content) {
141
- if (!(yield run((_f = currentEndpoint.static) === null || _f === void 0 ? void 0 : _f[key], params)))
148
+ if ((_d = (_c = currentEndpoint.static) === null || _c === void 0 ? void 0 : _c[key]) === null || _d === void 0 ? void 0 : _d.plugins) {
149
+ if (!(yield run((_e = currentEndpoint.static) === null || _e === void 0 ? void 0 : _e[key], params)))
142
150
  continue;
143
- return;
151
+ return null;
144
152
  }
145
153
  if (currentEndpoint.dynamic) {
146
154
  for (const dynamicEndpoint of currentEndpoint.dynamic) {
147
- if (dynamicEndpoint.content) {
155
+ if (dynamicEndpoint.plugins) {
148
156
  if (!(yield run(dynamicEndpoint, Object.assign(Object.assign({}, params), { [dynamicEndpoint.key.slice(1, -1)]: key }))))
149
157
  continue;
150
- return;
158
+ return null;
151
159
  }
152
160
  }
153
161
  }
154
162
  break;
155
163
  }
156
- if ((_g = currentEndpoint.static) === null || _g === void 0 ? void 0 : _g[key]) {
164
+ if ((_f = currentEndpoint.static) === null || _f === void 0 ? void 0 : _f[key]) {
157
165
  endpointQueue.push([deep + 1, currentEndpoint.static[key], params]);
158
166
  }
159
167
  if (currentEndpoint.dynamic) {
@@ -162,26 +170,16 @@ const api = () => {
162
170
  }
163
171
  }
164
172
  }
165
- for (const requestPlugin of requestPlugins) {
166
- if (requestPlugin(req, res))
167
- return;
168
- }
169
- if (context.fallback) {
170
- const newHandler = Object.create(context.fallback.handler);
171
- useResponse.responseContext.set(newHandler, res);
172
- useRequest.requestContext.set(newHandler, req);
173
+ for (const plugin of plugins) {
174
+ const result = yield plugin();
175
+ if (result === undefined)
176
+ continue;
177
+ const newHandler = Object.create(handler);
173
178
  useAction.actionContext.set(newHandler, action);
174
- innet__default["default"](context.fallback.children, newHandler);
175
- }
176
- else {
177
- res.statusCode = 404;
178
- res.end();
179
+ innet__default["default"](result, newHandler);
180
+ return null;
179
181
  }
180
- });
181
- server.on('request', listener);
182
- watchState.onDestroy(() => {
183
- server.off('request', listener);
184
- });
182
+ }));
185
183
  innet__default["default"](children, handler);
186
184
  };
187
185
 
@@ -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
- function any(value) {
6
- return value;
7
- }
5
+ var preset = require('./preset.js');
8
6
 
9
- exports.any = any;
7
+
8
+
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;
@@ -1 +1 @@
1
- export { response } from './response.es6.js';
1
+ export { response, statuses } from './response.es6.js';
@@ -7,3 +7,4 @@ var response = require('./response.js');
7
7
 
8
8
 
9
9
  exports.response = response.response;
10
+ exports.statuses = response.statuses;
@@ -1,16 +1,20 @@
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: string;
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;
17
+ type?: string;
15
18
  }
19
+ export declare const statuses: Record<StatusKey, number>;
16
20
  export declare const response: HandlerPlugin;
@@ -2,35 +2,43 @@ 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
- const { description, status = 'default' } = useProps();
17
+ var _a;
18
+ let { description = '', status = 'default', type = 'application/json', } = useProps() || {};
13
19
  const { operation, props: { path } } = useEndpoint();
14
20
  const children = useChildren();
15
21
  const handler = useNewHandler();
16
22
  const endpoint = useContext(endpointContext);
23
+ if (status in statuses) {
24
+ status = statuses[status];
25
+ }
17
26
  if (!endpoint) {
18
27
  useThrow('<{type}> MUST be placed in <endpoint> element');
19
28
  }
20
29
  if (!operation.responses) {
21
30
  operation.responses = {};
22
31
  }
23
- if (operation.responses[status]) {
24
- 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`);
25
35
  }
26
36
  const schema = {};
27
37
  const response = {
28
38
  description,
29
- content: {
30
- 'application/json': {
39
+ content: Object.assign(Object.assign({}, defaultResponse === null || defaultResponse === void 0 ? void 0 : defaultResponse.content), { [type]: {
31
40
  schema,
32
- },
33
- },
41
+ } }),
34
42
  };
35
43
  operation.responses[status] = response;
36
44
  schemaContext.set(handler, schema);
@@ -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,29 +20,33 @@ 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
- const { description, status = 'default' } = jsx.useProps();
25
+ var _a;
26
+ let { description = '', status = 'default', type = 'application/json', } = jsx.useProps() || {};
21
27
  const { operation, props: { path } } = useEndpoint.useEndpoint();
22
28
  const children = jsx.useChildren();
23
29
  const handler = innet.useNewHandler();
24
30
  const endpoint = jsx.useContext(useEndpoint.endpointContext);
31
+ if (status in statuses) {
32
+ status = statuses[status];
33
+ }
25
34
  if (!endpoint) {
26
35
  useThrow.useThrow('<{type}> MUST be placed in <endpoint> element');
27
36
  }
28
37
  if (!operation.responses) {
29
38
  operation.responses = {};
30
39
  }
31
- if (operation.responses[status]) {
32
- 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`);
33
43
  }
34
44
  const schema = {};
35
45
  const response = {
36
46
  description,
37
- content: {
38
- 'application/json': {
47
+ content: Object.assign(Object.assign({}, defaultResponse === null || defaultResponse === void 0 ? void 0 : defaultResponse.content), { [type]: {
39
48
  schema,
40
- },
41
- },
49
+ } }),
42
50
  };
43
51
  operation.responses[status] = response;
44
52
  useSchemaContext.schemaContext.set(handler, schema);
@@ -50,3 +58,4 @@ const response = () => {
50
58
  };
51
59
 
52
60
  exports.response = response;
61
+ exports.statuses = statuses;
@@ -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');
@@ -13,7 +18,7 @@ const server = () => {
13
18
  const handler = useNewHandler();
14
19
  const { props = {}, children } = useApp();
15
20
  const { env } = process;
16
- let { ssl: { key = (_a = env.SSL_KEY) !== null && _a !== void 0 ? _a : 'localhost.key', cert = (_b = env.SSL_CRT) !== null && _b !== void 0 ? _b : 'localhost.crt', } = {}, } = props;
21
+ let { ssl: { key = (_a = env.INNET_SSL_KEY) !== null && _a !== void 0 ? _a : 'localhost.key', cert = (_b = env.INNET_SSL_CRT) !== null && _b !== void 0 ? _b : 'localhost.crt', } = {}, } = props;
17
22
  try {
18
23
  if (!isInvalidPath(key)) {
19
24
  key = fs.readFileSync(key).toString();
@@ -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.INNET_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 });