@innet/server 2.0.0-alpha.0 → 2.0.0-alpha.10

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 (111) hide show
  1. package/README.md +309 -29
  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 +3 -1
  27. package/handler/handler.es6.js +2 -0
  28. package/handler/handler.js +2 -0
  29. package/hooks/index.d.ts +2 -0
  30. package/hooks/index.es6.js +2 -0
  31. package/hooks/index.js +2 -0
  32. package/hooks/useClientIp/index.d.ts +1 -0
  33. package/hooks/useClientIp/index.es6.js +1 -0
  34. package/hooks/useClientIp/index.js +9 -0
  35. package/hooks/useClientIp/useClientIp.d.ts +1 -0
  36. package/hooks/useClientIp/useClientIp.es6.js +9 -0
  37. package/hooks/useClientIp/useClientIp.js +13 -0
  38. package/hooks/useComponentName/useComponentName.d.ts +1 -1
  39. package/hooks/useRequestPlugin/index.d.ts +1 -0
  40. package/hooks/useRequestPlugin/index.es6.js +1 -0
  41. package/hooks/useRequestPlugin/index.js +9 -0
  42. package/hooks/useRequestPlugin/useRequestPlugin.d.ts +2 -0
  43. package/hooks/useRequestPlugin/useRequestPlugin.es6.js +13 -0
  44. package/hooks/useRequestPlugin/useRequestPlugin.js +17 -0
  45. package/index.es6.js +4 -0
  46. package/index.js +9 -0
  47. package/package.json +2 -2
  48. package/plugins/main/api/api.es6.js +18 -11
  49. package/plugins/main/api/api.js +18 -11
  50. package/plugins/main/response/response.d.ts +1 -1
  51. package/plugins/main/response/response.es6.js +1 -1
  52. package/plugins/main/response/response.js +1 -1
  53. package/plugins/main/server/server.es6.js +10 -4
  54. package/plugins/main/server/server.js +10 -4
  55. package/plugins/request/cookie/cookie.es6.js +3 -19
  56. package/plugins/request/cookie/cookie.js +3 -23
  57. package/plugins/request/success/success.d.ts +1 -0
  58. package/plugins/request/success/success.es6.js +13 -4
  59. package/plugins/request/success/success.js +13 -4
  60. package/plugins/schema/boolean/boolean.es6.js +2 -4
  61. package/plugins/schema/boolean/boolean.js +1 -3
  62. package/plugins/utils/dts/dts.d.ts +1 -2
  63. package/plugins/utils/dts/dts.es6.js +6 -25
  64. package/plugins/utils/dts/dts.js +5 -29
  65. package/plugins/utils/index.d.ts +1 -0
  66. package/plugins/utils/index.es6.js +1 -0
  67. package/plugins/utils/index.js +1 -0
  68. package/plugins/utils/protection/index.d.ts +1 -0
  69. package/plugins/utils/protection/index.es6.js +1 -0
  70. package/plugins/utils/protection/index.js +9 -0
  71. package/plugins/utils/protection/protection.d.ts +8 -0
  72. package/plugins/utils/protection/protection.es6.js +34 -0
  73. package/plugins/utils/protection/protection.js +38 -0
  74. package/plugins/utils/swagger/swagger.es6.js +7 -11
  75. package/plugins/utils/swagger/swagger.js +7 -11
  76. package/types.d.ts +2 -3
  77. package/utils/action/Action.d.ts +7 -4
  78. package/utils/action/Action.es6.js +25 -3
  79. package/utils/action/Action.js +26 -4
  80. package/utils/generateTypes/generateTypes.d.ts +3 -0
  81. package/utils/generateTypes/generateTypes.es6.js +102 -0
  82. package/utils/generateTypes/generateTypes.js +107 -0
  83. package/utils/generateTypes/generateTypes.test.d.ts +1 -0
  84. package/utils/generateTypes/index.d.ts +1 -0
  85. package/utils/generateTypes/index.es6.js +1 -0
  86. package/utils/generateTypes/index.js +10 -0
  87. package/utils/index.d.ts +1 -0
  88. package/utils/index.es6.js +1 -0
  89. package/utils/index.js +1 -0
  90. package/utils/rules/objectOf/objectOf.es6.js +4 -1
  91. package/utils/rules/objectOf/objectOf.js +4 -1
  92. package/example/requests/partners/EditPartner/EditPartner.d.ts +0 -1
  93. package/example/requests/partners/EditPartner/index.d.ts +0 -1
  94. package/example/requests/partners/GetPartner/GetPartner.d.ts +0 -1
  95. package/example/requests/partners/GetPartner/index.d.ts +0 -1
  96. package/example/requests/partners/GetPartners/GetPartners.d.ts +0 -1
  97. package/example/requests/partners/GetPartners/index.d.ts +0 -1
  98. package/example/requests/partners/index.d.ts +0 -3
  99. package/example/schemas/address/AddressSchema/AddressSchema.d.ts +0 -1
  100. package/example/schemas/address/AddressSchema/index.d.ts +0 -1
  101. package/example/schemas/address/index.d.ts +0 -1
  102. package/example/schemas/location/LocationSchema/LocationSchema.d.ts +0 -1
  103. package/example/schemas/location/LocationSchema/index.d.ts +0 -1
  104. package/example/schemas/location/index.d.ts +0 -1
  105. package/example/schemas/partner/EditPartnerSchema/EditPartnerSchema.d.ts +0 -1
  106. package/example/schemas/partner/EditPartnerSchema/index.d.ts +0 -1
  107. package/example/schemas/partner/PartnerSchema/PartnerSchema.d.ts +0 -1
  108. package/example/schemas/partner/PartnerSchema/index.d.ts +0 -1
  109. package/example/schemas/partner/index.d.ts +0 -2
  110. package/example/tags/Partner/Partner.d.ts +0 -1
  111. package/example/tags/Partner/index.d.ts +0 -1
