@miqro/core 3.0.3 → 3.0.5

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 (84) hide show
  1. package/build/cjs/api-router.js +2 -3
  2. package/build/cjs/api-router.js.map +1 -1
  3. package/build/cjs/common/api-router-types.d.ts +8 -5
  4. package/build/cjs/common/api-router-types.js +9 -1
  5. package/build/cjs/common/api-router-types.js.map +1 -1
  6. package/build/cjs/common/api-router-utils.d.ts +2 -4
  7. package/build/cjs/common/api-router-utils.js +65 -59
  8. package/build/cjs/common/api-router-utils.js.map +1 -1
  9. package/build/cjs/common/index.d.ts +1 -1
  10. package/build/cjs/common/index.js +1 -2
  11. package/build/cjs/common/index.js.map +1 -1
  12. package/build/cjs/common/logger/constants.d.ts +1 -0
  13. package/build/cjs/common/logger/constants.js +1 -0
  14. package/build/cjs/common/logger/constants.js.map +1 -1
  15. package/build/cjs/common/logger/interfaces.d.ts +1 -1
  16. package/build/cjs/common/logger/logger.js +1 -1
  17. package/build/cjs/common/logger/logger.js.map +1 -1
  18. package/build/cjs/common/logger/transport/console.js +3 -1
  19. package/build/cjs/common/logger/transport/console.js.map +1 -1
  20. package/build/cjs/common/router-default-logger-factory.d.ts +1 -0
  21. package/build/cjs/common/router-default-logger-factory.js +7 -1
  22. package/build/cjs/common/router-default-logger-factory.js.map +1 -1
  23. package/build/cjs/common/router-handler.d.ts +2 -1
  24. package/build/cjs/common/router-handler.js +41 -27
  25. package/build/cjs/common/router-handler.js.map +1 -1
  26. package/build/cjs/router.d.ts +9 -6
  27. package/build/cjs/router.js +21 -0
  28. package/build/cjs/router.js.map +1 -1
  29. package/build/esm/api-router.js +2 -3
  30. package/build/esm/api-router.js.map +1 -1
  31. package/build/esm/common/api-router-types.d.ts +8 -5
  32. package/build/esm/common/api-router-types.js +8 -0
  33. package/build/esm/common/api-router-types.js.map +1 -1
  34. package/build/esm/common/api-router-utils.d.ts +2 -4
  35. package/build/esm/common/api-router-utils.js +63 -57
  36. package/build/esm/common/api-router-utils.js.map +1 -1
  37. package/build/esm/common/index.d.ts +1 -1
  38. package/build/esm/common/index.js +1 -1
  39. package/build/esm/common/index.js.map +1 -1
  40. package/build/esm/common/logger/constants.d.ts +1 -0
  41. package/build/esm/common/logger/constants.js +1 -0
  42. package/build/esm/common/logger/constants.js.map +1 -1
  43. package/build/esm/common/logger/interfaces.d.ts +1 -1
  44. package/build/esm/common/logger/logger.js +1 -1
  45. package/build/esm/common/logger/logger.js.map +1 -1
  46. package/build/esm/common/logger/transport/console.js +3 -1
  47. package/build/esm/common/logger/transport/console.js.map +1 -1
  48. package/build/esm/common/router-default-logger-factory.d.ts +1 -0
  49. package/build/esm/common/router-default-logger-factory.js +5 -0
  50. package/build/esm/common/router-default-logger-factory.js.map +1 -1
  51. package/build/esm/common/router-handler.d.ts +2 -1
  52. package/build/esm/common/router-handler.js +41 -27
  53. package/build/esm/common/router-handler.js.map +1 -1
  54. package/build/esm/router.d.ts +9 -6
  55. package/build/esm/router.js +21 -0
  56. package/build/esm/router.js.map +1 -1
  57. package/package.json +4 -4
  58. package/src/api-router.ts +2 -3
  59. package/src/common/api-router-types.ts +15 -5
  60. package/src/common/api-router-utils.ts +67 -60
  61. package/src/common/index.ts +1 -1
  62. package/src/common/logger/constants.ts +1 -0
  63. package/src/common/logger/interfaces.ts +1 -1
  64. package/src/common/logger/logger.ts +1 -1
  65. package/src/common/logger/transport/console.ts +6 -4
  66. package/src/common/router-default-logger-factory.ts +6 -0
  67. package/src/common/router-handler.ts +39 -27
  68. package/src/router.ts +33 -6
  69. package/test.out.txt +3535 -0
  70. package/build/cjs/common/feature-router-types.d.ts +0 -12
  71. package/build/cjs/common/feature-router-types.js +0 -5
  72. package/build/cjs/common/feature-router-types.js.map +0 -1
  73. package/build/cjs/feature-router.d.ts +0 -4
  74. package/build/cjs/feature-router.js +0 -90
  75. package/build/cjs/feature-router.js.map +0 -1
  76. package/build/esm/common/feature-router-types.d.ts +0 -12
  77. package/build/esm/common/feature-router-types.js +0 -2
  78. package/build/esm/common/feature-router-types.js.map +0 -1
  79. package/build/esm/feature-router.d.ts +0 -4
  80. package/build/esm/feature-router.js +0 -86
  81. package/build/esm/feature-router.js.map +0 -1
  82. package/src/api-router.unit.test.js +0 -280
  83. package/src/common/feature-router-types.ts +0 -13
  84. package/src/feature-router.ts +0 -82
