@miqro/core 1.2.5 → 1.3.4

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 (304) hide show
  1. package/README.md +34 -18
  2. package/dist/handler/api-handler.d.ts +16 -0
  3. package/dist/handler/api-handler.js +30 -5
  4. package/dist/handler/api-handler.js.map +1 -0
  5. package/dist/handler/api-router-utils.js +11 -2
  6. package/dist/handler/api-router-utils.js.map +1 -0
  7. package/dist/handler/api-router.d.ts +30 -2
  8. package/dist/handler/api-router.js +30 -0
  9. package/dist/handler/api-router.js.map +1 -0
  10. package/dist/handler/app.d.ts +1 -2
  11. package/dist/handler/app.js +96 -13
  12. package/dist/handler/app.js.map +1 -0
  13. package/dist/handler/common/context.d.ts +57 -0
  14. package/dist/handler/common/context.js +123 -0
  15. package/dist/handler/common/context.js.map +1 -0
  16. package/dist/handler/common/helpers.js +5 -0
  17. package/dist/handler/common/helpers.js.map +1 -0
  18. package/dist/handler/common/index.d.ts +2 -45
  19. package/dist/handler/common/index.js +13 -109
  20. package/dist/handler/common/index.js.map +1 -0
  21. package/dist/handler/common/router-utils.d.ts +26 -0
  22. package/dist/handler/common/router-utils.js +59 -0
  23. package/dist/handler/common/router-utils.js.map +1 -0
  24. package/dist/handler/{router.d.ts → common/router.d.ts} +7 -16
  25. package/dist/handler/common/router.js +76 -0
  26. package/dist/handler/common/router.js.map +1 -0
  27. package/dist/handler/common/tokenize-match.d.ts +18 -9
  28. package/dist/handler/common/tokenize-match.js +42 -30
  29. package/dist/handler/common/tokenize-match.js.map +1 -0
  30. package/dist/handler/feature-router.d.ts +1 -2
  31. package/dist/handler/feature-router.js +3 -2
  32. package/dist/handler/feature-router.js.map +1 -0
  33. package/dist/handler/index.d.ts +1 -5
  34. package/dist/handler/index.js +2 -5
  35. package/dist/handler/index.js.map +1 -0
  36. package/dist/{middleware → handler/middleware}/buffer.d.ts +1 -1
  37. package/dist/{middleware → handler/middleware}/buffer.js +3 -2
  38. package/dist/handler/middleware/buffer.js.map +1 -0
  39. package/dist/handler/middleware/cookie.d.ts +4 -0
  40. package/dist/handler/middleware/cookie.js +18 -0
  41. package/dist/handler/middleware/cookie.js.map +1 -0
  42. package/dist/{middleware → handler/middleware}/cors.d.ts +1 -1
  43. package/dist/{middleware → handler/middleware}/cors.js +10 -2
  44. package/dist/handler/middleware/cors.js.map +1 -0
  45. package/dist/{middleware → handler/middleware}/form.d.ts +1 -1
  46. package/dist/{middleware → handler/middleware}/form.js +8 -5
  47. package/dist/handler/middleware/form.js.map +1 -0
  48. package/dist/handler/middleware/group.d.ts +3 -0
  49. package/dist/handler/{group.js → middleware/group.js} +4 -2
  50. package/dist/handler/middleware/group.js.map +1 -0
  51. package/dist/handler/middleware/index.d.ts +13 -0
  52. package/dist/handler/middleware/index.js +64 -0
  53. package/dist/handler/middleware/index.js.map +1 -0
  54. package/dist/{middleware → handler/middleware}/json.d.ts +1 -1
  55. package/dist/{middleware → handler/middleware}/json.js +3 -2
  56. package/dist/handler/middleware/json.js.map +1 -0
  57. package/dist/{middleware → handler/middleware}/logger.d.ts +1 -1
  58. package/dist/{middleware → handler/middleware}/logger.js +1 -0
  59. package/dist/handler/middleware/logger.js.map +1 -0
  60. package/dist/handler/{parse.d.ts → middleware/parse.d.ts} +4 -3
  61. package/dist/handler/{parse.js → middleware/parse.js} +17 -7
  62. package/dist/handler/middleware/parse.js.map +1 -0
  63. package/dist/handler/middleware/session.d.ts +21 -0
  64. package/dist/handler/middleware/session.js +140 -0
  65. package/dist/handler/middleware/session.js.map +1 -0
  66. package/dist/handler/middleware/tag.d.ts +4 -0
  67. package/dist/handler/middleware/tag.js +14 -0
  68. package/dist/handler/middleware/tag.js.map +1 -0
  69. package/dist/{middleware → handler/middleware}/text.d.ts +1 -1
  70. package/dist/{middleware → handler/middleware}/text.js +3 -2
  71. package/dist/handler/middleware/text.js.map +1 -0
  72. package/dist/handler/proxy.js +5 -11
  73. package/dist/handler/proxy.js.map +1 -0
  74. package/dist/handler/{html.d.ts → response/html.d.ts} +1 -1
  75. package/dist/handler/{html.js → response/html.js} +1 -0
  76. package/dist/handler/response/html.js.map +1 -0
  77. package/dist/handler/response/index.d.ts +3 -0
  78. package/dist/{middleware → handler/response}/index.js +3 -7
  79. package/dist/handler/response/index.js.map +1 -0
  80. package/dist/handler/{response.d.ts → response/json.d.ts} +1 -1
  81. package/dist/handler/{response.js → response/json.js} +3 -2
  82. package/dist/handler/response/json.js.map +1 -0
  83. package/dist/handler/response/result.d.ts +4 -0
  84. package/dist/handler/{result.js → response/result.js} +8 -8
  85. package/dist/handler/response/result.js.map +1 -0
  86. package/dist/index.d.ts +0 -1
  87. package/dist/index.js +1 -1
  88. package/dist/index.js.map +1 -0
  89. package/dist/util/cli/index.js +3 -0
  90. package/dist/util/cli/index.js.map +1 -0
  91. package/dist/util/config/browser.js +3 -0
  92. package/dist/util/config/browser.js.map +1 -0
  93. package/dist/util/config/config.js +6 -1
  94. package/dist/util/config/config.js.map +1 -0
  95. package/dist/util/config/featuretoggle.js +1 -0
  96. package/dist/util/config/featuretoggle.js.map +1 -0
  97. package/dist/util/config/index.js +1 -0
  98. package/dist/util/config/index.js.map +1 -0
  99. package/dist/util/cookie/cookie.d.ts +50 -0
  100. package/dist/util/cookie/cookie.js +168 -0
  101. package/dist/util/cookie/cookie.js.map +1 -0
  102. package/dist/util/cookie/index.d.ts +16 -0
  103. package/dist/util/cookie/index.js +9 -0
  104. package/dist/util/cookie/index.js.map +1 -0
  105. package/dist/util/error/configfilenotfound.js +1 -0
  106. package/dist/util/error/configfilenotfound.js.map +1 -0
  107. package/dist/util/error/forbiddenerror.js +1 -0
  108. package/dist/util/error/forbiddenerror.js.map +1 -0
  109. package/dist/util/error/index.js +1 -0
  110. package/dist/util/error/index.js.map +1 -0
  111. package/dist/util/error/named.js +1 -0
  112. package/dist/util/error/named.js.map +1 -0
  113. package/dist/util/error/notfound.js +1 -0
  114. package/dist/util/error/notfound.js.map +1 -0
  115. package/dist/util/error/notimplemented.js +1 -0
  116. package/dist/util/error/notimplemented.js.map +1 -0
  117. package/dist/util/error/parsererror.js +1 -0
  118. package/dist/util/error/parsererror.js.map +1 -0
  119. package/dist/util/error/unauthorizederror.js +1 -0
  120. package/dist/util/error/unauthorizederror.js.map +1 -0
  121. package/dist/util/index.js +3 -0
  122. package/dist/util/index.js.map +1 -0
  123. package/dist/util/loader/index.js +1 -0
  124. package/dist/util/loader/index.js.map +1 -0
  125. package/dist/util/logger/common.js +1 -0
  126. package/dist/util/logger/common.js.map +1 -0
  127. package/dist/util/logger/factory.js +1 -0
  128. package/dist/util/logger/factory.js.map +1 -0
  129. package/dist/util/logger/formatter.js +1 -0
  130. package/dist/util/logger/formatter.js.map +1 -0
  131. package/dist/util/logger/index.js +1 -0
  132. package/dist/util/logger/index.js.map +1 -0
  133. package/dist/util/logger/logger.js +12 -2
  134. package/dist/util/logger/logger.js.map +1 -0
  135. package/dist/util/logger/transport.js +1 -0
  136. package/dist/util/logger/transport.js.map +1 -0
  137. package/dist/util/parser/common.d.ts +5 -5
  138. package/dist/util/parser/common.js +1 -0
  139. package/dist/util/parser/common.js.map +1 -0
  140. package/dist/util/parser/get.js +11 -1
  141. package/dist/util/parser/get.js.map +1 -0
  142. package/dist/util/parser/index.js +1 -0
  143. package/dist/util/parser/index.js.map +1 -0
  144. package/dist/util/parser/parser.d.ts +24 -3
  145. package/dist/util/parser/parser.js +46 -11
  146. package/dist/util/parser/parser.js.map +1 -0
  147. package/dist/util/parser/types/any.js +1 -0
  148. package/dist/util/parser/types/any.js.map +1 -0
  149. package/dist/util/parser/types/array.js +1 -0
  150. package/dist/util/parser/types/array.js.map +1 -0
  151. package/dist/util/parser/types/boolean.js +1 -0
  152. package/dist/util/parser/types/boolean.js.map +1 -0
  153. package/dist/util/parser/types/email.js +1 -0
  154. package/dist/util/parser/types/email.js.map +1 -0
  155. package/dist/util/parser/types/enum.js +1 -0
  156. package/dist/util/parser/types/enum.js.map +1 -0
  157. package/dist/util/parser/types/index.js +1 -0
  158. package/dist/util/parser/types/index.js.map +1 -0
  159. package/dist/util/parser/types/multiple.js +6 -2
  160. package/dist/util/parser/types/multiple.js.map +1 -0
  161. package/dist/util/parser/types/nested.js +1 -0
  162. package/dist/util/parser/types/nested.js.map +1 -0
  163. package/dist/util/parser/types/number.js +1 -0
  164. package/dist/util/parser/types/number.js.map +1 -0
  165. package/dist/util/parser/types/object.js +1 -0
  166. package/dist/util/parser/types/object.js.map +1 -0
  167. package/dist/util/parser/types/regex.js +1 -0
  168. package/dist/util/parser/types/regex.js.map +1 -0
  169. package/dist/util/parser/types/string.js +1 -0
  170. package/dist/util/parser/types/string.js.map +1 -0
  171. package/dist/util/parser/types/url.js +1 -0
  172. package/dist/util/parser/types/url.js.map +1 -0
  173. package/dist/util/request/common.d.ts +9 -0
  174. package/dist/util/request/common.js +101 -1
  175. package/dist/util/request/common.js.map +1 -0
  176. package/dist/util/request/index.js +143 -185
  177. package/dist/util/request/index.js.map +1 -0
  178. package/dist/util/session/common.d.ts +16 -0
  179. package/dist/util/session/common.js +49 -0
  180. package/dist/util/session/common.js.map +1 -0
  181. package/dist/util/session/index.d.ts +2 -16
  182. package/dist/util/session/index.js +13 -46
  183. package/dist/util/session/index.js.map +1 -0
  184. package/dist/util/session/service.d.ts +26 -0
  185. package/dist/util/session/service.js +143 -0
  186. package/dist/util/session/service.js.map +1 -0
  187. package/dist/util/test/app.js +5 -1
  188. package/dist/util/test/app.js.map +1 -0
  189. package/dist/util/test/common.js +1 -0
  190. package/dist/util/test/common.js.map +1 -0
  191. package/dist/util/test/index.js +1 -0
  192. package/dist/util/test/index.js.map +1 -0
  193. package/dist/util/uuid/index.d.ts +7 -0
  194. package/dist/util/uuid/index.js +8 -0
  195. package/dist/util/uuid/index.js.map +1 -0
  196. package/dist/util/uuid/regex.d.ts +2 -0
  197. package/dist/util/uuid/regex.js +5 -0
  198. package/dist/util/uuid/regex.js.map +1 -0
  199. package/dist/util/uuid/rng.d.ts +1 -0
  200. package/dist/util/uuid/rng.js +18 -0
  201. package/dist/util/uuid/rng.js.map +1 -0
  202. package/dist/util/uuid/stringify.d.ts +2 -0
  203. package/dist/util/uuid/stringify.js +50 -0
  204. package/dist/util/uuid/stringify.js.map +1 -0
  205. package/dist/util/uuid/v4.d.ts +5 -0
  206. package/dist/util/uuid/v4.js +27 -0
  207. package/dist/util/uuid/v4.js.map +1 -0
  208. package/dist/util/uuid/validate.d.ts +2 -0
  209. package/dist/util/uuid/validate.js +12 -0
  210. package/dist/util/uuid/validate.js.map +1 -0
  211. package/package.json +11 -13
  212. package/src/handler/api-handler.ts +56 -0
  213. package/src/handler/api-router-utils.ts +101 -0
  214. package/src/handler/api-router.ts +60 -0
  215. package/src/handler/app.ts +138 -0
  216. package/src/handler/common/context.ts +161 -0
  217. package/src/handler/common/helpers.ts +11 -0
  218. package/src/handler/common/index.ts +3 -0
  219. package/src/handler/common/router-utils.ts +67 -0
  220. package/src/handler/common/router.ts +70 -0
  221. package/src/handler/common/tokenize-match.ts +152 -0
  222. package/src/handler/feature-router.ts +90 -0
  223. package/src/handler/index.ts +8 -0
  224. package/src/handler/middleware/buffer.ts +79 -0
  225. package/src/handler/middleware/cookie.ts +19 -0
  226. package/src/handler/middleware/cors.ts +98 -0
  227. package/src/handler/middleware/form.ts +53 -0
  228. package/src/handler/middleware/group.ts +28 -0
  229. package/src/handler/middleware/index.ts +52 -0
  230. package/src/handler/middleware/json.ts +50 -0
  231. package/src/handler/middleware/logger.ts +18 -0
  232. package/src/handler/middleware/parse.ts +93 -0
  233. package/src/handler/middleware/session.ts +155 -0
  234. package/src/handler/middleware/tag.ts +15 -0
  235. package/src/handler/middleware/text.ts +40 -0
  236. package/src/handler/proxy.ts +44 -0
  237. package/src/handler/response/html.ts +36 -0
  238. package/src/handler/response/index.ts +3 -0
  239. package/src/handler/response/json.ts +19 -0
  240. package/src/handler/response/result.ts +40 -0
  241. package/src/index.ts +2 -0
  242. package/src/util/cli/index.ts +57 -0
  243. package/src/util/config/browser.ts +5 -0
  244. package/src/util/config/config.ts +93 -0
  245. package/src/util/config/featuretoggle.ts +6 -0
  246. package/src/util/config/index.ts +3 -0
  247. package/src/util/cookie/cookie.ts +207 -0
  248. package/src/util/cookie/index.ts +6 -0
  249. package/src/util/error/configfilenotfound.ts +8 -0
  250. package/src/util/error/forbiddenerror.ts +8 -0
  251. package/src/util/error/index.ts +6 -0
  252. package/src/util/error/named.ts +3 -0
  253. package/src/util/error/notfound.ts +8 -0
  254. package/src/util/error/notimplemented.ts +8 -0
  255. package/src/util/error/parsererror.ts +10 -0
  256. package/src/util/error/unauthorizederror.ts +8 -0
  257. package/src/util/index.ts +11 -0
  258. package/src/util/loader/index.ts +67 -0
  259. package/src/util/logger/common.ts +6 -0
  260. package/src/util/logger/factory.ts +45 -0
  261. package/src/util/logger/formatter.ts +9 -0
  262. package/src/util/logger/index.ts +6 -0
  263. package/src/util/logger/logger.ts +114 -0
  264. package/src/util/logger/transport.ts +69 -0
  265. package/src/util/parser/common.ts +81 -0
  266. package/src/util/parser/get.ts +42 -0
  267. package/src/util/parser/index.ts +6 -0
  268. package/src/util/parser/parser.ts +235 -0
  269. package/src/util/parser/types/any.ts +8 -0
  270. package/src/util/parser/types/array.ts +73 -0
  271. package/src/util/parser/types/boolean.ts +13 -0
  272. package/src/util/parser/types/email.ts +26 -0
  273. package/src/util/parser/types/enum.ts +29 -0
  274. package/src/util/parser/types/index.ts +12 -0
  275. package/src/util/parser/types/multiple.ts +36 -0
  276. package/src/util/parser/types/nested.ts +19 -0
  277. package/src/util/parser/types/number.ts +34 -0
  278. package/src/util/parser/types/object.ts +10 -0
  279. package/src/util/parser/types/regex.ts +17 -0
  280. package/src/util/parser/types/string.ts +23 -0
  281. package/src/util/parser/types/url.ts +35 -0
  282. package/src/util/request/common.ts +193 -0
  283. package/src/util/request/index.ts +230 -0
  284. package/src/util/session/common.ts +63 -0
  285. package/src/util/session/index.ts +2 -0
  286. package/src/util/session/service.ts +154 -0
  287. package/src/util/test/app.ts +44 -0
  288. package/src/util/test/common.ts +17 -0
  289. package/src/util/test/index.ts +2 -0
  290. package/src/util/uuid/index.ts +5 -0
  291. package/src/util/uuid/regex.ts +2 -0
  292. package/src/util/uuid/rng.ts +14 -0
  293. package/src/util/uuid/stringify.ts +53 -0
  294. package/src/util/uuid/v4.ts +30 -0
  295. package/src/util/uuid/validate.ts +8 -0
  296. package/tsconfig.json +32 -0
  297. package/dist/handler/common/response.d.ts +0 -13
  298. package/dist/handler/common/response.js +0 -54
  299. package/dist/handler/error.d.ts +0 -2
  300. package/dist/handler/error.js +0 -46
  301. package/dist/handler/group.d.ts +0 -3
  302. package/dist/handler/result.d.ts +0 -4
  303. package/dist/handler/router.js +0 -116
  304. package/dist/middleware/index.d.ts +0 -6
