@innet/server 2.0.0-alpha.13 → 2.0.0-alpha.15

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 (82) hide show
  1. package/README.md +356 -363
  2. package/handler/handler.d.ts +1 -3
  3. package/handler/handler.es6.js +0 -2
  4. package/handler/handler.js +0 -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/useServer/useServer.d.ts +1 -0
  14. package/hooks/useServerPlugin/index.d.ts +1 -0
  15. package/hooks/useServerPlugin/index.es6.js +1 -0
  16. package/hooks/useServerPlugin/index.js +9 -0
  17. package/hooks/useServerPlugin/useServerPlugin.d.ts +2 -0
  18. package/hooks/useServerPlugin/useServerPlugin.es6.js +13 -0
  19. package/hooks/useServerPlugin/useServerPlugin.js +17 -0
  20. package/hooks/useServerPlugins/index.d.ts +1 -0
  21. package/hooks/useServerPlugins/index.es6.js +1 -0
  22. package/hooks/useServerPlugins/index.js +10 -0
  23. package/hooks/useServerPlugins/useServerPlugins.d.ts +4 -0
  24. package/hooks/useServerPlugins/useServerPlugins.es6.js +14 -0
  25. package/hooks/useServerPlugins/useServerPlugins.js +19 -0
  26. package/index.es6.js +3 -3
  27. package/index.js +5 -5
  28. package/package.json +1 -1
  29. package/plugins/main/api/api.es6.js +34 -48
  30. package/plugins/main/api/api.js +33 -47
  31. package/plugins/main/endpoint/endpoint.es6.js +4 -1
  32. package/plugins/main/endpoint/endpoint.js +4 -1
  33. package/plugins/main/index.d.ts +0 -1
  34. package/plugins/main/index.es6.js +0 -1
  35. package/plugins/main/index.js +0 -1
  36. package/plugins/main/preset/index.es6.js +1 -1
  37. package/plugins/main/preset/index.js +0 -1
  38. package/plugins/main/preset/preset.d.ts +0 -4
  39. package/plugins/main/preset/preset.es6.js +6 -19
  40. package/plugins/main/preset/preset.js +4 -18
  41. package/plugins/main/request/request.es6.js +2 -13
  42. package/plugins/main/request/request.js +2 -13
  43. package/plugins/main/response/response.d.ts +1 -0
  44. package/plugins/main/response/response.es6.js +7 -7
  45. package/plugins/main/response/response.js +7 -7
  46. package/plugins/main/server/server.d.ts +3 -3
  47. package/plugins/main/server/server.es6.js +28 -6
  48. package/plugins/main/server/server.js +28 -6
  49. package/plugins/request/proxy/proxy.d.ts +3 -0
  50. package/plugins/request/proxy/proxy.es6.js +4 -1
  51. package/plugins/request/proxy/proxy.js +4 -1
  52. package/plugins/request/success/success.d.ts +1 -1
  53. package/plugins/request/success/success.es6.js +4 -4
  54. package/plugins/request/success/success.js +4 -4
  55. package/plugins/utils/blacklist/blacklist.es6.js +4 -3
  56. package/plugins/utils/blacklist/blacklist.js +4 -3
  57. package/plugins/utils/protection/protection.es6.js +4 -2
  58. package/plugins/utils/protection/protection.js +4 -2
  59. package/plugins/utils/swagger/swagger.es6.js +5 -3
  60. package/plugins/utils/swagger/swagger.js +5 -3
  61. package/plugins/utils/whitelist/whitelist.es6.js +4 -2
  62. package/plugins/utils/whitelist/whitelist.js +4 -2
  63. package/types.d.ts +2 -9
  64. package/utils/action/Action.es6.js +12 -10
  65. package/utils/action/Action.js +12 -10
  66. package/utils/generateTypes/generateTypes.d.ts +1 -1
  67. package/utils/generateTypes/generateTypes.es6.js +27 -13
  68. package/utils/generateTypes/generateTypes.js +27 -13
  69. package/utils/getEndpoint/getEndpoint.es6.js +2 -2
  70. package/utils/getEndpoint/getEndpoint.js +2 -2
  71. package/hooks/useRequestPlugin/index.d.ts +0 -1
  72. package/hooks/useRequestPlugin/index.es6.js +0 -1
  73. package/hooks/useRequestPlugin/index.js +0 -9
  74. package/hooks/useRequestPlugin/useRequestPlugin.d.ts +0 -2
  75. package/hooks/useRequestPlugin/useRequestPlugin.es6.js +0 -13
  76. package/hooks/useRequestPlugin/useRequestPlugin.js +0 -17
  77. package/plugins/main/fallback/fallback.d.ts +0 -4
  78. package/plugins/main/fallback/fallback.es6.js +0 -19
  79. package/plugins/main/fallback/fallback.js +0 -23
  80. package/plugins/main/fallback/index.d.ts +0 -1
  81. package/plugins/main/fallback/index.es6.js +0 -1
  82. package/plugins/main/fallback/index.js +0 -9
