@miqro/core 1.3.2 → 1.3.6
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/README.md +79 -193
- package/dist/handler/api-handler.d.ts +1 -1
- package/dist/handler/api-handler.js +2 -1
- package/dist/handler/api-handler.js.map +1 -1
- package/dist/handler/api-router-utils.d.ts +1 -8
- package/dist/handler/api-router-utils.js +16 -14
- package/dist/handler/api-router-utils.js.map +1 -1
- package/dist/handler/app.d.ts +3 -9
- package/dist/handler/app.js +10 -14
- package/dist/handler/app.js.map +1 -1
- package/dist/handler/common/context.d.ts +1 -1
- package/dist/handler/common/router-utils.d.ts +59 -0
- package/dist/handler/common/router-utils.js +134 -0
- package/dist/handler/common/router-utils.js.map +1 -0
- package/dist/handler/common/router.d.ts +23 -21
- package/dist/handler/common/router.js +55 -81
- package/dist/handler/common/router.js.map +1 -1
- package/dist/handler/common/tokenize-match.d.ts +18 -9
- package/dist/handler/common/tokenize-match.js +34 -28
- package/dist/handler/common/tokenize-match.js.map +1 -1
- package/dist/handler/feature-router.d.ts +4 -2
- package/dist/handler/feature-router.js +1 -1
- package/dist/handler/feature-router.js.map +1 -1
- package/dist/handler/middleware/parse.d.ts +1 -0
- package/dist/handler/middleware/parse.js +10 -0
- package/dist/handler/middleware/parse.js.map +1 -1
- package/dist/util/client.d.ts +17 -0
- package/dist/util/client.js +55 -0
- package/dist/util/client.js.map +1 -0
- package/dist/util/cookie/cookie.d.ts +0 -1
- package/dist/util/parser/get.js +3 -6
- package/dist/util/parser/get.js.map +1 -1
- package/dist/util/request/common.d.ts +0 -1
- package/package.json +1 -2
- package/src/handler/api-handler.ts +3 -2
- package/src/handler/api-router-utils.ts +17 -20
- package/src/handler/app.ts +11 -19
- package/src/handler/common/context.ts +1 -1
- package/src/handler/common/router-utils.ts +184 -0
- package/src/handler/common/router.ts +66 -95
- package/src/handler/common/tokenize-match.ts +40 -40
- package/src/handler/feature-router.ts +4 -2
- package/src/handler/middleware/parse.ts +11 -1
- package/src/util/client.ts +64 -0
- package/src/util/parser/get.ts +3 -5
- package/tsconfig.json +2 -2
|
@@ -1,109 +1,65 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { IncomingMessage, RequestListener, ServerResponse } from "http";
|
|
2
|
+
import { Logger } from "../../util/logger";
|
|
3
|
+
import { Method, Handler, Context, ErrorHandler } from "./context";
|
|
4
|
+
import { RouterHandler, RouterJSONDoc, RouterHandlerOptions, HandlerWithOptions } from "./router-utils";
|
|
5
|
+
import { normalizePath, PathPartToken, splitPath } from "./tokenize-match";
|
|
3
6
|
|
|
4
|
-
interface
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
path?: string;
|
|
8
|
-
tokens: {
|
|
9
|
-
optional: boolean;
|
|
10
|
-
wild: boolean;
|
|
11
|
-
token: string;
|
|
12
|
-
}[]
|
|
7
|
+
export interface RouterOptions {
|
|
8
|
+
newRequestLogger?: (req: IncomingMessage) => Logger;
|
|
9
|
+
newContext?: (req: IncomingMessage, res: ServerResponse) => Context;
|
|
13
10
|
}
|
|
14
11
|
|
|
15
12
|
export class Router {
|
|
16
13
|
protected readonly handlers: RouterHandler[] = [];
|
|
17
14
|
protected readonly errorHandlers: ErrorHandler[] = [];
|
|
18
|
-
public
|
|
19
|
-
|
|
15
|
+
public readonly listener: RequestListener;
|
|
16
|
+
constructor(protected config?: RouterOptions) {
|
|
17
|
+
this.listener = async (req, res) => {
|
|
18
|
+
const ctx = this.config && this.config.newContext ?
|
|
19
|
+
this.config.newContext(req, res) :
|
|
20
|
+
new Context(
|
|
21
|
+
req,
|
|
22
|
+
res,
|
|
23
|
+
this.config && this.config.newRequestLogger ?
|
|
24
|
+
this.config.newRequestLogger(req) :
|
|
25
|
+
undefined);
|
|
26
|
+
ctx.logger.trace(`request received`);
|
|
27
|
+
await this.run(ctx);
|
|
28
|
+
};
|
|
20
29
|
}
|
|
21
|
-
public
|
|
22
|
-
return this.use(handler, path, "
|
|
30
|
+
public get(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
31
|
+
return this.use(handler, path, "get", options);
|
|
23
32
|
}
|
|
24
|
-
public
|
|
25
|
-
return this.use(handler, path, "
|
|
33
|
+
public post(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
34
|
+
return this.use(handler, path, "post", options);
|
|
26
35
|
}
|
|
27
|
-
public
|
|
28
|
-
return this.use(handler, path, "
|
|
36
|
+
public patch(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
37
|
+
return this.use(handler, path, "patch", options);
|
|
29
38
|
}
|
|
30
|
-
public
|
|
31
|
-
return this.use(handler, path, "
|
|
39
|
+
public delete(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
40
|
+
return this.use(handler, path, "delete", options);
|
|
32
41
|
}
|
|
33
|
-
public
|
|
34
|
-
return this.use(handler, path, "options
|
|
42
|
+
public put(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
43
|
+
return this.use(handler, path, "put", options);
|
|
35
44
|
}
|
|
36
|
-
public
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
path: nPath,
|
|
44
|
-
tokens: tokenizePath(nPath)
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
} else {
|
|
48
|
-
const nPath = path !== undefined ? normalizePath(path) : undefined;
|
|
49
|
-
this.handlers.push({
|
|
50
|
-
handler,
|
|
51
|
-
method: method ? method.toUpperCase() as Method : method,
|
|
52
|
-
path: nPath,
|
|
53
|
-
tokens: tokenizePath(nPath)
|
|
54
|
-
});
|
|
55
|
-
}
|
|
45
|
+
public options(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
46
|
+
return this.use(handler, path, "options", options);
|
|
47
|
+
}
|
|
48
|
+
public use(handler: Array<Handler> | HandlerWithOptions | Handler | Router, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
|
|
49
|
+
const nPath = path !== undefined ? normalizePath(path) : undefined;
|
|
50
|
+
const nMethod = method ? method.toUpperCase() as Method : method;
|
|
51
|
+
this.handlers.push(new RouterHandler(handler, nPath, nMethod, options));
|
|
56
52
|
return this;
|
|
57
53
|
}
|
|
58
54
|
public catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
this.errorHandlers.push(errorHandler);
|
|
55
|
+
const errorHandlers = errorHandler instanceof Array ? errorHandler : [errorHandler];
|
|
56
|
+
for (const e of errorHandlers) {
|
|
57
|
+
this.errorHandlers.push(e);
|
|
65
58
|
}
|
|
66
59
|
return this;
|
|
67
60
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
if (h.path === undefined) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const ret = typeof h.handler === "function" || h.handler instanceof Array ?
|
|
76
|
-
matchTokenizePath(false, h.tokens, ctx.path, prePath) :
|
|
77
|
-
matchTokenizePath(true, h.tokens, ctx.path, prePath);
|
|
78
|
-
ctx.params = ret.params;
|
|
79
|
-
return ret.match;
|
|
80
|
-
}
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
protected callHandler = async (ctx: Context, handler: Handler | Router, prePath?: string): Promise<boolean> => {
|
|
84
|
-
const shouldContinue = typeof handler === "function" ?
|
|
85
|
-
await handler(ctx) : await handler.run(ctx, prePath);
|
|
86
|
-
if (shouldContinue === false) {
|
|
87
|
-
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
88
|
-
return false;
|
|
89
|
-
} else if (shouldContinue !== true && shouldContinue !== undefined) {
|
|
90
|
-
ctx.logger.trace("pushing to results [%o]", shouldContinue);
|
|
91
|
-
ctx.results.push(shouldContinue);
|
|
92
|
-
} /*else {
|
|
93
|
-
// ctx.logger.debug("NOT pushing to results [%s]", String(shouldContinue));
|
|
94
|
-
}*/
|
|
95
|
-
return true;
|
|
96
|
-
};
|
|
97
|
-
protected async handleError(e: unknown, ctx: Context): Promise<boolean> {
|
|
98
|
-
for (const eH of this.errorHandlers) {
|
|
99
|
-
const ret = await eH(e, ctx);
|
|
100
|
-
if (ret === false || ctx.res.headersSent) {
|
|
101
|
-
return false;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
throw e; // pass the error along
|
|
105
|
-
}
|
|
106
|
-
public async run(ctx: Context, prePath?: string): Promise<boolean> {
|
|
61
|
+
public async run(ctx: Context, pathTokens?: { value: string; lower: string; }[], prePathTokens: PathPartToken[] = []): Promise<boolean> {
|
|
62
|
+
pathTokens = pathTokens ? pathTokens : splitPath(ctx.path);
|
|
107
63
|
try {
|
|
108
64
|
let shouldContinue = true;
|
|
109
65
|
for (const h of this.handlers) {
|
|
@@ -111,17 +67,32 @@ export class Router {
|
|
|
111
67
|
shouldContinue = false;
|
|
112
68
|
break;
|
|
113
69
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
70
|
+
const isMatch = h.isMatch(ctx.method, pathTokens, prePathTokens);
|
|
71
|
+
if (isMatch.match) {
|
|
72
|
+
if (isMatch.params) {
|
|
73
|
+
ctx.params = ctx.params ? {
|
|
74
|
+
...ctx.params,
|
|
75
|
+
...isMatch.params
|
|
76
|
+
} : isMatch.params;
|
|
77
|
+
}
|
|
78
|
+
shouldContinue = await h.invoke(ctx, pathTokens, prePathTokens);
|
|
120
79
|
}
|
|
121
80
|
}
|
|
122
81
|
return !ctx.res.headersSent;
|
|
123
82
|
} catch (e: any) {
|
|
124
|
-
|
|
83
|
+
for (const eH of this.errorHandlers) {
|
|
84
|
+
const ret = await eH(e, ctx);
|
|
85
|
+
if (ret === false || ctx.res.headersSent) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
throw e; // pass the error along
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
93
|
+
for (const h of this.handlers) {
|
|
94
|
+
doc = h.getJSONDoc(doc, prePath);
|
|
125
95
|
}
|
|
96
|
+
return doc;
|
|
126
97
|
}
|
|
127
98
|
}
|
|
@@ -59,23 +59,37 @@ export const normalizePath = (path: string): string => {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
-
export
|
|
62
|
+
export interface PathPartToken {
|
|
63
63
|
optional: boolean;
|
|
64
|
-
wild
|
|
64
|
+
wild?: string;
|
|
65
65
|
token: string;
|
|
66
|
-
|
|
66
|
+
lower: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const pathPartTokenize = (token: { value: string; lower: string; }): PathPartToken => {
|
|
70
|
+
const isWild = token.value[0] === ":";
|
|
71
|
+
const optional = token.value[token.value.length - 1] === "?";
|
|
72
|
+
const wild = isWild ? token.value.substring(1, token.value.length - (optional ? 1 : 0)) : undefined;
|
|
73
|
+
return {
|
|
74
|
+
optional,
|
|
75
|
+
wild,
|
|
76
|
+
token: token.value,
|
|
77
|
+
lower: token.lower
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const splitPath = (path?: string): { value: string; lower: string; }[] => path === undefined ? [] : path.split("/").filter(p => p).map(p => {
|
|
82
|
+
return {
|
|
83
|
+
value: p,
|
|
84
|
+
lower: p.toLocaleLowerCase()
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
export const tokenizePath = (path?: string): PathPartToken[] => {
|
|
67
89
|
if (path === undefined) {
|
|
68
90
|
return [];
|
|
69
91
|
}
|
|
70
|
-
const tokens = path
|
|
71
|
-
const wild = token[0] === ":";
|
|
72
|
-
const optional = token[token.length - 1] === "?";
|
|
73
|
-
return {
|
|
74
|
-
optional,
|
|
75
|
-
wild,
|
|
76
|
-
token
|
|
77
|
-
}
|
|
78
|
-
});
|
|
92
|
+
const tokens = splitPath(path).map(token => pathPartTokenize(token));
|
|
79
93
|
for (let i = 0; i < tokens.length; i++) {
|
|
80
94
|
if (tokens[i].optional && i != tokens.length - 1) {
|
|
81
95
|
throw new Error("cannot set a path token as optional that's not the last one");
|
|
@@ -84,52 +98,38 @@ export const tokenizePath = (path?: string): {
|
|
|
84
98
|
return tokens;
|
|
85
99
|
}
|
|
86
100
|
|
|
87
|
-
export const matchTokenizePath = (
|
|
88
|
-
optional: boolean;
|
|
89
|
-
wild: boolean;
|
|
90
|
-
token: string;
|
|
91
|
-
}[], normalizePath: string, prePath?: string): {
|
|
101
|
+
export const matchTokenizePath = (checkAsRouter: boolean, tokens: PathPartToken[], ctxTokens: { value: string; lower: string; }[]): {
|
|
92
102
|
match: boolean;
|
|
93
|
-
params
|
|
103
|
+
params?: { [name: string]: string | undefined }
|
|
94
104
|
} => {
|
|
95
|
-
|
|
96
|
-
tokens
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
token: s,
|
|
100
|
-
wild: false
|
|
101
|
-
};
|
|
102
|
-
}) as {
|
|
103
|
-
optional: boolean;
|
|
104
|
-
wild: boolean;
|
|
105
|
-
token: string;
|
|
106
|
-
}[]).concat(tokens);
|
|
105
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
106
|
+
if (tokens[i].optional && i != tokens.length - 1) {
|
|
107
|
+
throw new Error("cannot set a path token as optional that's not the last one");
|
|
108
|
+
}
|
|
107
109
|
}
|
|
108
|
-
|
|
109
|
-
const ctxTokens = normalizePath.split("/").filter(p => p);
|
|
110
110
|
const lastTokenIsOptional = tokens.length > 0 ? tokens[tokens.length - 1].optional : false;
|
|
111
111
|
const couldBeUsingOptional = (lastTokenIsOptional &&
|
|
112
112
|
(
|
|
113
|
-
(!
|
|
114
|
-
(
|
|
113
|
+
(!checkAsRouter && tokens.length - 1 === ctxTokens.length) ||
|
|
114
|
+
(checkAsRouter && tokens.length - 1 <= ctxTokens.length)
|
|
115
115
|
)
|
|
116
116
|
);
|
|
117
117
|
|
|
118
118
|
const params: { [name: string]: string | undefined } = {};
|
|
119
119
|
|
|
120
|
-
if ((!
|
|
120
|
+
if ((!checkAsRouter && tokens.length === ctxTokens.length) || couldBeUsingOptional || (checkAsRouter && tokens.length <= ctxTokens.length)) {
|
|
121
121
|
// similiar count of tokens
|
|
122
122
|
for (let i = 0; i < ctxTokens.length; i++) {
|
|
123
123
|
const ctxToken = ctxTokens[i];
|
|
124
|
-
if (
|
|
124
|
+
if (checkAsRouter && i >= tokens.length) {
|
|
125
125
|
break;
|
|
126
126
|
}
|
|
127
127
|
|
|
128
|
-
if (tokens[i].wild || ctxToken.
|
|
128
|
+
if (tokens[i].wild || ctxToken.lower === tokens[i].lower) {
|
|
129
129
|
// check pass
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
params[
|
|
130
|
+
const wild = tokens[i].wild;
|
|
131
|
+
if (wild) {
|
|
132
|
+
params[wild] = ctxToken.value;
|
|
133
133
|
}
|
|
134
134
|
continue;
|
|
135
135
|
}
|
|
@@ -3,14 +3,16 @@ import { Logger } from "../util/logger";
|
|
|
3
3
|
import { isFeatureEnabled } from "../util/config/featuretoggle";
|
|
4
4
|
import { normalizePath } from "./common/tokenize-match";
|
|
5
5
|
import { Router, Handler, Method } from "./common";
|
|
6
|
+
import { RouterHandlerOptions } from "./common/router-utils";
|
|
6
7
|
|
|
7
8
|
export interface FeatureRouterPathOptions {
|
|
8
9
|
path: string | string[];
|
|
9
10
|
method: Method | Method[];
|
|
10
11
|
handler: Array<Handler> | Handler;
|
|
12
|
+
options?: RouterHandlerOptions;
|
|
11
13
|
}
|
|
12
14
|
|
|
13
|
-
export type FeatureRouterOptions
|
|
15
|
+
export type FeatureRouterOptions = SimpleMap<FeatureRouterPathOptions>;
|
|
14
16
|
|
|
15
17
|
const FEATURE_ROUTER_METHODS: Method[] = ["get", "post", "put", "delete", "patch", "options"];
|
|
16
18
|
|
|
@@ -63,7 +65,7 @@ export const FeatureRouter = (options: FeatureRouterOptions, logger?: Logger): R
|
|
|
63
65
|
throw new Error(`${featureName} collides with feature ${enabledPaths[`${p}${m}`]} on path ${p} method ${m}`)
|
|
64
66
|
}
|
|
65
67
|
enabledPaths[`${p}${m}`] = featureName;
|
|
66
|
-
router.use(implementation, p, m.toLocaleLowerCase() as Method);
|
|
68
|
+
router.use(implementation, p, m.toLocaleLowerCase() as Method, handlerOptions.options);
|
|
67
69
|
}
|
|
68
70
|
}
|
|
69
71
|
} else {
|
|
@@ -3,6 +3,7 @@ import { ParseOptions } from "../../util/parser/common";
|
|
|
3
3
|
import { Context, Handler } from "../common";
|
|
4
4
|
|
|
5
5
|
export interface ParseRequestOptions {
|
|
6
|
+
headers?: ParseOptions | ParseOptions[];
|
|
6
7
|
query?: ParseOptions | false | ParseOptions[];
|
|
7
8
|
params?: ParseOptions | false | ParseOptions[];
|
|
8
9
|
body?: ParseOptions | false | ParseOptions[];
|
|
@@ -22,7 +23,7 @@ const ADD_EXTRA: ParseOptions[] = [{
|
|
|
22
23
|
const normalizeParseOptions = (option?: ParseOptions | false | ParseOptions[]): ParseOptions[] =>
|
|
23
24
|
option ? (option instanceof Array ? option : [option]) : (option === false ? NO_OPTIONS : ADD_EXTRA);
|
|
24
25
|
|
|
25
|
-
const parseRequestPart = (part: "query" | "body" | "params", ctx: Context, option: ParseOptions[], parser?: Parser): void => {
|
|
26
|
+
const parseRequestPart = (part: "query" | "body" | "params" | "headers", ctx: Context, option: ParseOptions[], parser?: Parser): void => {
|
|
26
27
|
const value = ctx[part] === undefined ? {} : ctx[part];
|
|
27
28
|
for (let i = 0; i < option.length; i++) {
|
|
28
29
|
const o = option[i];
|
|
@@ -46,9 +47,18 @@ export const ParseRequest = (options: ParseRequestOptions): Handler<void> => {
|
|
|
46
47
|
const query = normalizeParseOptions(options.query);
|
|
47
48
|
const params = normalizeParseOptions(options.params);
|
|
48
49
|
const body = normalizeParseOptions(options.body);
|
|
50
|
+
const headers = normalizeParseOptions(options.headers);
|
|
49
51
|
|
|
50
52
|
return async (ctx: Context): Promise<void> => {
|
|
51
53
|
try {
|
|
54
|
+
try {
|
|
55
|
+
if (options.headers !== undefined) {
|
|
56
|
+
parseRequestPart("headers", ctx, headers, options.parser);
|
|
57
|
+
}
|
|
58
|
+
} catch (e) {
|
|
59
|
+
ctx.logger.error("error parsing headers %o", ctx.headers);
|
|
60
|
+
throw e;
|
|
61
|
+
}
|
|
52
62
|
try {
|
|
53
63
|
if (options.query !== undefined) {
|
|
54
64
|
parseRequestPart("query", ctx, query, options.parser);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { request, RequestOptions, RequestResponse } from "./request";
|
|
2
|
+
import { Logger, LogLevel } from "./logger";
|
|
3
|
+
import { SimpleMap } from "./parser";
|
|
4
|
+
|
|
5
|
+
export type RequestMapEndpoint = (args: Partial<RequestOptions>, logger?: Logger) => Promise<RequestResponse>;
|
|
6
|
+
|
|
7
|
+
export type RequestMapEndpointMap = SimpleMap<RequestMapEndpoint>
|
|
8
|
+
|
|
9
|
+
const SEPARATOR = "_";
|
|
10
|
+
|
|
11
|
+
export class RequestMap {
|
|
12
|
+
|
|
13
|
+
public endpoints: RequestMapEndpointMap = {};
|
|
14
|
+
constructor(endpoints: {
|
|
15
|
+
[path: string]: {
|
|
16
|
+
[method: string]: {
|
|
17
|
+
name?: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}, hostname: string, private readonly logger?: Logger, baseOptions?: Partial<RequestOptions>) {
|
|
22
|
+
const paths = Object.keys(endpoints);
|
|
23
|
+
for (const p of paths) {
|
|
24
|
+
const methods = Object.keys(endpoints[p]);
|
|
25
|
+
for (const m of methods) {
|
|
26
|
+
const mL = m.toLocaleLowerCase();
|
|
27
|
+
const endpointPath = `${p.split("/").map(p => p.trim()).map(p => p.replaceAll(":", "").replaceAll("?", "")).filter(p => p).map(p => p.toLocaleLowerCase()).join(SEPARATOR)}${SEPARATOR}${mL}`;
|
|
28
|
+
//const routerOptions: RouterHandlerOptions = doc[p][m];
|
|
29
|
+
if (!this.endpoints[endpointPath]) {
|
|
30
|
+
this.endpoints[endpointPath] = async (args: Partial<RequestOptions>, logger?: Logger) => {
|
|
31
|
+
const log = (level: LogLevel, text: string, ...optionalParams: any[]) => {
|
|
32
|
+
if (this.logger) {
|
|
33
|
+
this.logger[level](text, ...optionalParams);
|
|
34
|
+
}
|
|
35
|
+
if (logger) {
|
|
36
|
+
logger[level](text, ...optionalParams);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
|
|
41
|
+
const realOptions = {
|
|
42
|
+
...baseOptions,
|
|
43
|
+
url: `${hostname}${p}`,
|
|
44
|
+
method: m,
|
|
45
|
+
...args
|
|
46
|
+
};
|
|
47
|
+
log("trace", "request to %o", realOptions);
|
|
48
|
+
log("debug", "request to %s %s", realOptions.method, realOptions.url);
|
|
49
|
+
const response = await request(realOptions);
|
|
50
|
+
log("trace", "response from %s %s %o", realOptions.method, realOptions.url, response);
|
|
51
|
+
log("debug", "response from %s %s", realOptions.method, realOptions.url);
|
|
52
|
+
return response;
|
|
53
|
+
} catch (e: any) {
|
|
54
|
+
log("error", "request failed with message %s", e.message);
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error("two methods for same endpoint");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/util/parser/get.ts
CHANGED
|
@@ -3,14 +3,12 @@ import { ParseOptionsError } from "../error";
|
|
|
3
3
|
import { BasicParseOption, ParseValueArgs } from "./common";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
usage const name = get(obj, "user.info.name", {type: "string"});
|
|
6
|
+
usage const name = get(obj, "user.info.name", defaultValue, {type: "string"}, parser);
|
|
7
7
|
*/
|
|
8
8
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
9
9
|
export const get = (obj: any, attrPath: string, defaultValue?: any, option?: BasicParseOption, parser?: Parser): any | undefined => {
|
|
10
|
-
if (typeof obj !== "object"
|
|
11
|
-
|
|
12
|
-
} else if (!obj && defaultValue !== undefined) {
|
|
13
|
-
return defaultValue;
|
|
10
|
+
if (!obj || typeof obj !== "object") {
|
|
11
|
+
return defaultValue !== undefined ? defaultValue : undefined
|
|
14
12
|
}
|
|
15
13
|
if (typeof attrPath !== "string") {
|
|
16
14
|
throw new Error(`attrPath must be typeof string`);
|