@innet/server 2.0.0-alpha.13 → 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 (79) 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/success/success.d.ts +1 -1
  50. package/plugins/request/success/success.es6.js +4 -4
  51. package/plugins/request/success/success.js +4 -4
  52. package/plugins/utils/blacklist/blacklist.es6.js +4 -3
  53. package/plugins/utils/blacklist/blacklist.js +4 -3
  54. package/plugins/utils/protection/protection.es6.js +4 -2
  55. package/plugins/utils/protection/protection.js +4 -2
  56. package/plugins/utils/swagger/swagger.es6.js +5 -3
  57. package/plugins/utils/swagger/swagger.js +5 -3
  58. package/plugins/utils/whitelist/whitelist.es6.js +4 -2
  59. package/plugins/utils/whitelist/whitelist.js +4 -2
  60. package/types.d.ts +2 -9
  61. package/utils/action/Action.es6.js +12 -10
  62. package/utils/action/Action.js +12 -10
  63. package/utils/generateTypes/generateTypes.d.ts +1 -1
  64. package/utils/generateTypes/generateTypes.es6.js +27 -13
  65. package/utils/generateTypes/generateTypes.js +27 -13
  66. package/utils/getEndpoint/getEndpoint.es6.js +2 -2
  67. package/utils/getEndpoint/getEndpoint.js +2 -2
  68. package/hooks/useRequestPlugin/index.d.ts +0 -1
  69. package/hooks/useRequestPlugin/index.es6.js +0 -1
  70. package/hooks/useRequestPlugin/index.js +0 -9
  71. package/hooks/useRequestPlugin/useRequestPlugin.d.ts +0 -2
  72. package/hooks/useRequestPlugin/useRequestPlugin.es6.js +0 -13
  73. package/hooks/useRequestPlugin/useRequestPlugin.js +0 -17
  74. package/plugins/main/fallback/fallback.d.ts +0 -4
  75. package/plugins/main/fallback/fallback.es6.js +0 -19
  76. package/plugins/main/fallback/fallback.js +0 -23
  77. package/plugins/main/fallback/index.d.ts +0 -1
  78. package/plugins/main/fallback/index.es6.js +0 -1
  79. package/plugins/main/fallback/index.js +0 -9
@@ -1,6 +1,6 @@
1
1
  import { context, type ContextProps, slot, type SlotProps, slots, type SlotsProps } from '@innet/jsx';
2
2
  import { arraySync, async } from '@innet/utils';
3
- import { type ApiProps, type ArrayProps, type BinaryProps, blacklist, type BlacklistProps, type BodyProps, type BooleanProps, cms, type CmsProps, type ContactProps, type CookieProps, type DateProps, type DevProps, type DtsProps, type EndpointProps, type ErrorProps, type FallbackProps, type FieldProps, file, type FileProps, type HeaderProps, type HostProps, type IntegerProps, type LicenseProps, type NullProps, type NumberProps, type ObjectProps, type ParamProps, preset, type PresetProps, type ProdProps, protection, type ProtectionProps, type ProxyProps, type RedirectProps, type RequestProps, type ResponseProps, type ServerProps, type StringProps, type SuccessProps, type SwaggerProps, type TagProps, type TupleProps, type UuidProps, type VariableProps, whitelist, type WhitelistProps } from '../plugins';
3
+ import { type ApiProps, type ArrayProps, type BinaryProps, blacklist, type BlacklistProps, type BodyProps, type BooleanProps, cms, type CmsProps, type ContactProps, type CookieProps, type DateProps, type DevProps, type DtsProps, type EndpointProps, type ErrorProps, type FieldProps, file, type FileProps, type HeaderProps, type HostProps, type IntegerProps, type LicenseProps, type NullProps, type NumberProps, type ObjectProps, type ParamProps, preset, type PresetProps, type ProdProps, protection, type ProtectionProps, type ProxyProps, type RedirectProps, type RequestProps, type ResponseProps, type ServerProps, type StringProps, type SuccessProps, type SwaggerProps, type TagProps, type TupleProps, type UuidProps, type VariableProps, whitelist, type WhitelistProps } from '../plugins';
4
4
  export declare const arrayPlugins: (typeof arraySync)[];