@@ -0,0 +1,34 @@
1
+ import { useProps, useChildren } from '@innet/jsx';
2
+ import '../../../hooks/index.es6.js';
3
+ import { useRequestPlugin } from '../../../hooks/useRequestPlugin/useRequestPlugin.es6.js';
4
+
5
+ function protection() {
6
+ 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() || {};
7
+ const children = useChildren();
8
+ if (!value)
9
+ return;
10
+ const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
11
+ useRequestPlugin(action => {
12
+ if (!action.clientIp)
13
+ return children;
14
+ if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
15
+ return;
16
+ const { [cookieKey]: cookieProtection } = action.cookies;
17
+ if (cookieProtection && cookieProtection === value)
18
+ return;
19
+ const { [searchKey]: searchProtection } = action.search;
20
+ if (searchProtection && searchProtection === value) {
21
+ action.setCookie(cookieKey, value, {
22
+ maxAge,
23
+ httpOnly: true,
24
+ secure: true,
25
+ path: '/',
26
+ });
27
+ return;
28
+ }
29
+ action.setCookie(cookieKey);
30
+ return children;
31
+ });
32
+ }
33
+
34
+ export { protection };
@@ -0,0 +1,38 @@
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 useRequestPlugin = require('../../../hooks/useRequestPlugin/useRequestPlugin.js');
8
+
9
+ function protection() {
10
+ 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() || {};
11
+ const children = jsx.useChildren();
12
+ if (!value)
13
+ return;
14
+ const excludeIps = Array.isArray(excludeIp) ? excludeIp : excludeIp === null || excludeIp === void 0 ? void 0 : excludeIp.split(',');
15
+ useRequestPlugin.useRequestPlugin(action => {
16
+ if (!action.clientIp)
17
+ return children;
18
+ if (excludeIps === null || excludeIps === void 0 ? void 0 : excludeIps.includes(action.clientIp))
19
+ return;
20
+ const { [cookieKey]: cookieProtection } = action.cookies;
21
+ if (cookieProtection && cookieProtection === value)
22
+ return;
23
+ const { [searchKey]: searchProtection } = action.search;
24
+ if (searchProtection && searchProtection === value) {
25
+ action.setCookie(cookieKey, value, {
26
+ maxAge,
27
+ httpOnly: true,
28
+ secure: true,
29
+ path: '/',
30
+ });
31
+ return;
32
+ }
33
+ action.setCookie(cookieKey);
34
+ return children;
35
+ });
36
+ }
37
+
38
+ exports.protection = protection;
@@ -1,27 +1,23 @@
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 { useRequestPlugin } from '../../../hooks/useRequestPlugin/useRequestPlugin.es6.js';
6
6
 
