@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
@@ -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,16 +28,18 @@ 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, {});
36
39
  }
37
40
  get parsedUrl() {
38
- var _a;
39
- const match = (_a = this.req.url) === null || _a === void 0 ? void 0 : _a.match(URL_PARSER);
41
+ var _b;
42
+ const match = (_b = this.req.url) === null || _b === void 0 ? void 0 : _b.match(URL_PARSER);
40
43
  if (!match)
41
44
  throw Error('cannot parse url');
42
45
  const result = match.groups;
@@ -73,8 +76,8 @@ let Action = (() => {
73
76
  tslib.__classPrivateFieldSet(this, _Action_headers, value, "f");
74
77
  }
75
78
  get originCookies() {
76
- var _a;
77
- return cookie__default["default"].parse((_a = this.req.headers.cookie) !== null && _a !== void 0 ? _a : '');
79
+ var _b;
80
+ return cookieLib__default["default"].parse((_b = this.req.headers.cookie) !== null && _b !== void 0 ? _b : '');
78
81
  }
79
82
  get cookies() {
80
83
  if (tslib.__classPrivateFieldGet(this, _Action_cookie, "f"))
@@ -111,21 +114,42 @@ 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(),
117
137
  _Action_cookie = new WeakMap(),
118
138
  (() => {
139
+ const _metadata = typeof Symbol === "function" && Symbol.metadata ? Object.create(null) : void 0;
119
140
  _get_parsedUrl_decorators = [once.once];
120
141
  _get_originSearch_decorators = [once.once];
121
142
  _get_originCookies_decorators = [once.once];
122
143
  _get_bodyType_decorators = [once.once];
123
144
  _parseBody_decorators = [once.once];
124
- 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
- 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
- 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
- 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
- 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);
145
+ _get_clientIp_decorators = [once.once];
146
+ 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 }, metadata: _metadata }, null, _instanceExtraInitializers);
147
+ 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 }, metadata: _metadata }, null, _instanceExtraInitializers);
148
+ 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 }, metadata: _metadata }, null, _instanceExtraInitializers);
149
+ 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 }, metadata: _metadata }, null, _instanceExtraInitializers);
150
+ tslib.__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);
151
+ 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 }, metadata: _metadata }, null, _instanceExtraInitializers);
152
+ if (_metadata) Object.defineProperty(_a, Symbol.metadata, { enumerable: true, configurable: true, writable: true, value: _metadata });
129
153
  })(),
130
154
  _a;
131
155
  })();
