@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.
Files changed (125) hide show
  1. package/README.md +38 -96
  2. package/dist/handler/api-router-utils.d.ts +12 -1
  3. package/dist/handler/api-router-utils.js +86 -1
  4. package/dist/handler/api-router-utils.js.map +1 -1
  5. package/dist/handler/api-router.d.ts +29 -2
  6. package/dist/handler/api-router.js +2 -2
  7. package/dist/handler/api-router.js.map +1 -1
  8. package/dist/handler/app.d.ts +2 -1
  9. package/dist/handler/app.js +4 -2
  10. package/dist/handler/app.js.map +1 -1
  11. package/dist/handler/common/context.d.ts +1 -1
  12. package/dist/handler/common/context.js +14 -9
  13. package/dist/handler/common/context.js.map +1 -1
  14. package/dist/handler/common/router-utils.js +60 -5
  15. package/dist/handler/common/router-utils.js.map +1 -1
  16. package/dist/handler/index.d.ts +0 -3
  17. package/dist/handler/index.js +0 -3
  18. package/dist/handler/index.js.map +1 -1
  19. package/dist/handler/middleware/form.js +7 -1
  20. package/dist/handler/middleware/form.js.map +1 -1
  21. package/dist/handler/middleware/group.js +44 -2
  22. package/dist/handler/middleware/group.js.map +1 -1
  23. package/dist/handler/middleware/index.js +2 -6
  24. package/dist/handler/middleware/index.js.map +1 -1
  25. package/dist/handler/middleware/json.js +8 -2
  26. package/dist/handler/middleware/json.js.map +1 -1
  27. package/dist/handler/middleware/text.js +8 -1
  28. package/dist/handler/middleware/text.js.map +1 -1
  29. package/dist/handler/proxy.d.ts +2 -5
  30. package/dist/handler/proxy.js +2 -3
  31. package/dist/handler/proxy.js.map +1 -1
  32. package/dist/index.d.ts +1 -0
  33. package/dist/index.js +1 -0
  34. package/dist/index.js.map +1 -1
  35. package/dist/{util/test → test/common}/app.d.ts +2 -2
  36. package/dist/{util/test → test/common}/app.js +3 -2
  37. package/dist/test/common/app.js.map +1 -0
  38. package/dist/{util/test → test/common}/common.d.ts +0 -0
  39. package/dist/{util/test → test/common}/common.js +0 -0
  40. package/dist/test/common/common.js.map +1 -0
  41. package/dist/{util/test → test/common}/index.d.ts +1 -0
  42. package/dist/{util/test → test/common}/index.js +1 -0
  43. package/dist/test/common/index.js.map +1 -0
  44. package/dist/test/common/runner.d.ts +13 -0
  45. package/dist/test/common/runner.js +92 -0
  46. package/dist/test/common/runner.js.map +1 -0
  47. package/dist/test/index.d.ts +1 -0
  48. package/dist/{handler/response → test}/index.js +1 -3
  49. package/dist/test/index.js.map +1 -0
  50. package/dist/test/main.d.ts +1 -0
  51. package/dist/test/main.js +19 -0
  52. package/dist/test/main.js.map +1 -0
  53. package/dist/util/cli/index.d.ts +1 -1
  54. package/dist/util/error/parsererror.d.ts +2 -1
  55. package/dist/util/error/parsererror.js +5 -1
  56. package/dist/util/error/parsererror.js.map +1 -1
  57. package/dist/util/index.d.ts +0 -1
  58. package/dist/util/index.js +0 -1
  59. package/dist/util/index.js.map +1 -1
  60. package/dist/util/loader/index.d.ts +0 -3
  61. package/dist/util/loader/index.js +2 -9
  62. package/dist/util/loader/index.js.map +1 -1
  63. package/dist/util/request/index.d.ts +1 -1
  64. package/dist/util/request/index.js +1 -3
  65. package/dist/util/request/index.js.map +1 -1
  66. package/dist/util/session/common.d.ts +0 -1
  67. package/dist/util/session/common.js +0 -46
  68. package/dist/util/session/common.js.map +1 -1
  69. package/package.json +6 -11
  70. package/src/handler/api-router-utils.ts +91 -2
  71. package/src/handler/api-router.ts +30 -4
  72. package/src/handler/app.ts +2 -1
  73. package/src/handler/common/context.ts +14 -10
  74. package/src/handler/common/router-utils.ts +199 -144
  75. package/src/handler/index.ts +0 -3
  76. package/src/handler/middleware/form.ts +8 -2
  77. package/src/handler/middleware/group.ts +44 -2
  78. package/src/handler/middleware/index.ts +0 -4
  79. package/src/handler/middleware/json.ts +9 -3
  80. package/src/handler/middleware/text.ts +8 -1
  81. package/src/handler/proxy.ts +3 -8
  82. package/src/index.ts +1 -0
  83. package/src/{util/test → test/common}/app.ts +6 -5
  84. package/src/{util/test → test/common}/common.ts +0 -0
  85. package/src/{util/test → test/common}/index.ts +1 -0
  86. package/src/test/common/runner.ts +97 -0
  87. package/src/test/index.ts +1 -0
  88. package/src/test/main.ts +17 -0
  89. package/src/util/cli/index.ts +1 -1
  90. package/src/util/error/parsererror.ts +6 -1
  91. package/src/util/index.ts +0 -1
  92. package/src/util/loader/index.ts +1 -7
  93. package/src/util/request/index.ts +1 -1
  94. package/src/util/session/common.ts +0 -43
  95. package/tsconfig-test.json +13 -0
  96. package/dist/handler/api-handler.d.ts +0 -29
  97. package/dist/handler/api-handler.js +0 -42
  98. package/dist/handler/api-handler.js.map +0 -1
  99. package/dist/handler/feature-router.d.ts +0 -12
  100. package/dist/handler/feature-router.js +0 -90
  101. package/dist/handler/feature-router.js.map +0 -1
  102. package/dist/handler/response/html.d.ts +0 -10
  103. package/dist/handler/response/html.js +0 -34
  104. package/dist/handler/response/html.js.map +0 -1
  105. package/dist/handler/response/index.d.ts +0 -3
  106. package/dist/handler/response/index.js.map +0 -1
  107. package/dist/handler/response/json.d.ts +0 -2
  108. package/dist/handler/response/json.js +0 -21
  109. package/dist/handler/response/json.js.map +0 -1
  110. package/dist/handler/response/result.d.ts +0 -4
  111. package/dist/handler/response/result.js +0 -46
  112. package/dist/handler/response/result.js.map +0 -1
  113. package/dist/util/client.d.ts +0 -17
  114. package/dist/util/client.js +0 -55
  115. package/dist/util/client.js.map +0 -1
  116. package/dist/util/test/app.js.map +0 -1
  117. package/dist/util/test/common.js.map +0 -1
  118. package/dist/util/test/index.js.map +0 -1
  119. package/src/handler/api-handler.ts +0 -55
  120. package/src/handler/feature-router.ts +0 -92
  121. package/src/handler/response/html.ts +0 -36
  122. package/src/handler/response/index.ts +0 -3
  123. package/src/handler/response/json.ts +0 -19
  124. package/src/handler/response/result.ts +0 -40
  125. package/src/util/client.ts +0 -64