7
7
  const swagger = () => {
8
8
  const { path = '/swagger-ui' } = useProps() || {};
9
- const { docs, requestPlugins, prefix } = useApi();
9
+ const { docs, prefix } = useApi();
10
10
  let swaggerResponse;
11
- const listener = (req, res) => {
12
- if (req.url === prefix + path) {
11
+ useRequestPlugin(action => {
12
+ if (action.req.url === prefix + path) {
13
13
  if (!swaggerResponse) {
14
14
  swaggerResponse = html.replace('spec: {},', `spec: ${JSON.stringify(docs)},`);
15
15
  }
16
- res.statusCode = 200;
17
- res.write(swaggerResponse);
18
- res.end();
16
+ action.res.statusCode = 200;
17
+ action.res.write(swaggerResponse);
18
+ action.res.end();
19
19
  return true;
20
20
  }
21
- };
22
- requestPlugins.add(listener);
23
- onDestroy(() => {
24
- requestPlugins.delete(listener);
25
21
  });
26
22
  };
27
23
 
@@ -3,29 +3,25 @@
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 useRequestPlugin = require('../../../hooks/useRequestPlugin/useRequestPlugin.js');
10
10
 
11
11
  const swagger = () => {
12
12
  const { path = '/swagger-ui' } = jsx.useProps() || {};
13
- const { docs, requestPlugins, prefix } = useApi.useApi();
13
+ const { docs, prefix } = useApi.useApi();
14
14
  let swaggerResponse;
15
- const listener = (req, res) => {
16
- if (req.url === prefix + path) {
15
+ useRequestPlugin.useRequestPlugin(action => {
16
+ if (action.req.url === prefix + path) {
17
17
  if (!swaggerResponse) {
18
18
  swaggerResponse = swagger$1["default"].replace('spec: {},', `spec: ${JSON.stringify(docs)},`);
19
19
  }
20
- res.statusCode = 200;
21
- res.write(swaggerResponse);
22
- res.end();
20
+ action.res.statusCode = 200;
21
+ action.res.write(swaggerResponse);
22
+ action.res.end();
23
23
  return true;
24
24
  }
25
- };
26
- requestPlugins.add(listener);
27
- watchState.onDestroy(() => {
28
- requestPlugins.delete(listener);
29
25
  });
30
26
  };
31
27
 
package/types.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- /// <reference types="node" />
2
- import type { IncomingMessage, ServerResponse } from 'http';
3
1
  import type { Handler } from 'innet';
4
2
  import type { OpenAPIV3_1 as API } from 'openapi-types';
5
3
  import type { ApiErrorValue } from './constants';
4
+ import { type Action } from './utils';
6
5
  import { type Rule, type RulesErrors } from './utils/rules';
7
6
  export type TagObject = API.TagObject;
8
7
  export type Document = API.Document;
@@ -73,4 +72,4 @@ export interface Fallback {
73
72
  children: any;
74
73
  handler: Handler;
75
74
  }
76
- export type RequestPlugin = (req: IncomingMessage, res: ServerResponse) => boolean | undefined;
75
+ export type RequestPlugin = (action: Action) => any;
@@ -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,9 +20,11 @@ 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, {});
@@ -66,7 +69,7 @@ let Action = (() => {
66
69
  }
67
70
  get originCookies() {
68
71
  var _a;
69
- return cookie.parse((_a = this.req.headers.cookie) !== null && _a !== void 0 ? _a : '');
72
+ return cookieLib.parse((_a = this.req.headers.cookie) !== null && _a !== void 0 ? _a : '');
70
73
  }
71
74
  get cookies() {
72
75
  if (__classPrivateFieldGet(this, _Action_cookie, "f"))
@@ -103,6 +106,23 @@ 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(),
@@ -113,11 +133,13 @@ let Action = (() => {
113
133
  _get_originCookies_decorators = [once];
114
134
  _get_bodyType_decorators = [once];
115
135
  _parseBody_decorators = [once];
136
+ _get_clientIp_decorators = [once];
116
137
  __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
138
  __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
139
  __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
140
  __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
141
  __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);
142
+ __esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp } }, null, _instanceExtraInitializers);
121
143
  })(),
122
144
  _a;
123
145
  })();
@@ -3,7 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var tslib = require('tslib');
6
- var cookie = require('cookie');
6
+ var cookieLib = require('cookie');
7
+ var requestIp = require('request-ip');
7
8
  require('../decorators/index.js');
8
9
  require('../parseBody/index.js');
9
10
  require('../parseFormBody/index.js');
@@ -16,7 +17,7 @@ var once = require('../decorators/once/once.js');
16
17
 
17
18
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
18
19
 
19
- var cookie__default = /*#__PURE__*/_interopDefaultLegacy(cookie);
20
+ var cookieLib__default = /*#__PURE__*/_interopDefaultLegacy(cookieLib);
20
21
 
21
22
  const URL_PARSER = /^(?<path>[^?]+)(\?(?<search>.*))?/;
22
23
  let Action = (() => {
@@ -27,9 +28,11 @@ let Action = (() => {
27
28
  let _get_originCookies_decorators;
28
29
  let _get_bodyType_decorators;
29
30
  let _parseBody_decorators;
31
+ let _get_clientIp_decorators;
30
32
  return _a = class Action {
31
- constructor(req) {
33
+ constructor(req, res) {
32
34
  this.req = (tslib.__runInitializers(this, _instanceExtraInitializers), req);
35
+ this.res = res;
33
36
  _Action_search.set(this, void 0);
34
37
  _Action_headers.set(this, {});
35
38
  _Action_cookie.set(this, {});
@@ -74,7 +77,7 @@ let Action = (() => {
74
77
  }
75
78
  get originCookies() {
76
79
  var _a;
77
- return cookie__default["default"].parse((_a = this.req.headers.cookie) !== null && _a !== void 0 ? _a : '');
80
+ return cookieLib__default["default"].parse((_a = this.req.headers.cookie) !== null && _a !== void 0 ? _a : '');
78
81
  }
79
82
  get cookies() {
80
83
  if (tslib.__classPrivateFieldGet(this, _Action_cookie, "f"))
@@ -111,6 +114,23 @@ let Action = (() => {
111
114
  }
112
115
  });
113
116
  }
117
+ setCookie(name, value, options) {
118
+ let cookies = this.res.getHeader('Set-Cookie');
119
+ if (typeof cookies === 'string') {
120
+ cookies = [cookies];
121
+ }
122
+ const normValue = typeof value === 'string' ? cookieLib__default["default"].serialize(name, value, options) : `${name}=; max-age=0`;
123
+ if (cookies) {
124
+ cookies.push(normValue);
125
+ }
126
+ else {
127
+ cookies = normValue;
128
+ }
129
+ this.res.setHeader('Set-Cookie', cookies);
130
+ }
131
+ get clientIp() {
132
+ return requestIp.getClientIp(this.req);
133
+ }
114
134
  },
115
135
  _Action_search = new WeakMap(),
116
136
  _Action_headers = new WeakMap(),
@@ -121,11 +141,13 @@ let Action = (() => {
121
141
  _get_originCookies_decorators = [once.once];
122
142
  _get_bodyType_decorators = [once.once];
123
143
  _parseBody_decorators = [once.once];
144
+ _get_clientIp_decorators = [once.once];
124
145
  tslib.__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);
125
146
  tslib.__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);
126
147
  tslib.__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);
127
148
  tslib.__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);
128
149
  tslib.__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);
150
+ tslib.__esDecorate(_a, null, _get_clientIp_decorators, { kind: "getter", name: "clientIp", static: false, private: false, access: { has: obj => "clientIp" in obj, get: obj => obj.clientIp } }, null, _instanceExtraInitializers);
129
151
  })(),
130
152
  _a;
131
153
  })();
