@miqro/core 1.3.6 → 1.3.8
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 +38 -96
- package/dist/handler/api-router-utils.d.ts +12 -1
- package/dist/handler/api-router-utils.js +86 -1
- package/dist/handler/api-router-utils.js.map +1 -1
- package/dist/handler/api-router.d.ts +29 -2
- package/dist/handler/api-router.js +2 -2
- package/dist/handler/api-router.js.map +1 -1
- package/dist/handler/app.d.ts +2 -1
- package/dist/handler/app.js +4 -2
- package/dist/handler/app.js.map +1 -1
- package/dist/handler/common/context.d.ts +1 -1
- package/dist/handler/common/context.js +14 -9
- package/dist/handler/common/context.js.map +1 -1
- package/dist/handler/common/router-utils.js +60 -5
- package/dist/handler/common/router-utils.js.map +1 -1
- package/dist/handler/index.d.ts +0 -3
- package/dist/handler/index.js +0 -3
- package/dist/handler/index.js.map +1 -1
- package/dist/handler/middleware/form.js +7 -1
- package/dist/handler/middleware/form.js.map +1 -1
- package/dist/handler/middleware/group.js +44 -2
- package/dist/handler/middleware/group.js.map +1 -1
- package/dist/handler/middleware/index.js +2 -6
- package/dist/handler/middleware/index.js.map +1 -1
- package/dist/handler/middleware/json.js +8 -2
- package/dist/handler/middleware/json.js.map +1 -1
- package/dist/handler/middleware/text.js +8 -1
- package/dist/handler/middleware/text.js.map +1 -1
- package/dist/handler/proxy.d.ts +2 -5
- package/dist/handler/proxy.js +2 -3
- package/dist/handler/proxy.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/{util/test → test/common}/app.d.ts +2 -2
- package/dist/{util/test → test/common}/app.js +3 -2
- package/dist/test/common/app.js.map +1 -0
- package/dist/{util/test → test/common}/common.d.ts +0 -0
- package/dist/{util/test → test/common}/common.js +0 -0
- package/dist/test/common/common.js.map +1 -0
- package/dist/{util/test → test/common}/index.d.ts +1 -0
- package/dist/{util/test → test/common}/index.js +1 -0
- package/dist/test/common/index.js.map +1 -0
- package/dist/test/common/runner.d.ts +13 -0
- package/dist/test/common/runner.js +92 -0
- package/dist/test/common/runner.js.map +1 -0
- package/dist/test/index.d.ts +1 -0
- package/dist/{handler/response → test}/index.js +1 -3
- package/dist/test/index.js.map +1 -0
- package/dist/test/main.d.ts +1 -0
- package/dist/test/main.js +19 -0
- package/dist/test/main.js.map +1 -0
- package/dist/util/cli/index.d.ts +1 -1
- package/dist/util/error/parsererror.d.ts +2 -1
- package/dist/util/error/parsererror.js +5 -1
- package/dist/util/error/parsererror.js.map +1 -1
- package/dist/util/index.d.ts +0 -1
- package/dist/util/index.js +0 -1
- package/dist/util/index.js.map +1 -1
- package/dist/util/loader/index.d.ts +0 -3
- package/dist/util/loader/index.js +2 -9
- package/dist/util/loader/index.js.map +1 -1
- package/dist/util/request/index.d.ts +1 -1
- package/dist/util/request/index.js +1 -3
- package/dist/util/request/index.js.map +1 -1
- package/dist/util/session/common.d.ts +0 -1
- package/dist/util/session/common.js +0 -46
- package/dist/util/session/common.js.map +1 -1
- package/package.json +6 -11
- package/src/handler/api-router-utils.ts +91 -2
- package/src/handler/api-router.ts +30 -4
- package/src/handler/app.ts +2 -1
- package/src/handler/common/context.ts +14 -10
- package/src/handler/common/router-utils.ts +199 -144
- package/src/handler/index.ts +0 -3
- package/src/handler/middleware/form.ts +8 -2
- package/src/handler/middleware/group.ts +44 -2
- package/src/handler/middleware/index.ts +0 -4
- package/src/handler/middleware/json.ts +9 -3
- package/src/handler/middleware/text.ts +8 -1
- package/src/handler/proxy.ts +3 -8
- package/src/index.ts +1 -0
- package/src/{util/test → test/common}/app.ts +6 -5
- package/src/{util/test → test/common}/common.ts +0 -0
- package/src/{util/test → test/common}/index.ts +1 -0
- package/src/test/common/runner.ts +97 -0
- package/src/test/index.ts +1 -0
- package/src/test/main.ts +17 -0
- package/src/util/cli/index.ts +1 -1
- package/src/util/error/parsererror.ts +6 -1
- package/src/util/index.ts +0 -1
- package/src/util/loader/index.ts +1 -7
- package/src/util/request/index.ts +1 -1
- package/src/util/session/common.ts +0 -43
- package/tsconfig-test.json +13 -0
- package/dist/handler/api-handler.d.ts +0 -29
- package/dist/handler/api-handler.js +0 -42
- package/dist/handler/api-handler.js.map +0 -1
- package/dist/handler/feature-router.d.ts +0 -12
- package/dist/handler/feature-router.js +0 -90
- package/dist/handler/feature-router.js.map +0 -1
- package/dist/handler/response/html.d.ts +0 -10
- package/dist/handler/response/html.js +0 -34
- package/dist/handler/response/html.js.map +0 -1
- package/dist/handler/response/index.d.ts +0 -3
- package/dist/handler/response/index.js.map +0 -1
- package/dist/handler/response/json.d.ts +0 -2
- package/dist/handler/response/json.js +0 -21
- package/dist/handler/response/json.js.map +0 -1
- package/dist/handler/response/result.d.ts +0 -4
- package/dist/handler/response/result.js +0 -46
- package/dist/handler/response/result.js.map +0 -1
- package/dist/util/client.d.ts +0 -17
- package/dist/util/client.js +0 -55
- package/dist/util/client.js.map +0 -1
- package/dist/util/test/app.js.map +0 -1
- package/dist/util/test/common.js.map +0 -1
- package/dist/util/test/index.js.map +0 -1
- package/src/handler/api-handler.ts +0 -55
- package/src/handler/feature-router.ts +0 -92
- package/src/handler/response/html.ts +0 -36
- package/src/handler/response/index.ts +0 -3
- package/src/handler/response/json.ts +0 -19
- package/src/handler/response/result.ts +0 -40
- package/src/util/client.ts +0 -64
package/dist/util/client.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RequestMap = void 0;
|
|
4
|
-
const request_1 = require("./request");
|
|
5
|
-
const SEPARATOR = "_";
|
|
6
|
-
class RequestMap {
|
|
7
|
-
constructor(endpoints, hostname, logger, baseOptions) {
|
|
8
|
-
this.logger = logger;
|
|
9
|
-
this.endpoints = {};
|
|
10
|
-
const paths = Object.keys(endpoints);
|
|
11
|
-
for (const p of paths) {
|
|
12
|
-
const methods = Object.keys(endpoints[p]);
|
|
13
|
-
for (const m of methods) {
|
|
14
|
-
const mL = m.toLocaleLowerCase();
|
|
15
|
-
const endpointPath = `${p.split("/").map(p => p.trim()).map(p => p.replaceAll(":", "").replaceAll("?", "")).filter(p => p).map(p => p.toLocaleLowerCase()).join(SEPARATOR)}${SEPARATOR}${mL}`;
|
|
16
|
-
//const routerOptions: RouterHandlerOptions = doc[p][m];
|
|
17
|
-
if (!this.endpoints[endpointPath]) {
|
|
18
|
-
this.endpoints[endpointPath] = async (args, logger) => {
|
|
19
|
-
const log = (level, text, ...optionalParams) => {
|
|
20
|
-
if (this.logger) {
|
|
21
|
-
this.logger[level](text, ...optionalParams);
|
|
22
|
-
}
|
|
23
|
-
if (logger) {
|
|
24
|
-
logger[level](text, ...optionalParams);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
try {
|
|
28
|
-
const realOptions = {
|
|
29
|
-
...baseOptions,
|
|
30
|
-
url: `${hostname}${p}`,
|
|
31
|
-
method: m,
|
|
32
|
-
...args
|
|
33
|
-
};
|
|
34
|
-
log("trace", "request to %o", realOptions);
|
|
35
|
-
log("debug", "request to %s %s", realOptions.method, realOptions.url);
|
|
36
|
-
const response = await (0, request_1.request)(realOptions);
|
|
37
|
-
log("trace", "response from %s %s %o", realOptions.method, realOptions.url, response);
|
|
38
|
-
log("debug", "response from %s %s", realOptions.method, realOptions.url);
|
|
39
|
-
return response;
|
|
40
|
-
}
|
|
41
|
-
catch (e) {
|
|
42
|
-
log("error", "request failed with message %s", e.message);
|
|
43
|
-
throw e;
|
|
44
|
-
}
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
throw new Error("two methods for same endpoint");
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
exports.RequestMap = RequestMap;
|
|
55
|
-
//# sourceMappingURL=client.js.map
|
package/dist/util/client.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file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AM;wBACL,MAAM,CAAC,CAAC,CAAC,CAAC;qBACX;gBACH,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAA;AAtCY,QAAA,UAAU,cAsCtB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/util/test/common.ts"],"names":[],"mappings":";;;AAOO,MAAM,IAAI,GAAG,CAAC,EAAY,EAAgB,EAAE;IACjD,MAAM,GAAG,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE;QAC7B,GAAG,CAAC,SAAS,EAAE,CAAC;QAChB,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACxB,OAAO,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;IACrB,CAAC,CAAC;IACF,GAAG,CAAC,SAAS,GAAG,CAAC,CAAC;IAClB,GAAG,CAAC,QAAQ,GAAG,EAAS,CAAC;IACzB,OAAO,GAAG,CAAC;AACb,CAAC,CAAA;AATY,QAAA,IAAI,QAShB"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/util/test/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAAyB;AACzB,wCAAsB"}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { GroupPolicy } from "../util/session";
|
|
2
|
-
import { SessionHandler, SessionHandlerOptions } from "./middleware/session";
|
|
3
|
-
import { GroupPolicyHandler } from "./middleware/group";
|
|
4
|
-
import { Parser } from "../util/parser";
|
|
5
|
-
import { ParseOptions } from "../util/parser/common";
|
|
6
|
-
import { Handler } from "./common";
|
|
7
|
-
import { ParseRequest } from "./middleware/parse";
|
|
8
|
-
import { ResponseHandler } from "./response/json";
|
|
9
|
-
import { ResultParser } from "./response/result";
|
|
10
|
-
|
|
11
|
-
export interface APIHandlerOptions {
|
|
12
|
-
parser?: Parser;
|
|
13
|
-
headers?: ParseOptions | ParseOptions[];
|
|
14
|
-
query?: ParseOptions | false | ParseOptions[];
|
|
15
|
-
params?: ParseOptions | false | ParseOptions[];
|
|
16
|
-
body?: ParseOptions | false | ParseOptions[];
|
|
17
|
-
result?: ParseOptions | ParseOptions[];
|
|
18
|
-
handler: Array<Handler> | Handler;
|
|
19
|
-
middleware?: Handler[];
|
|
20
|
-
session?: SessionHandlerOptions | Handler;
|
|
21
|
-
policy?: GroupPolicy;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* APIHandler
|
|
26
|
-
*
|
|
27
|
-
* ```typescript
|
|
28
|
-
*app.use(APIHandler({
|
|
29
|
-
* query: { ... },
|
|
30
|
-
* body: { ... },
|
|
31
|
-
* result: { ... }
|
|
32
|
-
*}));
|
|
33
|
-
* ```
|
|
34
|
-
*/
|
|
35
|
-
export const APIHandler = (options: APIHandlerOptions): Array<Handler> =>
|
|
36
|
-
// setup options.middleware
|
|
37
|
-
(options.middleware ? options.middleware : [])
|
|
38
|
-
// setup session
|
|
39
|
-
.concat(options.session ? typeof options.session === "function" ? [options.session] : [SessionHandler(options.session)] : [])
|
|
40
|
-
// setup policy
|
|
41
|
-
.concat(options.policy ? [GroupPolicyHandler(options.policy)] : [])
|
|
42
|
-
// setup request parse
|
|
43
|
-
.concat([ParseRequest({
|
|
44
|
-
headers: options.headers,
|
|
45
|
-
query: options.query,
|
|
46
|
-
params: options.params,
|
|
47
|
-
body: options.body,
|
|
48
|
-
parser: options.parser
|
|
49
|
-
})])
|
|
50
|
-
// setup options.handler
|
|
51
|
-
.concat(options.handler instanceof Array ? options.handler : [options.handler])
|
|
52
|
-
// setup response parse
|
|
53
|
-
.concat(options.result ? ResultParser(options.result, options.parser) : [])
|
|
54
|
-
// setup response handler
|
|
55
|
-
.concat(ResponseHandler());
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { SimpleMap } from "../util/parser/common";
|
|
2
|
-
import { Logger } from "../util/logger";
|
|
3
|
-
import { isFeatureEnabled } from "../util/config/featuretoggle";
|
|
4
|
-
import { normalizePath } from "./common/tokenize-match";
|
|
5
|
-
import { Router, Handler, Method } from "./common";
|
|
6
|
-
import { RouterHandlerOptions } from "./common/router-utils";
|
|
7
|
-
|
|
8
|
-
export interface FeatureRouterPathOptions {
|
|
9
|
-
path: string | string[];
|
|
10
|
-
method: Method | Method[];
|
|
11
|
-
handler: Array<Handler> | Handler;
|
|
12
|
-
options?: RouterHandlerOptions;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type FeatureRouterOptions = SimpleMap<FeatureRouterPathOptions>;
|
|
16
|
-
|
|
17
|
-
const FEATURE_ROUTER_METHODS: Method[] = ["get", "post", "put", "delete", "patch", "options"];
|
|
18
|
-
|
|
19
|
-
export const FeatureRouter = (options: FeatureRouterOptions, logger?: Logger): Router => {
|
|
20
|
-
const router: Router = new Router();
|
|
21
|
-
const toSetup = Object.keys(options);
|
|
22
|
-
const enabled: string[] = [];
|
|
23
|
-
const disabled: string[] = [];
|
|
24
|
-
const enabledPaths: SimpleMap<string> = {};
|
|
25
|
-
for (const featureName of toSetup) {
|
|
26
|
-
const handlerOptions = options[featureName];
|
|
27
|
-
if (!handlerOptions) {
|
|
28
|
-
throw new Error(`no handler options for feature [${featureName}]`);
|
|
29
|
-
} else {
|
|
30
|
-
const { path, handler: implementation, method } = handlerOptions;
|
|
31
|
-
if (!method) {
|
|
32
|
-
throw new Error(`no methods for feature [${featureName}]`);
|
|
33
|
-
} else if (!implementation) {
|
|
34
|
-
throw new Error(`no implementation for feature [${featureName}]`);
|
|
35
|
-
} else if (!path) {
|
|
36
|
-
throw new Error(`no path for feature [${featureName}]`);
|
|
37
|
-
} else {
|
|
38
|
-
if (method instanceof Array) {
|
|
39
|
-
for (const m of method) {
|
|
40
|
-
if (FEATURE_ROUTER_METHODS.indexOf(m.toLowerCase() as Method) === -1) {
|
|
41
|
-
throw new Error(`feature [${featureName}] method [${m.toUpperCase()}] not defined! use only [${FEATURE_ROUTER_METHODS.join(",")}]`);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
} else if (FEATURE_ROUTER_METHODS.indexOf(method.toLowerCase() as Method) === -1) {
|
|
45
|
-
throw new Error(`feature [${featureName}] method [${method.toUpperCase()}] not defined! use only [${FEATURE_ROUTER_METHODS.join(",")}]`);
|
|
46
|
-
}
|
|
47
|
-
const methods = method instanceof Array ? method : [method];
|
|
48
|
-
if (isFeatureEnabled(featureName)) {
|
|
49
|
-
if (logger) {
|
|
50
|
-
logger.trace(`feature [${featureName}] enabled`);
|
|
51
|
-
}
|
|
52
|
-
enabled.push(featureName);
|
|
53
|
-
const paths = path instanceof Array ? path : [path];
|
|
54
|
-
for (const nnP of paths) {
|
|
55
|
-
const p = normalizePath(nnP);
|
|
56
|
-
for (const m of methods) {
|
|
57
|
-
if (logger) {
|
|
58
|
-
logger.info("%s", featureName);
|
|
59
|
-
logger.info("\t%s:%s", m.toUpperCase(), p);
|
|
60
|
-
if ((handlerOptions as any).___filePath) {
|
|
61
|
-
logger.info("\t%s", (handlerOptions as any).___filePath);
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (enabledPaths[`${p}${m}`] !== undefined) {
|
|
65
|
-
throw new Error(`${featureName} collides with feature ${enabledPaths[`${p}${m}`]} on path ${p} method ${m}`)
|
|
66
|
-
}
|
|
67
|
-
enabledPaths[`${p}${m}`] = featureName;
|
|
68
|
-
router.use(implementation, p, m.toLocaleLowerCase() as Method, handlerOptions.options);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
} else {
|
|
72
|
-
if (logger) {
|
|
73
|
-
logger.debug("feature [%s] disabled", featureName);
|
|
74
|
-
}
|
|
75
|
-
disabled.push(featureName);
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
if (logger && enabled.length > 0) {
|
|
81
|
-
logger.debug("enabled features [%s]", enabled.join(","));
|
|
82
|
-
}
|
|
83
|
-
if (logger) {
|
|
84
|
-
if (disabled.length > 0) {
|
|
85
|
-
logger.warn("disabled features [%s]", disabled.join(","));
|
|
86
|
-
logger.warn(`to enable them just add the env var <feature>=true`);
|
|
87
|
-
} else {
|
|
88
|
-
logger.debug(`no features disabled`);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return router;
|
|
92
|
-
};
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { OutgoingHttpHeaders } from "http";
|
|
2
|
-
import { Context, Handler } from "../common";
|
|
3
|
-
|
|
4
|
-
export type HTMLResponseResult = string | { status?: number; headers?: OutgoingHttpHeaders, body?: string; template: (ctx: Context) => Promise<string>; }
|
|
5
|
-
|
|
6
|
-
export const HTMLResponseHandler = (): Handler<void> =>
|
|
7
|
-
async (ctx: Context): Promise<void> => {
|
|
8
|
-
const results = ctx.results;
|
|
9
|
-
const lastResult = results[results.length - 1];
|
|
10
|
-
ctx.logger.trace(`last result is [${lastResult}]`);
|
|
11
|
-
if (lastResult && typeof lastResult === "string" ||
|
|
12
|
-
(
|
|
13
|
-
(typeof lastResult.headers === "object" || lastResult.headers === undefined) &&
|
|
14
|
-
(typeof lastResult.status === "number" || lastResult.status === undefined) &&
|
|
15
|
-
(typeof lastResult.body === "string" || typeof lastResult.template === "function")
|
|
16
|
-
)
|
|
17
|
-
) {
|
|
18
|
-
const status = typeof lastResult.status === "number" ? lastResult.status : 200;
|
|
19
|
-
const headers = typeof lastResult.headers === "object" ? lastResult.headers : {
|
|
20
|
-
"content-type": "text/html; charset=utf-8"
|
|
21
|
-
};
|
|
22
|
-
const toSend = lastResult.template ? await lastResult.template(ctx) : (typeof lastResult.body === "string" ? lastResult.body : lastResult);
|
|
23
|
-
if (typeof toSend === "string") {
|
|
24
|
-
ctx.logger.debug(`sending [${toSend}]`);
|
|
25
|
-
await ctx.end({
|
|
26
|
-
status,
|
|
27
|
-
headers,
|
|
28
|
-
body: toSend
|
|
29
|
-
});
|
|
30
|
-
} else {
|
|
31
|
-
throw new Error("html result from HTMLResponseResult not string so last result not valid");
|
|
32
|
-
}
|
|
33
|
-
} else {
|
|
34
|
-
throw new Error("html result from HTMLResponseResult not string so last result not valid");
|
|
35
|
-
}
|
|
36
|
-
}
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Context, Handler } from "../common";
|
|
2
|
-
|
|
3
|
-
export const ResponseHandler = (): Handler<void> =>
|
|
4
|
-
async (ctx: Context): Promise<void> => {
|
|
5
|
-
ctx.logger.trace("results [%o]", ctx.results);
|
|
6
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
7
|
-
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
8
|
-
} else {
|
|
9
|
-
const lastResult = ctx.results[ctx.results.length - 1];
|
|
10
|
-
ctx.logger.debug("response [%o]", lastResult);
|
|
11
|
-
if (lastResult !== undefined) {
|
|
12
|
-
await ctx.json(lastResult);
|
|
13
|
-
} else {
|
|
14
|
-
throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { parse, Parser } from "../../util";
|
|
2
|
-
import { ParseOptions } from "../../util/parser/common";
|
|
3
|
-
import { Context, Handler } from "../common";
|
|
4
|
-
|
|
5
|
-
export const ResultParser = (options: ParseOptions | ParseOptions[], parser?: Parser): Handler<any> =>
|
|
6
|
-
async (ctx: Context): Promise<any> => {
|
|
7
|
-
if (!ctx.results || ctx.results.length === 0) {
|
|
8
|
-
ctx.logger.debug(`not parsing results`);
|
|
9
|
-
return undefined;
|
|
10
|
-
} else {
|
|
11
|
-
const index = ctx.results.length - 1;
|
|
12
|
-
const lastResult = ctx.results[index];
|
|
13
|
-
ctx.logger.trace(`parsing lastResult[%o]`, lastResult);
|
|
14
|
-
try {
|
|
15
|
-
if (options instanceof Array) {
|
|
16
|
-
for (let i = 0; i < options.length; i++) {
|
|
17
|
-
const o = options[i];
|
|
18
|
-
try {
|
|
19
|
-
const mappedResults = (parser ? parser.parse : parse)(`ctx.results[${index}]`, lastResult, o.options, o.mode, o.ignoreUndefined);
|
|
20
|
-
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
21
|
-
return mappedResults;
|
|
22
|
-
} catch (e) {
|
|
23
|
-
if (i === options.length - 1) {
|
|
24
|
-
throw e;
|
|
25
|
-
} else {
|
|
26
|
-
continue;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
} else {
|
|
31
|
-
const mappedResults = (parser ? parser.parse : parse)(`ctx.results[${index}]`, lastResult, options.options, options.mode, options.ignoreUndefined);
|
|
32
|
-
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
33
|
-
return mappedResults;
|
|
34
|
-
}
|
|
35
|
-
} catch (e) {
|
|
36
|
-
ctx.logger.error(`error parsing lastResult[%o]`, lastResult);
|
|
37
|
-
throw e;
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
};
|
package/src/util/client.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
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
|
-
}
|