@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
@@ -0,0 +1,22 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsx = require('@innet/jsx');
6
+ require('../../../hooks/index.js');
7
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
8
+ var useAction = require('../../../hooks/useAction/useAction.js');
9
+
10
+ function blacklist() {
11
+ const { ip = process.env.INNET_BLACKLIST_IP, } = jsx.useProps() || {};
12
+ const children = jsx.useChildren();
13
+ const ips = typeof ip === 'string' ? ip.split(',') : ip;
14
+ useServerPlugin.useServerPlugin(() => {
15
+ const action = useAction.useAction();
16
+ if (!action.clientIp || (ips === null || ips === void 0 ? void 0 : ips.includes(action.clientIp))) {
17
+ return children;
18
+ }
19
+ });
20
+ }
21
+
22
+ exports.blacklist = blacklist;
@@ -0,0 +1 @@
1
+ export * from './blacklist';
@@ -0,0 +1 @@
1
+ export { blacklist } from './blacklist.es6.js';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var blacklist = require('./blacklist.js');
6
+
7
+
8
+
9
+ exports.blacklist = blacklist.blacklist;
@@ -1,6 +1,6 @@
1
1
  import { type HandlerPlugin } from 'innet';
2
- import { type Config } from 'dtsgenerator/dist/core/config';
3
- export interface DtsProps extends Partial<Config> {
4
- path: string;
2
+ export interface DtsProps {
3
+ path?: string;
4
+ namespace?: string;
5
5
  }
6
6
  export declare const dts: HandlerPlugin;
@@ -1,34 +1,15 @@
1
- import { __rest, __awaiter } from 'tslib';
2
1
  import { useProps } from '@innet/jsx';
3
- import dtsGenerator, { parseSchema } from 'dtsgenerator';
4
- import fs from 'node:fs';
2
+ import { promises } from 'node:fs';
5
3
  import '../../../hooks/index.es6.js';
4
+ import '../../../utils/index.es6.js';
6
5
  import { useApi } from '../../../hooks/useApi/useApi.es6.js';
6
+ import { generateTypes } from '../../../utils/generateTypes/generateTypes.es6.js';
7
7
 
8
8
  const dts = () => {
9
- const _a = useProps(), { path } = _a, config = __rest(_a, ["path"]);
9
+ const { path = process.env.INNET_DTS_PATH || 'src/apiTypes.d.ts', namespace = process.env.INNET_DTS_NAMESPACE, } = useProps() || {};
10
10
  const { docs } = useApi();
11
- dtsGenerator({
12
- contents: [parseSchema(JSON.parse(JSON.stringify(docs)))],
13
- config,
14
- }).then((content) => __awaiter(void 0, void 0, void 0, function* () {
15
- yield fs.promises.writeFile(path, `interface Bin {
16
- filename: string
17
- fieldName: string
18
- originalFilename: string
19
- path: string
20
- type: string
21
- disposition: string
22
- size: number
23
- extension?: string
24
- }
25
- ${content
26
- .replaceAll(';', '')
27
- .replaceAll('number // int64', 'bigint')
28
- .replaceAll('string // binary', 'Bin')
29
- .replaceAll('string // date-time', 'Date')}`);
30
- })).catch(error => {
31
- console.warn(error);
11
+ promises.writeFile(path, generateTypes(docs, namespace)).catch(e => {
12
+ console.error(e);
32
13
  });
33
14
  };
34
15
 
@@ -2,42 +2,18 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var tslib = require('tslib');
6
5
  var jsx = require('@innet/jsx');
7
- var dtsGenerator = require('dtsgenerator');
8
6
  var fs = require('node:fs');
9
7
  require('../../../hooks/index.js');
8
+ require('../../../utils/index.js');
10
9
  var useApi = require('../../../hooks/useApi/useApi.js');
11
-
12
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
13
-
14
- var dtsGenerator__default = /*#__PURE__*/_interopDefaultLegacy(dtsGenerator);
15
- var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
10
+ var generateTypes = require('../../../utils/generateTypes/generateTypes.js');
16
11
 
17
12
  const dts = () => {
18
- const _a = jsx.useProps(), { path } = _a, config = tslib.__rest(_a, ["path"]);
13
+ const { path = process.env.INNET_DTS_PATH || 'src/apiTypes.d.ts', namespace = process.env.INNET_DTS_NAMESPACE, } = jsx.useProps() || {};
19
14
  const { docs } = useApi.useApi();
20
- dtsGenerator__default["default"]({
21
- contents: [dtsGenerator.parseSchema(JSON.parse(JSON.stringify(docs)))],
22
- config,
23
- }).then((content) => tslib.__awaiter(void 0, void 0, void 0, function* () {
24
- yield fs__default["default"].promises.writeFile(path, `interface Bin {
25
- filename: string
26
- fieldName: string
27
- originalFilename: string
28
- path: string
29
- type: string
30
- disposition: string
31
- size: number
32
- extension?: string
33
- }
34
- ${content
35
- .replaceAll(';', '')
36
- .replaceAll('number // int64', 'bigint')
37
- .replaceAll('string // binary', 'Bin')
38
- .replaceAll('string // date-time', 'Date')}`);
39
- })).catch(error => {
40
- console.warn(error);
15
+ fs.promises.writeFile(path, generateTypes.generateTypes(docs, namespace)).catch(e => {
16
+ console.error(e);
41
17
  });
42
18
  };
43
19
 
@@ -2,3 +2,6 @@ export * from './swagger';
2
2
  export * from './dts';
3
3
  export * from './dev';
4
4
  export * from './prod';
5
+ export * from './protection';
6
+ export * from './blacklist';
7
+ export * from './whitelist';
@@ -2,3 +2,6 @@ import './swagger/index.es6.js';
2
2
  import './dts/index.es6.js';
3
3
  import './dev/index.es6.js';
4
4
  import './prod/index.es6.js';
5
+ import './protection/index.es6.js';
6
+ import './blacklist/index.es6.js';
7
+ import './whitelist/index.es6.js';
@@ -4,4 +4,7 @@ require('./swagger/index.js');
4
4
  require('./dts/index.js');
5
5
  require('./dev/index.js');
6
6
  require('./prod/index.js');
7
+ require('./protection/index.js');
8
+ require('./blacklist/index.js');
9
+ require('./whitelist/index.js');
7
10
 
@@ -0,0 +1 @@
1
+ export * from './protection';
@@ -0,0 +1 @@
1
+ export { protection } from './protection.es6.js';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var protection = require('./protection.js');
6
+
7
+
8
+
9
+ exports.protection = protection.protection;
@@ -0,0 +1,8 @@
1
+ export interface ProtectionProps {
2
+ value?: string;
3
+ maxAge?: number;
4
+ excludeIp?: string | string[];
5
+ cookieKey?: string;
6
+ searchKey?: string;
7
+ }
8
+ export declare function protection(): void;
@@ -0,0 +1,36 @@
1
+ import { useProps, useChildren } from '@innet/jsx';
2
+ import '../../../hooks/index.es6.js';
3
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
4
+ import { useAction } from '../../../hooks/useAction/useAction.es6.js';
5
+
6
+ function protection() {
7
+ const { maxAge = Number(process.env.INNET_PROTECTION_MAX_AGE) || 365 * 24 * 60 * 60, value = process.env.INNET_PROTECTION, excludeIp = process.env.INNET_PROTECTED_IP, cookieKey = process.env.INNET_PROTECTION_COOKIE_KEY || 'protection', searchKey = process.env.INNET_PROTECTION_SEARCH_KEY || 'protection', } = useProps() || {};
8
+ const children = useChildren();
9
+ if (!value)
10
+ return;
11
+ const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
12
+ useServerPlugin(() => {
13
+ const action = useAction();
14
+ if (!action.clientIp)
15
+ return children;
16
+ if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
17
+ return;
18
+ const { [cookieKey]: cookieProtection } = action.cookies;
19
+ if (cookieProtection && cookieProtection === value)
20
+ return;
21
+ const { [searchKey]: searchProtection } = action.search;
22
+ if (searchProtection && searchProtection === value) {
23
+ action.setCookie(cookieKey, value, {
24
+ maxAge,
25
+ httpOnly: true,
26
+ secure: true,
27
+ path: '/',
28
+ });
29
+ return;
30
+ }
31
+ action.setCookie(cookieKey);
32
+ return children;
33
+ });
34
+ }
35
+
36
+ export { protection };
@@ -0,0 +1,40 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsx = require('@innet/jsx');
6
+ require('../../../hooks/index.js');
7
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
8
+ var useAction = require('../../../hooks/useAction/useAction.js');
9
+
10
+ function protection() {
11
+ const { maxAge = Number(process.env.INNET_PROTECTION_MAX_AGE) || 365 * 24 * 60 * 60, value = process.env.INNET_PROTECTION, excludeIp = process.env.INNET_PROTECTED_IP, cookieKey = process.env.INNET_PROTECTION_COOKIE_KEY || 'protection', searchKey = process.env.INNET_PROTECTION_SEARCH_KEY || 'protection', } = jsx.useProps() || {};
12
+ const children = jsx.useChildren();
13
+ if (!value)
14
+ return;
15
+ const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
16
+ useServerPlugin.useServerPlugin(() => {
17
+ const action = useAction.useAction();
18
+ if (!action.clientIp)
19
+ return children;
20
+ if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
21
+ return;
22
+ const { [cookieKey]: cookieProtection } = action.cookies;
23
+ if (cookieProtection && cookieProtection === value)
24
+ return;
25
+ const { [searchKey]: searchProtection } = action.search;
26
+ if (searchProtection && searchProtection === value) {
27
+ action.setCookie(cookieKey, value, {
28
+ maxAge,
29
+ httpOnly: true,
30
+ secure: true,
31
+ path: '/',
32
+ });
33
+ return;
34
+ }
35
+ action.setCookie(cookieKey);
36
+ return children;
37
+ });
38
+ }
39
+
40
+ exports.protection = protection;
@@ -1,27 +1,25 @@
1
1
  import { useProps } from '@innet/jsx';
2
- import { onDestroy } from 'watch-state';
3
2
  import '../../../hooks/index.es6.js';
4
3
  import html from './swagger.html.es6.js';
5
4
  import { useApi } from '../../../hooks/useApi/useApi.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
- const { path = '/swagger-ui' } = useProps() || {};
9
- const { docs, requestPlugins, prefix } = useApi();
9
+ const { path = process.env.INNET_SWAGGER_PATH || '/swagger-ui', } = useProps() || {};
10
+ const { docs, prefix } = useApi();
10
11
  let swaggerResponse;
11
- const listener = (req, res) => {
12
- if (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
  }
16
- res.statusCode = 200;
17
- res.write(swaggerResponse);
18
- res.end();
18
+ action.res.statusCode = 200;
19
+ action.res.write(swaggerResponse);
20
+ action.res.end();
19
21
  return true;
20
22
  }
21
- };
22
- requestPlugins.add(listener);
23
- onDestroy(() => {
24
- requestPlugins.delete(listener);
25
23
  });
26
24
  };
27
25
 
@@ -3,29 +3,27 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var jsx = require('@innet/jsx');
6
- var watchState = require('watch-state');
7
6
  require('../../../hooks/index.js');
8
7
  var swagger$1 = require('./swagger.html.js');
9
8
  var useApi = require('../../../hooks/useApi/useApi.js');
9
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
10
+ var useAction = require('../../../hooks/useAction/useAction.js');
10
11
 
11
12
  const swagger = () => {
12
- const { path = '/swagger-ui' } = jsx.useProps() || {};
13
- const { docs, requestPlugins, prefix } = useApi.useApi();
13
+ const { path = process.env.INNET_SWAGGER_PATH || '/swagger-ui', } = jsx.useProps() || {};
14
+ const { docs, prefix } = useApi.useApi();
14
15
  let swaggerResponse;
15
- const listener = (req, res) => {
16
- if (req.url === prefix + path) {
16
+ useServerPlugin.useServerPlugin(() => {
17
+ const action = useAction.useAction();
18
+ if (action.path === prefix + path) {
17
19
  if (!swaggerResponse) {
18
20
  swaggerResponse = swagger$1["default"].replace('spec: {},', `spec: ${JSON.stringify(docs)},`);
19
21
  }
20
- res.statusCode = 200;
21
- res.write(swaggerResponse);
22
- res.end();
22
+ action.res.statusCode = 200;
23
+ action.res.write(swaggerResponse);
24
+ action.res.end();
23
25
  return true;
24
26
  }
25
- };
26
- requestPlugins.add(listener);
27
- watchState.onDestroy(() => {
28
- requestPlugins.delete(listener);
29
27
  });
30
28
  };
31
29
 
@@ -0,0 +1 @@
1
+ export * from './whitelist';
@@ -0,0 +1 @@
1
+ export { whitelist } from './whitelist.es6.js';
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var whitelist = require('./whitelist.js');
6
+
7
+
8
+
9
+ exports.whitelist = whitelist.whitelist;
@@ -0,0 +1,4 @@
1
+ export interface WhitelistProps {
2
+ ip?: string | string[];
3
+ }
4
+ export declare function whitelist(): void;
@@ -0,0 +1,20 @@
1
+ import { useProps, useChildren } from '@innet/jsx';
2
+ import '../../../hooks/index.es6.js';
3
+ import { useServerPlugin } from '../../../hooks/useServerPlugin/useServerPlugin.es6.js';
4
+ import { useAction } from '../../../hooks/useAction/useAction.es6.js';
5
+
6
+ function whitelist() {
7
+ const { ip = process.env.INNET_WHITELIST_IP, } = useProps() || {};
8
+ const children = useChildren();
9
+ const ips = typeof ip === 'string' ? ip.split(',') : ip;
10
+ if (!ips)
11
+ return;
12
+ useServerPlugin(() => {
13
+ const action = useAction();
14
+ if (!action.clientIp || !ips.includes(action.clientIp)) {
15
+ return children;
16
+ }
17
+ });
18
+ }
19
+
20
+ export { whitelist };
@@ -0,0 +1,24 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var jsx = require('@innet/jsx');
6
+ require('../../../hooks/index.js');
7
+ var useServerPlugin = require('../../../hooks/useServerPlugin/useServerPlugin.js');
8
+ var useAction = require('../../../hooks/useAction/useAction.js');
9
+
10
+ function whitelist() {
11
+ const { ip = process.env.INNET_WHITELIST_IP, } = jsx.useProps() || {};
12
+ const children = jsx.useChildren();
13
+ const ips = typeof ip === 'string' ? ip.split(',') : ip;
14
+ if (!ips)
15
+ return;
16
+ useServerPlugin.useServerPlugin(() => {
17
+ const action = useAction.useAction();
18
+ if (!action.clientIp || !ips.includes(action.clientIp)) {
19
+ return children;
20
+ }
21
+ });
22
+ }
23
+
24
+ exports.whitelist = whitelist;
package/types.d.ts CHANGED
@@ -1,8 +1,6 @@
1
- /// <reference types="node" />
2
- import type { IncomingMessage, ServerResponse } from 'http';
3
- import type { Handler } from 'innet';
4
1
  import type { OpenAPIV3_1 as API } from 'openapi-types';
5
2
  import type { ApiErrorValue } from './constants';
3
+ import { type ServerPlugin } from './hooks';
6
4
  import { type Rule, type RulesErrors } from './utils/rules';
7
5
  export type TagObject = API.TagObject;
8
6
  export type Document = API.Document;
@@ -38,10 +36,9 @@ export interface EndpointRulesMaps {
38
36
  }
39
37
  export interface Endpoint<I = unknown, O = unknown, E extends ApiErrorValue = ApiErrorValue, P extends InValidationErrorParam = InValidationErrorParam, D extends ValidationErrorData = ValidationErrorData> {
40
38
  key: string;
41
- content?: any;
39
+ plugins: Set<ServerPlugin>;
42
40
  rules?: EndpointRules;
43
41
  rulesMaps?: EndpointRulesMaps;
44
- handler?: Handler;
45
42
  static?: Record<string, Endpoint<I, O, E, P, D>>;
46
43
  dynamic?: Endpoint<I, O, E, P, D>[];
47
44
  }
@@ -69,8 +66,3 @@ export interface BaseSchemaProps<T> {
69
66
  export interface ValuesSchemaProps<T> extends BaseSchemaProps<T> {
70
67
  values?: T[];
71
68
  }
72
- export interface Fallback {
73
- children: any;
74
- handler: Handler;
75
- }
76
- export type RequestPlugin = (req: IncomingMessage, res: ServerResponse) => boolean | undefined;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
- import type http from 'http';
3
- import { type IncomingHttpHeaders } from 'http';
2
+ import { type CookieSerializeOptions } from 'cookie';
3
+ import { type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from 'http';
4
4
  import { type ParsedQs } from 'qs';
5
5
  import { type BodyType } from '../../types';
6
6
  export declare const URL_PARSER: RegExp;
@@ -10,8 +10,9 @@ export interface ParsedUrl {
10
10
  }
11
11
  export declare class Action {
12
12
  #private;
13
- private readonly req;
14
- constructor(req: http.IncomingMessage);
13
+ readonly req: IncomingMessage;
14
+ readonly res: ServerResponse;
15
+ constructor(req: IncomingMessage, res: ServerResponse);
15
16
  get parsedUrl(): ParsedUrl;
16
17
  get path(): string;
17
18
  get originSearch(): ParsedQs;
@@ -26,4 +27,6 @@ export declare class Action {
26
27
  get bodyType(): BodyType | undefined;
27
28
  body?: object;
28
29
  parseBody(): Promise<void>;
30
+ setCookie(name: string, value?: string, options?: CookieSerializeOptions): void;
31
+ get clientIp(): string | null;
29
32
  }
@@ -1,5 +1,6 @@
1
1
  import { __runInitializers, __classPrivateFieldGet, __classPrivateFieldSet, __awaiter, __esDecorate } from 'tslib';
2
- import cookie from 'cookie';
2
+ import cookieLib from 'cookie';
3
+ import { getClientIp } from 'request-ip';
3
4
  import '../decorators/index.es6.js';
4
5
  import '../parseBody/index.es6.js';
5
6
  import '../parseFormBody/index.es6.js';
@@ -19,16 +20,18 @@ let Action = (() => {
19
20
  let _get_originCookies_decorators;
20
21
  let _get_bodyType_decorators;
21
22
  let _parseBody_decorators;
23
+ let _get_clientIp_decorators;
22
24
  return _a = class Action {
23
- constructor(req) {
25
+ constructor(req, res) {
24
26
  this.req = (__runInitializers(this, _instanceExtraInitializers), req);
27
+ this.res = res;
25
28
  _Action_search.set(this, void 0);
26
29
  _Action_headers.set(this, {});
27
30
  _Action_cookie.set(this, {});
28
31
  }
29
32
  get parsedUrl() {
30
- var _a;
31
- const match = (_a = this.req.url) === null || _a === void 0 ? void 0 : _a.match(URL_PARSER);
33
+ var _b;
34
+ const match = (_b = this.req.url) === null || _b === void 0 ? void 0 : _b.match(URL_PARSER);
32
35
  if (!match)
33
36
  throw Error('cannot parse url');
34
37
  const result = match.groups;
@@ -65,8 +68,8 @@ let Action = (() => {
65
68
  __classPrivateFieldSet(this, _Action_headers, value, "f");
66
69
  }
67
70
  get originCookies() {
68
- var _a;
69
- return cookie.parse((_a = this.req.headers.cookie) !== null && _a !== void 0 ? _a : '');
71
+ var _b;
72
+ return cookieLib.parse((_b = this.req.headers.cookie) !== null && _b !== void 0 ? _b : '');
70
73
  }
71
74
  get cookies() {
72
75
  if (__classPrivateFieldGet(this, _Action_cookie, "f"))
@@ -103,21 +106,42 @@ let Action = (() => {
103
106
  }
104
107
  });
105
108
  }
109
+ setCookie(name, value, options) {
110
+ let cookies = this.res.getHeader('Set-Cookie');
111
+ if (typeof cookies === 'string') {
112
+ cookies = [cookies];
113
+ }
114
+ const normValue = typeof value === 'string' ? cookieLib.serialize(name, value, options) : `${name}=; max-age=0`;
115
+ if (cookies) {
116
+ cookies.push(normValue);
117
+ }
118
+ else {
119
+ cookies = normValue;
120
+ }
121
+ this.res.setHeader('Set-Cookie', cookies);
122
+ }
123
+ get clientIp() {
124
+ return getClientIp(this.req);
125
+ }
106
126
  },
107
127
  _Action_search = new WeakMap(),
108
128
  _Action_headers = new WeakMap(),
109
129
  _Action_cookie = new WeakMap(),
110
130
  (() => {
131
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
111
132
  _get_parsedUrl_decorators = [once];
112
133
  _get_originSearch_decorators = [once];
113
134
  _get_originCookies_decorators = [once];
114
135
  _get_bodyType_decorators = [once];
115
136
  _parseBody_decorators = [once];
116
- __esDecorate(_a, null, _get_parsedUrl_decorators, { kind: "getter", name: "parsedUrl", static: false, private: false, access: { has: obj => "parsedUrl" in obj, get: obj => obj.parsedUrl } }, null, _instanceExtraInitializers);
117
- __esDecorate(_a, null, _get_originSearch_decorators, { kind: "getter", name: "originSearch", static: false, private: false, access: { has: obj => "originSearch" in obj, get: obj => obj.originSearch } }, null, _instanceExtraInitializers);
118
- __esDecorate(_a, null, _get_originCookies_decorators, { kind: "getter", name: "originCookies", static: false, private: false, access: { has: obj => "originCookies" in obj, get: obj => obj.originCookies } }, null, _instanceExtraInitializers);
119
- __esDecorate(_a, null, _get_bodyType_decorators, { kind: "getter", name: "bodyType", static: false, private: false, access: { has: obj => "bodyType" in obj, get: obj => obj.bodyType } }, null, _instanceExtraInitializers);
120
- __esDecorate(_a, null, _parseBody_decorators, { kind: "method", name: "parseBody", static: false, private: false, access: { has: obj => "parseBody" in obj, get: obj => obj.parseBody } }, null, _instanceExtraInitializers);
137
+ _get_clientIp_decorators = [once];
138
+ __esDecorate(_a, null, _get_parsedUrl_decorators, { kind: "getter", name: "parsedUrl", static: false, private: false, access: { has: obj => "parsedUrl" in obj, get: obj => obj.parsedUrl }, metadata: _metadata }, null, _instanceExtraInitializers);
139
+ __esDecorate(_a, null, _get_originSearch_decorators, { kind: "getter", name: "originSearch", static: false, private: false, access: { has: obj => "originSearch" in obj, get: obj => obj.originSearch }, metadata: _metadata }, null, _instanceExtraInitializers);
140
+ __esDecorate(_a, null, _get_originCookies_decorators, { kind: "getter", name: "originCookies", static: false, private: false, access: { has: obj => "originCookies" in obj, get: obj => obj.originCookies }, metadata: _metadata }, null, _instanceExtraInitializers);
141
+ __esDecorate(_a, null, _get_bodyType_decorators, { kind: "getter", name: "bodyType", static: false, private: false, access: { has: obj => "bodyType" in obj, get: obj => obj.bodyType }, metadata: _metadata }, null, _instanceExtraInitializers);
142
+ __esDecorate(_a, null, _parseBody_decorators, { kind: "method", name: "parseBody", static: false, private: false, access: { has: obj => "parseBody" in obj, get: obj => obj.parseBody }, metadata: _metadata }, null, _instanceExtraInitializers);
143
+ __esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp }, metadata: _metadata }, null, _instanceExtraInitializers);
144
+ if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
121
145
  })(),
122
146
  _a;
123
147
  })();