@@ -19,7 +19,7 @@ export class RouterHandler {
19
19
  private readonly options?: RouterHandlerOptions;
20
20
 
21
21
  public constructor(
22
- handler: Array<Handler> | HandlerWithOptions | Handler | MinimalRouter,
22
+ private readonly originalHandler: Array<Handler> | HandlerWithOptions | Handler | MinimalRouter,
23
23
  private readonly path: string | undefined,
24
24
  private readonly method: Method | undefined,
25
25
  options?: RouterHandlerOptions) {
@@ -30,29 +30,30 @@ export class RouterHandler {
30
30
  }
31
31
 
32
32
  let handlers: Array<Handler | MinimalRouter> = []
33
- this.isRouter = isMinimalRouter(handler); //handler instanceof Router;
33
+ this.isRouter = isMinimalRouter(originalHandler); //handler instanceof Router;
34
34
 
35
- if (!this.isRouter && !(handler instanceof Array) && typeof handler === "object") {
35
+ if (!this.isRouter && !(originalHandler instanceof Array) && typeof originalHandler === "object") {
36
36
  // HandlerWithOptions
37
37
  if (options) {
38
38
  throw new Error("bad arguments. cannot send options with HandlerWithOptions");
39
39
  }
40
40
  //console.dir(handler);
41
- handler = parse(handler, handlerWithOptionsParseOptions, "no_extra", "handler");
42
- const realHandler = (handler as HandlerWithOptions).handler;
41
+ this.originalHandler = parse(originalHandler, handlerWithOptionsParseOptions, "no_extra", "handler");
42
+ const realHandler = (originalHandler as HandlerWithOptions).handler;
43
43
  const newOptions = {
44
- ...(handler as HandlerWithOptions)
44
+ ...(originalHandler as HandlerWithOptions)
45
45
  };
46
46
  delete (newOptions as any).handler;
47
- handler = realHandler;
47
+ this.originalHandler = realHandler;
48
48
  this.options = newOptions;
49
49
  } else {
50
50
  this.options = options;
51
51
  }
52
- const routeHandlers = handler instanceof Array ? handler : [handler] as Array<Handler | MinimalRouter>;
52
+ const routeHandlers = this.originalHandler instanceof Array ? this.originalHandler : [this.originalHandler] as Array<Handler | MinimalRouter>;
53
53
 
54
- if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
55
- throw new Error("bad arguments");
54
+ //if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
55
+ if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length)) {
56
+ throw new Error(`bad arguments isRouter[${this.isRouter}]`);
56
57
  }
57
58
 
58
59
  if (this.options && this.options.middleware) {
@@ -96,30 +97,41 @@ export class RouterHandler {
96
97
  const nMethod = this.method ? this.method : "";
97
98
 
98
99
  if (this.isRouter) {
99
- const router: MinimalRouter = this.handler[0] as MinimalRouter;
100
+ const router: MinimalRouter = this.originalHandler as MinimalRouter;
100
101
  router.getJSONDoc(doc, `${prePath}${nPath}`);
101
- } else if (this.options) {
102
+ } else {
102
103
  if (!doc[`${prePath}${nPath}`]) {
103
104
  doc[`${prePath}${nPath}`] = Object.create(null);
104
105
  }
105
106
  if (doc[`${prePath}${nPath}`][nMethod]) {
106
107
  throw new Error("cannot create doc routes override each other");
107
108
  }
108
- doc[`${prePath}${nPath}`][nMethod] = {
109
- description: this.options.description,
110
- name: this.options.name,
111
- identifier: this.options.identifier ? this.options.identifier : `${prePath}${nPath}${this.options.name ? this.options.name : ""}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
112
- policy: this.options.policy,
113
- request: this.options.request ? {
114
- body: this.options.request.body,
115
- headers: this.options.request.headers,
116
- params: this.options.request.params,
117
- query: this.options.request.query,
118
- } : undefined,
119
- response: this.options.response ? {
120
- body: this.options.response.body
121
- } : undefined
122
- };
109
+ if(this.options) {
110
+ doc[`${prePath}${nPath}`][nMethod] = {
111
+ description: this.options.description,
112
+ name: this.options.name,
113
+ identifier: this.options.identifier ? this.options.identifier : `${prePath}${nPath}${this.options.name ? this.options.name : ""}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
114
+ policy: this.options.policy,
115
+ request: this.options.request ? {
116
+ body: this.options.request.body,
117
+ headers: this.options.request.headers,
118
+ params: this.options.request.params,
119
+ query: this.options.request.query,
120
+ } : undefined,
121
+ response: this.options.response ? {
122
+ body: this.options.response.body
123
+ } : undefined
124
+ };
125
+ } else {
126
+ doc[`${prePath}${nPath}`][nMethod] = {
127
+ description: undefined,
128
+ name: undefined,
129
+ identifier: `${prePath}${nPath}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
130
+ policy: undefined,
131
+ request: undefined,
132
+ response: undefined
133
+ };
134
+ }
123
135
  }
124
136
  return doc;
125
137
  }
package/src/router.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { RequestListener } from "http";
2
+ import { Logger } from "./common/logger";
2
3
  import { Context, ErrorHandler, Handler, Method } from "./common/context";
3
4
  import { HandlerWithOptions, MinimalRouter, RouterHandlerOptions, RouterJSONDoc, RouterOptions } from "./common/router-types";
4
5
  import { PathPart, PathPartToken, splitPath } from "./common/tokenize-match";
@@ -23,27 +24,27 @@ export class Router implements MinimalRouter {
23
24
  };
24
25
  }
25
26
 
26
- public get(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
27
+ public get(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
27
28
  return this.use(handler, path, "get", options);
28
29
  }
29
30
 
30
- public post(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
31
+ public post(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
31
32
  return this.use(handler, path, "post", options);
32
33
  }
33
34
 
34
- public patch(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
35
+ public patch(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
35
36
  return this.use(handler, path, "patch", options);
36
37
  }
37
38
 
38
- public delete(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
39
+ public delete(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
39
40
  return this.use(handler, path, "delete", options);
40
41
  }
41
42
 
42
- public put(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
43
+ public put(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
43
44
  return this.use(handler, path, "put", options);
44
45
  }
45
46
 
46
- public options(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
47
+ public options(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router {
47
48
  return this.use(handler, path, "options", options);
48
49
  }
49
50
 
@@ -104,4 +105,30 @@ export class Router implements MinimalRouter {
104
105
  }
105
106
  return doc;
106
107
  }
108
+
109
+ public logPaths(logger: Logger): void {
110
+ this.toString().split("\n").forEach(l=>logger.info(l));
111
+ }
112
+
113
+ public toString(): string {
114
+ let ret = "";
115
+ const doc = this.getJSONDoc();
116
+ const paths = Object.keys(doc);
117
+
118
+ for(const path of paths) {
119
+ if (path) {
120
+ const pathData = doc[path];
121
+
122
+ const methods = Object.keys(pathData);
123
+
124
+ for (const method of methods) {
125
+ if (method) {
126
+ const methodData = pathData[method];
127
+ ret += `${ret === "" ? "" : "\n"}${method}:${path}\n\t${methodData.identifier}${methodData.name ? `\n\t\t${methodData.name}` : ""}${methodData.description ? `\n\t\t${methodData.description}` : ""}`;
128
+ }
129
+ }
130
+ }
131
+ }
132
+ return ret;
133
+ }
107
134
  }