@@ -0,0 +1,3 @@
1
+ import { type Document, type SchemaObject } from '../../types';
2
+ export declare function generateSchemaTypes(schema: SchemaObject, spaces?: number): string;
3
+ export declare function generateTypes(docs: Document): string;
@@ -0,0 +1,102 @@
1
+ function hasDefault(target) {
2
+ return Boolean(target && ('default' in target || 'x-default' in target));
3
+ }
4
+ function generateSchemaTypes(schema, spaces = 2) {
5
+ const space = [...new Array(spaces)].map(() => ' ').join('');
6
+ if ('$ref' in schema) {
7
+ return `Schemas.${schema.$ref.slice(21)}\n`;
8
+ }
9
+ if (schema.type === 'integer') {
10
+ return `${schema.format === 'int64' ? 'bigint' : 'number'}\n`;
11
+ }
12
+ if (schema.type === 'string') {
13
+ if (schema.format === 'date-time') {
14
+ return 'Date\n';
15
+ }
16
+ if (schema.format === 'binary') {
17
+ return 'Bin\n';
18
+ }
19
+ return 'string\n';
20
+ }
21
+ if (['boolean', 'number', 'null'].includes(schema.type)) {
22
+ return `${schema.type}\n`;
23
+ }
24
+ if (schema.type === 'array') {
25
+ if (!schema.items)
26
+ return 'any[]\n';
27
+ return `(${generateSchemaTypes(schema.items, spaces + 2).slice(0, -1)})[]\n`;
28
+ }
29
+ if (schema.type !== 'object') {
30
+ console.error('unknown type', schema);
31
+ return 'any\n';
32
+ }
33
+ let result = '{\n';
34
+ const required = schema.required || [];
35
+ for (const key in schema.properties) {
36
+ const prop = schema.properties[key];
37
+ const splitter = required.includes(key) || hasDefault(prop)
38
+ ? ':'
39
+ : '?:';
40
+ result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
41
+ }
42
+ return `${result}${space.slice(0, -2)}}\n`;
43
+ }
44
+ function generateTypes(docs) {
45
+ var _a;
46
+ let result = 'declare namespace Api {\n export interface Bin {\n filename: string\n fieldName: string\n originalFilename: string\n path: string\n type: string\n disposition: string\n size: number\n extension?: string\n }\n';
47
+ const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
48
+ const paths = docs.paths;
49
+ if (schemas) {
50
+ result += ' namespace Schemas {\n';
51
+ for (const name in schemas) {
52
+ result += ` export type ${name} = ${generateSchemaTypes(schemas[name], 6)}`;
53
+ }
54
+ result += ' }\n';
55
+ }
56
+ result += ' export interface Endpoints {\n';
57
+ for (const path in paths) {
58
+ const pathObject = paths[path];
59
+ for (const method in pathObject) {
60
+ // @ts-expect-error: FIXME
61
+ const endpoint = pathObject[method];
62
+ const parameters = endpoint.parameters;
63
+ const requestBody = endpoint.requestBody;
64
+ const responses = endpoint.responses;
65
+ result += ` ['${method.toUpperCase()}:${path}']: {\n`;
66
+ if (parameters) {
67
+ const params = {
68
+ query: '',
69
+ header: '',
70
+ path: '',
71
+ cookie: '',
72
+ };
73
+ for (const param of parameters) {
74
+ const splitter = param.in === 'path' || hasDefault(param.schema) ? ':' : '?:';
75
+ params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
76
+ }
77
+ if (params.path) {
78
+ result += ` Params: {\n${params.path} }\n`;
79
+ }
80
+ if (params.query) {
81
+ result += ` Search: {\n${params.query} }\n`;
82
+ }
83
+ if (params.header) {
84
+ result += ` Headers: {\n${params.header} }\n`;
85
+ }
86
+ if (params.cookie) {
87
+ result += ` Cookies: {\n${params.cookie} }\n`;
88
+ }
89
+ }
90
+ if (requestBody) {
91
+ result += ` Body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
92
+ }
93
+ if (responses === null || responses === void 0 ? void 0 : responses.default) {
94
+ result += ` Response: ${generateSchemaTypes(responses.default.content['application/json'].schema, 8)}`;
95
+ }
96
+ result += ' }\n';
97
+ }
98
+ }
99
+ return result + ' }\n}';
100
+ }
101
+
102
+ export { generateSchemaTypes, generateTypes };
@@ -0,0 +1,107 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ function hasDefault(target) {
6
+ return Boolean(target && ('default' in target || 'x-default' in target));
7
+ }
8
+ function generateSchemaTypes(schema, spaces = 2) {
9
+ const space = [...new Array(spaces)].map(() => ' ').join('');
10
+ if ('$ref' in schema) {
11
+ return `Schemas.${schema.$ref.slice(21)}\n`;
12
+ }
13
+ if (schema.type === 'integer') {
14
+ return `${schema.format === 'int64' ? 'bigint' : 'number'}\n`;
15
+ }
16
+ if (schema.type === 'string') {
17
+ if (schema.format === 'date-time') {
18
+ return 'Date\n';
19
+ }
20
+ if (schema.format === 'binary') {
21
+ return 'Bin\n';
22
+ }
23
+ return 'string\n';
24
+ }
25
+ if (['boolean', 'number', 'null'].includes(schema.type)) {
26
+ return `${schema.type}\n`;
27
+ }
28
+ if (schema.type === 'array') {
29
+ if (!schema.items)
30
+ return 'any[]\n';
31
+ return `(${generateSchemaTypes(schema.items, spaces + 2).slice(0, -1)})[]\n`;
32
+ }
33
+ if (schema.type !== 'object') {
34
+ console.error('unknown type', schema);
35
+ return 'any\n';
36
+ }
37
+ let result = '{\n';
38
+ const required = schema.required || [];
39
+ for (const key in schema.properties) {
40
+ const prop = schema.properties[key];
41
+ const splitter = required.includes(key) || hasDefault(prop)
42
+ ? ':'
43
+ : '?:';
44
+ result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
45
+ }
46
+ return `${result}${space.slice(0, -2)}}\n`;
47
+ }
48
+ function generateTypes(docs) {
49
+ var _a;
50
+ let result = 'declare namespace Api {\n export interface Bin {\n filename: string\n fieldName: string\n originalFilename: string\n path: string\n type: string\n disposition: string\n size: number\n extension?: string\n }\n';
51
+ const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
52
+ const paths = docs.paths;
53
+ if (schemas) {
54
+ result += ' namespace Schemas {\n';
55
+ for (const name in schemas) {
56
+ result += ` export type ${name} = ${generateSchemaTypes(schemas[name], 6)}`;
57
+ }
58
+ result += ' }\n';
59
+ }
60
+ result += ' export interface Endpoints {\n';
61
+ for (const path in paths) {
62
+ const pathObject = paths[path];
63
+ for (const method in pathObject) {
64
+ // @ts-expect-error: FIXME
65
+ const endpoint = pathObject[method];
66
+ const parameters = endpoint.parameters;
67
+ const requestBody = endpoint.requestBody;
68
+ const responses = endpoint.responses;
69
+ result += ` ['${method.toUpperCase()}:${path}']: {\n`;
70
+ if (parameters) {
71
+ const params = {
72
+ query: '',
73
+ header: '',
74
+ path: '',
75
+ cookie: '',
76
+ };
77
+ for (const param of parameters) {
78
+ const splitter = param.in === 'path' || hasDefault(param.schema) ? ':' : '?:';
79
+ params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
80
+ }
81
+ if (params.path) {
82
+ result += ` Params: {\n${params.path} }\n`;
83
+ }
84
+ if (params.query) {
85
+ result += ` Search: {\n${params.query} }\n`;
86
+ }
87
+ if (params.header) {
88
+ result += ` Headers: {\n${params.header} }\n`;
89
+ }
90
+ if (params.cookie) {
91
+ result += ` Cookies: {\n${params.cookie} }\n`;
92
+ }
93
+ }
94
+ if (requestBody) {
95
+ result += ` Body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
96
+ }
97
+ if (responses === null || responses === void 0 ? void 0 : responses.default) {
98
+ result += ` Response: ${generateSchemaTypes(responses.default.content['application/json'].schema, 8)}`;
99
+ }
100
+ result += ' }\n';
101
+ }
102
+ }
103
+ return result + ' }\n}';
104
+ }
105
+
106
+ exports.generateSchemaTypes = generateSchemaTypes;
107
+ exports.generateTypes = generateTypes;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export * from './generateTypes';
@@ -0,0 +1 @@
1
+ export { generateSchemaTypes, generateTypes } from './generateTypes.es6.js';
@@ -0,0 +1,10 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var generateTypes = require('./generateTypes.js');
6
+
7
+
8
+
9
+ exports.generateSchemaTypes = generateTypes.generateSchemaTypes;
10
+ exports.generateTypes = generateTypes.generateTypes;
package/utils/index.d.ts CHANGED
@@ -10,3 +10,4 @@ export * from './parseFormBody';
10
10
  export * from './rules';
