@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
|
@@ -35,8 +35,18 @@ const ParseRequest = (options) => {
|
|
|
35
35
|
const query = normalizeParseOptions(options.query);
|
|
36
36
|
const params = normalizeParseOptions(options.params);
|
|
37
37
|
const body = normalizeParseOptions(options.body);
|
|
38
|
+
const headers = normalizeParseOptions(options.headers);
|
|
38
39
|
return async (ctx) => {
|
|
39
40
|
try {
|
|
41
|
+
try {
|
|
42
|
+
if (options.headers !== undefined) {
|
|
43
|
+
parseRequestPart("headers", ctx, headers, options.parser);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
catch (e) {
|
|
47
|
+
ctx.logger.error("error parsing headers %o", ctx.headers);
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
40
50
|
try {
|
|
41
51
|
if (options.query !== undefined) {
|
|
42
52
|
parseRequestPart("query", ctx, query, options.parser);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../../../src/handler/middleware/parse.ts"],"names":[],"mappings":";;;AAAA,8CAAkD;
|
|
1
|
+
{"version":3,"file":"parse.js","sourceRoot":"","sources":["../../../src/handler/middleware/parse.ts"],"names":[],"mappings":";;;AAAA,8CAAkD;AAYlD,MAAM,UAAU,GAAmB,CAAC;QAClC,OAAO,EAAE,EAAE;QACX,IAAI,EAAE,UAAU;KACjB,CAAC,CAAC;AAEH,MAAM,SAAS,GAAmB,CAAC;QACjC,OAAO,EAAE,EAAE;QACX,IAAI,EAAE,WAAW;KAClB,CAAC,CAAC;AAEH,MAAM,qBAAqB,GAAG,CAAC,MAA8C,EAAkB,EAAE,CAC/F,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,YAAY,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,KAAK,KAAK,CAAC,CAA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F;YAAC,OAAO,CAAM,EAAE;gBACf,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,uBAAuB,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;gBACpD,MAAM,CAAC,CAAC;aACT;SACF;QAAC,OAAO,CAAM,EAAE;YACf,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,2BAA2B,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;YACxD,MAAM,CAAC,CAAC;SACT;IACH,CAAC,CAAA;AACH,CAAC,CAAC;AA/CW,QAAA,YAAY,gBA+CvB"}
|
|
@@ -0,0 +1,17 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
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
|
|
@@ -0,0 +1 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/parser/get.js
CHANGED
|
@@ -4,15 +4,12 @@ exports.get = void 0;
|
|
|
4
4
|
const parser_1 = require("./parser");
|
|
5
5
|
const error_1 = require("../error");
|
|
6
6
|
/*
|
|
7
|
-
usage const name = get(obj, "user.info.name", {type: "string"});
|
|
7
|
+
usage const name = get(obj, "user.info.name", defaultValue, {type: "string"}, parser);
|
|
8
8
|
*/
|
|
9
9
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
10
10
|
const get = (obj, attrPath, defaultValue, option, parser) => {
|
|
11
|
-
if (typeof obj !== "object"
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
else if (!obj && defaultValue !== undefined) {
|
|
15
|
-
return defaultValue;
|
|
11
|
+
if (!obj || typeof obj !== "object") {
|
|
12
|
+
return defaultValue !== undefined ? defaultValue : undefined;
|
|
16
13
|
}
|
|
17
14
|
if (typeof attrPath !== "string") {
|
|
18
15
|
throw new Error(`attrPath must be typeof string`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"get.js","sourceRoot":"","sources":["../../../src/util/parser/get.ts"],"names":[],"mappings":";;;AAAA,qCAA8C;AAC9C,oCAA6C;AAG7C;;EAEE;AACF,uEAAuE;AAChE,MAAM,GAAG,GAAG,CAAC,GAAQ,EAAE,QAAgB,EAAE,YAAkB,EAAE,MAAyB,EAAE,MAAe,EAAmB,EAAE;IACjI,IAAI,
|
|
1
|
+
{"version":3,"file":"get.js","sourceRoot":"","sources":["../../../src/util/parser/get.ts"],"names":[],"mappings":";;;AAAA,qCAA8C;AAC9C,oCAA6C;AAG7C;;EAEE;AACF,uEAAuE;AAChE,MAAM,GAAG,GAAG,CAAC,GAAQ,EAAE,QAAgB,EAAE,YAAkB,EAAE,MAAyB,EAAE,MAAe,EAAmB,EAAE;IACjI,IAAI,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;QACnC,OAAO,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,C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
|
2
2
|
"name": "@miqro/core",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -18,7 +18,6 @@
|
|
|
18
18
|
"author": "claukers",
|
|
19
19
|
"license": "ISC",
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@types/mocha": "^9.0.0",
|
|
22
21
|
"@types/node": "^16.7.10",
|
|
23
22
|
"@typescript-eslint/eslint-plugin": "^5.3.0",
|
|
24
23
|
"@typescript-eslint/parser": "^5.3.0",
|
|
@@ -10,6 +10,7 @@ import { ResultParser } from "./response/result";
|
|
|
10
10
|
|
|
11
11
|
export interface APIHandlerOptions {
|
|
12
12
|
parser?: Parser;
|
|
13
|
+
headers?: ParseOptions | ParseOptions[];
|
|
13
14
|
query?: ParseOptions | false | ParseOptions[];
|
|
14
15
|
params?: ParseOptions | false | ParseOptions[];
|
|
15
16
|
body?: ParseOptions | false | ParseOptions[];
|
|
@@ -18,7 +19,6 @@ export interface APIHandlerOptions {
|
|
|
18
19
|
middleware?: Handler[];
|
|
19
20
|
session?: SessionHandlerOptions | Handler;
|
|
20
21
|
policy?: GroupPolicy;
|
|
21
|
-
responseHandler?: Handler;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
/**
|
|
@@ -41,6 +41,7 @@ export const APIHandler = (options: APIHandlerOptions): Array<Handler> =>
|
|
|
41
41
|
.concat(options.policy ? [GroupPolicyHandler(options.policy)] : [])
|
|
42
42
|
// setup request parse
|
|
43
43
|
.concat([ParseRequest({
|
|
44
|
+
headers: options.headers,
|
|
44
45
|
query: options.query,
|
|
45
46
|
params: options.params,
|
|
46
47
|
body: options.body,
|
|
@@ -51,4 +52,4 @@ export const APIHandler = (options: APIHandlerOptions): Array<Handler> =>
|
|
|
51
52
|
// setup response parse
|
|
52
53
|
.concat(options.result ? ResultParser(options.result, options.parser) : [])
|
|
53
54
|
// setup response handler
|
|
54
|
-
.concat(
|
|
55
|
+
.concat(ResponseHandler());
|
|
@@ -1,22 +1,16 @@
|
|
|
1
1
|
import { lstatSync, readdirSync } from "fs";
|
|
2
2
|
import { join, parse, resolve } from "path";
|
|
3
3
|
import { Logger } from "../util/logger";
|
|
4
|
-
import { APIHandler } from "./api-handler";
|
|
5
4
|
import { APIRoute } from "./api-router";
|
|
6
5
|
import { Method } from "./common";
|
|
7
6
|
import { normalizePath } from "./common/tokenize-match";
|
|
8
7
|
import { FeatureRouterOptions } from "./feature-router";
|
|
9
8
|
|
|
10
|
-
interface TraversedAPIRoute extends APIRoute {
|
|
11
|
-
method: Method | Method[];
|
|
12
|
-
path: string | string[];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
9
|
export const traverseAPIRouteDir = (
|
|
16
10
|
featureName: string,
|
|
17
11
|
dirname: string,
|
|
18
12
|
basePath = "/",
|
|
19
|
-
features: FeatureRouterOptions
|
|
13
|
+
features: FeatureRouterOptions = {}, logger?: Logger): FeatureRouterOptions => {
|
|
20
14
|
if (logger) {
|
|
21
15
|
logger.trace("loading routes from [%s]", dirname);
|
|
22
16
|
}
|
|
@@ -77,21 +71,24 @@ export const traverseAPIRouteDir = (
|
|
|
77
71
|
throw new Error(`${___filePath} feature name collides with ${(features[newFeatureSubPath] as any).___filePath}`)
|
|
78
72
|
}
|
|
79
73
|
features[newFeatureSubPath] = {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
74
|
+
options: {
|
|
75
|
+
name: route.name,
|
|
76
|
+
description: route.description,
|
|
77
|
+
parser: route.parser,
|
|
78
|
+
middleware: route.middleware,
|
|
84
79
|
session: route.session,
|
|
85
80
|
policy: route.policy,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
81
|
+
request: {
|
|
82
|
+
headers: route.headers,
|
|
83
|
+
query: route.query,
|
|
84
|
+
body: route.body,
|
|
85
|
+
params: route.params,
|
|
86
|
+
},
|
|
87
|
+
response: route.result ? route.result : true
|
|
88
|
+
},
|
|
89
|
+
path,
|
|
90
|
+
method: routeMethods,
|
|
91
|
+
handler: route.handler
|
|
95
92
|
};
|
|
96
93
|
(features[newFeatureSubPath] as any).___filePath = ___filePath;
|
|
97
94
|
}
|
package/src/handler/app.ts
CHANGED
|
@@ -1,26 +1,20 @@
|
|
|
1
|
-
import { createServer,
|
|
2
|
-
import {
|
|
3
|
-
import { Router, Context, ErrorHandler } from "./common";
|
|
4
|
-
|
|
5
|
-
export interface AppOptions {
|
|
6
|
-
newRequestLogger?: (req: IncomingMessage) => Logger;
|
|
7
|
-
newContext?: (req: IncomingMessage, res: ServerResponse) => Context;
|
|
8
|
-
}
|
|
1
|
+
import { createServer, RequestListener, Server } from "http";
|
|
2
|
+
import { Router, Context, ErrorHandler, RouterOptions } from "./common";
|
|
9
3
|
|
|
10
4
|
export class App extends Router {
|
|
11
5
|
public readonly listener: RequestListener;
|
|
12
6
|
|
|
13
|
-
constructor(
|
|
14
|
-
super();
|
|
7
|
+
constructor(config?: RouterOptions) {
|
|
8
|
+
super(config);
|
|
15
9
|
const errorHandler = DefaultErrorHandler();
|
|
16
10
|
this.listener = async (req, res) => {
|
|
17
|
-
const ctx = this.
|
|
18
|
-
this.
|
|
11
|
+
const ctx = this.config && this.config.newContext ?
|
|
12
|
+
this.config.newContext(req, res) :
|
|
19
13
|
new Context(
|
|
20
14
|
req,
|
|
21
15
|
res,
|
|
22
|
-
this.
|
|
23
|
-
this.
|
|
16
|
+
this.config && this.config.newRequestLogger ?
|
|
17
|
+
this.config.newRequestLogger(req) :
|
|
24
18
|
undefined);
|
|
25
19
|
ctx.logger.trace(`request received`);
|
|
26
20
|
try {
|
|
@@ -34,9 +28,7 @@ export class App extends Router {
|
|
|
34
28
|
}
|
|
35
29
|
} catch (e: any) {
|
|
36
30
|
try {
|
|
37
|
-
|
|
38
|
-
} catch (e2) {
|
|
39
|
-
try {
|
|
31
|
+
if (!ctx.res.headersSent) {
|
|
40
32
|
await errorHandler(e, ctx);
|
|
41
33
|
if (!ctx.res.headersSent) {
|
|
42
34
|
ctx.logger.error(e);
|
|
@@ -46,9 +38,9 @@ export class App extends Router {
|
|
|
46
38
|
body: "SERVER ERROR"
|
|
47
39
|
});
|
|
48
40
|
}
|
|
49
|
-
} catch (e3) {
|
|
50
|
-
ctx.logger.error(e3);
|
|
51
41
|
}
|
|
42
|
+
} catch (e3) {
|
|
43
|
+
ctx.logger.error(e3);
|
|
52
44
|
}
|
|
53
45
|
}
|
|
54
46
|
};
|
|
@@ -26,7 +26,7 @@ export class Context {
|
|
|
26
26
|
public readonly path: string; // normalized path
|
|
27
27
|
public readonly hash: string; // from this.url.hash
|
|
28
28
|
public readonly method: string; // from this.url.method
|
|
29
|
-
public
|
|
29
|
+
public headers: IncomingHttpHeaders; // from this.req.headers
|
|
30
30
|
public readonly cookies: { [name: string]: string }; // a middleware will fill this
|
|
31
31
|
public query: { [name: string]: string | string[] }; // parseSearchParams will fill this with this.url.searchParams
|
|
32
32
|
public params: { [name: string]: string | undefined } = {}; // the router will fill this
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Router } from "./router";
|
|
2
|
+
import { Context, Handler } from "./context";
|
|
3
|
+
import { Method } from "./context";
|
|
4
|
+
import { matchTokenizePath, PathPartToken, tokenizePath } from "./tokenize-match"
|
|
5
|
+
import { ParseRequest } from "../middleware/parse";
|
|
6
|
+
import { Parser, ParseOptions } from "../../util/parser";
|
|
7
|
+
import { SessionHandler, SessionHandlerOptions } from "../middleware/session";
|
|
8
|
+
import { GroupPolicyHandler } from "../middleware/group";
|
|
9
|
+
import { ResultParser } from "../response/result";
|
|
10
|
+
import { ResponseHandler } from "../response/json";
|
|
11
|
+
import { GroupPolicy } from "../../util/session";
|
|
12
|
+
|
|
13
|
+
export interface HandlerWithOptions extends RouterHandlerOptions {
|
|
14
|
+
handler: Handler | Array<Handler>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface RouteOptions {
|
|
18
|
+
name?: string;
|
|
19
|
+
description?: string;
|
|
20
|
+
policy?: GroupPolicy;
|
|
21
|
+
request?: {
|
|
22
|
+
headers?: ParseOptions | ParseOptions[];
|
|
23
|
+
query?: ParseOptions | false | ParseOptions[];
|
|
24
|
+
params?: ParseOptions | false | ParseOptions[];
|
|
25
|
+
body?: ParseOptions | false | ParseOptions[];
|
|
26
|
+
};
|
|
27
|
+
response?: true | ParseOptions | ParseOptions[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface RouteJSONDoc extends RouteOptions {
|
|
31
|
+
identifier: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface RouterJSONDoc {
|
|
35
|
+
[path: string]: {
|
|
36
|
+
[method: string]: RouteJSONDoc
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface RouterHandlerOptions extends RouteOptions {
|
|
41
|
+
parser?: Parser;
|
|
42
|
+
middleware?: Handler | Handler[];
|
|
43
|
+
session?: SessionHandlerOptions | Handler;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export class RouterHandler {
|
|
47
|
+
|
|
48
|
+
private readonly tokens: PathPartToken[];
|
|
49
|
+
private readonly handler: Array<Handler | Handler | Router>;
|
|
50
|
+
private readonly isRouter: boolean;
|
|
51
|
+
private readonly options?: RouterHandlerOptions;
|
|
52
|
+
|
|
53
|
+
public constructor(handler: Array<Handler> | HandlerWithOptions | Handler | Router, private readonly path: string | undefined, private readonly method: Method | undefined, options?: RouterHandlerOptions) {
|
|
54
|
+
this.tokens = tokenizePath(path);
|
|
55
|
+
|
|
56
|
+
let handlers: Array<Handler | Router> = []
|
|
57
|
+
this.isRouter = handler instanceof Router;
|
|
58
|
+
|
|
59
|
+
if (!this.isRouter && !(handler instanceof Array) && typeof handler === "object") {
|
|
60
|
+
// HandlerWithOptions
|
|
61
|
+
if (options) {
|
|
62
|
+
throw new Error("bad arguments. cannot send options with HandlerWithOptions");
|
|
63
|
+
}
|
|
64
|
+
const realHandler = (handler as HandlerWithOptions).handler;
|
|
65
|
+
const newOptions = {
|
|
66
|
+
...(handler as HandlerWithOptions)
|
|
67
|
+
};
|
|
68
|
+
delete (newOptions as any).handler;
|
|
69
|
+
handler = realHandler;
|
|
70
|
+
this.options = newOptions;
|
|
71
|
+
} else {
|
|
72
|
+
this.options = options;
|
|
73
|
+
}
|
|
74
|
+
const routeHandlers = handler instanceof Array ? handler : [handler] as Array<Handler | Router>;
|
|
75
|
+
|
|
76
|
+
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
77
|
+
throw new Error("bad arguments");
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
if (this.options && this.options.middleware) {
|
|
82
|
+
handlers = handlers.concat(this.options.middleware);
|
|
83
|
+
}
|
|
84
|
+
if (this.options && this.options.session) {
|
|
85
|
+
handlers.push(typeof this.options.session !== "function" ? SessionHandler(this.options.session) : this.options.session);
|
|
86
|
+
}
|
|
87
|
+
if (this.options && this.options.policy) {
|
|
88
|
+
handlers.push(GroupPolicyHandler(this.options.policy));
|
|
89
|
+
}
|
|
90
|
+
if (this.options && this.options.request) {
|
|
91
|
+
handlers.push(ParseRequest(this.options.request));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
handlers = handlers.concat(routeHandlers);
|
|
95
|
+
|
|
96
|
+
if (this.options && this.options.response) {
|
|
97
|
+
if (typeof this.options.response === "boolean") {
|
|
98
|
+
(handlers as Handler[]).push(ResponseHandler());
|
|
99
|
+
} else {
|
|
100
|
+
(handlers as Handler[]).push(ResultParser(this.options.response, this.options.parser));
|
|
101
|
+
(handlers as Handler[]).push(ResponseHandler());
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
this.handler = handlers;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
108
|
+
const nPath = this.path && this.path.length > 1 ? this.path.substring(1) : "";
|
|
109
|
+
const nMethod = this.method ? this.method : "";
|
|
110
|
+
|
|
111
|
+
if (this.isRouter) {
|
|
112
|
+
const router: Router = this.handler[0] as Router;
|
|
113
|
+
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
114
|
+
} else if (this.options) {
|
|
115
|
+
if (!doc[`${prePath}${nPath}`]) {
|
|
116
|
+
doc[`${prePath}${nPath}`] = {};
|
|
117
|
+
}
|
|
118
|
+
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
119
|
+
throw new Error("cannot create doc routes override each other");
|
|
120
|
+
}
|
|
121
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
122
|
+
description: this.options.description,
|
|
123
|
+
name: this.options.name,
|
|
124
|
+
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(),
|
|
125
|
+
policy: this.options.policy,
|
|
126
|
+
request: this.options.request ? {
|
|
127
|
+
body: this.options.request.body,
|
|
128
|
+
headers: this.options.request.headers,
|
|
129
|
+
params: this.options.request.params,
|
|
130
|
+
query: this.options.request.query,
|
|
131
|
+
} : undefined,
|
|
132
|
+
response: this.options.response
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
return doc;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
public async invoke(ctx: Context, pathTokens: { value: string; lower: string; }[], prePathTokens: PathPartToken[]): Promise<boolean> {
|
|
139
|
+
for (let i = 0; i < this.handler.length; i++) {
|
|
140
|
+
const h = this.handler[i];
|
|
141
|
+
if (h instanceof Router) {
|
|
142
|
+
if (!this.pushResults(await h.run(ctx, pathTokens, prePathTokens.concat(this.tokens)), ctx)) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
} else {
|
|
146
|
+
if (!this.pushResults(await h(ctx), ctx)) {
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
public isMatch(
|
|
155
|
+
method: string,
|
|
156
|
+
pathTokens: { value: string; lower: string; }[],
|
|
157
|
+
prePathTokens: PathPartToken[]): {
|
|
158
|
+
match: boolean;
|
|
159
|
+
params?: { [name: string]: string | undefined }
|
|
160
|
+
} {
|
|
161
|
+
if ((this.method === undefined || this.method === method)) {
|
|
162
|
+
if (this.path === undefined) {
|
|
163
|
+
return {
|
|
164
|
+
match: true
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
return matchTokenizePath(this.isRouter, prePathTokens.concat(this.tokens), pathTokens);
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
match: false
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private pushResults(result: boolean, ctx: Context): boolean {
|
|
175
|
+
if (result === false) {
|
|
176
|
+
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
177
|
+
return false;
|
|
178
|
+
} else if (result !== true && result !== undefined) {
|
|
179
|
+
ctx.logger.trace("pushing to results [%o]", result);
|
|
180
|
+
ctx.results.push(result);
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
}
|