@miqro/core 1.5.13 → 1.5.16
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/dist/api-router.d.ts +6 -5
- package/dist/api-router.js +9 -3
- package/dist/api-router.js.map +1 -1
- package/dist/app-utils.d.ts +13 -0
- package/dist/app-utils.js +82 -0
- package/dist/app-utils.js.map +1 -0
- package/dist/app.js +6 -88
- package/dist/app.js.map +1 -1
- package/dist/common/api-router-utils.d.ts +1 -1
- package/dist/common/api-router-utils.js +4 -3
- package/dist/common/api-router-utils.js.map +1 -1
- package/dist/common/context.js +4 -1
- package/dist/common/context.js.map +1 -1
- package/dist/common/router-handler.d.ts +20 -0
- package/dist/common/router-handler.js +136 -0
- package/dist/common/router-handler.js.map +1 -0
- package/dist/common/router-utils.d.ts +7 -5
- package/dist/common/router-utils.js +37 -47
- package/dist/common/router-utils.js.map +1 -1
- package/dist/common/session/common.d.ts +5 -2
- package/dist/common/tokenize-match.d.ts +13 -20
- package/dist/common/tokenize-match.js +25 -57
- package/dist/common/tokenize-match.js.map +1 -1
- package/dist/middleware/group.js +2 -0
- package/dist/middleware/group.js.map +1 -1
- package/dist/middleware/index.d.ts +1 -0
- package/dist/middleware/index.js +1 -0
- package/dist/middleware/index.js.map +1 -1
- package/dist/middleware/result.d.ts +3 -0
- package/dist/middleware/result.js +51 -0
- package/dist/middleware/result.js.map +1 -0
- package/dist/router.d.ts +3 -30
- package/dist/router.js +5 -167
- package/dist/router.js.map +1 -1
- package/package.json +8 -6
- package/src/api-router.ts +16 -7
- package/src/api-router.unit.test.js +273 -0
- package/src/app-utils.ts +82 -0
- package/src/app-utils.unit.test.js +100 -0
- package/src/app.ts +4 -89
- package/src/app.unit.test.js +334 -0
- package/src/common/api-router-utils.ts +3 -3
- package/src/common/context.ts +4 -1
- package/src/common/context.unit.test.js +286 -0
- package/src/common/router-handler.ts +159 -0
- package/src/common/router-utils.ts +46 -44
- package/src/common/session/common.ts +5 -2
- package/src/common/tokenize-match.ts +30 -52
- package/src/middleware/group.ts +2 -0
- package/src/middleware/index.ts +1 -0
- package/src/middleware/result.ts +43 -0
- package/src/proxy.unit.test.js +157 -0
- package/src/router.ts +7 -196
- package/src/router.unit.test.js +397 -0
- package/src/setup-test.js +5 -0
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
const {fake, requireMock} = require("@miqro/test");
|
|
2
|
+
const {resolve} = require("path");
|
|
3
|
+
const {format} = require("util");
|
|
4
|
+
const {strictEqual} = require("assert");
|
|
5
|
+
const {distPath} = require("../setup-test.js");
|
|
6
|
+
const {serialize} = require("cookie");
|
|
7
|
+
|
|
8
|
+
const testFilePath = resolve(distPath, "common", "context.js");
|
|
9
|
+
|
|
10
|
+
describe("context unit tests", () => {
|
|
11
|
+
|
|
12
|
+
const res = {
|
|
13
|
+
headersSent: false,
|
|
14
|
+
end: fake((body, cb) => {
|
|
15
|
+
res.headersSent = true
|
|
16
|
+
cb();
|
|
17
|
+
}),
|
|
18
|
+
setHeader: fake(() => {
|
|
19
|
+
|
|
20
|
+
}),
|
|
21
|
+
removeHeader: fake(() => {
|
|
22
|
+
|
|
23
|
+
})
|
|
24
|
+
};
|
|
25
|
+
const logger = "logger1";
|
|
26
|
+
const newURLRet = {
|
|
27
|
+
pathname: "pathname",
|
|
28
|
+
searchParams: "searchParams"
|
|
29
|
+
};
|
|
30
|
+
const parseSearchParamsRet = "parseSearchParamsRet";
|
|
31
|
+
const normalizePathRet = "normalizePathRet";
|
|
32
|
+
const newURL = fake(() => {
|
|
33
|
+
return newURLRet;
|
|
34
|
+
});
|
|
35
|
+
const normalizePath = fake(() => {
|
|
36
|
+
return normalizePathRet;
|
|
37
|
+
});
|
|
38
|
+
const parseSearchParams = fake(() => {
|
|
39
|
+
return parseSearchParamsRet;
|
|
40
|
+
});
|
|
41
|
+
const cookieSerializeRet = "cookieSerializeRet";
|
|
42
|
+
const cookieSerialize = fake(() => {
|
|
43
|
+
return cookieSerializeRet;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
let Context;
|
|
47
|
+
|
|
48
|
+
before(() => {
|
|
49
|
+
res.headersSent = false;
|
|
50
|
+
cookieSerialize.reset();
|
|
51
|
+
res.end.reset();
|
|
52
|
+
res.setHeader.reset();
|
|
53
|
+
res.removeHeader.reset();
|
|
54
|
+
newURL.reset();
|
|
55
|
+
normalizePath.reset();
|
|
56
|
+
parseSearchParams.reset();
|
|
57
|
+
const lib = requireMock(testFilePath, {
|
|
58
|
+
"./logger": {},
|
|
59
|
+
"./session": {},
|
|
60
|
+
"http": {},
|
|
61
|
+
"./tokenize-match": {
|
|
62
|
+
normalizePath,
|
|
63
|
+
parseSearchParams
|
|
64
|
+
},
|
|
65
|
+
"url": {},
|
|
66
|
+
"./helpers": {
|
|
67
|
+
newURL
|
|
68
|
+
},
|
|
69
|
+
"cookie": {
|
|
70
|
+
serialize: cookieSerialize
|
|
71
|
+
},
|
|
72
|
+
}, distPath);
|
|
73
|
+
Context = lib.Context;
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const uuid = "uuid1";
|
|
77
|
+
[{
|
|
78
|
+
req: {
|
|
79
|
+
url: "/url",
|
|
80
|
+
socket: {
|
|
81
|
+
remoteAddress: "remoteAddress"
|
|
82
|
+
},
|
|
83
|
+
headers: {
|
|
84
|
+
host: "fakeHost"
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}, {
|
|
88
|
+
req: {
|
|
89
|
+
url: "/url",
|
|
90
|
+
method: "MeThoD",
|
|
91
|
+
socket: {
|
|
92
|
+
remoteAddress: "remoteAddress"
|
|
93
|
+
},
|
|
94
|
+
headers: {
|
|
95
|
+
host: "fakeHost"
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}].forEach(({req}) => {
|
|
99
|
+
|
|
100
|
+
it(format("ctor req %o", req), async () => {
|
|
101
|
+
const context = new Context(uuid, req, res, logger);
|
|
102
|
+
strictEqual(context.uuid, uuid);
|
|
103
|
+
strictEqual(context.body, undefined);
|
|
104
|
+
strictEqual(res.removeHeader.callCount, 1);
|
|
105
|
+
strictEqual(res.removeHeader.callArgs[0][0], "Date");
|
|
106
|
+
strictEqual(context.req, req);
|
|
107
|
+
strictEqual(context.res, res);
|
|
108
|
+
strictEqual(context.query, parseSearchParamsRet);
|
|
109
|
+
strictEqual(cookieSerialize.callCount, 0);
|
|
110
|
+
strictEqual(parseSearchParams.callCount, 1);
|
|
111
|
+
strictEqual(parseSearchParams.callArgs[0][0], newURLRet.searchParams);
|
|
112
|
+
strictEqual(normalizePath.callCount, 1);
|
|
113
|
+
strictEqual(normalizePath.callArgs[0][0], newURLRet.pathname);
|
|
114
|
+
strictEqual(context.path, normalizePathRet);
|
|
115
|
+
strictEqual(context.method, req.method ? req.method.toUpperCase() : "GET");
|
|
116
|
+
strictEqual(context.logger, logger);
|
|
117
|
+
strictEqual(context.url, newURLRet);
|
|
118
|
+
strictEqual(newURL.callCount, 1);
|
|
119
|
+
strictEqual(newURL.callArgs[0][0], req.url);
|
|
120
|
+
strictEqual(newURL.callArgs[0][1], `http://${req.headers.host}`);
|
|
121
|
+
strictEqual(res.end.callCount, 0);
|
|
122
|
+
strictEqual(res.setHeader.callCount, 0);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
it("this.setHeader alias for this.res.setHeader", () => {
|
|
128
|
+
const context = new Context(uuid, {
|
|
129
|
+
url: "/url",
|
|
130
|
+
method: "MeThoD",
|
|
131
|
+
socket: {
|
|
132
|
+
remoteAddress: "remoteAddress"
|
|
133
|
+
},
|
|
134
|
+
headers: {
|
|
135
|
+
host: "fakeHost"
|
|
136
|
+
}
|
|
137
|
+
}, res, logger);
|
|
138
|
+
context.setHeader("key", "value");
|
|
139
|
+
strictEqual(cookieSerialize.callCount, 0);
|
|
140
|
+
strictEqual(res.end.callCount, 0);
|
|
141
|
+
strictEqual(res.setHeader.callCount, 1);
|
|
142
|
+
strictEqual(res.setHeader.callArgs[0][0], "key");
|
|
143
|
+
strictEqual(res.setHeader.callArgs[0][1], "value");
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("this.close calls this.end", async () => {
|
|
147
|
+
const context = new Context(uuid, {
|
|
148
|
+
url: "/url",
|
|
149
|
+
method: "MeThoD",
|
|
150
|
+
socket: {
|
|
151
|
+
remoteAddress: "remoteAddress"
|
|
152
|
+
},
|
|
153
|
+
headers: {
|
|
154
|
+
host: "fakeHost"
|
|
155
|
+
}
|
|
156
|
+
}, res, logger);
|
|
157
|
+
context.end = fake(async () => {
|
|
158
|
+
|
|
159
|
+
});
|
|
160
|
+
await context.close();
|
|
161
|
+
strictEqual(cookieSerialize.callCount, 0);
|
|
162
|
+
strictEqual(res.end.callCount, 0);
|
|
163
|
+
strictEqual(res.setHeader.callCount, 0);
|
|
164
|
+
strictEqual(context.end.callCount, 1);
|
|
165
|
+
strictEqual(context.end.callArgs[0][0].status, 400);
|
|
166
|
+
strictEqual(context.end.callArgs[0][0].headers.connection, "close");
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("this.addVaryHeader", () => {
|
|
170
|
+
// TODO
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it("this.setCookie alias for this.res.setHeader with cookie.serializer", async () => {
|
|
174
|
+
const context = new Context(uuid, {
|
|
175
|
+
url: "/url",
|
|
176
|
+
method: "MeThoD",
|
|
177
|
+
socket: {
|
|
178
|
+
remoteAddress: "remoteAddress"
|
|
179
|
+
},
|
|
180
|
+
headers: {
|
|
181
|
+
host: "fakeHost"
|
|
182
|
+
}
|
|
183
|
+
}, res, logger);
|
|
184
|
+
await context.setCookie("name", "value");
|
|
185
|
+
strictEqual(cookieSerialize.callCount, 1);
|
|
186
|
+
strictEqual(cookieSerialize.callArgs[0][0], "name");
|
|
187
|
+
strictEqual(cookieSerialize.callArgs[0][1], "value");
|
|
188
|
+
strictEqual(res.end.callCount, 0);
|
|
189
|
+
strictEqual(res.setHeader.callCount, 1);
|
|
190
|
+
strictEqual(res.setHeader.callArgs[0][0], "Set-Cookie");
|
|
191
|
+
strictEqual(res.setHeader.callArgs[0][1], cookieSerializeRet);
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
it("this.end with headers not sent", async () => {
|
|
195
|
+
const context = new Context(uuid, {
|
|
196
|
+
url: "/url",
|
|
197
|
+
method: "MeThoD",
|
|
198
|
+
socket: {
|
|
199
|
+
remoteAddress: "remoteAddress"
|
|
200
|
+
},
|
|
201
|
+
headers: {
|
|
202
|
+
host: "fakeHost"
|
|
203
|
+
}
|
|
204
|
+
}, res, logger);
|
|
205
|
+
res.headersSent = true;
|
|
206
|
+
try {
|
|
207
|
+
await context.end("end1");
|
|
208
|
+
strictEqual(false, true);
|
|
209
|
+
} catch (e) {
|
|
210
|
+
strictEqual(e.message, "already ended");
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
it("this.end with headers sent", async () => {
|
|
215
|
+
const context = new Context(uuid, {
|
|
216
|
+
url: "/url",
|
|
217
|
+
method: "MeThoD",
|
|
218
|
+
socket: {
|
|
219
|
+
remoteAddress: "remoteAddress"
|
|
220
|
+
},
|
|
221
|
+
headers: {
|
|
222
|
+
host: "fakeHost"
|
|
223
|
+
}
|
|
224
|
+
}, res, logger
|
|
225
|
+
);
|
|
226
|
+
const headers = {
|
|
227
|
+
key1: "value1",
|
|
228
|
+
key2: "value2"
|
|
229
|
+
};
|
|
230
|
+
const body = "body";
|
|
231
|
+
const status = "status";
|
|
232
|
+
await context.end({
|
|
233
|
+
headers,
|
|
234
|
+
body,
|
|
235
|
+
status
|
|
236
|
+
});
|
|
237
|
+
strictEqual(res.end.callCount, 1);
|
|
238
|
+
strictEqual(res.end.callArgs[0][0], body);
|
|
239
|
+
strictEqual(res.setHeader.callCount, 2);
|
|
240
|
+
strictEqual(res.setHeader.callArgs[0][0], "key1");
|
|
241
|
+
strictEqual(res.setHeader.callArgs[0][1], "value1");
|
|
242
|
+
strictEqual(res.setHeader.callArgs[1][0], "key2");
|
|
243
|
+
strictEqual(res.setHeader.callArgs[1][1], "value2");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
[{status: "status"}, {status: undefined}].forEach(({status}) => {
|
|
247
|
+
[{headers: "headers"}, {status: undefined}].forEach(({headers}) => {
|
|
248
|
+
[{body: {attr1: "value2"}}].forEach(({body}) => {
|
|
249
|
+
it(format("this.json alias for this.end %o", {status, headers, body}), async () => {
|
|
250
|
+
const context = new Context(uuid, {
|
|
251
|
+
url: "/url",
|
|
252
|
+
method: "MeThoD",
|
|
253
|
+
socket: {
|
|
254
|
+
remoteAddress: "remoteAddress"
|
|
255
|
+
},
|
|
256
|
+
headers: {
|
|
257
|
+
host: "fakeHost"
|
|
258
|
+
}
|
|
259
|
+
}, res, logger);
|
|
260
|
+
context.end = fake(async () => {
|
|
261
|
+
|
|
262
|
+
});
|
|
263
|
+
await context.json(body, headers, status);
|
|
264
|
+
strictEqual(cookieSerialize.callCount, 0);
|
|
265
|
+
strictEqual(res.end.callCount, 0);
|
|
266
|
+
strictEqual(res.setHeader.callCount, 0);
|
|
267
|
+
strictEqual(context.end.callCount, 1);
|
|
268
|
+
strictEqual(context.end.callArgs[0][0].status, status ? status : 200);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
|
|
275
|
+
it("this.text", () => {
|
|
276
|
+
// TODO
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
it("this.html", () => {
|
|
280
|
+
// TODO
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it("this.redirect", () => {
|
|
284
|
+
// TODO
|
|
285
|
+
});
|
|
286
|
+
});
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {matchTokenizePath, PathPart, PathParams, PathPartToken, tokenizePath} from "./tokenize-match";
|
|
2
|
+
import {Context, Handler, Method} from "./context";
|
|
3
|
+
import {Router} from "../router";
|
|
4
|
+
import {parse} from "@miqro/parser";
|
|
5
|
+
import {GroupPolicyHandler, ParseRequest, SessionHandler, ResultParser} from "../middleware";
|
|
6
|
+
import {
|
|
7
|
+
HandlerWithOptions,
|
|
8
|
+
handlerWithOptionsParseOptions, ResponseHandler,
|
|
9
|
+
RouterHandlerOptions,
|
|
10
|
+
routerHandlerParseOptions, RouterJSONDoc
|
|
11
|
+
} from "./router-utils";
|
|
12
|
+
|
|
13
|
+
export class RouterHandler {
|
|
14
|
+
|
|
15
|
+
private readonly tokens: PathPartToken[];
|
|
16
|
+
private readonly handler: Array<Handler | Router>;
|
|
17
|
+
private readonly isRouter: boolean;
|
|
18
|
+
private readonly options?: RouterHandlerOptions;
|
|
19
|
+
|
|
20
|
+
public constructor(
|
|
21
|
+
handler: Array<Handler> | HandlerWithOptions | Handler | Router,
|
|
22
|
+
private readonly path: string | undefined,
|
|
23
|
+
private readonly method: Method | undefined,
|
|
24
|
+
options?: RouterHandlerOptions) {
|
|
25
|
+
this.tokens = tokenizePath(path);
|
|
26
|
+
|
|
27
|
+
if (options) {
|
|
28
|
+
options = parse(options, routerHandlerParseOptions, "no_extra", "options");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
let handlers: Array<Handler | Router> = []
|
|
32
|
+
this.isRouter = handler instanceof Router;
|
|
33
|
+
|
|
34
|
+
if (!this.isRouter && !(handler instanceof Array) && typeof handler === "object") {
|
|
35
|
+
// HandlerWithOptions
|
|
36
|
+
if (options) {
|
|
37
|
+
throw new Error("bad arguments. cannot send options with HandlerWithOptions");
|
|
38
|
+
}
|
|
39
|
+
handler = parse(handler, handlerWithOptionsParseOptions, "no_extra", "handler");
|
|
40
|
+
const realHandler = (handler as HandlerWithOptions).handler;
|
|
41
|
+
const newOptions = {
|
|
42
|
+
...(handler as HandlerWithOptions)
|
|
43
|
+
};
|
|
44
|
+
delete (newOptions as any).handler;
|
|
45
|
+
handler = realHandler;
|
|
46
|
+
this.options = newOptions;
|
|
47
|
+
} else {
|
|
48
|
+
this.options = options;
|
|
49
|
+
}
|
|
50
|
+
const routeHandlers = handler instanceof Array ? handler : [handler] as Array<Handler | Router>;
|
|
51
|
+
|
|
52
|
+
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
53
|
+
throw new Error("bad arguments");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (this.options && this.options.middleware) {
|
|
57
|
+
handlers = handlers.concat(this.options.middleware);
|
|
58
|
+
}
|
|
59
|
+
if (this.options && this.options.session) {
|
|
60
|
+
handlers.push(typeof this.options.session !== "function" ? SessionHandler(this.options.session) : this.options.session);
|
|
61
|
+
}
|
|
62
|
+
if (this.options && this.options.policy) {
|
|
63
|
+
handlers.push(GroupPolicyHandler(this.options.policy));
|
|
64
|
+
}
|
|
65
|
+
if (this.options && this.options.request) {
|
|
66
|
+
handlers.push(ParseRequest(this.options.request, this.options.parser));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
handlers = handlers.concat(routeHandlers);
|
|
70
|
+
|
|
71
|
+
if (this.options && this.options.response) {
|
|
72
|
+
if (typeof this.options.response.body === "boolean" && this.options.response.body) {
|
|
73
|
+
(handlers as Handler[]).push(ResponseHandler());
|
|
74
|
+
} else if (this.options.response.body) {
|
|
75
|
+
(handlers as Handler[]).push(ResultParser(this.options.response.body, this.options.parser));
|
|
76
|
+
(handlers as Handler[]).push(ResponseHandler());
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
this.handler = handlers;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
public getJSONDoc(doc: RouterJSONDoc = {}, prePath = "/"): RouterJSONDoc {
|
|
83
|
+
const nPath = this.path && this.path.length > 1 ? this.path.substring(1) : "";
|
|
84
|
+
const nMethod = this.method ? this.method : "";
|
|
85
|
+
|
|
86
|
+
if (this.isRouter) {
|
|
87
|
+
const router: Router = this.handler[0] as Router;
|
|
88
|
+
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
89
|
+
} else if (this.options) {
|
|
90
|
+
if (!doc[`${prePath}${nPath}`]) {
|
|
91
|
+
doc[`${prePath}${nPath}`] = {};
|
|
92
|
+
}
|
|
93
|
+
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
94
|
+
throw new Error("cannot create doc routes override each other");
|
|
95
|
+
}
|
|
96
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
97
|
+
description: this.options.description,
|
|
98
|
+
name: this.options.name,
|
|
99
|
+
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(),
|
|
100
|
+
policy: this.options.policy,
|
|
101
|
+
request: this.options.request ? {
|
|
102
|
+
body: this.options.request.body,
|
|
103
|
+
headers: this.options.request.headers,
|
|
104
|
+
params: this.options.request.params,
|
|
105
|
+
query: this.options.request.query,
|
|
106
|
+
} : undefined,
|
|
107
|
+
response: this.options.response
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
return doc;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
public async invoke(ctx: Context, pathTokens: PathPart[], prePathTokens: PathPartToken[]): Promise<boolean> {
|
|
114
|
+
for (let i = 0; i < this.handler.length; i++) {
|
|
115
|
+
const h = this.handler[i];
|
|
116
|
+
if (h instanceof Router) {
|
|
117
|
+
if (!RouterHandler.pushResults(await h.run(ctx, pathTokens, prePathTokens.concat(this.tokens)), ctx)) {
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
if (!RouterHandler.pushResults(await h(ctx), ctx)) {
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
public isMatch(
|
|
130
|
+
method: string,
|
|
131
|
+
requestParts: PathPart[],
|
|
132
|
+
prePathTokens: PathPartToken[]): {
|
|
133
|
+
match: boolean;
|
|
134
|
+
params?: PathParams
|
|
135
|
+
} {
|
|
136
|
+
if ((this.method === undefined || this.method === method)) {
|
|
137
|
+
if (this.path === undefined) {
|
|
138
|
+
return {
|
|
139
|
+
match: true
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
return matchTokenizePath(this.isRouter, prePathTokens.concat(this.tokens), requestParts);
|
|
143
|
+
}
|
|
144
|
+
return {
|
|
145
|
+
match: false
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
private static pushResults(result: any, ctx: Context): boolean {
|
|
150
|
+
if (result === false) {
|
|
151
|
+
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
152
|
+
return false;
|
|
153
|
+
} else if (result !== true && result !== undefined) {
|
|
154
|
+
ctx.logger.trace("pushing to results [%o]", result);
|
|
155
|
+
ctx.results.push(result);
|
|
156
|
+
}
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
@@ -1,47 +1,7 @@
|
|
|
1
1
|
import {Context, Handler} from "./context";
|
|
2
|
-
import {
|
|
2
|
+
import {ParseOptionMap, ParseOptions, ParserInterface} from "@miqro/parser";
|
|
3
3
|
import {GroupPolicy, SessionHandlerOptions} from "./session";
|
|
4
4
|
|
|
5
|
-
export const ResultParser = (options: ParseOptions | ParseOptions[] | string, parser?: ParserInterface): Handler => {
|
|
6
|
-
return async function ResultParser(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(lastResult, o.options, o.mode, `ctx.results[${index}]`);
|
|
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
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
} else if (typeof options !== "string") {
|
|
29
|
-
const mappedResults = (parser ? parser : {parse}).parse(lastResult, options.options, options.mode, `ctx.results[${index}]`);
|
|
30
|
-
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
31
|
-
return mappedResults;
|
|
32
|
-
} else {
|
|
33
|
-
const mappedResults = (parser ? parser : {parse}).parse(lastResult, options, "no_extra", `ctx.results[${index}]`);
|
|
34
|
-
ctx.logger.trace(`ctx.results[%s] mapped to %o`, index, mappedResults);
|
|
35
|
-
return mappedResults;
|
|
36
|
-
}
|
|
37
|
-
} catch (e) {
|
|
38
|
-
ctx.logger.error(`error parsing lastResult[%o]`, lastResult);
|
|
39
|
-
throw e;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
5
|
export const ResponseHandler = (): Handler<void> => async (ctx: Context): Promise<void> => {
|
|
46
6
|
ctx.logger.trace("results [%o]", ctx.results);
|
|
47
7
|
if (!ctx.results || ctx.results.length === 0) {
|
|
@@ -57,9 +17,33 @@ export const ResponseHandler = (): Handler<void> => async (ctx: Context): Promis
|
|
|
57
17
|
}
|
|
58
18
|
};
|
|
59
19
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
20
|
+
|
|
21
|
+
export const routeParseOptions: ParseOptionMap = {
|
|
22
|
+
name: "string?",
|
|
23
|
+
description: "string?",
|
|
24
|
+
policy: "GroupPolicy?",
|
|
25
|
+
response: {
|
|
26
|
+
type: "nested?",
|
|
27
|
+
nestedOptions: {
|
|
28
|
+
options: {
|
|
29
|
+
body: "boolean?|ParseOptions[]!?|string?"
|
|
30
|
+
},
|
|
31
|
+
mode: "no_extra"
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
request: {
|
|
35
|
+
type: "nested?",
|
|
36
|
+
nestedOptions: {
|
|
37
|
+
options: {
|
|
38
|
+
headers: "ParseOptions[]!?",
|
|
39
|
+
query: "boolean?|ParseOptions[]!?|string?",
|
|
40
|
+
params: "boolean?|ParseOptions[]!?|string?",
|
|
41
|
+
body: "boolean?|ParseOptions[]!?|string?"
|
|
42
|
+
},
|
|
43
|
+
mode: "no_extra"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
63
47
|
|
|
64
48
|
export interface RouteOptions {
|
|
65
49
|
name?: string;
|
|
@@ -86,8 +70,26 @@ export interface RouterJSONDoc {
|
|
|
86
70
|
}
|
|
87
71
|
}
|
|
88
72
|
|
|
73
|
+
export const routerHandlerParseOptions: ParseOptionMap = {
|
|
74
|
+
...routeParseOptions,
|
|
75
|
+
parser: "ParserInterface?",
|
|
76
|
+
middleware: "function[]!?",
|
|
77
|
+
session: "SessionHandlerOptions?|function?"
|
|
78
|
+
};
|
|
79
|
+
|
|
89
80
|
export interface RouterHandlerOptions extends RouteOptions {
|
|
90
81
|
parser?: ParserInterface;
|
|
91
82
|
middleware?: Handler | Handler[];
|
|
92
83
|
session?: SessionHandlerOptions | Handler;
|
|
93
84
|
}
|
|
85
|
+
|
|
86
|
+
export const handlerWithOptionsParseOptions: ParseOptionMap = {
|
|
87
|
+
...routerHandlerParseOptions,
|
|
88
|
+
handler: "function[]!",
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export interface HandlerWithOptions extends RouterHandlerOptions {
|
|
92
|
+
handler: Handler | Array<Handler>;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {Dict} from "@miqro/parser";
|
|
2
1
|
import {Context} from "../context";
|
|
3
2
|
|
|
4
3
|
export interface SessionHandlerOptions {
|
|
@@ -17,7 +16,11 @@ export interface SessionHandlerOptions {
|
|
|
17
16
|
}
|
|
18
17
|
}
|
|
19
18
|
|
|
20
|
-
|
|
19
|
+
type Dict<T = any> = {
|
|
20
|
+
[key: string]: T
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface NoTokenSession extends Dict {
|
|
21
24
|
account: string;
|
|
22
25
|
username: string;
|
|
23
26
|
groups: string[];
|