5
5
  export declare const JSXPlugins: {
6
6
  api: import("innet").HandlerPlugin;
@@ -18,7 +18,6 @@ export declare const JSXPlugins: {
18
18
  dts: import("innet").HandlerPlugin;
19
19
  endpoint: import("innet").HandlerPlugin;
20
20
  error: import("innet").HandlerPlugin;
21
- fallback: import("innet").HandlerPlugin;
22
21
  field: import("innet").HandlerPlugin;
23
22
  file: typeof file;
24
23
  header: import("innet").HandlerPlugin;
@@ -70,7 +69,6 @@ declare global {
70
69
  dts: DtsProps;
71
70
  endpoint: EndpointProps;
72
71
  error: ErrorProps;
73
- fallback: FallbackProps;
74
72
  field: FieldProps;
75
73
  file: FileProps;
76
74
  header: HeaderProps;
@@ -16,7 +16,6 @@ import { dev } from '../plugins/utils/dev/dev.es6.js';
16
16
  import { dts } from '../plugins/utils/dts/dts.es6.js';
17
17
  import { endpoint } from '../plugins/main/endpoint/endpoint.es6.js';
18
18
  import { error } from '../plugins/request/error/error.es6.js';
19
- import { fallback } from '../plugins/main/fallback/fallback.es6.js';
20
19
  import { field } from '../plugins/schema/field/field.es6.js';
21
20
  import { file } from '../plugins/request/file/file.es6.js';
22
21
  import { header } from '../plugins/request/header/header.es6.js';
@@ -64,7 +63,6 @@ const JSXPlugins = {
64
63
  dts,
65
64
  endpoint,
66
65
  error,
67
- fallback,
68
66
  field,
69
67
  file,
70
68
  header,
@@ -20,7 +20,6 @@ var dev = require('../plugins/utils/dev/dev.js');
20
20
  var dts = require('../plugins/utils/dts/dts.js');
21
21
  var endpoint = require('../plugins/main/endpoint/endpoint.js');
22
22
  var error = require('../plugins/request/error/error.js');
23
- var fallback = require('../plugins/main/fallback/fallback.js');
24
23
  var field = require('../plugins/schema/field/field.js');
25
24
  var file = require('../plugins/request/file/file.js');
26
25
  var header = require('../plugins/request/header/header.js');
@@ -68,7 +67,6 @@ const JSXPlugins = {
68
67
  dts: dts.dts,
69
68
  endpoint: endpoint.endpoint,
70
69
  error: error.error,
71
- fallback: fallback.fallback,
72
70
  field: field.field,
73
71
  file: file.file,
74
72
  header: header.header,
package/hooks/index.d.ts CHANGED
@@ -23,5 +23,6 @@ export * from './useRule';
23
23
  export * from './useObjectRule';
24
24
  export * from './useBodyFile';
25
25
  export * from './usePath';
26
- export * from './useRequestPlugin';
26
+ export * from './useServerPlugin';
27
27
  export * from './useClientIp';
28
+ export * from './useServerPlugins';
@@ -23,5 +23,6 @@ import './useRule/index.es6.js';
23
23
  import './useObjectRule/index.es6.js';
24
24
  import './useBodyFile/index.es6.js';
25
25
  import './usePath/index.es6.js';
26
- import './useRequestPlugin/index.es6.js';
26
+ import './useServerPlugin/index.es6.js';
27
27
  import './useClientIp/index.es6.js';
28
+ import './useServerPlugins/index.es6.js';
package/hooks/index.js CHANGED
@@ -25,6 +25,7 @@ require('./useRule/index.js');
25
25
  require('./useObjectRule/index.js');
26
26
  require('./useBodyFile/index.js');
27
27
  require('./usePath/index.js');
28
- require('./useRequestPlugin/index.js');
28
+ require('./useServerPlugin/index.js');
29
29
  require('./useClientIp/index.js');
30
+ require('./useServerPlugins/index.js');
30
31
 
@@ -6,7 +6,7 @@ const actionContext = new Context();
6
6
  function useAction() {
7
7
  const action = useContext(actionContext);
8
8
  if (!action) {
9
- useThrow('<{type}> MUST be in <request> or <fallback>');
9
+ useThrow('<{type}> MUST be in <request>, <preset> or <fallback>');
10
10
  }
11
11
  return action;
12
12
  }
@@ -10,7 +10,7 @@ const actionContext = new jsx.Context();
10
10
  function useAction() {
11
11
  const action = jsx.useContext(actionContext);
12
12
  if (!action) {
13
- useThrow.useThrow('<{type}> MUST be in <request> or <fallback>');
13
+ useThrow.useThrow('<{type}> MUST be in <request>, <preset> or <fallback>');
14
14
  }
15
15
  return action;
16
16
  }
@@ -1,12 +1,10 @@
1
1
  import { Context } from '@innet/jsx';
2
- import { type Document, type Endpoints, type Fallback, type RequestPlugin } from '../../types';
2
+ import { type Document, type Endpoints } from '../../types';
3
3
  import { type Rule } from '../../utils';
4
4
  export interface ApiContext {
5
5
  docs: Document;
6
6
  endpoints: Endpoints;
7
7
  prefix: string;
8
- requestPlugins: Set<RequestPlugin>;
9
- fallback?: Fallback;
10
8
  refRules: Record<string, Rule>;
11
9
  }
12
10
  export declare const apiContext: Context<ApiContext, ApiContext | undefined>;
@@ -1,10 +1,12 @@
1
1
  import { Context, useContext } from '@innet/jsx';
2
+ import '../useThrow/index.es6.js';
3
+ import { useThrow } from '../useThrow/useThrow.es6.js';
2
4
 
3
5
  const endpointContext = new Context();
4
6
  function useEndpoint() {
5
7
  const endpoint = useContext(endpointContext);
6
8
  if (!endpoint) {
7
- throw Error('useEndpoint MUST be used in <endpoint>');
9
+ useThrow('Use <{type}> in <endpoint>');
8
10
  }
9
11
  return endpoint;
10
12
  }
@@ -3,12 +3,14 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var jsx = require('@innet/jsx');
6
+ require('../useThrow/index.js');
7
+ var useThrow = require('../useThrow/useThrow.js');
6
8
 
7
9
  const endpointContext = new jsx.Context();
8
10
  function useEndpoint() {
9
11
  const endpoint = jsx.useContext(endpointContext);
10
12
  if (!endpoint) {
11
- throw Error('useEndpoint MUST be used in <endpoint>');
13
+ useThrow.useThrow('Use <{type}> in <endpoint>');
12
14
  }
13
15
  return endpoint;
14
16
  }
@@ -3,6 +3,7 @@
3
3
  import { Context } from '@innet/jsx';
4
4
  import { type Server as HttpServer } from 'http';
5
5
  import { type Server as HttpsServer } from 'https';
6
+ export type ServerPlugin = () => any;
6
7
  export interface ServerContext {
7
8
  server: HttpServer | HttpsServer;
8
9
  port: number;
@@ -0,0 +1 @@
1
+ export * from './useServerPlugin';
@@ -0,0 +1 @@
1
+ export { useServerPlugin } from './useServerPlugin.es6.js';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var useServerPlugin = require('./useServerPlugin.js');
6
+
7
+
8
+
9
+ exports.useServerPlugin = useServerPlugin.useServerPlugin;
@@ -0,0 +1,2 @@
1
+ import { type ServerPlugin } from '../useServer';
2
+ export declare function useServerPlugin(listener: ServerPlugin): void;
@@ -0,0 +1,13 @@
1
+ import { onDestroy } from 'watch-state';
2
+ import '../useServerPlugins/index.es6.js';
3
+ import { useServerPlugins } from '../useServerPlugins/useServerPlugins.es6.js';
4
+
5
+ function useServerPlugin(listener) {
6
+ const requests = useServerPlugins();
7
+ requests.add(listener);
8
+ onDestroy(() => {
9
+ requests.delete(listener);
10
+ });
11
+ }
12
+
13
+ export { useServerPlugin };
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var watchState = require('watch-state');
6
+ require('../useServerPlugins/index.js');
7
+ var useServerPlugins = require('../useServerPlugins/useServerPlugins.js');
8
+
9
+ function useServerPlugin(listener) {
10
+ const requests = useServerPlugins.useServerPlugins();
11
+ requests.add(listener);
12
+ watchState.onDestroy(() => {
13
+ requests.delete(listener);
14
+ });
15
+ }
16
+
17
+ exports.useServerPlugin = useServerPlugin;
@@ -0,0 +1 @@
1
+ export * from './useServerPlugins';
@@ -0,0 +1 @@
1
+ export { serverPlugins, useServerPlugins } from './useServerPlugins.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var useServerPlugins = require('./useServerPlugins.js');
6
+
7
+
8
+
9
+ exports.serverPlugins = useServerPlugins.serverPlugins;
10
+ exports.useServerPlugins = useServerPlugins.useServerPlugins;
@@ -0,0 +1,4 @@
1
+ import { Context } from '@innet/jsx';
2
+ import { type ServerPlugin } from '../useServer';
3
+ export declare const serverPlugins: Context<Set<ServerPlugin>, Set<ServerPlugin> | undefined>;
4
+ export declare function useServerPlugins(): Set<ServerPlugin>;
@@ -0,0 +1,14 @@
1
+ import { Context, useContext } from '@innet/jsx';
2
+ import '../useThrow/index.es6.js';
3
+ import { useThrow } from '../useThrow/useThrow.es6.js';
4
+
5
+ const serverPlugins = new Context();
6
+ function useServerPlugins() {
7
+ const plugins = useContext(serverPlugins);
8
+ if (!plugins) {
9
+ useThrow('Use <{type}> in <server>');
10
+ }
11
+ return plugins;
12
+ }
13
+
14
+ export { serverPlugins, useServerPlugins };
@@ -0,0 +1,19 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsx = require('@innet/jsx');
6
+ require('../useThrow/index.js');
7
+ var useThrow = require('../useThrow/useThrow.js');
8
+
9
+ const serverPlugins = new jsx.Context();
10
+ function useServerPlugins() {
11
+ const plugins = jsx.useContext(serverPlugins);
12
+ if (!plugins) {
13
+ useThrow.useThrow('Use <{type}> in <server>');
14
+ }
15
+ return plugins;
16
+ }
17
+
18
+ exports.serverPlugins = serverPlugins;
19
+ exports.useServerPlugins = useServerPlugins;
package/index.es6.js CHANGED
@@ -16,8 +16,7 @@ export { response, statuses } from './plugins/main/response/response.es6.js';
16
16
  export { request } from './plugins/main/request/request.es6.js';
17
17
  export { param } from './plugins/main/param/param.es6.js';
18
18
  export { body } from './plugins/main/body/body.es6.js';
19
- export { fallback } from './plugins/main/fallback/fallback.es6.js';
20
- export { preset, presetCondition } from './plugins/main/preset/preset.es6.js';
19
+ export { preset } from './plugins/main/preset/preset.es6.js';
21
20
  export { object } from './plugins/schema/object/object.es6.js';
22
21
  export { field } from './plugins/schema/field/field.es6.js';
23
22
  export { number } from './plugins/schema/number/number.es6.js';
@@ -111,5 +110,6 @@ export { ruleContext, useRule, useSetRule } from './hooks/useRule/useRule.es6.js
111
110
  export { objectRuleContext, useObjectRule } from './hooks/useObjectRule/useObjectRule.es6.js';
112
111
  export { bodyFileContext, useBodyFile } from './hooks/useBodyFile/useBodyFile.es6.js';
113
112
  export { usePath } from './hooks/usePath/usePath.es6.js';
114
- export { useRequestPlugin } from './hooks/useRequestPlugin/useRequestPlugin.es6.js';
113
+ export { useServerPlugin } from './hooks/useServerPlugin/useServerPlugin.es6.js';
115
114
  export { useClientIp } from './hooks/useClientIp/useClientIp.es6.js';
115
+ export { serverPlugins, useServerPlugins } from './hooks/useServerPlugins/useServerPlugins.es6.js';
package/index.js CHANGED
@@ -20,7 +20,6 @@ var response = require('./plugins/main/response/response.js');
20
20
  var request = require('./plugins/main/request/request.js');
21
21
  var param = require('./plugins/main/param/param.js');
22
22
  var body = require('./plugins/main/body/body.js');
23
- var fallback = require('./plugins/main/fallback/fallback.js');
24
23
  var preset = require('./plugins/main/preset/preset.js');
25
24
  var object = require('./plugins/schema/object/object.js');
26
25
  var field = require('./plugins/schema/field/field.js');
@@ -115,8 +114,9 @@ var useRule = require('./hooks/useRule/useRule.js');
115
114
  var useObjectRule = require('./hooks/useObjectRule/useObjectRule.js');
116
115
  var useBodyFile = require('./hooks/useBodyFile/useBodyFile.js');
117
116
  var usePath = require('./hooks/usePath/usePath.js');
118
- var useRequestPlugin = require('./hooks/useRequestPlugin/useRequestPlugin.js');
117
+ var useServerPlugin = require('./hooks/useServerPlugin/useServerPlugin.js');
119
118
  var useClientIp = require('./hooks/useClientIp/useClientIp.js');
119
+ var useServerPlugins = require('./hooks/useServerPlugins/useServerPlugins.js');
120
120
 
121
121
 
122
122
 
@@ -140,9 +140,7 @@ exports.statuses = response.statuses;
140
140
  exports.request = request.request;
141
141
  exports.param = param.param;
142
142
  exports.body = body.body;
143
- exports.fallback = fallback.fallback;
144
143
  exports.preset = preset.preset;
145
- exports.presetCondition = preset.presetCondition;
146
144
  exports.object = object.object;
147
145
  exports.field = field.field;
148
146
  exports.number = number.number;
@@ -256,5 +254,7 @@ exports.useObjectRule = useObjectRule.useObjectRule;
256
254
  exports.bodyFileContext = useBodyFile.bodyFileContext;
257
255
  exports.useBodyFile = useBodyFile.useBodyFile;
258
256
  exports.usePath = usePath.usePath;
259
- exports.useRequestPlugin = useRequestPlugin.useRequestPlugin;
257
+ exports.useServerPlugin = useServerPlugin.useServerPlugin;
260
258
  exports.useClientIp = useClientIp.useClientIp;
259
+ exports.serverPlugins = useServerPlugins.serverPlugins;
260
+ exports.useServerPlugins = useServerPlugins.useServerPlugins;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@innet/server",
3
- "version": "2.0.0-alpha.13",
3
+ "version": "2.0.0-alpha.14",
4
4
  "description": "Create server-side application with innet",
5
5
  "main": "index.js",
6
6
  "module": "index.es6.js",
@@ -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
- import '../preset/index.es6.js';
5
3
  import '../../../hooks/index.es6.js';
6
4
  import '../../../utils/index.es6.js';
7
5
  import '../../../utils/rules/index.es6.js';
8
- import { useServer } from '../../../hooks/useServer/useServer.es6.js';
6
+ import { serverPlugins } from '../../../hooks/useServerPlugins/useServerPlugins.es6.js';
9
7
  import { apiContext } from '../../../hooks/useApi/useApi.es6.js';
10
- import { presetCondition } from '../preset/preset.es6.js';
11
- import { Action } from '../../../utils/action/Action.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,7 +15,6 @@ const api = () => {
18
15
  var _a;
19
16
  const handler = useNewHandler();
20
17
  const { props = {}, children } = useApp();
21
- const { server } = useServer();
22
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 = {};
@@ -27,9 +23,9 @@ const api = () => {
27
23
  info,
28
24
  paths: {},
29
25
  };
30
- const requestPlugins = new Set();
31
- const context = { docs, endpoints, prefix, requestPlugins, refRules: {} };
32
- const condition = action => {
26
+ const plugins = new Set();
27
+ const context = { docs, endpoints, prefix, refRules: {} };
28
+ const condition = (action) => {
33
29
  const path = action.parsedUrl.path;
34
30
  const url = path.endsWith('/') ? path.slice(0, -1) : path;
35
31
  if (!url.startsWith(prefix) || (exclude === null || exclude === void 0 ? void 0 : exclude.test(url))) {
@@ -40,33 +36,22 @@ const api = () => {
40
36
  }
41
37
  return true;
42
38
  };
39
+ serverPlugins.set(handler, plugins);
43
40
  apiContext.set(handler, context);
44
- presetCondition.set(handler, condition);
45
- innet(children, handler);
46
- const listener = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
41
+ useServerPlugin(() => __awaiter(void 0, void 0, void 0, function* () {
47
42
  var _b, _c, _d, _e, _f, _g;
48
- if (res.writableEnded)
43
+ const action = useAction();
44
+ if (!condition(action))
49
45
  return;
50
- const action = new Action(req, res);
46
+ const actionHandler = useNewHandler();
51
47
  const path = action.parsedUrl.path;
52
48
  const url = path.endsWith('/') ? path.slice(0, -1) : path;
53
- if (!condition(action)) {
54
- return;
55
- }
56
- for (const requestPlugin of requestPlugins) {
57
- const result = requestPlugin(action);
58
- if (!result)
59
- continue;
60
- const newHandler = Object.create(handler);
61
- actionContext.set(newHandler, action);
62
- innet(result, newHandler);
63
- return;
64
- }
49
+ const { req, res } = action;
65
50
  if (url === (prefix || '')) {
66
51
  res.setHeader('Content-Type', 'application/json');
67
52
  res.write(JSONString(docs));
68
53
  res.end();
69
- return;
54
+ return null;
70
55
  }
71
56
  const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
72
57
  const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
@@ -142,24 +127,28 @@ const api = () => {
142
127
  if (checkActionRules(bodyRules, 'body'))
143
128
  return true;
144
129
  }
145
- const newHandler = Object.create(runEndpoint.handler);
146
- paramsContext.set(newHandler, params);
147
- actionContext.set(newHandler, action);
148
- 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
+ }
149
138
  return true;
150
139
  });
151
140
  }
152
- 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) {
153
142
  if (!(yield run((_f = currentEndpoint.static) === null || _f === void 0 ? void 0 : _f[key], params)))
154
143
  continue;
155
- return;
144
+ return null;
156
145
  }
157
146
  if (currentEndpoint.dynamic) {
158
147
  for (const dynamicEndpoint of currentEndpoint.dynamic) {
159
- if (dynamicEndpoint.content) {
148
+ if (dynamicEndpoint.plugins) {
160
149
  if (!(yield run(dynamicEndpoint, Object.assign(Object.assign({}, params), { [dynamicEndpoint.key.slice(1, -1)]: key }))))
161
150
  continue;
162
- return;
151
+ return null;
163
152
  }
164
153
  }
165
154
  }
@@ -174,20 +163,17 @@ const api = () => {
174
163
  }
175
164
  }
176
165
  }
177
- if (context.fallback) {
178
- const newHandler = Object.create(context.fallback.handler);
166
+ for (const plugin of plugins) {
167
+ const result = yield plugin();
168
+ if (result === undefined)
169
+ continue;
170
+ const newHandler = Object.create(handler);
179
171
  actionContext.set(newHandler, action);
180
- innet(context.fallback.children, newHandler);
181
- }
182
- else {
183
- res.statusCode = 404;
184
- res.end();
172
+ innet(result, newHandler);
173
+ return null;
185
174
  }
186
- });
187
- server.on('request', listener);
188
- onDestroy(() => {
189
- server.off('request', listener);
190
- });
175
+ }));
176
+ innet(children, handler);
191
177
  };
