@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.
- package/build/cjs/api-router.js +2 -3
- package/build/cjs/api-router.js.map +1 -1
- package/build/cjs/common/api-router-types.d.ts +8 -5
- package/build/cjs/common/api-router-types.js +9 -1
- package/build/cjs/common/api-router-types.js.map +1 -1
- package/build/cjs/common/api-router-utils.d.ts +2 -4
- package/build/cjs/common/api-router-utils.js +65 -59
- package/build/cjs/common/api-router-utils.js.map +1 -1
- package/build/cjs/common/index.d.ts +1 -1
- package/build/cjs/common/index.js +1 -2
- package/build/cjs/common/index.js.map +1 -1
- package/build/cjs/common/logger/constants.d.ts +1 -0
- package/build/cjs/common/logger/constants.js +1 -0
- package/build/cjs/common/logger/constants.js.map +1 -1
- package/build/cjs/common/logger/interfaces.d.ts +1 -1
- package/build/cjs/common/logger/logger.js +1 -1
- package/build/cjs/common/logger/logger.js.map +1 -1
- package/build/cjs/common/logger/transport/console.js +3 -1
- package/build/cjs/common/logger/transport/console.js.map +1 -1
- package/build/cjs/common/router-default-logger-factory.d.ts +1 -0
- package/build/cjs/common/router-default-logger-factory.js +7 -1
- package/build/cjs/common/router-default-logger-factory.js.map +1 -1
- package/build/cjs/common/router-handler.d.ts +2 -1
- package/build/cjs/common/router-handler.js +41 -27
- package/build/cjs/common/router-handler.js.map +1 -1
- package/build/cjs/router.d.ts +9 -6
- package/build/cjs/router.js +21 -0
- package/build/cjs/router.js.map +1 -1
- package/build/esm/api-router.js +2 -3
- package/build/esm/api-router.js.map +1 -1
- package/build/esm/common/api-router-types.d.ts +8 -5
- package/build/esm/common/api-router-types.js +8 -0
- package/build/esm/common/api-router-types.js.map +1 -1
- package/build/esm/common/api-router-utils.d.ts +2 -4
- package/build/esm/common/api-router-utils.js +63 -57
- package/build/esm/common/api-router-utils.js.map +1 -1
- package/build/esm/common/index.d.ts +1 -1
- package/build/esm/common/index.js +1 -1
- package/build/esm/common/index.js.map +1 -1
- package/build/esm/common/logger/constants.d.ts +1 -0
- package/build/esm/common/logger/constants.js +1 -0
- package/build/esm/common/logger/constants.js.map +1 -1
- package/build/esm/common/logger/interfaces.d.ts +1 -1
- package/build/esm/common/logger/logger.js +1 -1
- package/build/esm/common/logger/logger.js.map +1 -1
- package/build/esm/common/logger/transport/console.js +3 -1
- package/build/esm/common/logger/transport/console.js.map +1 -1
- package/build/esm/common/router-default-logger-factory.d.ts +1 -0
- package/build/esm/common/router-default-logger-factory.js +5 -0
- package/build/esm/common/router-default-logger-factory.js.map +1 -1
- package/build/esm/common/router-handler.d.ts +2 -1
- package/build/esm/common/router-handler.js +41 -27
- package/build/esm/common/router-handler.js.map +1 -1
- package/build/esm/router.d.ts +9 -6
- package/build/esm/router.js +21 -0
- package/build/esm/router.js.map +1 -1
- package/package.json +4 -4
- package/src/api-router.ts +2 -3
- package/src/common/api-router-types.ts +15 -5
- package/src/common/api-router-utils.ts +67 -60
- package/src/common/index.ts +1 -1
- package/src/common/logger/constants.ts +1 -0
- package/src/common/logger/interfaces.ts +1 -1
- package/src/common/logger/logger.ts +1 -1
- package/src/common/logger/transport/console.ts +6 -4
- package/src/common/router-default-logger-factory.ts +6 -0
- package/src/common/router-handler.ts +39 -27
- package/src/router.ts +33 -6
- package/test.out.txt +3535 -0
- package/build/cjs/common/feature-router-types.d.ts +0 -12
- package/build/cjs/common/feature-router-types.js +0 -5
- package/build/cjs/common/feature-router-types.js.map +0 -1
- package/build/cjs/feature-router.d.ts +0 -4
- package/build/cjs/feature-router.js +0 -90
- package/build/cjs/feature-router.js.map +0 -1
- package/build/esm/common/feature-router-types.d.ts +0 -12
- package/build/esm/common/feature-router-types.js +0 -2
- package/build/esm/common/feature-router-types.js.map +0 -1
- package/build/esm/feature-router.d.ts +0 -4
- package/build/esm/feature-router.js +0 -86
- package/build/esm/feature-router.js.map +0 -1
- package/src/api-router.unit.test.js +0 -280
- package/src/common/feature-router-types.ts +0 -13
- package/src/feature-router.ts +0 -82
|
@@ -5,7 +5,8 @@ import { GroupPolicyHandler, ParseRequest, SessionHandler, ResultParser } from "
|
|
|
5
5
|
import { handlerWithOptionsParseOptions, routerHandlerParseOptions } from "./router-types";
|
|
6
6
|
import { ResponseHandler } from ".";
|
|
7
7
|
export class RouterHandler {
|
|
8
|
-
constructor(
|
|
8
|
+
constructor(originalHandler, path, method, options) {
|
|
9
|
+
this.originalHandler = originalHandler;
|
|
9
10
|
this.path = path;
|
|
10
11
|
this.method = method;
|
|
11
12
|
this.tokens = tokenizePath(path);
|
|
@@ -13,28 +14,29 @@ export class RouterHandler {
|
|
|
13
14
|
options = parse(options, routerHandlerParseOptions, "no_extra", "options");
|
|
14
15
|
}
|
|
15
16
|
let handlers = [];
|
|
16
|
-
this.isRouter = isMinimalRouter(
|
|
17
|
-
if (!this.isRouter && !(
|
|
17
|
+
this.isRouter = isMinimalRouter(originalHandler); //handler instanceof Router;
|
|
18
|
+
if (!this.isRouter && !(originalHandler instanceof Array) && typeof originalHandler === "object") {
|
|
18
19
|
// HandlerWithOptions
|
|
19
20
|
if (options) {
|
|
20
21
|
throw new Error("bad arguments. cannot send options with HandlerWithOptions");
|
|
21
22
|
}
|
|
22
23
|
//console.dir(handler);
|
|
23
|
-
|
|
24
|
-
const realHandler =
|
|
24
|
+
this.originalHandler = parse(originalHandler, handlerWithOptionsParseOptions, "no_extra", "handler");
|
|
25
|
+
const realHandler = originalHandler.handler;
|
|
25
26
|
const newOptions = {
|
|
26
|
-
...
|
|
27
|
+
...originalHandler
|
|
27
28
|
};
|
|
28
29
|
delete newOptions.handler;
|
|
29
|
-
|
|
30
|
+
this.originalHandler = realHandler;
|
|
30
31
|
this.options = newOptions;
|
|
31
32
|
}
|
|
32
33
|
else {
|
|
33
34
|
this.options = options;
|
|
34
35
|
}
|
|
35
|
-
const routeHandlers =
|
|
36
|
-
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
37
|
-
|
|
36
|
+
const routeHandlers = this.originalHandler instanceof Array ? this.originalHandler : [this.originalHandler];
|
|
37
|
+
//if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
38
|
+
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length)) {
|
|
39
|
+
throw new Error(`bad arguments isRouter[${this.isRouter}]`);
|
|
38
40
|
}
|
|
39
41
|
if (this.options && this.options.middleware) {
|
|
40
42
|
handlers = handlers.concat(this.options.middleware);
|
|
@@ -70,31 +72,43 @@ export class RouterHandler {
|
|
|
70
72
|
const nPath = this.path && this.path.length > 1 ? this.path.substring(1) : "";
|
|
71
73
|
const nMethod = this.method ? this.method : "";
|
|
72
74
|
if (this.isRouter) {
|
|
73
|
-
const router = this.
|
|
75
|
+
const router = this.originalHandler;
|
|
74
76
|
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
75
77
|
}
|
|
76
|
-
else
|
|
78
|
+
else {
|
|
77
79
|
if (!doc[`${prePath}${nPath}`]) {
|
|
78
80
|
doc[`${prePath}${nPath}`] = Object.create(null);
|
|
79
81
|
}
|
|
80
82
|
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
81
83
|
throw new Error("cannot create doc routes override each other");
|
|
82
84
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
85
|
+
if (this.options) {
|
|
86
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
87
|
+
description: this.options.description,
|
|
88
|
+
name: this.options.name,
|
|
89
|
+
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(),
|
|
90
|
+
policy: this.options.policy,
|
|
91
|
+
request: this.options.request ? {
|
|
92
|
+
body: this.options.request.body,
|
|
93
|
+
headers: this.options.request.headers,
|
|
94
|
+
params: this.options.request.params,
|
|
95
|
+
query: this.options.request.query,
|
|
96
|
+
} : undefined,
|
|
97
|
+
response: this.options.response ? {
|
|
98
|
+
body: this.options.response.body
|
|
99
|
+
} : undefined
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
104
|
+
description: undefined,
|
|
105
|
+
name: undefined,
|
|
106
|
+
identifier: `${prePath}${nPath}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
|
|
107
|
+
policy: undefined,
|
|
108
|
+
request: undefined,
|
|
109
|
+
response: undefined
|
|
110
|
+
};
|
|
111
|
+
}
|
|
98
112
|
}
|
|
99
113
|
return doc;
|
|
100
114
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-handler.js","sourceRoot":"","sources":["../../../src/common/router-handler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAuC,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAExG,OAAO,EAAiB,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAE,YAAY,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC/F,OAAO,EAEL,8BAA8B,EAE9B,yBAAyB,EAC1B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,GAAG,CAAC;AAEpC,MAAM,OAAO,aAAa;IAOxB,
|
|
1
|
+
{"version":3,"file":"router-handler.js","sourceRoot":"","sources":["../../../src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IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC,EAAE;YACzD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE;gBAC3B,OAAO;oBACL,KAAK,EAAE,IAAI;iBACZ,CAAC;aACH;YACD,OAAO,iBAAiB,CAAC,IAAI,CAAC,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,YAAY,CAAC,CAAC;SAC1F;QACD,OAAO;YACL,KAAK,EAAE,KAAK;SACb,CAAC;IACJ,CAAC;IAEO,MAAM,CAAC,WAAW,CAAC,MAAW,EAAE,GAAY;QAClD,IAAI,MAAM,KAAK,KAAK,EAAE;YACpB,8EAA8E;YAC9E,OAAO,KAAK,CAAC;SACd;aAAM,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,EAAE;YAClD,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;YACpD,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SAC1B;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
package/build/esm/router.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { RequestListener } from "http";
|
|
3
|
+
import { Logger } from "./common/logger";
|
|
3
4
|
import { Context, ErrorHandler, Handler, Method } from "./common/context";
|
|
4
5
|
import { HandlerWithOptions, MinimalRouter, RouterHandlerOptions, RouterJSONDoc, RouterOptions } from "./common/router-types";
|
|
5
6
|
import { PathPart, PathPartToken } from "./common/tokenize-match";
|
|
@@ -10,14 +11,16 @@ export declare class Router implements MinimalRouter {
|
|
|
10
11
|
protected readonly errorHandlers: ErrorHandler[];
|
|
11
12
|
readonly listener: RequestListener;
|
|
12
13
|
constructor(config?: RouterOptions | undefined);
|
|
13
|
-
get(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
14
|
-
post(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
15
|
-
patch(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
16
|
-
delete(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
17
|
-
put(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
18
|
-
options(path: string, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
14
|
+
get(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
15
|
+
post(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
16
|
+
patch(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
17
|
+
delete(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
18
|
+
put(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
19
|
+
options(path: string | undefined, handler: Array<Handler> | Handler | MinimalRouter | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
19
20
|
use(handler: Array<Handler> | HandlerWithOptions | Handler | MinimalRouter, path?: string, method?: Method, options?: RouterHandlerOptions): Router;
|
|
20
21
|
catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router;
|
|
21
22
|
run(ctx: Context, pathTokens?: PathPart[], prePathTokens?: PathPartToken[]): Promise<boolean>;
|
|
22
23
|
getJSONDoc(doc?: RouterJSONDoc, prePath?: string): RouterJSONDoc;
|
|
24
|
+
logPaths(logger: Logger): void;
|
|
25
|
+
toString(): string;
|
|
23
26
|
}
|
package/build/esm/router.js
CHANGED
|
@@ -91,5 +91,26 @@ export class Router {
|
|
|
91
91
|
}
|
|
92
92
|
return doc;
|
|
93
93
|
}
|
|
94
|
+
logPaths(logger) {
|
|
95
|
+
this.toString().split("\n").forEach(l => logger.info(l));
|
|
96
|
+
}
|
|
97
|
+
toString() {
|
|
98
|
+
let ret = "";
|
|
99
|
+
const doc = this.getJSONDoc();
|
|
100
|
+
const paths = Object.keys(doc);
|
|
101
|
+
for (const path of paths) {
|
|
102
|
+
if (path) {
|
|
103
|
+
const pathData = doc[path];
|
|
104
|
+
const methods = Object.keys(pathData);
|
|
105
|
+
for (const method of methods) {
|
|
106
|
+
if (method) {
|
|
107
|
+
const methodData = pathData[method];
|
|
108
|
+
ret += `${ret === "" ? "" : "\n"}${method}:${path}\n\t${methodData.identifier}${methodData.name ? `\n\t\t${methodData.name}` : ""}${methodData.description ? `\n\t\t${methodData.description}` : ""}`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return ret;
|
|
114
|
+
}
|
|
94
115
|
}
|
|
95
116
|
//# sourceMappingURL=router.js.map
|
package/build/esm/router.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../src/router.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAiC,MAAM,kBAAkB,CAAC;AAE1E,OAAO,EAA2B,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAC7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,wCAAwC,CAAC;AAC9E,OAAO,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,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AAS,UAAU,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;qBACvM;iBACF;aACF;SACF;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@miqro/core",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "build/cjs/index.js",
|
|
6
6
|
"types": "build/cjs/index.d.ts",
|
|
@@ -10,8 +10,8 @@
|
|
|
10
10
|
"prebuild": "rm -Rf build/;",
|
|
11
11
|
"build": "tsc && tsc -p tsconfig-esm.json",
|
|
12
12
|
"pretest": "npm run build",
|
|
13
|
-
"test": "LOG_LEVEL=
|
|
14
|
-
"coverage": "node --enable-source-maps --experimental-test-coverage --test src/**/*.test.js src/*.test.js test/*.test.js"
|
|
13
|
+
"test": "LOG_LEVEL=none NODE_ENV=test node --enable-source-maps --test src/**/*.test.js src/*.test.js test/*.test.js",
|
|
14
|
+
"coverage": "LOG_LEVEL=none node --enable-source-maps --experimental-test-coverage --test src/**/*.test.js src/*.test.js test/*.test.js"
|
|
15
15
|
},
|
|
16
16
|
"author": "claukers",
|
|
17
17
|
"license": "ISC",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"npm": ">=6.0.0"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@miqro/parser": "
|
|
30
|
+
"@miqro/parser": "../parser",
|
|
31
31
|
"@miqro/request": "^0.2.1",
|
|
32
32
|
"cookie": "0.5.0"
|
|
33
33
|
}
|
package/src/api-router.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { basename } from "path";
|
|
|
3
3
|
import { traverseAPIRouteDir } from "./common/api-router-utils";
|
|
4
4
|
import { Logger } from "./common/logger/index.js";
|
|
5
5
|
import { parse} from "@miqro/parser";
|
|
6
|
-
import { FeatureRouter } from "./feature-router";
|
|
7
6
|
import { MinimalRouter } from "./common/router-types";
|
|
8
7
|
import { APIRouterOptions, APIRouterParseOptions } from "./common/api-router-types";
|
|
9
8
|
|
|
@@ -46,6 +45,6 @@ export async function APIRouter(options: APIRouterOptions, logger?: Logger): Pro
|
|
|
46
45
|
throw new Error(`${dirname} not a directory!`);
|
|
47
46
|
}
|
|
48
47
|
const apiName = options.apiName !== undefined ? options.apiName : basename(dirname);
|
|
49
|
-
const apiPath = options.path !== undefined ? options.path :
|
|
50
|
-
return
|
|
48
|
+
const apiPath = options.path !== undefined ? options.path : "";
|
|
49
|
+
return await traverseAPIRouteDir(apiName.toUpperCase(), dirname, apiPath, undefined, logger);
|
|
51
50
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ParseOptionMap } from "@miqro/parser";
|
|
2
|
-
import { Method } from "./context";
|
|
3
|
-
import {
|
|
2
|
+
import { Handler, Method } from "./context";
|
|
3
|
+
import { MinimalRouter, RouterHandlerOptions, routerHandlerParseOptions } from "./router-types";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* # APIRoute
|
|
@@ -14,9 +14,11 @@ import { HandlerWithOptions } from "./router-types";
|
|
|
14
14
|
*export default route;
|
|
15
15
|
* ```
|
|
16
16
|
*/
|
|
17
|
-
export interface APIRoute extends
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export interface APIRoute extends RouterHandlerOptions {
|
|
18
|
+
postFolder?: boolean,
|
|
19
|
+
method?: Method | "use" | "USE";
|
|
20
|
+
path?: string;
|
|
21
|
+
handler: Handler | Array<Handler> | MinimalRouter;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
export interface APIRouterOptions {
|
|
@@ -30,3 +32,11 @@ export const APIRouterParseOptions: ParseOptionMap = {
|
|
|
30
32
|
apiName: "string?",
|
|
31
33
|
path: "string?"
|
|
32
34
|
};
|
|
35
|
+
|
|
36
|
+
export const APIRouteValidator: ParseOptionMap = {
|
|
37
|
+
...routerHandlerParseOptions,
|
|
38
|
+
postFolder: "boolean?",
|
|
39
|
+
method: "string?",
|
|
40
|
+
path: "string?",
|
|
41
|
+
handler: "function[]!|MinimalRouter",
|
|
42
|
+
};
|
|
@@ -1,34 +1,30 @@
|
|
|
1
1
|
import { lstatSync, readdirSync } from "fs";
|
|
2
2
|
import { join, parse as pathParse, resolve } from "path";
|
|
3
3
|
import { Logger } from "./logger";
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { parse
|
|
7
|
-
import {
|
|
8
|
-
import { Method } from "./context.js";
|
|
4
|
+
import { getPathIdentifier } from "./router-default-logger-factory.js";
|
|
5
|
+
import { APIRoute, APIRouteValidator } from "./api-router-types.js";
|
|
6
|
+
import { parse } from "@miqro/parser";
|
|
7
|
+
import { Handler, Method } from "./context.js";
|
|
9
8
|
import { normalizePath } from "./url-utils";
|
|
9
|
+
import { Router } from "../router";
|
|
10
|
+
import { RouterHandlerOptions, MinimalRouter, isMinimalRouter } from "./router-types";
|
|
10
11
|
|
|
11
|
-
|
|
12
|
-
...handlerWithOptionsParseOptions,
|
|
13
|
-
method: "string?|string[]?",
|
|
14
|
-
path: "string?|string[]?"
|
|
15
|
-
};
|
|
12
|
+
type UseCallArg = [Handler | Handler[] | MinimalRouter, string | undefined, Method | undefined, RouterHandlerOptions];
|
|
16
13
|
|
|
17
14
|
export async function traverseAPIRouteDir(
|
|
18
15
|
featureName: string,
|
|
19
16
|
dirname: string,
|
|
20
17
|
basePath = "/",
|
|
21
|
-
|
|
18
|
+
router: Router = new Router(), logger?: Logger): Promise<Router> {
|
|
22
19
|
if (logger) {
|
|
23
20
|
logger.trace("loading routes from [%s]", dirname);
|
|
24
21
|
}
|
|
25
22
|
const files = readdirSync(dirname);
|
|
26
23
|
const dirs = files.filter(f => lstatSync(join(dirname, f)).isDirectory());
|
|
27
24
|
const methods = files.filter(f => !lstatSync(join(dirname, f)).isDirectory());
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
25
|
+
|
|
26
|
+
const postFolder: UseCallArg[] = [];
|
|
27
|
+
|
|
32
28
|
for (const f of methods) {
|
|
33
29
|
const { name, ext } = pathParse(f);
|
|
34
30
|
if (name !== "index" && ((ext === ".ts" || ext === ".js") && name.slice(-2) !== ".d")) {
|
|
@@ -44,21 +40,24 @@ export async function traverseAPIRouteDir(
|
|
|
44
40
|
}
|
|
45
41
|
//const ___filePath = filePath + ext;
|
|
46
42
|
const ___filePath = filePath;
|
|
47
|
-
let routeMethods: Method[] = [name] as Method[];
|
|
43
|
+
//let routeMethods: Method[] = [name] as Method[];
|
|
44
|
+
let routeMethod: Method | "use" | "USE" = name as Method;
|
|
48
45
|
if (typeof route === "function") {
|
|
49
46
|
route = {
|
|
50
47
|
handler: route
|
|
51
48
|
} as any;
|
|
52
49
|
} else {
|
|
53
|
-
if (typeof route.method !== "undefined" && (
|
|
54
|
-
throw new Error(`${resolve(dirname, name)} doesnt export method as a string or string array`);
|
|
50
|
+
if (typeof route.method !== "undefined" && (/*!(route.method instanceof Array) &&*/ (typeof route.method !== "string"))) {
|
|
51
|
+
//throw new Error(`${resolve(dirname, name)} doesnt export method as a string or string array`);
|
|
52
|
+
throw new Error(`${resolve(dirname, name)} doesnt export method as a string`);
|
|
55
53
|
} else if (typeof route.method !== "undefined") {
|
|
56
|
-
|
|
54
|
+
routeMethod = route.method;//route.method instanceof Array ? route.method : [route.method];
|
|
57
55
|
}
|
|
58
|
-
if (typeof route.path !== "string" && typeof route.path !== "undefined" && !(route.path instanceof Array)) {
|
|
59
|
-
throw new Error(`${resolve(dirname, name)} doesnt export path as a string or a string array`);
|
|
56
|
+
if (typeof route.path !== "string" && typeof route.path !== "undefined"/* && !(route.path instanceof Array)*/) {
|
|
57
|
+
//throw new Error(`${resolve(dirname, name)} doesnt export path as a string or a string array`);
|
|
58
|
+
throw new Error(`${resolve(dirname, name)} doesnt export path as a string`);
|
|
60
59
|
}
|
|
61
|
-
if ((!(route.handler instanceof Array) && typeof route.handler !== "function") || typeof route.handler === "undefined") {
|
|
60
|
+
if ((!(route.handler instanceof Array) && typeof route.handler !== "function" && !isMinimalRouter(route.handler)) || typeof route.handler === "undefined") {
|
|
62
61
|
throw new Error(`${resolve(dirname, name)} doesnt export handler as a function or an array`);
|
|
63
62
|
}
|
|
64
63
|
if (typeof route.name !== "string" && typeof route.name !== "undefined") {
|
|
@@ -66,49 +65,57 @@ export async function traverseAPIRouteDir(
|
|
|
66
65
|
}
|
|
67
66
|
}
|
|
68
67
|
route = parse(route, APIRouteValidator, "no_extra", ___filePath) as APIRoute;
|
|
69
|
-
const paths = route.path instanceof Array ? route.path : route.path === undefined ? [route.method && route.method.indexOf(name as Method) === -1 ? `/${name}` : "/"] : [route.path];
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
}
|
|
75
|
-
|
|
68
|
+
//const paths = route.path instanceof Array ? route.path : route.path === undefined ? [route.method && route.method.indexOf(name as Method) === -1 ? `/${name}` : "/"] : [route.path];
|
|
69
|
+
const p = route.path === undefined ? route.method && route.method.indexOf(name as Method) === -1 ? `/${name}` : "/" : route.path;
|
|
70
|
+
//for (let i = 0; i < paths.length; i++) {
|
|
71
|
+
//const p = paths[i];
|
|
72
|
+
if (typeof p !== "string" || !p) {
|
|
73
|
+
throw new Error(`${resolve(dirname, name)} doesnt export path as a string or a string array`);
|
|
74
|
+
}
|
|
75
|
+
const path = normalizePath(`${basePath}${p && p !== "/" ? `${p}` : p}`)
|
|
76
76
|
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
const subName = (p.charAt(0) === "/" ? p.substring(1) : p).replace(/[^a-z0-9+]+/gi, '_');
|
|
78
|
+
const routeName = route.name ? route.name : name;
|
|
79
79
|
|
|
80
|
-
|
|
80
|
+
//const identifier = route.identifier ? route.identifier : getPathIdentifier(path, routeMethod);
|
|
81
81
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
82
|
+
/*let newFeatureSubPath = `${featureName}_${route.name ? route.name : name}${routeName !== subName ? `_${subName}` : ""}`.toUpperCase();
|
|
83
|
+
|
|
84
|
+
newFeatureSubPath = newFeatureSubPath.length > 1 && newFeatureSubPath.charAt(newFeatureSubPath.length - 1) === "_" ? newFeatureSubPath.substring(0, newFeatureSubPath.length - 1) : newFeatureSubPath;*/
|
|
85
|
+
/*if (features[identifier]) {
|
|
86
|
+
throw new Error(`${___filePath} feature name collides with ${(features[identifier] as any).___filePath}`)
|
|
87
|
+
}*/
|
|
88
|
+
if (route.identifier && route.identifier === "__prototype__" || route.identifier === "__proto__") {
|
|
89
|
+
throw new Error(`invalid feature name ${route.identifier}`);
|
|
90
|
+
}
|
|
91
|
+
const useCallArgs: UseCallArg = [route.handler, path, routeMethod !== "use" && routeMethod !== "USE" ? routeMethod : undefined, {
|
|
92
|
+
identifier: route.identifier,
|
|
93
|
+
apiName: route.apiName,
|
|
94
|
+
name: route.name,
|
|
95
|
+
description: route.description,
|
|
96
|
+
parser: route.parser,
|
|
97
|
+
middleware: route.middleware,
|
|
98
|
+
session: route.session,
|
|
99
|
+
policy: route.policy,
|
|
100
|
+
request: route.request,
|
|
101
|
+
response: {
|
|
102
|
+
body: route.response && route.response.body !== undefined ? route.response.body : !isMinimalRouter(route.handler),
|
|
103
|
+
middleware: route.response && route.response.middleware !== undefined ? route.response.middleware : undefined
|
|
88
104
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
description: route.description,
|
|
95
|
-
parser: route.parser,
|
|
96
|
-
middleware: route.middleware,
|
|
97
|
-
session: route.session,
|
|
98
|
-
policy: route.policy,
|
|
99
|
-
request: route.request,
|
|
100
|
-
response: {
|
|
101
|
-
body: route.response && route.response.body ? route.response.body : true,
|
|
102
|
-
middleware: route.response && route.response.middleware ? route.response.middleware : undefined
|
|
103
|
-
}
|
|
104
|
-
},
|
|
105
|
-
path,
|
|
106
|
-
method: routeMethods,
|
|
107
|
-
handler: route.handler
|
|
108
|
-
};
|
|
109
|
-
(features[newFeatureSubPath] as any).___filePath = ___filePath;
|
|
105
|
+
}];
|
|
106
|
+
if (route.postFolder) {
|
|
107
|
+
postFolder.push(useCallArgs);
|
|
108
|
+
} else {
|
|
109
|
+
router.use(...useCallArgs);
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
|
-
|
|
113
|
+
for (const f of dirs) {
|
|
114
|
+
const { name } = pathParse(f);
|
|
115
|
+
router = await traverseAPIRouteDir(`${featureName}_${name}`.toUpperCase(), join(dirname, f), `${basePath}/${name}`, router, logger);
|
|
116
|
+
}
|
|
117
|
+
for (const post of postFolder) {
|
|
118
|
+
router.use(...post);
|
|
119
|
+
}
|
|
120
|
+
return router;
|
|
114
121
|
};
|
package/src/common/index.ts
CHANGED
|
@@ -7,5 +7,5 @@ export * from "./error";
|
|
|
7
7
|
export * from "./config";
|
|
8
8
|
export * from "./logger";
|
|
9
9
|
export { normalizePath, newURL } from "./url-utils";
|
|
10
|
-
export { traverseAPIRouteDir
|
|
10
|
+
export { traverseAPIRouteDir } from "./api-router-utils";
|
|
11
11
|
export { OldResponseHandler, ResponseHandler } from "./router-utils";
|
|
@@ -43,7 +43,7 @@ export class Logger extends EventTarget implements Logger {
|
|
|
43
43
|
|
|
44
44
|
constructor(protected readonly identifier: string, protected level: LogLevel = "info", options?: { transports?: LoggerTransport[]; formatter?: LoggerFormatter; }) {
|
|
45
45
|
super();
|
|
46
|
-
if (
|
|
46
|
+
if (LOG_LEVEL_MAP[level] === undefined) {
|
|
47
47
|
throw new Error(`Unknown level [${level}]`);
|
|
48
48
|
}
|
|
49
49
|
this.addEventListener(LoggerEvents.error, (ev: any) => {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {LoggerTransportWriteArgs, LogLevel} from "../interfaces.js";
|
|
1
|
+
import { LoggerTransportWriteArgs, LogLevel } from "../interfaces.js";
|
|
2
2
|
|
|
3
3
|
export function ConsoleTransport(color: boolean = true, level?: LogLevel) {
|
|
4
4
|
return {
|
|
5
5
|
level,
|
|
6
|
-
write: async ({level, out}: LoggerTransportWriteArgs): Promise<void> => {
|
|
6
|
+
write: async ({ level, out }: LoggerTransportWriteArgs): Promise<void> => {
|
|
7
7
|
if (color) {
|
|
8
8
|
switch (level) {
|
|
9
9
|
case "warn":
|
|
@@ -18,14 +18,16 @@ export function ConsoleTransport(color: boolean = true, level?: LogLevel) {
|
|
|
18
18
|
case "debug":
|
|
19
19
|
console.log("\x1b[36m%s\x1b[0m", out);
|
|
20
20
|
break;
|
|
21
|
+
case "none":
|
|
22
|
+
break;
|
|
21
23
|
default:
|
|
22
24
|
console[level](out);
|
|
23
25
|
break;
|
|
24
26
|
}
|
|
25
27
|
} else {
|
|
26
|
-
if(level === "trace" || level === "debug") {
|
|
28
|
+
if (level === "trace" || level === "debug") {
|
|
27
29
|
console.log(out);
|
|
28
|
-
} else {
|
|
30
|
+
} else if (level !== "none") {
|
|
29
31
|
console[level](out);
|
|
30
32
|
}
|
|
31
33
|
}
|
|
@@ -34,3 +34,9 @@ export const defaultLoggerFactory = (uuid: string, req: IncomingMessage): Logger
|
|
|
34
34
|
|
|
35
35
|
return logger;
|
|
36
36
|
}
|
|
37
|
+
|
|
38
|
+
export function getPathIdentifier(path: string, method: string) {
|
|
39
|
+
const pathToEnv = path.replace(/[^a-z0-9+]+/gi, '_').toUpperCase();
|
|
40
|
+
const identifier = `${pathToEnv === "_" ? "" : pathToEnv.substring(1)}${method.toUpperCase()}`;
|
|
41
|
+
return identifier;
|
|
42
|
+
}
|