@@ -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,5 +10,4 @@ export * from './response';
10
10
  export * from './request';
11
11
  export * from './param';
12
12
  export * from './body';
13
- export * from './fallback';
14
13
  export * from './preset';
@@ -10,5 +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';
14
13
  import './preset/index.es6.js';
@@ -12,6 +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');
16
15
  require('./preset/index.js');
17
16
 
@@ -1 +1 @@
1
- export { preset, presetCondition } from './preset.es6.js';
1
+ export { preset } from './preset.es6.js';
@@ -7,4 +7,3 @@ var preset = require('./preset.js');
7
7
 
8
8
 
9
9
  exports.preset = preset.preset;
10
- exports.presetCondition = preset.presetCondition;
@@ -1,7 +1,3 @@
1
- import { Context } from '@innet/jsx';
2
- import { Action } from '../../../utils';
3
1
  export interface PresetProps {
4
2
  }
5
- export type PresetCondition = (action: Action) => boolean;
6
- export declare const presetCondition: Context<PresetCondition, PresetCondition>;
7
3
  export declare function preset(): void;
@@ -1,26 +1,13 @@
1
1
  import innet, { useHandler } from 'innet';
2
- import { Context, useChildren, useContext } from '@innet/jsx';
2
+ import { useChildren } from '@innet/jsx';
3
3
  import '../../../hooks/index.es6.js';
4
- import '../../../utils/index.es6.js';
5
- import { useServer } from '../../../hooks/useServer/useServer.es6.js';
6
- import { Action } from '../../../utils/action/Action.es6.js';
7
- import { actionContext } from '../../../hooks/useAction/useAction.es6.js';
4
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
8
5
 
9
- const presetCondition = new Context(() => true);
10
6
  function preset() {
11
- const { server } = useServer();
12
- const handler = useHandler();
13
7
  const children = useChildren();
14
- const condition = useContext(presetCondition);
15
- const listener = (req, res) => {
16
- const action = new Action(req, res);
17
- if (condition(action)) {
18
- const newHandler = Object.create(handler);
19
- actionContext.set(newHandler, action);
20
- innet(children, newHandler);
21
- }
22
- };
23
- server.addListener('request', listener);
8
+ useServerPlugin(() => {
9
+ innet(children, useHandler());
10
+ });
24
11
  }
25
12
 
26
- export { preset, presetCondition };
13
+ export { preset };
@@ -5,31 +5,17 @@ Object.defineProperty(exports, '__esModule', { value: true });
5
5
  var innet = require('innet');
6
6
  var jsx = require('@innet/jsx');
7
7
  require('../../../hooks/index.js');
8
- require('../../../utils/index.js');
9
- var useServer = require('../../../hooks/useServer/useServer.js');
10
- var Action = require('../../../utils/action/Action.js');
11
- var useAction = require('../../../hooks/useAction/useAction.js');
8
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
12
9
 
13
10
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
11
 
15
12
  var innet__default = /*#__PURE__*/_interopDefaultLegacy(innet);
16
13
 
17
- const presetCondition = new jsx.Context(() => true);
18
14
  function preset() {
19
- const { server } = useServer.useServer();
20
- const handler = innet.useHandler();
21
15
  const children = jsx.useChildren();
22
- const condition = jsx.useContext(presetCondition);
23
- const listener = (req, res) => {
24
- const action = new Action.Action(req, res);
25
- if (condition(action)) {
26
- const newHandler = Object.create(handler);
27
- useAction.actionContext.set(newHandler, action);
28
- innet__default["default"](children, newHandler);
29
- }
30
- };
31
- server.addListener('request', listener);
16
+ useServerPlugin.useServerPlugin(() => {
17
+ innet__default["default"](children, innet.useHandler());
18
+ });
32
19
  }