192
178
 
193
179
  export { api };
@@ -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
- require('../preset/index.js');
9
7
  require('../../../hooks/index.js');
10
8
  require('../../../utils/index.js');
11
9
  require('../../../utils/rules/index.js');
12
- var useServer = require('../../../hooks/useServer/useServer.js');
10
+ var useServerPlugins = require('../../../hooks/useServerPlugins/useServerPlugins.js');
13
11
  var useApi = require('../../../hooks/useApi/useApi.js');
14
- var preset = require('../preset/preset.js');
15
- var Action = require('../../../utils/action/Action.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,7 +23,6 @@ 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
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 = {};
@@ -35,9 +31,9 @@ const api = () => {
35
31
  info,
36
32
  paths: {},
37
33
  };
38
- const requestPlugins = new Set();
39
- const context = { docs, endpoints, prefix, requestPlugins, refRules: {} };
40
- const condition = action => {
34
+ const plugins = new Set();
35
+ const context = { docs, endpoints, prefix, refRules: {} };
36
+ const condition = (action) => {
41
37
  const path = action.parsedUrl.path;
42
38
  const url = path.endsWith('/') ? path.slice(0, -1) : path;
43
39
  if (!url.startsWith(prefix) || (exclude === null || exclude === void 0 ? void 0 : exclude.test(url))) {
@@ -48,33 +44,22 @@ const api = () => {
48
44
  }
49
45
  return true;
50
46
  };
47
+ useServerPlugins.serverPlugins.set(handler, plugins);
51
48
  useApi.apiContext.set(handler, context);
52
- preset.presetCondition.set(handler, condition);
53
- innet__default["default"](children, handler);
54
- 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* () {
55
50
  var _b, _c, _d, _e, _f, _g;
56
- if (res.writableEnded)
51
+ const action = useAction.useAction();
52
+ if (!condition(action))
57
53
  return;
58
- const action = new Action.Action(req, res);
54
+ const actionHandler = innet.useNewHandler();
59
55
  const path = action.parsedUrl.path;
60
56
  const url = path.endsWith('/') ? path.slice(0, -1) : path;
61
- if (!condition(action)) {
62
- return;
63
- }
64
- for (const requestPlugin of requestPlugins) {
65
- const result = requestPlugin(action);
66
- if (!result)
67
- continue;
68
- const newHandler = Object.create(handler);
69
- useAction.actionContext.set(newHandler, action);
70
- innet__default["default"](result, newHandler);
71
- return;
72
- }
57
+ const { req, res } = action;
73
58
  if (url === (prefix || '')) {
74
59
  res.setHeader('Content-Type', 'application/json');
75
60
  res.write(JSONString.JSONString(docs));
76
61
  res.end();
77
- return;
62
+ return null;
78
63
  }
79
64
  const method = ((_c = (_b = req.method) === null || _b === void 0 ? void 0 : _b.toLowerCase()) !== null && _c !== void 0 ? _c : 'get');
80
65
  const rawSplitPath = url.slice(prefix.length).split('/').slice(1);
@@ -150,24 +135,28 @@ const api = () => {
150
135
  if (checkActionRules(bodyRules, 'body'))
151
136
  return true;
152
137
  }
153
- const newHandler = Object.create(runEndpoint.handler);
154
- useParams.paramsContext.set(newHandler, params);
155
- useAction.actionContext.set(newHandler, action);
156
- 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
+ }
157
146
  return true;
158
147
  });
159
148
  }