@@ -0,0 +1,3 @@
1
+ import { type Document, type SchemaObject } from '../../types';
2
+ export declare function generateSchemaTypes(schema: SchemaObject, spaces?: number, lastChar?: string): string;
3
+ export declare function generateTypes(docs: Document, namespace?: string): string;
@@ -0,0 +1,151 @@
1
+ function hasDefault(target) {
2
+ return Boolean(target && ('default' in target || 'x-default' in target));
3
+ }
4
+ function generateSchemaTypes(schema, spaces = 2, lastChar = '\n') {
5
+ const space = [...new Array(spaces)].map(() => ' ').join('');
6
+ if ('$ref' in schema) {
7
+ return `Schemas.${schema.$ref.slice(21)}${lastChar}`;
8
+ }
9
+ if (schema.type === 'integer') {
10
+ return `${schema.format === 'int64' ? 'bigint' : 'number'}${lastChar}`;
11
+ }
12
+ if (schema.type === 'string') {
13
+ if (schema.format === 'date-time') {
14
+ return `Date${lastChar}`;
15
+ }
16
+ if (schema.format === 'binary') {
17
+ return `Bin${lastChar}`;
18
+ }
19
+ return `string${lastChar}`;
20
+ }
21
+ if (['boolean', 'number', 'null'].includes(schema.type)) {
22
+ return `${schema.type}${lastChar}`;
23
+ }
24
+ if (schema.oneOf) {
25
+ let result = '';
26
+ for (const item of schema.oneOf) {
27
+ if (result) {
28
+ result += ' | ';
29
+ }
30
+ result += generateSchemaTypes(item, spaces + 2, '');
31
+ }
32
+ return result + lastChar;
33
+ }
34
+ if (schema.type === 'array') {
35
+ if (!schema.items)
36
+ return `any[]${lastChar}`;
37
+ return `Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>${lastChar}`;
38
+ }
39
+ if (!schema.type) {
40
+ return `any${lastChar}`;
41
+ }
42
+ if (schema.type !== 'object') {
43
+ console.error('unknown type', schema);
44
+ return `any${lastChar}`;
45
+ }
46
+ let result = '{\n';
47
+ const required = schema.required || [];
48
+ const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
49
+ const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
50
+ Object.keys(schema.additionalProperties).length);
51
+ if (hasProps) {
52
+ for (const key in schema.properties) {
53
+ const prop = schema.properties[key];
54
+ const splitter = required.includes(key) || hasDefault(prop)
55
+ ? ':'
56
+ : '?:';
57
+ result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
58
+ }
59
+ }
60
+ if (hasRestProps) {
61
+ const value = hasProps
62
+ ? 'any\n'
63
+ : generateSchemaTypes(schema.additionalProperties, spaces + 2);
64
+ result += `${space}[key: string]: ${value}`;
65
+ }
66
+ return `${result}${space.slice(0, -2)}}${lastChar}`;
67
+ }
68
+ function generateTypes(docs, namespace = 'Api') {
69
+ var _a;
70
+ let result = `declare namespace ${namespace} {
71
+ export interface Bin {
72
+ filename: string
73
+ fieldName: string
74
+ originalFilename: string
75
+ path: string
76
+ type: string
77
+ disposition: string
78
+ size: number
79
+ extension?: string
80
+ }
81
+ `;
82
+ const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
83
+ const paths = docs.paths;
84
+ if (schemas) {
85
+ result += ' namespace Schemas {\n';
86
+ for (const name in schemas) {
87
+ result += ` export type ${name} = ${generateSchemaTypes(schemas[name], 6)}`;
88
+ }
89
+ result += ' }\n';
90
+ }
91
+ result += ' export interface Endpoints {\n';
92
+ for (const path in paths) {
93
+ const pathObject = paths[path];
94
+ for (const method in pathObject) {
95
+ // @ts-expect-error: FIXME
96
+ const endpoint = pathObject[method];
97
+ const parameters = endpoint.parameters;
98
+ const requestBody = endpoint.requestBody;
99
+ const responses = endpoint.responses;
100
+ result += ` ['${method.toUpperCase()}:${path}']: {\n`;
101
+ if (parameters) {
102
+ const params = {
103
+ query: '',
104
+ header: '',
105
+ path: '',
106
+ cookie: '',
107
+ };
108
+ for (const param of parameters) {
109
+ const splitter = param.in === 'path' || hasDefault(param.schema) ? ':' : '?:';
110
+ params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
111
+ }
112
+ if (params.path) {
113
+ result += ` Params: {\n${params.path} }\n`;
114
+ }
115
+ if (params.query) {
116
+ result += ` Search: {\n${params.query} }\n`;
117
+ }
118
+ if (params.header) {
119
+ result += ` Headers: {\n${params.header} }\n`;
120
+ }
121
+ if (params.cookie) {
122
+ result += ` Cookies: {\n${params.cookie} }\n`;
123
+ }
124
+ }
125
+ if (requestBody) {
126
+ result += ` Body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
127
+ }
128
+ if (responses) {
129
+ result += ' Response: {\n';
130
+ for (const key in responses) {
131
+ let multiple = false;
132
+ const response = responses[key];
133
+ result += ` ['${key}']: `;
134
+ for (const type in response.content) {
135
+ if (multiple) {
136
+ result += ' | ';
137
+ }
138
+ result += generateSchemaTypes(response.content[type].schema, 10, '');
139
+ multiple = true;
140
+ }
141
+ result += '\n';
142
+ }
143
+ result += ' }\n';
144
+ }
145
+ result += ' }\n';
146
+ }
147
+ }
148
+ return result + ' }\n}';
149
+ }
150
+
151
+ export { generateSchemaTypes, generateTypes };
@@ -0,0 +1,156 @@
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, lastChar = '\n') {
9
+ const space = [...new Array(spaces)].map(() => ' ').join('');
10
+ if ('$ref' in schema) {
11
+ return `Schemas.${schema.$ref.slice(21)}${lastChar}`;
12
+ }
13
+ if (schema.type === 'integer') {
14
+ return `${schema.format === 'int64' ? 'bigint' : 'number'}${lastChar}`;
15
+ }
16
+ if (schema.type === 'string') {
17
+ if (schema.format === 'date-time') {
18
+ return `Date${lastChar}`;
19
+ }
20
+ if (schema.format === 'binary') {
21
+ return `Bin${lastChar}`;
22
+ }
23
+ return `string${lastChar}`;
24
+ }
25
+ if (['boolean', 'number', 'null'].includes(schema.type)) {
26
+ return `${schema.type}${lastChar}`;
27
+ }
28
+ if (schema.oneOf) {
29
+ let result = '';
30
+ for (const item of schema.oneOf) {
31
+ if (result) {
32
+ result += ' | ';
33
+ }
34
+ result += generateSchemaTypes(item, spaces + 2, '');
35
+ }
36
+ return result + lastChar;
37
+ }
38
+ if (schema.type === 'array') {
39
+ if (!schema.items)
40
+ return `any[]${lastChar}`;
41
+ return `Array<${generateSchemaTypes(schema.items, spaces + 2, '')}>${lastChar}`;
42
+ }
43
+ if (!schema.type) {
44
+ return `any${lastChar}`;
45
+ }
46
+ if (schema.type !== 'object') {
47
+ console.error('unknown type', schema);
48
+ return `any${lastChar}`;
49
+ }
50
+ let result = '{\n';
51
+ const required = schema.required || [];
52
+ const hasProps = Boolean(schema.properties && Object.keys(schema.properties).length);
53
+ const hasRestProps = Boolean(typeof schema.additionalProperties === 'object' &&
54
+ Object.keys(schema.additionalProperties).length);
55
+ if (hasProps) {
56
+ for (const key in schema.properties) {
57
+ const prop = schema.properties[key];
58
+ const splitter = required.includes(key) || hasDefault(prop)
59
+ ? ':'
60
+ : '?:';
61
+ result += `${space}${key}${splitter} ${generateSchemaTypes(prop, spaces + 2)}`;
62
+ }
63
+ }
64
+ if (hasRestProps) {
65
+ const value = hasProps
66
+ ? 'any\n'
67
+ : generateSchemaTypes(schema.additionalProperties, spaces + 2);
68
+ result += `${space}[key: string]: ${value}`;
69
+ }
70
+ return `${result}${space.slice(0, -2)}}${lastChar}`;
71
+ }
72
+ function generateTypes(docs, namespace = 'Api') {
73
+ var _a;
74
+ let result = `declare namespace ${namespace} {
75
+ export interface Bin {
76
+ filename: string
77
+ fieldName: string
78
+ originalFilename: string
79
+ path: string
80
+ type: string
81
+ disposition: string
82
+ size: number
83
+ extension?: string
84
+ }
85
+ `;
86
+ const schemas = (_a = docs.components) === null || _a === void 0 ? void 0 : _a.schemas;
87
+ const paths = docs.paths;
88
+ if (schemas) {
89
+ result += ' namespace Schemas {\n';
90
+ for (const name in schemas) {
91
+ result += ` export type ${name} = ${generateSchemaTypes(schemas[name], 6)}`;
92
+ }
93
+ result += ' }\n';
94
+ }
95
+ result += ' export interface Endpoints {\n';
96
+ for (const path in paths) {
97
+ const pathObject = paths[path];
98
+ for (const method in pathObject) {
99
+ // @ts-expect-error: FIXME
100
+ const endpoint = pathObject[method];
101
+ const parameters = endpoint.parameters;
102
+ const requestBody = endpoint.requestBody;
103
+ const responses = endpoint.responses;
104
+ result += ` ['${method.toUpperCase()}:${path}']: {\n`;
105
+ if (parameters) {
106
+ const params = {
107
+ query: '',
108
+ header: '',
109
+ path: '',
110
+ cookie: '',
111
+ };
112
+ for (const param of parameters) {
113
+ const splitter = param.in === 'path' || hasDefault(param.schema) ? ':' : '?:';
114
+ params[param.in] += ` ${param.name}${splitter} ${generateSchemaTypes(param.schema)}`;
115
+ }
116
+ if (params.path) {
117
+ result += ` Params: {\n${params.path} }\n`;
118
+ }
119
+ if (params.query) {
120
+ result += ` Search: {\n${params.query} }\n`;
121
+ }
122
+ if (params.header) {
123
+ result += ` Headers: {\n${params.header} }\n`;
124
+ }
125
+ if (params.cookie) {
126
+ result += ` Cookies: {\n${params.cookie} }\n`;
127
+ }
128
+ }
129
+ if (requestBody) {
130
+ result += ` Body: ${generateSchemaTypes(requestBody.content['multipart/form-data'].schema, 8)}`;
131
+ }
132
+ if (responses) {
133
+ result += ' Response: {\n';
134
+ for (const key in responses) {
135
+ let multiple = false;
136
+ const response = responses[key];
137
+ result += ` ['${key}']: `;
138
+ for (const type in response.content) {
139
+ if (multiple) {
140
+ result += ' | ';
141
+ }
142
+ result += generateSchemaTypes(response.content[type].schema, 10, '');
143
+ multiple = true;
144
+ }
145
+ result += '\n';
146
+ }
147
+ result += ' }\n';
148
+ }
149
+ result += ' }\n';
150
+ }
151
+ }
152
+ return result + ' }\n}';
153
+ }
154
+
155
+ exports.generateSchemaTypes = generateSchemaTypes;
156
+ 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;
@@ -8,7 +8,7 @@ function getEndpoint(path, parentEndpoint) {
8
8
  parentEndpoint.static = {};
9
9
  }
10
10
  if (!parentEndpoint.static[key]) {
11
- parentEndpoint.static[key] = { key };
11
+ parentEndpoint.static[key] = { key, plugins: new Set() };
12
12
  }
13
13
  parentEndpoint = parentEndpoint.static[key];
14
14
  if (deep + 1 === splitPath.length) {
@@ -19,7 +19,7 @@ function getEndpoint(path, parentEndpoint) {
19
19
  if (!parentEndpoint.dynamic) {
20
20
  parentEndpoint.dynamic = [];
21
21
  }
22
- const newEndpoint = { key };
22
+ const newEndpoint = { key, plugins: new Set() };
23
23
  parentEndpoint.dynamic.push(newEndpoint);
24
24
  parentEndpoint = newEndpoint;
25
25
  if (deep + 1 === splitPath.length) {
@@ -12,7 +12,7 @@ function getEndpoint(path, parentEndpoint) {
12
12
  parentEndpoint.static = {};
13
13
  }
14
14
  if (!parentEndpoint.static[key]) {
15
- parentEndpoint.static[key] = { key };
15
+ parentEndpoint.static[key] = { key, plugins: new Set() };
16
16
  }
17
17
  parentEndpoint = parentEndpoint.static[key];
18
18
  if (deep + 1 === splitPath.length) {
@@ -23,7 +23,7 @@ function getEndpoint(path, parentEndpoint) {
23
23
  if (!parentEndpoint.dynamic) {
24
24
  parentEndpoint.dynamic = [];
25
25
  }
26
- const newEndpoint = { key };
26
+ const newEndpoint = { key, plugins: new Set() };
27
27
  parentEndpoint.dynamic.push(newEndpoint);
28
28
  parentEndpoint = newEndpoint;
29
29
  if (deep + 1 === splitPath.length) {
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
 
@@ -22,7 +22,6 @@ export * from './minLength';
22
22
  export * from './max';
23
23
  export * from './min';
24
24
  export * from './defaultTo';
25
- export * from './any';
26
25
  export * from './bin';
27
26
  export * from './minBin';
28
27
  export * from './maxBin';
@@ -22,7 +22,6 @@ import './minLength/index.es6.js';
22
22
  import './max/index.es6.js';
23
23
  import './min/index.es6.js';
24
24
  import './defaultTo/index.es6.js';
25
- import './any/index.es6.js';
26
25
  import './bin/index.es6.js';
27
26
  import './minBin/index.es6.js';
28
27
  import './maxBin/index.es6.js';
@@ -26,7 +26,6 @@ require('./minLength/index.js');
26
26
  require('./max/index.js');
27
27
  require('./min/index.js');
28
28
  require('./defaultTo/index.js');
29
- require('./any/index.js');
30
29
  require('./bin/index.js');
31
30
  require('./minBin/index.js');
32
31
  require('./maxBin/index.js');
@@ -1,3 +1,3 @@
1
1
  import { type Rule } from '../types';
2
2
  export type ObjectOf = Record<string, Rule>;
3
- export declare function objectOf(map: ObjectOf): (value: any, data?: Record<string, any>) => any;
3
+ export declare function objectOf(map: ObjectOf, rest?: Rule): (value: any, data?: Record<string, any>) => any;
@@ -1,13 +1,26 @@
1
1
  import { RulesError, addKey } from '../helpers.es6.js';
2
2
 
3
- function objectOf(map) {
3
+ function objectOf(map, rest) {
4
4
  return (value, data) => {
5
5
  if (value === null || typeof value !== 'object') {
6
6
  throw new RulesError('object', Object.assign({ value }, data));
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
+ }
14
+ }
15
+ if (rest) {
16
+ for (const key in value) {
17
+ if (key in map)
18
+ continue;
19
+ const val = rest(value[key], Object.assign(Object.assign({}, data), { key: addKey(key, data) }));
20
+ if (val !== undefined) {
21
+ result[key] = val;
22
+ }
23
+ }
11
24
  }
12
25
  return result;
13
26
  };
@@ -4,14 +4,27 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var helpers = require('../helpers.js');
6
6
 
7
- function objectOf(map) {
7
+ function objectOf(map, rest) {
8
8
  return (value, data) => {
9
9
  if (value === null || typeof value !== 'object') {
10
10
  throw new helpers.RulesError('object', Object.assign({ value }, data));
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
+ }
18
+ }
19
+ if (rest) {
20
+ for (const key in value) {
21
+ if (key in map)
22
+ continue;
23
+ const val = rest(value[key], Object.assign(Object.assign({}, data), { key: helpers.addKey(key, data) }));
24
+ if (val !== undefined) {
25
+ result[key] = val;
26
+ }
27
+ }
15
28
  }
16
29
  return result;
17
30
  };
@@ -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';
@@ -1 +0,0 @@
1
- export declare function LocationSchema(): any;
@@ -1 +0,0 @@
1
- export * from './LocationSchema';
@@ -1 +0,0 @@
1
- export * from './LocationSchema';
@@ -1 +0,0 @@
1
- export declare function EditPartnerSchema(): any;
@@ -1 +0,0 @@
1
- export * from './EditPartnerSchema';
@@ -1 +0,0 @@
1
- export declare function PartnerSchema(): any;
@@ -1 +0,0 @@
1
- export * from './PartnerSchema';
@@ -1,2 +0,0 @@
1
- export * from './PartnerSchema';
2
- export * from './EditPartnerSchema';
@@ -1 +0,0 @@
1
- export declare function Partner(): any;