33
20
 
34
21
  exports.preset = preset;
35
- exports.presetCondition = presetCondition;
@@ -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 });
@@ -2,13 +2,18 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var tslib = require('tslib');
5
6
  var innet = require('innet');
6
7
  var fs = require('node:fs');
7
8
  var http = require('node:http');
8
9
  var http2 = require('node:https');
9
10
  var watchState = require('watch-state');
10
11
  require('../../../hooks/index.js');
12
+ require('../../../utils/index.js');
11
13
  var useServer = require('../../../hooks/useServer/useServer.js');
14
+ var useServerPlugins = require('../../../hooks/useServerPlugins/useServerPlugins.js');
15
+ var Action = require('../../../utils/action/Action.js');
16
+ var useAction = require('../../../hooks/useAction/useAction.js');
12
17
 
13
18
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
14
19
 
@@ -38,20 +43,37 @@ const server = () => {
38
43
  cert = '';
39
44
  }
40
45
  const https = Boolean(key && cert);
41
- const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, } = props;
46
+ const { port = Number((_c = env.PORT) !== null && _c !== void 0 ? _c : (https ? 442 : 80)), onStart, onError, onRequest, onClose, } = props;
47
+ const plugins = new Set();
42
48
  const server = https ? http2__default["default"].createServer({ key, cert }) : http__default["default"].createServer();
43
- handler[useServer.serverContext.key] = { server, port };
49
+ useServer.serverContext.set(handler, { server, port });
50
+ useServerPlugins.serverPlugins.set(handler, plugins);
44
51
  watchState.onDestroy(() => {
45
- var _a;
46
- (_a = props.onDestroy) === null || _a === void 0 ? void 0 : _a.call(props);
47
52
  server.close();
48
53
  });
49
54
  if (onError) {
50
55
  server.on('error', onError);
51
56
  }