160
- 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) {
161
150
  if (!(yield run((_f = currentEndpoint.static) === null || _f === void 0 ? void 0 : _f[key], params)))
162
151
  continue;
163
- return;
152
+ return null;
164
153
  }
165
154
  if (currentEndpoint.dynamic) {
166
155
  for (const dynamicEndpoint of currentEndpoint.dynamic) {
167
- if (dynamicEndpoint.content) {
156
+ if (dynamicEndpoint.plugins) {
168
157
  if (!(yield run(dynamicEndpoint, Object.assign(Object.assign({}, params), { [dynamicEndpoint.key.slice(1, -1)]: key }))))
169
158
  continue;
170
- return;
159
+ return null;
171
160
  }
172
161
  }
173
162
  }
@@ -182,20 +171,17 @@ const api = () => {
182
171
  }
183
172
  }
184
173
  }
185
- if (context.fallback) {
186
- const newHandler = Object.create(context.fallback.handler);
174
+ for (const plugin of plugins) {
175
+ const result = yield plugin();
176
+ if (result === undefined)
177
+ continue;
178
+ const newHandler = Object.create(handler);
187
179
  useAction.actionContext.set(newHandler, action);
188
- innet__default["default"](context.fallback.children, newHandler);
189
- }
190
- else {
191
- res.statusCode = 404;
192
- res.end();
180
+ innet__default["default"](result, newHandler);
181
+ return null;
193
182
  }
194
- });
195
- server.on('request', listener);
196
- watchState.onDestroy(() => {
197
- server.off('request', listener);
198
- });
183
+ }));
184
+ innet__default["default"](children, handler);
199
185
  };
200
186
 
201
187
  exports.api = api;