@@ -0,0 +1,97 @@
1
+ import { Console } from "console";
2
+ import { lstatSync, readdirSync } from "fs";
3
+ import { basename, resolve } from "path";
4
+ import { format } from "util";
5
+ import { Logger } from "../../util";
6
+
7
+ export type TestFunction = () => void | Promise<void>;
8
+ const DEFAULT_TIMEOUT = 2000;
9
+
10
+ interface Test { run: TestFunction; title: string; category?: string; fullName: string; }
11
+
12
+ const tests: Test[] = [];
13
+
14
+ export const getTestCount = () => tests.length;
15
+
16
+ export const it = (title: string, impl: TestFunction, options?: {
17
+ category?: string;
18
+ timeout?: number;
19
+ before?: TestFunction;
20
+ after?: TestFunction;
21
+ }, logger: Logger | Console = new Console(process.stdout)): void => {
22
+ const category = options && options.category ? options.category : undefined;
23
+ const fullName = `${category ? `${category} [` : ""}${title}${category ? "]" : ""}`;
24
+ tests.push({
25
+ run: () => new Promise((resolve, reject) => {
26
+ const timeoutMS = options && options.timeout ? options.timeout : DEFAULT_TIMEOUT;
27
+ const timeout = setTimeout(() => {
28
+ reject(new Error(format("\n\n%s failed\x1b[31m\n%s timeout %o\x1b", fullName, fullName, timeoutMS)));
29
+ }, timeoutMS);
30
+ try {
31
+ (async () => {
32
+ if (options && options.before) {
33
+ await options.before();
34
+ }
35
+ const startMS = Date.now();
36
+ await impl();
37
+ const took = Date.now() - startMS;
38
+ if (options && options.after) {
39
+ await options.after();
40
+ }
41
+ return took;
42
+ })().then((took) => {
43
+ clearTimeout(timeout);
44
+ logger.log("\x1b[32mpassed %s (took %sms)\x1b[0m\n", fullName, took);
45
+ resolve();
46
+ }).catch(e => {
47
+ clearTimeout(timeout);
48
+ reject(new Error(format("\n\n%s failed\x1b[31m\n%s finished with errors %o\x1b", fullName, fullName, e)));
49
+ });
50
+ } catch (e) {
51
+ clearTimeout(timeout);
52
+ reject(new Error(format("\n\n%s failed\x1b[31m\n%s finished with errors %o\x1b", fullName, fullName, e)));
53
+ }
54
+ }), title,
55
+ category,
56
+ fullName
57
+ });
58
+ };
59
+
60
+ export const runTests = async (title?: string | string[], logger: Logger | Console = new Console(process.stdout)) => {
61
+ const titles = title ? title instanceof Array ? title : [title] : undefined;
62
+ for (const test of tests) {
63
+ if (!titles || titles.indexOf(test.title) !== -1) {
64
+ try {
65
+ await test.run();
66
+ } catch (e) {
67
+ logger.error(e);
68
+ process.exit(1);
69
+ }
70
+ }
71
+ }
72
+ };
73
+
74
+ export const runTestModules = async (modules: string[], title?: string | string[]) => {
75
+ for (const path of modules) {
76
+ require(path);
77
+ }
78
+ await runTests(title);
79
+ }
80
+
81
+ export const runTestDir = async (dirs: string[], title?: string | string[], exts: string[] = ["test.js"]) => {
82
+ for (const dir of dirs) {
83
+ const files = readdirSync(resolve(process.cwd(), dir));
84
+ for (const f of files) {
85
+ const path = resolve(dir, f);
86
+ if (!lstatSync(path).isDirectory()) {
87
+ for (const ext of exts) {
88
+ const name = basename(f);
89
+ if (name.indexOf(ext) === name.length - ext.length) {
90
+ require(path);
91
+ }
92
+ }
93
+ }
94
+ }
95
+ }
96
+ await runTests(title);
97
+ }
@@ -0,0 +1 @@
1
+ export * from "./common";
@@ -0,0 +1,17 @@
1
+ import { resolve } from "path";
2
+ import { runTestModules, getTestCount } from "./common";
3
+
4
+ const startMS = Date.now();
5
+ const name = process.argv[2];
6
+ if (!name) {
7
+ throw new Error("bad arguments");
8
+ }
9
+ const modules = process.argv.slice(3);
10
+ runTestModules(modules.map(m => resolve(process.cwd(), m)), name.toLowerCase() !== "all" ? name : undefined).then(() => {
11
+ const took = Date.now() - startMS;
12
+ const count = getTestCount();
13
+ console.log("\n\n" + count + " tests pased took " + took + "ms\n\n");
14
+ }).catch(e => {
15
+ console.error(e);
16
+ process.exit(1);
17
+ });
@@ -1,6 +1,6 @@
1
1
  import { SimpleMap } from "../parser/common";
2
2
  import { Logger } from "../logger";
3
- import { Callback } from "../test";
3
+ import { Callback } from "../../test/common";
4
4
 
5
5
  // noinspection SpellCheckingInspection
6
6
  const routeCMDModule = async (cmdArg: string | undefined, cmds: CMDMapType, logger: Logger | Console, exit = true): Promise<void> => {
@@ -7,4 +7,9 @@ export class ParseOptionsError extends NamedError {
7
7
  }
8
8
  }
9
9
 
10
- export class BadRequestError extends ParseOptionsError { }
10
+ export class BadRequestError extends NamedError {
11
+ constructor(message = "BAD REQUEST") {
12
+ super(message);
13
+ this.name = "BadRequestError";
14
+ }
15
+ }
package/src/util/index.ts CHANGED
@@ -6,6 +6,5 @@ export * from "./config";
6
6
  export * from "./logger";
7
7
  export * from "./session";
8
8
  export * from "./loader";
9
- export * from "./test";
10
9
  //export * from "./uuid";
11
10
  //export * from "./cookie";
@@ -3,9 +3,8 @@ import { Logger } from "../logger";
3
3
  import { existsSync, readdirSync, readFileSync } from "fs";
4
4
  import { ConfigOutput, ConfigPathResolver, ConfigStore, LoadConfigOut } from "../config";
5
5
  import { ConfigFileNotFoundError } from "../error";
6
- import { RequestOptions, request } from "../request";
7
6
 
8
- export const loadConfigString = (envFileContent: string, combined?: ConfigOutput): ConfigOutput => {
7
+ const loadConfigString = (envFileContent: string, combined?: ConfigOutput): ConfigOutput => {
9
8
  const ret: ConfigOutput = {};
10
9
  envFileContent.split("\n")
11
10
  .filter(value => value && value.length > 0 && value.substr(0, 1) !== "#")
@@ -26,11 +25,6 @@ export const loadConfigString = (envFileContent: string, combined?: ConfigOutput
26
25
  return ret;
27
26
  }
28
27
 
29
- export const loadRemoteConfigFile = async (options: RequestOptions, logger?: Logger): Promise<ConfigOutput> => {
30
- const { data } = await request(options, logger);
31
- return loadConfigString(data);
32
- }
33
-
34
28
  export const loadConfigFile = (envFilePath: string, combined?: ConfigOutput, logger?: Logger): ConfigOutput => {
35
29
  if (!existsSync(envFilePath)) {
36
30
  throw new ConfigFileNotFoundError(`config file [${envFilePath}] doesnt exists!`);
@@ -5,7 +5,7 @@ import { parseRedirectLocation, RequestOptions, RequestResponse, ResponseError,
5
5
  import { gunzipSync } from "zlib";
6
6
  import { isBrowser } from "../config/browser";
7
7
 
8
- export { RequestOptions, RequestResponse, ResponseError } from "./common";
8
+ export { RequestOptions, RequestResponse } from "./common";
9
9
 
10
10
  const DEFAULT_USER_AGENT = "curl/7.69.1";
11
11
  const CONTENT_TYPE_HEADER = "Content-Type";
@@ -1,4 +1,3 @@
1
- import { ParseOptionsError } from "../error";
2
1
  import { SimpleMap, SimpleTypes } from "../parser/common";
3
2
 
4
3
  export interface NoTokenSession extends SimpleMap<SimpleTypes> {
@@ -19,45 +18,3 @@ export interface GroupPolicy {
19
18
  groups: GroupPolicyGroups[];
20
19
  groupPolicy: GroupPolicyType;
21
20
  }
22
-
23
- export const policyCheck = (session: Session, options: GroupPolicy): boolean => {
24
- switch (options.groupPolicy) {
25
- case "at_least_one":
26
- for (const group of options.groups) {
27
- if (group instanceof Array) {
28
- let ret = true;
29
- for (const g of group) {
30
- if (session.groups.indexOf(g) === -1) {
31
- ret = false;
32
- break;
33
- }
34
- }
35
- if (ret) {
36
- return true;
37
- }
38
- } else {
39
- if (session.groups.indexOf(group) !== -1) {
40
- return true;
41
- }
42
- }
43
- }
44
- return false;
45
- case "all":
46
- for (const group of options.groups) {
47
- if (group instanceof Array) {
48
- for (const g of group) {
49
- if (session.groups.indexOf(g) === -1) {
50
- return false;
51
- }
52
- }
53
- } else {
54
- if (session.groups.indexOf(group) === -1) {
55
- return false;
56
- }
57
- }
58
- }
59
- return true;
60
- default:
61
- throw new ParseOptionsError(`policy [${options.groupPolicy}] not implemented!!`);
62
- }
63
- };
@@ -0,0 +1,13 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "./test-dist/",
5
+ },
6
+ "include": [
7
+ "src/**/*",
8
+ "test/**/*"
9
+ ],
10
+ "exclude": [
11
+ "node_modules/"
12
+ ]
13
+ }
@@ -1,29 +0,0 @@
1
- import { GroupPolicy } from "../util/session";
2
- import { SessionHandlerOptions } from "./middleware/session";
3
- import { Parser } from "../util/parser";
4
- import { ParseOptions } from "../util/parser/common";
5
- import { Handler } from "./common";
6
- export interface APIHandlerOptions {
7
- parser?: Parser;
8
- headers?: ParseOptions | ParseOptions[];
9
- query?: ParseOptions | false | ParseOptions[];
10
- params?: ParseOptions | false | ParseOptions[];
11
- body?: ParseOptions | false | ParseOptions[];
12
- result?: ParseOptions | ParseOptions[];
13
- handler: Array<Handler> | Handler;
14
- middleware?: Handler[];
15
- session?: SessionHandlerOptions | Handler;
16
- policy?: GroupPolicy;
17
- }
18
- /**
19
- * APIHandler
20
- *
21
- * ```typescript
22
- *app.use(APIHandler({
23
- * query: { ... },
24
- * body: { ... },
25
- * result: { ... }
26
- *}));
27
- * ```
28
- */
29
- export declare const APIHandler: (options: APIHandlerOptions) => Array<Handler>;
@@ -1,42 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.APIHandler = void 0;
4
- const session_1 = require("./middleware/session");
5
- const group_1 = require("./middleware/group");
6
- const parse_1 = require("./middleware/parse");
7
- const json_1 = require("./response/json");
8
- const result_1 = require("./response/result");
9
- /**
10
- * APIHandler
11
- *
12
- * ```typescript
13
- *app.use(APIHandler({
14
- * query: { ... },
15
- * body: { ... },
16
- * result: { ... }
17
- *}));
18
- * ```
19
- */
20
- const APIHandler = (options) =>
21
- // setup options.middleware
22
- (options.middleware ? options.middleware : [])
23
- // setup session
24
- .concat(options.session ? typeof options.session === "function" ? [options.session] : [(0, session_1.SessionHandler)(options.session)] : [])
25
- // setup policy
26
- .concat(options.policy ? [(0, group_1.GroupPolicyHandler)(options.policy)] : [])
27
- // setup request parse
28
- .concat([(0, parse_1.ParseRequest)({
29
- headers: options.headers,
30
- query: options.query,
31
- params: options.params,
32
- body: options.body,
33
- parser: options.parser
34
- })])
35
- // setup options.handler
36
- .concat(options.handler instanceof Array ? options.handler : [options.handler])
37
- // setup response parse
38
- .concat(options.result ? (0, result_1.ResultParser)(options.result, options.parser) : [])
39
- // setup response handler
40
- .concat((0, json_1.ResponseHandler)());
41
- exports.APIHandler = APIHandler;
42
- //# sourceMappingURL=api-handler.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"api-handler.js","sourceRoot":"","sources":["../../src/handler/api-handler.ts"],"names":[],"mappings":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C,CAAC,EAAE,CAAC;IAC3E,yBAAyB;KACxB,MAAM,CAAC,IAAA,sBAAe,GAAE,CAAC,CAAC;AApBlB,QAAA,UAAU,cAoBQ"}
@@ -1,12 +0,0 @@
1
- import { SimpleMap } from "../util/parser/common";
2
- import { Logger } from "../util/logger";
3
- import { Router, Handler, Method } from "./common";
4
- import { RouterHandlerOptions } from "./common/router-utils";
5
- export interface FeatureRouterPathOptions {
6
- path: string | string[];
7
- method: Method | Method[];
8
- handler: Array<Handler> | Handler;
9
- options?: RouterHandlerOptions;
10
- }
11
- export declare type FeatureRouterOptions = SimpleMap<FeatureRouterPathOptions>;
12
- export declare const FeatureRouter: (options: FeatureRouterOptions, logger?: Logger | undefined) => Router;
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FeatureRouter = void 0;
4
- const featuretoggle_1 = require("../util/config/featuretoggle");
5
- const tokenize_match_1 = require("./common/tokenize-match");
6
- const common_1 = require("./common");
7
- const FEATURE_ROUTER_METHODS = ["get", "post", "put", "delete", "patch", "options"];
8
- const FeatureRouter = (options, logger) => {
9
- const router = new common_1.Router();
10
- const toSetup = Object.keys(options);
11
- const enabled = [];
12
- const disabled = [];
13
- const enabledPaths = {};
14
- for (const featureName of toSetup) {
15
- const handlerOptions = options[featureName];
16
- if (!handlerOptions) {
17
- throw new Error(`no handler options for feature [${featureName}]`);
18
- }
19
- else {
20
- const { path, handler: implementation, method } = handlerOptions;
21
- if (!method) {
22
- throw new Error(`no methods for feature [${featureName}]`);
23
- }
24
- else if (!implementation) {
25
- throw new Error(`no implementation for feature [${featureName}]`);
26
- }
27
- else if (!path) {
28
- throw new Error(`no path for feature [${featureName}]`);
29
- }
30
- else {
31
- if (method instanceof Array) {
32
- for (const m of method) {
33
- if (FEATURE_ROUTER_METHODS.indexOf(m.toLowerCase()) === -1) {
34
- throw new Error(`feature [${featureName}] method [${m.toUpperCase()}] not defined! use only [${FEATURE_ROUTER_METHODS.join(",")}]`);
35
- }
36
- }
37
- }
38
- else if (FEATURE_ROUTER_METHODS.indexOf(method.toLowerCase()) === -1) {
39
- throw new Error(`feature [${featureName}] method [${method.toUpperCase()}] not defined! use only [${FEATURE_ROUTER_METHODS.join(",")}]`);
40
- }
41
- const methods = method instanceof Array ? method : [method];
42
- if ((0, featuretoggle_1.isFeatureEnabled)(featureName)) {
43
- if (logger) {
44
- logger.trace(`feature [${featureName}] enabled`);
45
- }
46
- enabled.push(featureName);
47
- const paths = path instanceof Array ? path : [path];
48
- for (const nnP of paths) {
49
- const p = (0, tokenize_match_1.normalizePath)(nnP);
50
- for (const m of methods) {
51
- if (logger) {
52
- logger.info("%s", featureName);
53
- logger.info("\t%s:%s", m.toUpperCase(), p);
54
- if (handlerOptions.___filePath) {
55
- logger.info("\t%s", handlerOptions.___filePath);
56
- }
57
- }
58
- if (enabledPaths[`${p}${m}`] !== undefined) {
59
- throw new Error(`${featureName} collides with feature ${enabledPaths[`${p}${m}`]} on path ${p} method ${m}`);
60
- }
61
- enabledPaths[`${p}${m}`] = featureName;
62
- router.use(implementation, p, m.toLocaleLowerCase(), handlerOptions.options);
63
- }
64
- }
65
- }
66
- else {
67
- if (logger) {
68
- logger.debug("feature [%s] disabled", featureName);
69
- }
70
- disabled.push(featureName);
71
- }
72
- }
73
- }
74
- }
75
- if (logger && enabled.length > 0) {
76
- logger.debug("enabled features [%s]", enabled.join(","));
77
- }
78
- if (logger) {
79
- if (disabled.length > 0) {
80
- logger.warn("disabled features [%s]", disabled.join(","));
81
- logger.warn(`to enable them just add the env var <feature>=true`);
82
- }
83
- else {
84
- logger.debug(`no features disabled`);
85
- }
86
- }
87
- return router;
88
- };
89
- exports.FeatureRouter = FeatureRouter;
90
- //# sourceMappingURL=feature-router.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"feature-router.js","sourceRoot":"","sources":["../../src/handler/feature-router.ts"],"names":[],"mappings":";;;AAEA,gEAAgE;AAChE,4DAAwD;AACxD,qCAAmD;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C,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,KAAK,SAAS,EAAE;gCAC1C,MAAM,IAAI,KAAK,CAAC,GAAG,WAAW,0BAA0B,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,YAAY,CAAC,WAAW,CAAC,EAAE,CAAC,CAAA;6BAC7G;4BACD,YAAY,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC;4BACvC,MAAM,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC,EAAE,CAAC,CAAC,iBAAiB,EAAY,EAAE,cAAc,CAAC,OAAO,CAAC,CAAC;yBACxF;qBACF;iBACF;qBAAM;oBACL,IAAI,MAAM,EAAE;wBACV,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,WAAW,CAAC,CAAC;qBACpD;oBACD,QAAQ,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;iBAC5B;aACF;SACF;KACF;IACD,IAAI,MAAM,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;QAChC,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;KAC1D;IACD,IAAI,MAAM,EAAE;QACV,IAAI,QAAQ,CAAC,MAAM,GAAG,CAAC,EAAE;YACvB,MAAM,CAAC,IAAI,CAAC,wBAAwB,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;YAC1D,MAAM,CAAC,IAAI,CAAC,oDAAoD,CAAC,CAAC;SACnE;aAAM;YACL,MAAM,CAAC,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACtC;KACF;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAzEW,QAAA,aAAa,iBAyExB"}
@@ -1,10 +0,0 @@
1
- /// <reference types="node" />
2
- import { OutgoingHttpHeaders } from "http";
3
- import { Context, Handler } from "../common";
4
- export declare type HTMLResponseResult = string | {
5
- status?: number;
6
- headers?: OutgoingHttpHeaders;
7
- body?: string;
8
- template: (ctx: Context) => Promise<string>;
9
- };
10
- export declare const HTMLResponseHandler: () => Handler<void>;
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.HTMLResponseHandler = void 0;
4
- const HTMLResponseHandler = () => async (ctx) => {
5
- const results = ctx.results;
6
- const lastResult = results[results.length - 1];
7
- ctx.logger.trace(`last result is [${lastResult}]`);
8
- if (lastResult && typeof lastResult === "string" ||
9
- ((typeof lastResult.headers === "object" || lastResult.headers === undefined) &&
10
- (typeof lastResult.status === "number" || lastResult.status === undefined) &&
11
- (typeof lastResult.body === "string" || typeof lastResult.template === "function"))) {
12
- const status = typeof lastResult.status === "number" ? lastResult.status : 200;
13
- const headers = typeof lastResult.headers === "object" ? lastResult.headers : {
14
- "content-type": "text/html; charset=utf-8"
15
- };
16
- const toSend = lastResult.template ? await lastResult.template(ctx) : (typeof lastResult.body === "string" ? lastResult.body : lastResult);
17
- if (typeof toSend === "string") {
18
- ctx.logger.debug(`sending [${toSend}]`);
19
- await ctx.end({
20
- status,
21
- headers,
22
- body: toSend
23
- });
24
- }
25
- else {
26
- throw new Error("html result from HTMLResponseResult not string so last result not valid");
27
- }
28
- }
29
- else {
30
- throw new Error("html result from HTMLResponseResult not string so last result not valid");
31
- }
32
- };
33
- exports.HTMLResponseHandler = HTMLResponseHandler;
34
- //# sourceMappingURL=html.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"html.js","sourceRoot":"","sources":["../../../src/handler/response/html.ts"],"names":[],"mappings":";;;AAKO,MAAM,mBAAmB,GAAG,GAAkB,EAAE,CACrD,KAAK,EAAE,GAAY,EAAiB,EAAE;IACpC,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC;IAC5B,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAC/C,GAAG,CAAC,MAAM,CAAC,KAAK,C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AAC,yEAAyE,CAAC,CAAC;KAC5F;AACH,CAAC,CAAA;AA9BU,QAAA,mBAAmB,uBA8B7B"}
@@ -1,3 +0,0 @@
1
- export * from "./html";
2
- export * from "./json";
3
- export * from "./result";
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/handler/response/index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,yCAAuB;AACvB,yCAAuB;AACvB,2CAAyB"}
@@ -1,2 +0,0 @@
1
- import { Handler } from "../common";
2
- export declare const ResponseHandler: () => Handler<void>;
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ResponseHandler = void 0;
4
- const ResponseHandler = () => async (ctx) => {
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
- }
9
- else {
10
- const lastResult = ctx.results[ctx.results.length - 1];
11
- ctx.logger.debug("response [%o]", lastResult);
12
- if (lastResult !== undefined) {
13
- await ctx.json(lastResult);
14
- }
15
- else {
16
- throw new Error(`no response to send. Is your handler returning a value differente than boolean 'true|false' ?`);
17
- }
18
- }
19
- };
20
- exports.ResponseHandler = ResponseHandler;
21
- //# sourceMappingURL=json.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"json.js","sourceRoot":"","sources":["../../../src/handler/response/json.ts"],"names":[],"mappings":";;;AAEO,MAAM,eAAe,GAAG,GAAkB,EAAE,CACjD,KAAK,EAAE,GAAY,EAAiB,EAAE;IACpC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,cAAc,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IAC9C,IAAI,CAAC,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;QAC5C,MAAM,IAAI,KAAK,CAAC,+FAA+F,CAAC,CAAC;KAClH;SAAM;QACL,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;QACvD,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,EAAE,UAAU,CAAC,CAAC;QAC9C,IAAI,UAAU,KAAK,SAAS,EAAE;YAC5B,MAAM,GAAG,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;SAC5B;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,+FAA+F,CAAC,CAAC;SAClH;KACF;AACH,CAAC,CAAC;AAdS,QAAA,eAAe,mBAcxB"}
@@ -1,4 +0,0 @@
1
- import { Parser } from "../../util";
2
- import { ParseOptions } from "../../util/parser/common";
3
- import { Handler } from "../common";
4
- export declare const ResultParser: (options: ParseOptions | ParseOptions[], parser?: Parser | undefined) => Handler<any>;
@@ -1,46 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ResultParser = void 0;
4
- const util_1 = require("../../util");
5
- const ResultParser = (options, parser) => async (ctx) => {
6
- if (!ctx.results || ctx.results.length === 0) {
7
- ctx.logger.debug(`not parsing results`);
8
- return undefined;
9
- }
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 : util_1.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
- }
23
- catch (e) {
24
- if (i === options.length - 1) {
25
- throw e;
26
- }
27
- else {
28
- continue;
29
- }
30
- }
31
- }
32
- }
33
- else {
34
- const mappedResults = (parser ? parser.parse : util_1.parse)(`ctx.results[${index}]`, lastResult, options.options, options.mode, options.ignoreUndefined);
35
- ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
36
- return mappedResults;
37
- }
38
- }
39
- catch (e) {
40
- ctx.logger.error(`error parsing lastResult[%o]`, lastResult);
41
- throw e;
42
- }
43
- }
44
- };
45
- exports.ResultParser = ResultParser;
46
- //# sourceMappingURL=result.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"result.js","sourceRoot":"","sources":["../../../src/handler/response/result.ts"],"names":[],"mappings":";;;AAAA,qCAA2C;AAIpC,MAAM,YAAY,GAAG,CAAC,OAAsC,EAAE,MAAe,EAAgB,EAAE,CACpG,KAAK,EAAE,GAAY,EAAgB,EAAE;IACnC,IAAI,CAAC,GAAG,CAAC,OAAO,IAAI,GAAG,CAAC,OAAO,CAAC,MAAM,KAAK,CAAC,EAAE;QAC5C,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACxC,OAAO,SAAS,CAAC;KAClB;SAAM;QACL,MAAM,KAAK,GAAG,GAAG,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;QACrC,MAAM,UAAU,GAAG,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;QACtC,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,wBAAwB,EAAE,UAAU,CAAC,CAAC;QACvD,IAAI;YACF,IAAI,OAAO,YAAY,KAAK,EAAE;gBAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE;oBACvC,MAAM,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;oBACrB,IAAI;wBACF,MAAM,aAAa,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,YAAK,CAAC,CAAC,eAAe,KAAK,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,eAAe,CAAC,CAAC;wBACjI,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC;wBACvE,OAAO,aAAa,CAAC;qBACtB;oBAAC,OAAO,CAAC,EAAE;wBACV,IAAI,CAAC,KAAK,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE;4BAC5B,MAAM,CAAC,CAAC;yBACT;6BAAM;4BACL,SAAS;yBACV;qBACF;iBACF;aACF;iBAAM;gBACL,MAAM,aAAa,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,YAAK,CAAC,CAAC,eAAe,KAAK,GAAG,EAAE,UAAU,EAAE,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,eAAe,CAAC,CAAC;gBACnJ,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,8BAA8B,EAAE,KAAK,EAAE,aAAa,CAAC,CAAC;gBACvE,OAAO,aAAa,CAAC;aACtB;SACF;QAAC,OAAO,CAAC,EAAE;YACV,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,8BAA8B,EAAE,UAAU,CAAC,CAAC;YAC7D,MAAM,CAAC,CAAC;SACT;KACF;AACH,CAAC,CAAC;AAnCS,QAAA,YAAY,gBAmCrB"}
@@ -1,17 +0,0 @@
1
- import { RequestOptions, RequestResponse } from "./request";
2
- import { Logger } from "./logger";
3
- import { SimpleMap } from "./parser";
4
- export declare type RequestMapEndpoint = (args: Partial<RequestOptions>, logger?: Logger) => Promise<RequestResponse>;
5
- export declare type RequestMapEndpointMap = SimpleMap<RequestMapEndpoint>;
6
- export declare class RequestMap {
7
- private readonly logger?;
8
- endpoints: RequestMapEndpointMap;
9
- constructor(endpoints: {
10
- [path: string]: {
11
- [method: string]: {
12
- name?: string;
13
- description?: string;
14
- };
15
- };
16
- }, hostname: string, logger?: Logger | undefined, baseOptions?: Partial<RequestOptions>);
17
- }