52
- if (onRequest) {
53
- server.on('request', onRequest);
57
+ if (onClose) {
58
+ server.addListener('close', onClose);
54
59
  }
60
+ server.on('request', (req, res) => {
61
+ onRequest === null || onRequest === void 0 ? void 0 : onRequest(req, res);
62
+ const action = new Action.Action(req, res);
63
+ const requestHandler = Object.create(handler);
64
+ useAction.actionContext.set(requestHandler, action);
65
+ function server() {
66
+ return tslib.__awaiter(this, void 0, void 0, function* () {
67
+ for (const plugin of plugins) {
68
+ const result = yield plugin();
69
+ if (result !== undefined) {
70
+ return result;
71
+ }
72
+ }
73
+ });
74
+ }
75
+ innet__default["default"]({ type: server, props }, requestHandler);
76
+ });
55
77
  innet__default["default"](children, handler);
56
78
  server.listen(port, () => {
57
79
  onStart === null || onStart === void 0 ? void 0 : onStart({ port, https });
@@ -1,6 +1,9 @@
1
+ /// <reference types="node" />
1
2
  import { type HandlerPlugin } from 'innet';
3
+ import { type IncomingMessage } from 'http';
2
4
  export interface ProxyProps {
3
5
  to: string;
4
6
  secure?: boolean;
7
+ onProxyRes?: (res: IncomingMessage) => void;
5
8
  }
6
9
  export declare const proxy: HandlerPlugin;
@@ -6,7 +6,7 @@ import { useResponse } from '../../../hooks/useResponse/useResponse.es6.js';
6
6
  import { useThrow } from '../../../hooks/useThrow/useThrow.es6.js';
7
7
 
8
8
  const proxy = () => {
9
- const { to, secure } = useProps();
9
+ const { to, secure, onProxyRes } = useProps();
10
10
  const req = useRequest();
11
11
  const res = useResponse();
12
12
  if (!req || !res) {
@@ -14,6 +14,9 @@ const proxy = () => {
14
14
  }
15
15
  const proxyServer = httpProxy.createProxyServer({});
16
16
  delete req.headers.host;
17
+ if (onProxyRes) {
18
+ proxyServer.on('proxyRes', onProxyRes);
19
+ }
17
20
  proxyServer.web(req, res, { target: to, secure });
18
21
  };
19
22
 
@@ -14,7 +14,7 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
14
14
  var httpProxy__default = /*#__PURE__*/_interopDefaultLegacy(httpProxy);
15
15
 
16
16
  const proxy = () => {
17
- const { to, secure } = jsx.useProps();
17
+ const { to, secure, onProxyRes } = jsx.useProps();
18
18
  const req = useRequest.useRequest();
19
19
  const res = useResponse.useResponse();
20
20
  if (!req || !res) {
@@ -22,6 +22,9 @@ const proxy = () => {
22
22
  }
23
23
  const proxyServer = httpProxy__default["default"].createProxyServer({});
24
24
  delete req.headers.host;
25
+ if (onProxyRes) {
26
+ proxyServer.on('proxyRes', onProxyRes);
27
+ }
25
28
  proxyServer.web(req, res, { target: to, secure });
26
29
  };
27
30
 
@@ -13,6 +13,6 @@ export declare const successStatuses: {
13
13
  export type SuccessStatuses = keyof typeof successStatuses;
14
14
  export interface SuccessProps {
15
15
  status?: SuccessStatuses | number;
16
- contentType?: string;
16
+ type?: string;
17
17
  }
18
18
  export declare const success: HandlerPlugin;
@@ -17,7 +17,7 @@ const successStatuses = {
17
17
  };
18
18
  const success = () => {
19
19
  const children = useChildren();
20
- const { status, contentType } = useProps() || {};
20
+ const { status, type } = useProps() || {};
21
21
  const res = useResponse();
22
22
  if (!res) {
23
23
  throw Error('<success> MUST be in <request>');
@@ -25,11 +25,11 @@ const success = () => {
25
25
  res.statusCode = typeof status === 'string' ? successStatuses[status] : status !== null && status !== void 0 ? status : ((children) ? 200 : 204);
26
26
  if (children === null || children === void 0 ? void 0 : children[0]) {
27
27
  const child = children[0];
28
- const type = contentType || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
28
+ const contentType = type || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
29
29
  ? 'text/plain'
30
30
  : 'application/json');
31
- res.setHeader('Content-Type', type);
32
- if (type === 'application/json') {
31
+ res.setHeader('Content-Type', contentType);
32
+ if (contentType === 'application/json') {
33
33
  res.write(JSONString(child));
34
34
  }
35
35
  else {
@@ -21,7 +21,7 @@ const successStatuses = {
21
21
  };
22
22
  const success = () => {
23
23
  const children = jsx.useChildren();
24
- const { status, contentType } = jsx.useProps() || {};
24
+ const { status, type } = jsx.useProps() || {};
25
25
  const res = useResponse.useResponse();
26
26
  if (!res) {
27
27
  throw Error('<success> MUST be in <request>');
@@ -29,11 +29,11 @@ const success = () => {
29
29
  res.statusCode = typeof status === 'string' ? successStatuses[status] : status !== null && status !== void 0 ? status : ((children) ? 200 : 204);
30
30
  if (children === null || children === void 0 ? void 0 : children[0]) {
31
31
  const child = children[0];
32
- const type = contentType || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
32
+ const contentType = type || (['string', 'number', 'boolean', 'bigint'].includes(typeof child)
33
33
  ? 'text/plain'
34
34
  : 'application/json');
35
- res.setHeader('Content-Type', type);
36
- if (type === 'application/json') {
35
+ res.setHeader('Content-Type', contentType);
36
+ if (contentType === 'application/json') {
37
37
  res.write(JSONString.JSONString(child));
38
38
  }
39
39
  else {
@@ -1,13 +1,14 @@
1
1
  import { useProps, useChildren } from '@innet/jsx';
2
2
  import '../../../hooks/index.es6.js';
3
- import { useRequestPlugin } from '../../../hooks/useRequestPlugin/useRequestPlugin.es6.js';
3
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
4
+ import { useAction } from '../../../hooks/useAction/useAction.es6.js';
4
5
 
5
6
  function blacklist() {
6
7
  const { ip = process.env.BLACKLIST_IP, } = useProps() || {};
7
8
  const children = useChildren();
8
9
  const ips = typeof ip === 'string' ? ip.split(',') : ip;
9
- useRequestPlugin(action => {
10
- console.log(action.clientIp);
10
+ useServerPlugin(() => {
11
+ const action = useAction();
11
12
  if (!action.clientIp || (ips === null || ips === void 0 ? void 0 : ips.includes(action.clientIp))) {
12
13
  return children;
13
14
  }
@@ -4,14 +4,15 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var jsx = require('@innet/jsx');
6
6
  require('../../../hooks/index.js');
7
- var useRequestPlugin = require('../../../hooks/useRequestPlugin/useRequestPlugin.js');
7
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
8
+ var useAction = require('../../../hooks/useAction/useAction.js');
8
9
 
9
10
  function blacklist() {
10
11
  const { ip = process.env.BLACKLIST_IP, } = jsx.useProps() || {};
11
12
  const children = jsx.useChildren();
12
13
  const ips = typeof ip === 'string' ? ip.split(',') : ip;
13
- useRequestPlugin.useRequestPlugin(action => {
14
- console.log(action.clientIp);
14
+ useServerPlugin.useServerPlugin(() => {
15
+ const action = useAction.useAction();
15
16
  if (!action.clientIp || (ips === null || ips === void 0 ? void 0 : ips.includes(action.clientIp))) {
16
17
  return children;
17
18
  }
@@ -1,6 +1,7 @@
1
1
  import { useProps, useChildren } from '@innet/jsx';
2
2
  import '../../../hooks/index.es6.js';
3
- import { useRequestPlugin } from '../../../hooks/useRequestPlugin/useRequestPlugin.es6.js';
3
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
4
+ import { useAction } from '../../../hooks/useAction/useAction.es6.js';
4
5
 
5
6
  function protection() {
6
7
  const { maxAge = Number(process.env.PROTECTION_MAX_AGE) || 365 * 24 * 60 * 60, value = process.env.PROTECTION, excludeIp = process.env.PROTECTED_IP, cookieKey = process.env.PROTECTION_COOKIE_KEY || 'protection', searchKey = process.env.PROTECTION_SEARCH_KEY || 'protection', } = useProps() || {};
@@ -8,7 +9,8 @@ function protection() {
8
9
  if (!value)
9
10
  return;
10
11
  const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
11
- useRequestPlugin(action => {
12
+ useServerPlugin(() => {
13
+ const action = useAction();
12
14
  if (!action.clientIp)
13
15
  return children;
14
16
  if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
@@ -4,7 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var jsx = require('@innet/jsx');
6
6
  require('../../../hooks/index.js');
7
- var useRequestPlugin = require('../../../hooks/useRequestPlugin/useRequestPlugin.js');
7
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
8
+ var useAction = require('../../../hooks/useAction/useAction.js');
8
9
 
9
10
  function protection() {
10
11
  const { maxAge = Number(process.env.PROTECTION_MAX_AGE) || 365 * 24 * 60 * 60, value = process.env.PROTECTION, excludeIp = process.env.PROTECTED_IP, cookieKey = process.env.PROTECTION_COOKIE_KEY || 'protection', searchKey = process.env.PROTECTION_SEARCH_KEY || 'protection', } = jsx.useProps() || {};
@@ -12,7 +13,8 @@ function protection() {
12
13
  if (!value)
13
14
  return;
14
15
  const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
15
- useRequestPlugin.useRequestPlugin(action => {
16
+ useServerPlugin.useServerPlugin(() => {
17
+ const action = useAction.useAction();
16
18
  if (!action.clientIp)
17
19
  return children;
18
20
  if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
@@ -2,14 +2,16 @@ import { useProps } from '@innet/jsx';
2
2
  import '../../../hooks/index.es6.js';
3
3
  import html from './swagger.html.es6.js';
4
4
  import { useApi } from '../../../hooks/useApi/useApi.es6.js';
5
- import { useRequestPlugin } from '../../../hooks/useRequestPlugin/useRequestPlugin.es6.js';
5
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
6
+ import { useAction } from '../../../hooks/useAction/useAction.es6.js';
6
7
 
7
8
  const swagger = () => {
8
9
  const { path = '/swagger-ui' } = useProps() || {};
9
10
  const { docs, prefix } = useApi();
10
11
  let swaggerResponse;
11
- useRequestPlugin(action => {
12
- if (action.req.url === prefix + path) {
12
+ useServerPlugin(() => {
13
+ const action = useAction();
14
+ if (action.path === prefix + path) {
13
15
  if (!swaggerResponse) {
14
16
  swaggerResponse = html.replace('spec: {},', `spec: ${JSON.stringify(docs)},`);
15
17
  }