@miqro/core 3.0.1 → 3.0.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.
- 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 +4 -2
- package/build/cjs/common/api-router-types.js +8 -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 +64 -46
- package/build/cjs/common/api-router-utils.js.map +1 -1
- package/build/cjs/common/context.d.ts +1 -1
- package/build/cjs/common/context.js +3 -3
- package/build/cjs/common/context.js.map +1 -1
- package/build/cjs/common/index.d.ts +2 -2
- package/build/cjs/common/index.js +2 -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 +3 -2
- 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.d.ts +1 -1
- package/build/cjs/common/logger/transport/console.js +6 -4
- package/build/cjs/common/logger/transport/console.js.map +1 -1
- package/build/cjs/common/logger/transport/file.d.ts +1 -1
- package/build/cjs/common/logger/transport/file.js +3 -3
- package/build/cjs/common/logger/transport/file.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 +10 -2
- package/build/cjs/common/router-default-logger-factory.js.map +1 -1
- package/build/cjs/common/router-handler.js +28 -16
- package/build/cjs/common/router-handler.js.map +1 -1
- package/build/cjs/common/url-utils.d.ts +0 -1
- package/build/cjs/common/url-utils.js +8 -7
- package/build/cjs/common/url-utils.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 +4 -2
- package/build/esm/common/api-router-types.js +7 -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 +62 -44
- package/build/esm/common/api-router-utils.js.map +1 -1
- package/build/esm/common/context.d.ts +1 -1
- package/build/esm/common/context.js +2 -2
- package/build/esm/common/context.js.map +1 -1
- package/build/esm/common/index.d.ts +2 -2
- package/build/esm/common/index.js +2 -2
- 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 +5 -4
- 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.d.ts +1 -1
- package/build/esm/common/logger/transport/console.js +4 -2
- package/build/esm/common/logger/transport/console.js.map +1 -1
- package/build/esm/common/logger/transport/file.d.ts +1 -1
- package/build/esm/common/logger/transport/file.js +1 -1
- package/build/esm/common/logger/transport/file.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 +8 -1
- package/build/esm/common/router-default-logger-factory.js.map +1 -1
- package/build/esm/common/router-handler.js +28 -16
- package/build/esm/common/router-handler.js.map +1 -1
- package/build/esm/common/url-utils.d.ts +0 -1
- package/build/esm/common/url-utils.js +8 -7
- package/build/esm/common/url-utils.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 +3 -3
- package/src/api-router.ts +2 -3
- package/src/common/api-router-types.ts +12 -4
- package/src/common/api-router-utils.ts +79 -61
- package/src/common/context.ts +3 -2
- package/src/common/index.ts +2 -2
- package/src/common/logger/constants.ts +5 -4
- package/src/common/logger/interfaces.ts +1 -1
- package/src/common/logger/logger.ts +1 -1
- package/src/common/logger/transport/console.ts +7 -5
- package/src/common/logger/transport/file.ts +1 -1
- package/src/common/router-default-logger-factory.ts +9 -1
- package/src/common/router-handler.ts +27 -16
- package/src/common/url-utils.ts +8 -9
- 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
|
@@ -1,34 +1,28 @@
|
|
|
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";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
...handlerWithOptionsParseOptions,
|
|
13
|
-
method: "string?|string[]?",
|
|
14
|
-
path: "string?|string[]?"
|
|
15
|
-
};
|
|
9
|
+
import { Router } from "../router";
|
|
10
|
+
import { RouterHandlerOptions } from "./router-types";
|
|
16
11
|
|
|
17
12
|
export async function traverseAPIRouteDir(
|
|
18
13
|
featureName: string,
|
|
19
14
|
dirname: string,
|
|
20
15
|
basePath = "/",
|
|
21
|
-
|
|
16
|
+
router: Router = new Router(), logger?: Logger): Promise<Router> {
|
|
22
17
|
if (logger) {
|
|
23
18
|
logger.trace("loading routes from [%s]", dirname);
|
|
24
19
|
}
|
|
25
20
|
const files = readdirSync(dirname);
|
|
26
21
|
const dirs = files.filter(f => lstatSync(join(dirname, f)).isDirectory());
|
|
27
22
|
const methods = files.filter(f => !lstatSync(join(dirname, f)).isDirectory());
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
23
|
+
|
|
24
|
+
const postFolder: [Handler | Handler[], string | undefined, Method | undefined, RouterHandlerOptions][] = [];
|
|
25
|
+
|
|
32
26
|
for (const f of methods) {
|
|
33
27
|
const { name, ext } = pathParse(f);
|
|
34
28
|
if (name !== "index" && ((ext === ".ts" || ext === ".js") && name.slice(-2) !== ".d")) {
|
|
@@ -44,19 +38,22 @@ export async function traverseAPIRouteDir(
|
|
|
44
38
|
}
|
|
45
39
|
//const ___filePath = filePath + ext;
|
|
46
40
|
const ___filePath = filePath;
|
|
47
|
-
let routeMethods: Method[] = [name] as Method[];
|
|
41
|
+
//let routeMethods: Method[] = [name] as Method[];
|
|
42
|
+
let routeMethod: Method | "use" | "USE" = name as Method;
|
|
48
43
|
if (typeof route === "function") {
|
|
49
44
|
route = {
|
|
50
45
|
handler: route
|
|
51
46
|
} as any;
|
|
52
47
|
} else {
|
|
53
|
-
if (typeof route.method !== "undefined" && (
|
|
54
|
-
throw new Error(`${resolve(dirname, name)} doesnt export method as a string or string array`);
|
|
48
|
+
if (typeof route.method !== "undefined" && (/*!(route.method instanceof Array) &&*/ (typeof route.method !== "string"))) {
|
|
49
|
+
//throw new Error(`${resolve(dirname, name)} doesnt export method as a string or string array`);
|
|
50
|
+
throw new Error(`${resolve(dirname, name)} doesnt export method as a string`);
|
|
55
51
|
} else if (typeof route.method !== "undefined") {
|
|
56
|
-
|
|
52
|
+
routeMethod = route.method;//route.method instanceof Array ? route.method : [route.method];
|
|
57
53
|
}
|
|
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`);
|
|
54
|
+
if (typeof route.path !== "string" && typeof route.path !== "undefined"/* && !(route.path instanceof Array)*/) {
|
|
55
|
+
//throw new Error(`${resolve(dirname, name)} doesnt export path as a string or a string array`);
|
|
56
|
+
throw new Error(`${resolve(dirname, name)} doesnt export path as a string`);
|
|
60
57
|
}
|
|
61
58
|
if ((!(route.handler instanceof Array) && typeof route.handler !== "function") || typeof route.handler === "undefined") {
|
|
62
59
|
throw new Error(`${resolve(dirname, name)} doesnt export handler as a function or an array`);
|
|
@@ -66,49 +63,70 @@ export async function traverseAPIRouteDir(
|
|
|
66
63
|
}
|
|
67
64
|
}
|
|
68
65
|
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
|
-
|
|
66
|
+
//const paths = route.path instanceof Array ? route.path : route.path === undefined ? [route.method && route.method.indexOf(name as Method) === -1 ? `/${name}` : "/"] : [route.path];
|
|
67
|
+
const p = route.path === undefined ? route.method && route.method.indexOf(name as Method) === -1 ? `/${name}` : "/" : route.path;
|
|
68
|
+
//for (let i = 0; i < paths.length; i++) {
|
|
69
|
+
//const p = paths[i];
|
|
70
|
+
if (typeof p !== "string" || !p) {
|
|
71
|
+
throw new Error(`${resolve(dirname, name)} doesnt export path as a string or a string array`);
|
|
72
|
+
}
|
|
73
|
+
const path = normalizePath(`${basePath}${p && p !== "/" ? `${p}` : ""}`)
|
|
76
74
|
|
|
77
|
-
|
|
78
|
-
|
|
75
|
+
const subName = (p.charAt(0) === "/" ? p.substring(1) : p).replace(/[^a-z0-9+]+/gi, '_');
|
|
76
|
+
const routeName = route.name ? route.name : name;
|
|
79
77
|
|
|
80
|
-
|
|
78
|
+
//const identifier = route.identifier ? route.identifier : getPathIdentifier(path, routeMethod);
|
|
81
79
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
80
|
+
/*let newFeatureSubPath = `${featureName}_${route.name ? route.name : name}${routeName !== subName ? `_${subName}` : ""}`.toUpperCase();
|
|
81
|
+
|
|
82
|
+
newFeatureSubPath = newFeatureSubPath.length > 1 && newFeatureSubPath.charAt(newFeatureSubPath.length - 1) === "_" ? newFeatureSubPath.substring(0, newFeatureSubPath.length - 1) : newFeatureSubPath;*/
|
|
83
|
+
/*if (features[identifier]) {
|
|
84
|
+
throw new Error(`${___filePath} feature name collides with ${(features[identifier] as any).___filePath}`)
|
|
85
|
+
}*/
|
|
86
|
+
if (route.identifier && route.identifier === "__prototype__" || route.identifier === "__proto__") {
|
|
87
|
+
throw new Error(`invalid feature name ${route.identifier}`);
|
|
88
|
+
}
|
|
89
|
+
if (route.postFolder) {
|
|
90
|
+
postFolder.push([route.handler, path, routeMethod !== "use" && routeMethod !== "USE" ? routeMethod : undefined, {
|
|
91
|
+
identifier: route.identifier,
|
|
92
|
+
apiName: route.apiName,
|
|
93
|
+
name: route.name,
|
|
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
|
+
} else {
|
|
106
|
+
router.use(route.handler, path, routeMethod !== "use" && routeMethod !== "USE" ? routeMethod : undefined, {
|
|
107
|
+
identifier: route.identifier,
|
|
108
|
+
apiName: route.apiName,
|
|
109
|
+
name: route.name,
|
|
110
|
+
description: route.description,
|
|
111
|
+
parser: route.parser,
|
|
112
|
+
middleware: route.middleware,
|
|
113
|
+
session: route.session,
|
|
114
|
+
policy: route.policy,
|
|
115
|
+
request: route.request,
|
|
116
|
+
response: {
|
|
117
|
+
body: route.response && route.response.body ? route.response.body : true,
|
|
118
|
+
middleware: route.response && route.response.middleware ? route.response.middleware : undefined
|
|
119
|
+
}
|
|
120
|
+
});
|
|
110
121
|
}
|
|
111
122
|
}
|
|
112
123
|
}
|
|
113
|
-
|
|
124
|
+
for (const f of dirs) {
|
|
125
|
+
const { name } = pathParse(f);
|
|
126
|
+
router = await traverseAPIRouteDir(`${featureName}_${name}`.toUpperCase(), join(dirname, f), `${basePath}/${name}`, router, logger);
|
|
127
|
+
}
|
|
128
|
+
for (const post of postFolder) {
|
|
129
|
+
router.use(...post);
|
|
130
|
+
}
|
|
131
|
+
return router;
|
|
114
132
|
};
|
package/src/common/context.ts
CHANGED
|
@@ -19,7 +19,7 @@ export type Method =
|
|
|
19
19
|
| "PATCH"
|
|
20
20
|
| "OPTIONS";
|
|
21
21
|
|
|
22
|
-
export const IGNORE_HEADER = ["keep-alive", "connection"];
|
|
22
|
+
//export const IGNORE_HEADER = ["keep-alive", "connection"];
|
|
23
23
|
|
|
24
24
|
export interface HandlerResponse {
|
|
25
25
|
status?: number;
|
|
@@ -34,6 +34,7 @@ export type ErrorHandler<T = boolean | void | any> =
|
|
|
34
34
|
| ((e: unknown, ctx: Context) => T);
|
|
35
35
|
|
|
36
36
|
export class Context {
|
|
37
|
+
public static IGNORE_HEADER = ["keep-alive", "connection"];
|
|
37
38
|
//public readonly url: URL; // new URL(req.url ? req.url : "/", `http://${req.headers.host}`)
|
|
38
39
|
public readonly startMS: number; // Date.now()
|
|
39
40
|
public session?: Session; // a middleware will fill this
|
|
@@ -124,7 +125,7 @@ export class Context {
|
|
|
124
125
|
this.res.statusCode = status !== undefined ? status : 200;
|
|
125
126
|
const keys = Object.keys(headers);
|
|
126
127
|
for (const key of keys) {
|
|
127
|
-
if (IGNORE_HEADER.indexOf(key.toLocaleLowerCase()) !== -1) {
|
|
128
|
+
if (Context.IGNORE_HEADER.indexOf(key.toLocaleLowerCase()) !== -1) {
|
|
128
129
|
this.logger.warn("ignoring " + key);
|
|
129
130
|
continue;
|
|
130
131
|
}
|
package/src/common/index.ts
CHANGED
|
@@ -6,6 +6,6 @@ export * from "./session";
|
|
|
6
6
|
export * from "./error";
|
|
7
7
|
export * from "./config";
|
|
8
8
|
export * from "./logger";
|
|
9
|
-
export { normalizePath } from "./url-utils";
|
|
10
|
-
export { traverseAPIRouteDir
|
|
9
|
+
export { normalizePath, newURL } from "./url-utils";
|
|
10
|
+
export { traverseAPIRouteDir } from "./api-router-utils";
|
|
11
11
|
export { OldResponseHandler, ResponseHandler } from "./router-utils";
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {LoggerFormatter, LoggerTransport} from "./interfaces.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import {FileTransport} from "./transport/file.js";
|
|
3
|
+
import {ConsoleTransport} from "./transport/console.js";
|
|
4
4
|
|
|
5
5
|
export const DEFAULT_LOGGER_TRANSPORTS: () => LoggerTransport[] = () => [
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
ConsoleTransport(),
|
|
7
|
+
FileTransport()
|
|
8
8
|
];
|
|
9
9
|
|
|
10
10
|
export const DEFAULT_LOGGER_FORMATTER: LoggerFormatter =
|
|
@@ -15,6 +15,7 @@ export const DEFAULT_LOGGER_FORMATTER: LoggerFormatter =
|
|
|
15
15
|
|
|
16
16
|
export const LOG_LEVEL_MAP =
|
|
17
17
|
{
|
|
18
|
+
"none": 0,
|
|
18
19
|
"error": 1,
|
|
19
20
|
"warn": 2,
|
|
20
21
|
"info": 3,
|
|
@@ -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
|
-
export function
|
|
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 Console(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
|
}
|
|
@@ -4,7 +4,7 @@ import {LoggerTransport, LoggerTransportWriteArgs, LogLevel} from "../interfaces
|
|
|
4
4
|
|
|
5
5
|
const FILE_TRANSPORT_ENVVAR = "LOG_FILE";
|
|
6
6
|
|
|
7
|
-
export const
|
|
7
|
+
export const FileTransport: (filePath?: string | null, level?: LogLevel) => LoggerTransport = (filePath = process.env[FILE_TRANSPORT_ENVVAR] ? process.env[FILE_TRANSPORT_ENVVAR] : null, level?: LogLevel) => {
|
|
8
8
|
const fileHandler = filePath ? createWriteStream(resolve(filePath), {flags: "a"}) : null;
|
|
9
9
|
return {
|
|
10
10
|
level,
|
|
@@ -5,11 +5,13 @@ import { DEFAULT_LOGGER_FORMATTER, getLogger, Logger } from "./logger";
|
|
|
5
5
|
|
|
6
6
|
export const defaultLoggerFactory = (uuid: string, req: IncomingMessage): Logger => {
|
|
7
7
|
let path = "";
|
|
8
|
+
let query = "";
|
|
8
9
|
let urlParsingError;
|
|
9
10
|
let method = "GET";
|
|
10
11
|
let remoteAddress: string | undefined = "";
|
|
11
12
|
try {
|
|
12
13
|
const url = newURL(req.url ? req.url : "/") as URL;
|
|
14
|
+
query = url.searchParams.toString();
|
|
13
15
|
path = normalizePath(url.pathname); // normalized path
|
|
14
16
|
method = req.method ? req.method.toUpperCase() as string : "GET";
|
|
15
17
|
remoteAddress = req.socket.remoteAddress;
|
|
@@ -21,7 +23,7 @@ export const defaultLoggerFactory = (uuid: string, req: IncomingMessage): Logger
|
|
|
21
23
|
|
|
22
24
|
const logger = getLogger(identifier, {
|
|
23
25
|
formatter: (args) => {
|
|
24
|
-
args.message = `${method} ${path}${uuid ? ` [${uuid}]` : ""}${remoteAddress ? ` (${remoteAddress})` : ""} ${args.message}`;
|
|
26
|
+
args.message = `${method} ${path}${query ? `?${query}` : ""}${uuid ? ` [${uuid}]` : ""}${remoteAddress ? ` (${remoteAddress})` : ""} ${args.message}`;
|
|
25
27
|
return DEFAULT_LOGGER_FORMATTER(args);
|
|
26
28
|
}
|
|
27
29
|
}, false);
|
|
@@ -32,3 +34,9 @@ export const defaultLoggerFactory = (uuid: string, req: IncomingMessage): Logger
|
|
|
32
34
|
|
|
33
35
|
return logger;
|
|
34
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
|
+
}
|
|
@@ -98,28 +98,39 @@ export class RouterHandler {
|
|
|
98
98
|
if (this.isRouter) {
|
|
99
99
|
const router: MinimalRouter = this.handler[0] as MinimalRouter;
|
|
100
100
|
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
101
|
-
} else
|
|
101
|
+
} else {
|
|
102
102
|
if (!doc[`${prePath}${nPath}`]) {
|
|
103
103
|
doc[`${prePath}${nPath}`] = Object.create(null);
|
|
104
104
|
}
|
|
105
105
|
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
106
106
|
throw new Error("cannot create doc routes override each other");
|
|
107
107
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
108
|
+
if(this.options) {
|
|
109
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
110
|
+
description: this.options.description,
|
|
111
|
+
name: this.options.name,
|
|
112
|
+
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(),
|
|
113
|
+
policy: this.options.policy,
|
|
114
|
+
request: this.options.request ? {
|
|
115
|
+
body: this.options.request.body,
|
|
116
|
+
headers: this.options.request.headers,
|
|
117
|
+
params: this.options.request.params,
|
|
118
|
+
query: this.options.request.query,
|
|
119
|
+
} : undefined,
|
|
120
|
+
response: this.options.response ? {
|
|
121
|
+
body: this.options.response.body
|
|
122
|
+
} : undefined
|
|
123
|
+
};
|
|
124
|
+
} else {
|
|
125
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
126
|
+
description: undefined,
|
|
127
|
+
name: undefined,
|
|
128
|
+
identifier: `${prePath}${nPath}${nMethod ? `_${nMethod}` : ""}`.replace(/[^a-z0-9+]+/gi, '_').split("_").map(s => s.trim()).filter(s => s).join("_").toUpperCase(),
|
|
129
|
+
policy: undefined,
|
|
130
|
+
request: undefined,
|
|
131
|
+
response: undefined
|
|
132
|
+
};
|
|
133
|
+
}
|
|
123
134
|
}
|
|
124
135
|
return doc;
|
|
125
136
|
}
|
package/src/common/url-utils.ts
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
|
-
import {URLSearchParams} from "url";
|
|
2
|
-
|
|
3
1
|
export const newURL = (input: string): URL => {
|
|
4
2
|
/*
|
|
5
|
-
|
|
3
|
+
remove /+ at the start of the input before using new URL ( it changes the host )
|
|
6
4
|
*/
|
|
7
|
-
let
|
|
8
|
-
for(
|
|
9
|
-
|
|
10
|
-
if(c !== "/") {
|
|
11
|
-
isOnlyTrailing = false;
|
|
5
|
+
let slashIndex = 0
|
|
6
|
+
for (slashIndex = 0; slashIndex < input.length; slashIndex++) {
|
|
7
|
+
if (input.charAt(slashIndex) !== "/") {
|
|
12
8
|
break;
|
|
13
9
|
}
|
|
14
10
|
}
|
|
15
|
-
const
|
|
11
|
+
const removedSlashes = input.substring(0, slashIndex - 1);
|
|
12
|
+
const ret = new URL(input.substring(slashIndex), "http://localhost");
|
|
13
|
+
// restore path
|
|
14
|
+
ret.pathname = `${slashIndex > 0 ? removedSlashes : ""}${ret.pathname}`;
|
|
16
15
|
return ret;
|
|
17
16
|
}
|
|
18
17
|
|
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
|
}
|