11
11
  export * from './JSONString';
12
12
  export * from './FileData';
13
+ export * from './generateTypes';
@@ -10,3 +10,4 @@ import './parseFormBody/index.es6.js';
10
10
  import './rules/index.es6.js';
11
11
  import './JSONString/index.es6.js';
12
12
  import './FileData/index.es6.js';
13
+ import './generateTypes/index.es6.js';
package/utils/index.js CHANGED
@@ -12,4 +12,5 @@ require('./parseFormBody/index.js');
12
12
  require('./rules/index.js');
13
13
  require('./JSONString/index.js');
14
14
  require('./FileData/index.js');
15
+ require('./generateTypes/index.js');
15
16
 
@@ -7,7 +7,10 @@ function objectOf(map) {
7
7
  }
8
8
  const result = {};
9
9
  for (const key in map) {
10
- result[key] = map[key](value[key], Object.assign(Object.assign({}, data), { key: addKey(key, data) }));
10
+ const val = map[key](value[key], Object.assign(Object.assign({}, data), { key: addKey(key, data) }));
11
+ if (val !== undefined) {
12
+ result[key] = val;
13
+ }
11
14
  }
12
15
  return result;
13
16
  };
@@ -11,7 +11,10 @@ function objectOf(map) {
11
11
  }
12
12
  const result = {};
13
13
  for (const key in map) {
14
- result[key] = map[key](value[key], Object.assign(Object.assign({}, data), { key: helpers.addKey(key, data) }));
14
+ const val = map[key](value[key], Object.assign(Object.assign({}, data), { key: helpers.addKey(key, data) }));
15
+ if (val !== undefined) {
16
+ result[key] = val;
17
+ }
15
18
  }
16
19
  return result;
17
20
  };
@@ -1 +0,0 @@
1
- export declare function EditPartner(): any;
@@ -1 +0,0 @@
1
- export * from './EditPartner';
@@ -1 +0,0 @@
1
- export declare function GetPartner(): any;
@@ -1 +0,0 @@
1
- export * from './GetPartner';
@@ -1 +0,0 @@
1
- export declare function GetPartners(): any;
@@ -1 +0,0 @@
1
- export * from './GetPartners';
@@ -1,3 +0,0 @@
1
- export * from './GetPartner';
2
- export * from './GetPartners';
3
- export * from './EditPartner';
@@ -1 +0,0 @@
1
- export declare function AddressSchema(): any;
@@ -1 +0,0 @@
1
- export * from './AddressSchema';
@@ -1 +0,0 @@
1
- export * from './AddressSchema';