@@ -0,0 +1,81 @@
1
+ export interface ParseOptions {
2
+ description?: string;
3
+ options: ParseOption[] | ParseOptionMap;
4
+ mode?: ParseOptionsMode;
5
+ ignoreUndefined?: boolean;
6
+ }
7
+
8
+ export interface SimpleMap<T> {
9
+ [key: string]: T;
10
+ }
11
+
12
+ export type ParseOptionsMode = "remove_extra" | "add_extra" | "no_extra";
13
+ export type SimpleTypes = string | boolean | number | Array<SimpleTypes> | SimpleMap<SimpleTypes>;
14
+ export type ParseOptionTypeWithOutOptions = "string" | "boolean" | "number" | "object" | "any" | "array" | string;
15
+ export type ParseOptionType = "regex" | "nested" | "enum" | "multiple" | ParseOptionTypeWithOutOptions;
16
+
17
+ export type ParseOptionMap = SimpleMap<NoNameParseOption | ParseOptionTypeWithOutOptions>;
18
+
19
+ interface ParseOptionsBase {
20
+ type: ParseOptionType;
21
+ options?: any;
22
+ regex?: string;
23
+ multipleOptions?: BasicParseOption[];
24
+ forceArray?: boolean;
25
+ allowNull?: boolean;
26
+ arrayType?: ParseOptionType;
27
+ arrayMinLength?: number;
28
+ arrayMaxLength?: number;
29
+ numberMax?: number;
30
+ numberMaxDecimals?: number;
31
+ numberMinDecimals?: number;
32
+ numberMin?: number;
33
+ stringMaxLength?: number;
34
+ stringMinLength?: number;
35
+ nestedOptions?: {
36
+ ignoreUndefined?: boolean;
37
+ options: ParseOption[] | ParseOptionMap;
38
+ mode?: ParseOptionsMode;
39
+ };
40
+ enumValues?: string[];
41
+ parseJSON?: boolean;
42
+ }
43
+
44
+ export interface BasicParseOption extends ParseOptionsBase {
45
+ description?: string;
46
+ required?: boolean;
47
+ }
48
+
49
+ export interface NoNameParseOption extends ParseOptionsBase {
50
+ defaultValue?: any;
51
+ description?: string;
52
+ required?: boolean;
53
+ }
54
+
55
+ export interface ParseOption extends ParseOptionsBase {
56
+ name: string;
57
+ description?: string;
58
+ defaultValue?: any;
59
+ required?: boolean;
60
+ }
61
+
62
+ export interface ParseValueArgs extends ParseOptionsBase {
63
+ name: string;
64
+ attrName: string;
65
+ value: any;
66
+ }
67
+
68
+
69
+ export interface ParseValueValidatorResponse { isType: boolean; parsedValue: any; message?: string; }
70
+
71
+ export type ParseValueValidator = (args: ParseValueArgs, parser: ParserInterface) => ParseValueValidatorResponse;
72
+
73
+ export interface ParserInterface {
74
+ parseValue(args: ParseValueArgs): ParseValueValidatorResponse;
75
+ parse(
76
+ name: string,
77
+ arg: any,
78
+ options: ParseOption[] | ParseOptionMap,
79
+ mode?: ParseOptionsMode,
80
+ ignoreUndefined?: boolean): any
81
+ }
@@ -0,0 +1,42 @@
1
+ import { Parser, parseValue } from "./parser";
2
+ import { ParseOptionsError } from "../error";
3
+ import { BasicParseOption, ParseValueArgs } from "./common";
4
+
5
+ /*
6
+ usage const name = get(obj, "user.info.name", {type: "string"});
7
+ */
8
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
9
+ export const get = (obj: any, attrPath: string, defaultValue?: any, option?: BasicParseOption, parser?: Parser): any | undefined => {
10
+ if (typeof obj !== "object" && defaultValue === undefined) {
11
+ throw new Error(`obj must be typeof object`);
12
+ } else if (!obj && defaultValue !== undefined) {
13
+ return defaultValue;
14
+ }
15
+ if (typeof attrPath !== "string") {
16
+ throw new Error(`attrPath must be typeof string`);
17
+ }
18
+ const path = attrPath.split(".").reverse();
19
+ let value = obj;
20
+ while (path.length > 0) {
21
+ const p = path.pop() as string;
22
+ if (value[p] === undefined) {
23
+ return defaultValue !== undefined ? defaultValue : undefined;
24
+ }
25
+ value = value[p];
26
+ }
27
+ if (!option) {
28
+ return value;
29
+ }
30
+ const args: ParseValueArgs = {
31
+ name: attrPath,
32
+ attrName: attrPath,
33
+ value,
34
+ ...option
35
+ };
36
+ const parserResult = parser ? parser.parseValue(args) : parseValue(args);
37
+ if (parserResult.isType) {
38
+ return parserResult.parsedValue;
39
+ } else {
40
+ throw new ParseOptionsError(parserResult.message ? parserResult.message : `${attrPath} not ${args.type}`, attrPath);
41
+ }
42
+ }
@@ -0,0 +1,6 @@
1
+ export * from "./parser";
2
+ export * from "./common";
3
+ export * from "./get";
4
+
5
+
6
+
@@ -0,0 +1,235 @@
1
+ import { ParserInterface, ParseValueValidator, ParseValueValidatorResponse } from ".";
2
+ import { ParseOptionsError } from "../error";
3
+ import {
4
+ ParseOption,
5
+ ParseValueArgs,
6
+ ParseOptionMap,
7
+ ParseOptionsMode
8
+ } from "./common";
9
+ import {
10
+ parseAny,
11
+ parseArray,
12
+ parseBoolean,
13
+ parseEmail,
14
+ parseEnum,
15
+ parseMultiple,
16
+ parseNested,
17
+ parseNumber,
18
+ parseObject,
19
+ parseRegex,
20
+ parseString,
21
+ parseURL
22
+ } from "./types";
23
+
24
+ const parseOptionMap2ParseOptionList = (map: ParseOptionMap): ParseOption[] => {
25
+ return Object.keys(map).map(name => {
26
+ const val = map[name];
27
+ return typeof val !== "object" ? {
28
+ name,
29
+ required: true,
30
+ type: val
31
+ } : val.required === undefined ? {
32
+ ...val,
33
+ required: true,
34
+ name
35
+ } : {
36
+ ...val,
37
+ name
38
+ };
39
+ });
40
+ }
41
+
42
+ /**
43
+ * # Parser
44
+ *
45
+ * ```typescript
46
+ *import { Parser } from "@miqro/core";
47
+ *const parser = new Parser();
48
+ *
49
+ *const parsedValue = parser.parse("body", req.body, {
50
+ * name: "string",
51
+ * optional: {
52
+ * type: "string",
53
+ * required: false
54
+ * },
55
+ * mandatory: {
56
+ * type: "boolean",
57
+ * required: true
58
+ * }
59
+ *}, "no_extra");
60
+ *
61
+ * ```
62
+ */
63
+ export class Parser implements ParserInterface {
64
+ protected parsers: { [name: string]: ParseValueValidator };
65
+ constructor() {
66
+ this.parsers = {};
67
+ this.registerParser("any", parseAny);
68
+ this.registerParser("array", parseArray);
69
+ this.registerParser("boolean", parseBoolean);
70
+ this.registerParser("enum", parseEnum);
71
+ this.registerParser("multiple", parseMultiple);
72
+ this.registerParser("nested", parseNested);
73
+ this.registerParser("number", parseNumber);
74
+ this.registerParser("object", parseObject);
75
+ this.registerParser("string", parseString);
76
+ this.registerParser("regex", parseRegex);
77
+ this.registerParser("url", parseURL);
78
+ this.registerParser("email", parseEmail);
79
+ }
80
+ public registerParser(t: string, parser: ParseValueValidator): void {
81
+ if (this.parsers[t]) {
82
+ throw new Error("already registered!");
83
+ }
84
+ this.parsers[t] = parser;
85
+ }
86
+ public unRegisterParser(t: string): void {
87
+ if (!this.parsers[t]) {
88
+ throw new Error("not registered!");
89
+ }
90
+ delete this.parsers[t];
91
+ }
92
+ public parseValue(args: ParseValueArgs): ParseValueValidatorResponse {
93
+ const {
94
+ type,
95
+ forceArray,
96
+ parseJSON,
97
+ allowNull
98
+ } = args;
99
+ // check parsers
100
+ const parser: ParseValueValidator = this.parsers[type];
101
+ if (parser === undefined) {
102
+ throw new ParseOptionsError(`unsupported type ${type}`);
103
+ }
104
+ // prepare args
105
+ if (parseJSON) {
106
+ if (typeof args.value !== "string") {
107
+ throw new ParseOptionsError(`parseJSON not available to non string value`);
108
+ }
109
+ try {
110
+ args.value = JSON.parse(args.value);
111
+ } catch (e) {
112
+ throw new ParseOptionsError(`value not json!`);
113
+ }
114
+ }
115
+
116
+ if (allowNull && args.value === null) {
117
+ return {
118
+ isType: true,
119
+ parsedValue: null
120
+ };
121
+ } else {
122
+ if (forceArray && type === "array" && !(args.value instanceof Array)) {
123
+ args.value = [args.value];
124
+ } else if (forceArray && type !== "array") {
125
+ throw new ParseOptionsError(`unsupported type ${type} with forceArray`);
126
+ }
127
+ }
128
+ // run parser
129
+ return parser(args, this);
130
+ }
131
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
132
+ public parse(
133
+ name: string,
134
+ arg: any,
135
+ options: ParseOption[] | ParseOptionMap,
136
+ mode: ParseOptionsMode = "no_extra",
137
+ ignoreUndefined = false): any {
138
+ const ret: { [name: string]: any } = {};
139
+ if (!arg || typeof arg !== "object") {
140
+ throw new ParseOptionsError(`invalid ${name}`, name);
141
+ }
142
+ if (!(options instanceof Array)) {
143
+ options = parseOptionMap2ParseOptionList(options);
144
+ }
145
+
146
+ for (const option of options) {
147
+ const value = arg[option.name];
148
+ if (value === undefined && option.defaultValue !== undefined) {
149
+ ret[option.name] = option.defaultValue;
150
+ continue;
151
+ }
152
+ const exists = ignoreUndefined ? value !== undefined : arg.hasOwnProperty(option.name);
153
+ if (!exists && option.required === false) {
154
+ continue;
155
+ } else if (!exists && (option.required === true || option.required === undefined)) {
156
+ throw new ParseOptionsError(`${name}.${option.name} not defined`, `${name}.${option.name}`);
157
+ }
158
+ const { isType, parsedValue, message } = this.parseValue({
159
+ name,
160
+ attrName: option.name,
161
+ type: option.type,
162
+ regex: option.regex,
163
+ value,
164
+ numberMaxDecimals: option.numberMaxDecimals,
165
+ numberMinDecimals: option.numberMinDecimals,
166
+ numberMin: option.numberMin,
167
+ numberMax: option.numberMax,
168
+ allowNull: option.allowNull,
169
+ options: option.options,
170
+ multipleOptions: option.multipleOptions,
171
+ stringMinLength: option.stringMinLength,
172
+ stringMaxLength: option.stringMaxLength,
173
+ arrayType: option.arrayType,
174
+ nestedOptions: option.nestedOptions,
175
+ enumValues: option.enumValues,
176
+ parseJSON: option.parseJSON,
177
+ arrayMaxLength: option.arrayMaxLength,
178
+ arrayMinLength: option.arrayMinLength,
179
+ forceArray: option.forceArray
180
+ });
181
+ if (!isType) {
182
+ throw new ParseOptionsError(
183
+ `${name}.${option.name} not ${option.type}${message ? `: ${message}.` : ""}` +
184
+ `${option.type === "number" && option.numberMin !== undefined ? `${option.numberMin}:` : ""}${option.type === "number" && option.numberMax !== undefined ? `:${option.numberMax}` : ""}${option.numberMinDecimals !== undefined ? ` min decimals[${option.numberMinDecimals}]` : ""}${option.numberMaxDecimals !== undefined ? ` max decimals[${option.numberMaxDecimals}]` : ""}` +
185
+ `${option.type === "string" && option.stringMinLength !== undefined ? `${option.stringMinLength}:` : ""}${option.type === "string" && option.stringMaxLength !== undefined ? `:${option.stringMaxLength}` : ""}` +
186
+ `${option.type === "array" && option.arrayMinLength !== undefined ? `${option.arrayMinLength}:` : ""}${option.type === "array" && option.arrayMaxLength !== undefined ? `:${option.arrayMaxLength}` : ""}` +
187
+ `${option.type === "array" && option.arrayType ? (option.arrayType !== "enum" ? ` of ${option.arrayType}` : ` of ${option.arrayType} as defined. valid values [${option.enumValues}]`) : ""}` +
188
+ `${option.type === "nested" ? ` as defined!` : ""}` +
189
+ `${option.type === "enum" ? ` as defined. valid values [${option.enumValues}]` : ""}` +
190
+ `${option.type === "multiple" ? ` as defined.` : ""}`,
191
+ `${name}.${option.name}`
192
+ );
193
+ }
194
+ ret[option.name] = parsedValue;
195
+ }
196
+ switch (mode) {
197
+ case "no_extra":
198
+ const argKeys = Object.keys(arg);
199
+ for (const argKey of argKeys) {
200
+ if (ignoreUndefined && arg[argKey] === undefined) {
201
+ continue;
202
+ } else if (!ret.hasOwnProperty(argKey)) {
203
+ throw new ParseOptionsError(`${name}.${argKey} option not valid [${argKey}]`, `${name}.${argKey}`);
204
+ }
205
+ }
206
+ return ret;
207
+ case "add_extra":
208
+ return {
209
+ ...arg,
210
+ ...ret
211
+ };
212
+ case "remove_extra":
213
+ return ret;
214
+ default:
215
+ throw new ParseOptionsError(`unsupported mode ${mode}`);
216
+ }
217
+ }
218
+ }
219
+
220
+ const defaultParser = new Parser();
221
+
222
+ export const parseValue = (
223
+ args: ParseValueArgs
224
+ ): ParseValueValidatorResponse => {
225
+ return defaultParser.parseValue(args);
226
+ }
227
+
228
+ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */
229
+ export const parse = (
230
+ name: string,
231
+ arg: any,
232
+ options: ParseOption[] | ParseOptionMap,
233
+ mode: ParseOptionsMode = "no_extra",
234
+ ignoreUndefined = false
235
+ ): any => defaultParser.parse(name, arg, options, mode, ignoreUndefined);
@@ -0,0 +1,8 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ export const parseAny: ParseValueValidator = (args: ParseValueArgs) => {
4
+ return {
5
+ isType: true,
6
+ parsedValue: args.value
7
+ };
8
+ }
@@ -0,0 +1,73 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ export const parseArray: ParseValueValidator = ({
4
+ value,
5
+ arrayType,
6
+ name,
7
+ attrName,
8
+ numberMin,
9
+ numberMax,
10
+ allowNull,
11
+ multipleOptions,
12
+ stringMinLength,
13
+ numberMaxDecimals,
14
+ numberMinDecimals,
15
+ stringMaxLength,
16
+ nestedOptions,
17
+ enumValues,
18
+ options,
19
+ regex,
20
+ arrayMaxLength,
21
+ arrayMinLength
22
+ }: ParseValueArgs, parser) => {
23
+ const parsedList: any[] = [];
24
+ let isType = value instanceof Array;
25
+ if (isType) {
26
+ if (arrayType === undefined) {
27
+ for (const v of value) {
28
+ parsedList.push(v);
29
+ }
30
+ } else {
31
+ for (let i = 0; i < value.length; i++) {
32
+ const v = value[i];
33
+ const aiType = parser.parseValue({
34
+ name: `${name}.${attrName}`,
35
+ attrName: `[${i}]`,
36
+ type: arrayType,
37
+ options,
38
+ regex,
39
+ forceArray: false,
40
+ value: v,
41
+ numberMin,
42
+ numberMax,
43
+ numberMaxDecimals,
44
+ numberMinDecimals,
45
+ allowNull,
46
+ multipleOptions,
47
+ stringMinLength,
48
+ stringMaxLength,
49
+ nestedOptions,
50
+ enumValues,
51
+ arrayMaxLength,
52
+ arrayMinLength,
53
+ });
54
+ if (!aiType.isType) {
55
+ isType = false;
56
+ break;
57
+ } else {
58
+ parsedList.push(aiType.parsedValue);
59
+ }
60
+ }
61
+ }
62
+ }
63
+ if (isType && arrayMinLength !== undefined && parsedList.length < arrayMinLength) {
64
+ isType = false;
65
+ }
66
+ if (isType && arrayMaxLength !== undefined && parsedList.length > arrayMaxLength) {
67
+ isType = false;
68
+ }
69
+ return {
70
+ isType,
71
+ parsedValue: isType ? parsedList : value
72
+ };
73
+ }
@@ -0,0 +1,13 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ export const parseBoolean: ParseValueValidator = ({
4
+ value
5
+ }: ParseValueArgs) => {
6
+ const parsedValue = value === "true" || value === true ? true : value === "false" || value === false ? false : null;
7
+ return {
8
+ isType: parsedValue !== null,
9
+ parsedValue: parsedValue !== null ? parsedValue : value
10
+ };
11
+ }
12
+
13
+
@@ -0,0 +1,26 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
4
+
5
+ export const parseEmail: ParseValueValidator = ({
6
+ value,
7
+ stringMinLength,
8
+ stringMaxLength
9
+ }: ParseValueArgs) => {
10
+ let isType = typeof value === "string";
11
+ const parsedValue = value;
12
+ if (isType && stringMinLength !== undefined && parsedValue.length < stringMinLength) {
13
+ isType = false;
14
+ }
15
+ if (isType && stringMaxLength !== undefined && parsedValue.length > stringMaxLength) {
16
+ isType = false;
17
+ }
18
+
19
+ if (isType) {
20
+ isType = regex.test(String(value).toLowerCase())
21
+ }
22
+ return {
23
+ isType,
24
+ parsedValue: value
25
+ }
26
+ }
@@ -0,0 +1,29 @@
1
+ import { ParseOptionsError } from "../../error";
2
+ import { ParseValueArgs, ParseValueValidator } from "../common";
3
+
4
+ export const parseEnum: ParseValueValidator = ({
5
+ value,
6
+ attrName,
7
+ name,
8
+ enumValues
9
+ }: ParseValueArgs, parser) => {
10
+ const enumCheck = parser.parseValue({
11
+ name: `${name}.${attrName}`,
12
+ attrName: `enumList`,
13
+ forceArray: false,
14
+ type: "array",
15
+ value: enumValues,
16
+ arrayType: "string"
17
+ });
18
+ if (enumCheck.isType && enumValues && enumValues.length > 0) {
19
+ enumCheck.isType = enumValues.indexOf(value) !== -1;
20
+ } else {
21
+ throw new ParseOptionsError(`options.enumValues not a string array`);
22
+ }
23
+ return {
24
+ isType: enumCheck.isType,
25
+ parsedValue: value
26
+ };
27
+ }
28
+
29
+
@@ -0,0 +1,12 @@
1
+ export * from "./any";
2
+ export * from "./array";
3
+ export * from "./boolean";
4
+ export * from "./email";
5
+ export * from "./enum";
6
+ export * from "./multiple";
7
+ export * from "./nested";
8
+ export * from "./number";
9
+ export * from "./object";
10
+ export * from "./regex";
11
+ export * from "./string";
12
+ export * from "./url";
@@ -0,0 +1,36 @@
1
+ import { ParseOptionsError } from "../../error";
2
+ import { ParseValueArgs, ParseValueValidator } from "../common";
3
+
4
+ export const parseMultiple: ParseValueValidator = ({
5
+ multipleOptions,
6
+ name,
7
+ attrName,
8
+ forceArray,
9
+ type,
10
+ value
11
+ }: ParseValueArgs, parser) => {
12
+ if (!multipleOptions) {
13
+ throw new ParseOptionsError(`unsupported type ${type} without multipleOptions`);
14
+ }
15
+ for (let i = 0; i < multipleOptions.length; i++) {
16
+ const basicOption = multipleOptions[i];
17
+ try {
18
+ const aiType = parser.parseValue({
19
+ ...basicOption,
20
+ forceArray,
21
+ name,
22
+ attrName,
23
+ value
24
+ });
25
+ if (aiType.isType) {
26
+ return aiType;
27
+ }
28
+ } catch (e) {
29
+ continue;
30
+ }
31
+ }
32
+ return {
33
+ isType: false,
34
+ parsedValue: value
35
+ };
36
+ }
@@ -0,0 +1,19 @@
1
+ import { ParseOptionsError } from "../../error";
2
+ import { ParseValueArgs, ParseValueValidator } from "../common";
3
+
4
+ export const parseNested: ParseValueValidator = ({
5
+ nestedOptions,
6
+ name,
7
+ attrName,
8
+ type,
9
+ value
10
+ }: ParseValueArgs, parser) => {
11
+ if (!nestedOptions) {
12
+ throw new ParseOptionsError(`unsupported type ${type} without nestedOptions`);
13
+ }
14
+ const pValue = parser.parse(`${name}.${attrName}`, value, nestedOptions.options, nestedOptions.mode, nestedOptions.ignoreUndefined);
15
+ return {
16
+ isType: pValue !== null,
17
+ parsedValue: pValue === null ? value : pValue
18
+ };
19
+ }
@@ -0,0 +1,34 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ export const parseNumber: ParseValueValidator = ({
4
+ value,
5
+ numberMax,
6
+ numberMin,
7
+ numberMaxDecimals,
8
+ numberMinDecimals
9
+ }: ParseValueArgs) => {
10
+ let isType = value === null ? false : !isNaN(value);
11
+ const parsedValue = isType ? parseFloat(value) : value;
12
+ if (isType && numberMin !== undefined && parsedValue < numberMin) {
13
+ isType = false;
14
+ }
15
+ if (isType && numberMax !== undefined && parsedValue > numberMax) {
16
+ isType = false;
17
+ }
18
+ if (isType && (numberMaxDecimals !== undefined || numberMinDecimals !== undefined)) {
19
+ const decimalString = String(parsedValue).split(".")[1];
20
+ const decimalLength: number = decimalString !== undefined ? decimalString.length : 0;
21
+ if (numberMinDecimals !== undefined && decimalLength < numberMinDecimals) {
22
+ isType = false;
23
+ }
24
+ if (isType && numberMaxDecimals !== undefined && decimalLength > numberMaxDecimals) {
25
+ isType = false;
26
+ }
27
+ }
28
+ return {
29
+ isType,
30
+ parsedValue
31
+ };
32
+ }
33
+
34
+
@@ -0,0 +1,10 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ export const parseObject: ParseValueValidator = ({
4
+ value
5
+ }: ParseValueArgs) => {
6
+ return {
7
+ isType: typeof value === 'object',
8
+ parsedValue: value
9
+ };
10
+ }
@@ -0,0 +1,17 @@
1
+ import { ParseOptionsError } from "../../error";
2
+ import { ParseValueValidator } from "../common";
3
+
4
+ export const parseRegex: ParseValueValidator = ({
5
+ regex,
6
+ type,
7
+ value
8
+ }) => {
9
+ if (regex === undefined || typeof regex !== "string") {
10
+ throw new ParseOptionsError(`unsupported type ${type} without regex as string`);
11
+ }
12
+ const regExp = new RegExp(regex);
13
+ return {
14
+ isType: regExp.test(value),
15
+ parsedValue: value
16
+ };
17
+ }
@@ -0,0 +1,23 @@
1
+ import { ParseValueArgs, ParseValueValidator } from "../common";
2
+
3
+ export const parseString: ParseValueValidator = ({
4
+ value,
5
+ stringMinLength,
6
+ type,
7
+ stringMaxLength
8
+ }: ParseValueArgs) => {
9
+ let isType = typeof value === type;
10
+ const parsedValue = value;
11
+ if (isType && stringMinLength !== undefined && parsedValue.length < stringMinLength) {
12
+ isType = false;
13
+ }
14
+ if (isType && stringMaxLength !== undefined && parsedValue.length > stringMaxLength) {
15
+ isType = false;
16
+ }
17
+ return {
18
+ isType,
19
+ parsedValue
20
+ };
21
+ }
22
+
23
+