@miqro/core 1.3.5 → 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-router-utils.d.ts +1 -1
- package/dist/handler/app.d.ts +3 -9
- package/dist/handler/app.js +6 -7
- package/dist/handler/app.js.map +1 -1
- package/dist/handler/common/router-utils.d.ts +18 -10
- package/dist/handler/common/router-utils.js +37 -10
- package/dist/handler/common/router-utils.js.map +1 -1
- package/dist/handler/common/router.d.ts +18 -8
- package/dist/handler/common/router.js +12 -1
- package/dist/handler/common/router.js.map +1 -1
- package/dist/handler/feature-router.d.ts +2 -2
- 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/parser/get.js +3 -6
- package/dist/util/parser/get.js.map +1 -1
- package/package.json +1 -1
- package/src/handler/app.ts +8 -14
- package/src/handler/common/router-utils.ts +63 -19
- package/src/handler/common/router.ts +30 -8
- package/src/handler/feature-router.ts +1 -1
- package/src/util/client.ts +64 -0
- package/src/util/parser/get.ts +3 -5
package/README.md
CHANGED
|
@@ -2,87 +2,24 @@
|
|
|
2
2
|
|
|
3
3
|
this module provides helpers functions to develop nodejs applications like **logging**, **config** and **request parsing**
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
|
|
7
|
-
#### parse
|
|
8
|
-
|
|
9
|
-
```typescript
|
|
10
|
-
import { Parser } from "@miqro/core";
|
|
11
|
-
|
|
12
|
-
const parser = new Parser();
|
|
13
|
-
const parsedValue = parser.parse("body", req.body, {
|
|
14
|
-
name: "string",
|
|
15
|
-
age: "number"
|
|
16
|
-
}, "no_extra");
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
#### custom type
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
import { Parser } from "@miqro/core";
|
|
23
|
-
|
|
24
|
-
const parser = new Parser();
|
|
25
|
-
|
|
26
|
-
// registerParser
|
|
27
|
-
parser.registerParser("custom-type", ({ value }) => {
|
|
28
|
-
const value = parseInt(value, 10);
|
|
29
|
-
return {
|
|
30
|
-
isType: !isNaN(value),
|
|
31
|
-
value
|
|
32
|
-
};
|
|
33
|
-
});
|
|
34
|
-
// parse
|
|
35
|
-
const parsedValue = parser.parse("body", req.body, {
|
|
36
|
-
name: "string",
|
|
37
|
-
age: "number",
|
|
38
|
-
custom: {
|
|
39
|
-
type: "custom-type",
|
|
40
|
-
required: false,
|
|
41
|
-
defaultValue: 0,
|
|
42
|
-
allowNull: true
|
|
43
|
-
},
|
|
44
|
-
customArray: {
|
|
45
|
-
type: "array",
|
|
46
|
-
arrayType: "custom-type",
|
|
47
|
-
required: false,
|
|
48
|
-
defaultValue: 0,
|
|
49
|
-
allowNull: true
|
|
50
|
-
}
|
|
51
|
-
}, "no_extra"):
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
## get
|
|
55
|
-
|
|
56
|
-
```typescript
|
|
57
|
-
import { get } from "@miqro/core";
|
|
58
|
-
...
|
|
59
|
-
const name = get(obj, "user.info.name", "no_name");
|
|
60
|
-
// with parse
|
|
61
|
-
const age = get(obj, "user.info.age", 0, {
|
|
62
|
-
type:"number",
|
|
63
|
-
numberMin: 0,
|
|
64
|
-
numberMaxDecimals: 0
|
|
65
|
-
});
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
## request
|
|
5
|
+
## async request with http.request
|
|
69
6
|
|
|
70
7
|
```typescript
|
|
71
8
|
import { request } from "@miqro/core";
|
|
72
9
|
|
|
73
10
|
const response = await request({
|
|
74
11
|
url: "https://hostname/path",
|
|
12
|
+
followRedirect: true, // defaults to false
|
|
75
13
|
query: {
|
|
76
14
|
limit: 100,
|
|
77
15
|
offset: 0,
|
|
78
16
|
q: "search"
|
|
79
17
|
},
|
|
80
18
|
method: "GET"
|
|
81
|
-
|
|
82
19
|
});
|
|
83
20
|
```
|
|
84
21
|
|
|
85
|
-
|
|
22
|
+
### options
|
|
86
23
|
|
|
87
24
|
```typescript
|
|
88
25
|
interface RequestOptions {
|
|
@@ -104,16 +41,7 @@ interface RequestOptions {
|
|
|
104
41
|
## Logger
|
|
105
42
|
|
|
106
43
|
```typescript
|
|
107
|
-
import { getLogger,
|
|
108
|
-
|
|
109
|
-
/*
|
|
110
|
-
// custom factory
|
|
111
|
-
setLoggerFactory((args)=>{
|
|
112
|
-
return new Logger(args.identifier, args.level, {
|
|
113
|
-
transports: [...],
|
|
114
|
-
formatter: (args) => `${args.message}`;
|
|
115
|
-
});
|
|
116
|
-
});*/
|
|
44
|
+
import { getLogger, Logger } from "@miqro/core";
|
|
117
45
|
|
|
118
46
|
// const logger = new Logger();
|
|
119
47
|
const logger = getLogger("identifier");
|
|
@@ -125,7 +53,25 @@ logger.error("error");
|
|
|
125
53
|
logger.trace("trace");
|
|
126
54
|
```
|
|
127
55
|
|
|
128
|
-
|
|
56
|
+
### custom logger factory
|
|
57
|
+
|
|
58
|
+
for example if you want to add default transports or a different formatting for all Loggers created with ***getLogger(....)***
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { setLoggerFactory, Logger } from "@miqro/core";
|
|
62
|
+
// custom factory
|
|
63
|
+
const transports = [
|
|
64
|
+
|
|
65
|
+
];
|
|
66
|
+
setLoggerFactory((args)=>{
|
|
67
|
+
return new Logger(args.identifier, args.level, {
|
|
68
|
+
transports,
|
|
69
|
+
formatter: (args) => `${args.message}`;
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## checkEnvVariables and loadConfig
|
|
129
75
|
|
|
130
76
|
```typescript
|
|
131
77
|
import { checkEnvVariables, loadConfig } from "@miqro/core";
|
|
@@ -146,74 +92,22 @@ const [ URL_A, DB_URI ] = checkEnvVariables([
|
|
|
146
92
|
]);
|
|
147
93
|
```
|
|
148
94
|
|
|
149
|
-
## http
|
|
150
|
-
|
|
151
|
-
#### Context
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
class Context {
|
|
155
|
-
req: IncomingMessage;
|
|
156
|
-
res: ServerResponse;
|
|
157
|
-
url: URL;
|
|
158
|
-
logger: Logger;
|
|
159
|
-
startMS: number;
|
|
160
|
-
uuid: string;
|
|
161
|
-
session?: Session;
|
|
162
|
-
results: any[];
|
|
163
|
-
path: string;
|
|
164
|
-
hash: string;
|
|
165
|
-
method: string;
|
|
166
|
-
headers: IncomingHttpHeaders;
|
|
167
|
-
cookies: {
|
|
168
|
-
[name: string]: string;
|
|
169
|
-
};
|
|
170
|
-
query: {
|
|
171
|
-
[name: string]: string | string[];
|
|
172
|
-
};
|
|
173
|
-
params: {
|
|
174
|
-
[name: string]: string | undefined;
|
|
175
|
-
};
|
|
176
|
-
buffer: Buffer;
|
|
177
|
-
remoteAddress?: string;
|
|
178
|
-
body: any;
|
|
179
|
-
constructor(req: IncomingMessage, res: ServerResponse, logger?: Logger);
|
|
180
|
-
close(): Promise<void>;
|
|
181
|
-
addVaryHeader(value: number | string | ReadonlyArray<string>): ServerResponse;
|
|
182
|
-
setHeader(name: string, value: number | string | ReadonlyArray<string>): ServerResponse;
|
|
183
|
-
setCookie(name: string, value: string, options?: {
|
|
184
|
-
encode?: (uriComponent: string | number | boolean) => string;
|
|
185
|
-
maxAge?: number | null;
|
|
186
|
-
domain?: string;
|
|
187
|
-
httpOnly?: boolean;
|
|
188
|
-
secure?: boolean;
|
|
189
|
-
path?: string;
|
|
190
|
-
expires?: Date;
|
|
191
|
-
sameSite?: "lax" | "strict" | "none" | true;
|
|
192
|
-
}): ServerResponse;
|
|
193
|
-
end({ status, headers, body }: Response): Promise<void>;
|
|
194
|
-
json(body: any, headers?: OutgoingHttpHeaders, status?: number): Promise<void>;
|
|
195
|
-
text(text: string, headers?: OutgoingHttpHeaders, status?: number): Promise<void>;
|
|
196
|
-
html(html: string, headers?: OutgoingHttpHeaders, status?: number): Promise<void>;
|
|
197
|
-
redirect(url: string, headers?: OutgoingHttpHeaders, status?: number): Promise<void>;
|
|
198
|
-
}
|
|
199
|
-
```
|
|
200
|
-
|
|
201
|
-
#### Router
|
|
95
|
+
## helpers for http module
|
|
202
96
|
|
|
203
97
|
```typescript
|
|
204
|
-
import
|
|
205
|
-
import {
|
|
206
|
-
|
|
207
|
-
const app = new App();
|
|
98
|
+
import * as http from "http";
|
|
99
|
+
import { App, Router, JSONParser, ReadBuffer } from "@miqro/core";
|
|
208
100
|
|
|
209
101
|
const router = new Router();
|
|
210
|
-
|
|
211
102
|
router.use([
|
|
212
|
-
ReadBuffer(),
|
|
213
|
-
JSONParser(),
|
|
103
|
+
ReadBuffer(), // needed to read the buffer of the request and set ctx.buffer
|
|
104
|
+
JSONParser(), // needed to read the buffer and check if is a valid JSON and set ctx.body
|
|
214
105
|
]);
|
|
215
106
|
|
|
216
107
|
router.post("/echo", async (ctx) => {
|
|
108
|
+
// the incomingMessage is in ctx.req
|
|
109
|
+
// the ServerResponse is in ctx.res
|
|
110
|
+
ctx.logger.info("we got a echo request");
|
|
217
111
|
ctx.json({
|
|
218
112
|
body: ctx.body
|
|
219
113
|
}):
|
|
@@ -223,73 +117,65 @@ router.get("/user/:userId", async (ctx) => {
|
|
|
223
117
|
userId: ctx.params.userId
|
|
224
118
|
}):
|
|
225
119
|
});
|
|
226
|
-
app.use(router, "/api");
|
|
227
120
|
|
|
228
|
-
//
|
|
229
|
-
//
|
|
230
|
-
createServer(
|
|
231
|
-
|
|
121
|
+
// for using just the router and not the default error handling and 404
|
|
122
|
+
// http.createServer(router.listener).listen(8080);
|
|
123
|
+
http.createServer(new App().use(router).listener).listen(8080);
|
|
124
|
+
// or
|
|
125
|
+
// new App().use(router).listen(8080)
|
|
232
126
|
```
|
|
233
127
|
|
|
234
|
-
|
|
128
|
+
## router.use extended options for request parsing
|
|
235
129
|
|
|
236
130
|
```typescript
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
131
|
+
app.get("/add/:a/:b", {
|
|
132
|
+
request: {
|
|
133
|
+
query: false, // do not allow query params
|
|
134
|
+
body: false, // do not allow body
|
|
135
|
+
params: {
|
|
136
|
+
options: {
|
|
137
|
+
a: "number", // parse param :a to a number if posible if not throw 400
|
|
138
|
+
b: "number" // parse param :b to a number if posible if not throw 400
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
},
|
|
142
|
+
handler: async (ctx) => {
|
|
143
|
+
// ctx.params.a and ctx.params.b are numbers and not strings
|
|
144
|
+
const result = ctx.params.a + ctx.params.b;
|
|
145
|
+
ctx.logger.info("adding %s + %s = %s", ctx.params.a, ctx.params.b, result);
|
|
146
|
+
ctx.json({
|
|
147
|
+
result
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
// or
|
|
152
|
+
/*
|
|
153
|
+
app.get("/add/:a/:b", async (ctx) => {
|
|
154
|
+
return {
|
|
155
|
+
result: ctx.params.a + ctx.params.b
|
|
156
|
+
}
|
|
157
|
+
}, {
|
|
158
|
+
request: {
|
|
260
159
|
...
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
return 2;
|
|
266
|
-
},
|
|
267
|
-
async (ctx)=>{
|
|
268
|
-
// ctx.results will have [123, 2]
|
|
160
|
+
},
|
|
161
|
+
response: {
|
|
162
|
+
options: {
|
|
163
|
+
result: "number"
|
|
269
164
|
},
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
165
|
+
mode: "remove_extra"
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
*/
|
|
273
169
|
```
|
|
274
170
|
|
|
275
|
-
|
|
171
|
+
## error handling
|
|
276
172
|
|
|
277
173
|
```typescript
|
|
278
|
-
...
|
|
279
174
|
app.catch(myFallBackerrorHandler1) // this will catch all throws
|
|
280
|
-
app.catch(myFallBackerrorHandler2) //
|
|
281
|
-
|
|
282
|
-
app.use(
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
// for example this is interpreted as a 400 if req.body doesnt match
|
|
286
|
-
parse("body", body, [
|
|
287
|
-
{ name: "name", type: "string", required: true },
|
|
288
|
-
{ name: "age", type: "number", required: true },
|
|
289
|
-
{ name: "likes", type: "array", required: true, arrayType: "string" }
|
|
290
|
-
], "no_extra");
|
|
291
|
-
},
|
|
292
|
-
...
|
|
293
|
-
]);
|
|
175
|
+
app.catch(myFallBackerrorHandler2) // if myFallBackerrorHandler1 didnt ended the request myFallBackerrorHandler2 will be called
|
|
176
|
+
|
|
177
|
+
app.use(async (ctx) => {
|
|
178
|
+
throw new Error("");
|
|
179
|
+
});
|
|
294
180
|
```
|
|
295
181
|
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { Logger } from "../util/logger";
|
|
2
2
|
import { FeatureRouterOptions } from "./feature-router";
|
|
3
|
-
export declare const traverseAPIRouteDir: (featureName: string, dirname: string, basePath?: string, features?: FeatureRouterOptions
|
|
3
|
+
export declare const traverseAPIRouteDir: (featureName: string, dirname: string, basePath?: string, features?: FeatureRouterOptions, logger?: Logger | undefined) => FeatureRouterOptions;
|
package/dist/handler/app.d.ts
CHANGED
|
@@ -1,14 +1,8 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { Router, Context } from "./common";
|
|
5
|
-
export interface AppOptions {
|
|
6
|
-
newRequestLogger?: (req: IncomingMessage) => Logger;
|
|
7
|
-
newContext?: (req: IncomingMessage, res: ServerResponse) => Context;
|
|
8
|
-
}
|
|
2
|
+
import { RequestListener, Server } from "http";
|
|
3
|
+
import { Router, RouterOptions } from "./common";
|
|
9
4
|
export declare class App extends Router {
|
|
10
|
-
private appOptions?;
|
|
11
5
|
readonly listener: RequestListener;
|
|
12
|
-
constructor(
|
|
6
|
+
constructor(config?: RouterOptions);
|
|
13
7
|
listen(...args: any[]): Server;
|
|
14
8
|
}
|
package/dist/handler/app.js
CHANGED
|
@@ -4,15 +4,14 @@ exports.App = void 0;
|
|
|
4
4
|
const http_1 = require("http");
|
|
5
5
|
const common_1 = require("./common");
|
|
6
6
|
class App extends common_1.Router {
|
|
7
|
-
constructor(
|
|
8
|
-
super();
|
|
9
|
-
this.appOptions = appOptions;
|
|
7
|
+
constructor(config) {
|
|
8
|
+
super(config);
|
|
10
9
|
const errorHandler = DefaultErrorHandler();
|
|
11
10
|
this.listener = async (req, res) => {
|
|
12
|
-
const ctx = this.
|
|
13
|
-
this.
|
|
14
|
-
new common_1.Context(req, res, this.
|
|
15
|
-
this.
|
|
11
|
+
const ctx = this.config && this.config.newContext ?
|
|
12
|
+
this.config.newContext(req, res) :
|
|
13
|
+
new common_1.Context(req, res, this.config && this.config.newRequestLogger ?
|
|
14
|
+
this.config.newRequestLogger(req) :
|
|
16
15
|
undefined);
|
|
17
16
|
ctx.logger.trace(`request received`);
|
|
18
17
|
try {
|
package/dist/handler/app.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"app.js","sourceRoot":"","sources":["../../src/handler/app.ts"],"names":[],"mappings":";;;AAAA,+
|
|
1
|
+
{"version":3,"file":"app.js","sourceRoot":"","sources":["../../src/handler/app.ts"],"names":[],"mappings":";;;AAAA,+BAA6D;AAC7D,qCAAwE;AAExE,MAAa,GAAI,SAAQ,eAAM;IAG7B,YAAY,MAAsB;QAChC,KAAK,CAAC,MAAM,CAAC,CAAC;QACd,MAAM,YAAY,GAAG,mBAAmB,EAAE,CAAC;QAC3C,IAAI,CAAC,QAAQ,GAAG,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;YACjC,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAA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,MAAM,GAAG,CAAC,GAAG,CAAC;wBACZ,OAAO,EAAE,YAAY;wBACrB,MAAM,EAAE,MAAM,CAAC,WAAW;wBAC1B,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,aAAa;qBAC5C,CAAC,CAAC;oBACH,MAAM;gBACR;oBACE,MAAM,GAAG,CAAC,GAAG,CAAC;wBACZ,OAAO,EAAE,YAAY;wBACrB,MAAM,EAAE,MAAM,CAAC,KAAK;wBACpB,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,cAAc;qBAC7C,CAAC,CAAC;oBACH,MAAM;aACT;YACD,OAAO,KAAK,CAAC;SACd;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -5,17 +5,12 @@ import { PathPartToken } from "./tokenize-match";
|
|
|
5
5
|
import { Parser, ParseOptions } from "../../util/parser";
|
|
6
6
|
import { SessionHandlerOptions } from "../middleware/session";
|
|
7
7
|
import { GroupPolicy } from "../../util/session";
|
|
8
|
-
export interface
|
|
9
|
-
|
|
10
|
-
[method: string]: RouterHandlerOptions;
|
|
11
|
-
};
|
|
8
|
+
export interface HandlerWithOptions extends RouterHandlerOptions {
|
|
9
|
+
handler: Handler | Array<Handler>;
|
|
12
10
|
}
|
|
13
|
-
export interface
|
|
11
|
+
export interface RouteOptions {
|
|
14
12
|
name?: string;
|
|
15
13
|
description?: string;
|
|
16
|
-
parser?: Parser;
|
|
17
|
-
middleware?: Handler | Handler[];
|
|
18
|
-
session?: SessionHandlerOptions | Handler;
|
|
19
14
|
policy?: GroupPolicy;
|
|
20
15
|
request?: {
|
|
21
16
|
headers?: ParseOptions | ParseOptions[];
|
|
@@ -25,14 +20,27 @@ export interface RouterHandlerOptions {
|
|
|
25
20
|
};
|
|
26
21
|
response?: true | ParseOptions | ParseOptions[];
|
|
27
22
|
}
|
|
23
|
+
export interface RouteJSONDoc extends RouteOptions {
|
|
24
|
+
identifier: string;
|
|
25
|
+
}
|
|
26
|
+
export interface RouterJSONDoc {
|
|
27
|
+
[path: string]: {
|
|
28
|
+
[method: string]: RouteJSONDoc;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface RouterHandlerOptions extends RouteOptions {
|
|
32
|
+
parser?: Parser;
|
|
33
|
+
middleware?: Handler | Handler[];
|
|
34
|
+
session?: SessionHandlerOptions | Handler;
|
|
35
|
+
}
|
|
28
36
|
export declare class RouterHandler {
|
|
29
37
|
private readonly path;
|
|
30
38
|
private readonly method;
|
|
31
|
-
private readonly options?;
|
|
32
39
|
private readonly tokens;
|
|
33
40
|
private readonly handler;
|
|
34
41
|
private readonly isRouter;
|
|
35
|
-
|
|
42
|
+
private readonly options?;
|
|
43
|
+
constructor(handler: Array<Handler> | HandlerWithOptions | Handler | Router, path: string | undefined, method: Method | undefined, options?: RouterHandlerOptions);
|
|
36
44
|
getJSONDoc(doc?: RouterJSONDoc, prePath?: string): RouterJSONDoc;
|
|
37
45
|
invoke(ctx: Context, pathTokens: {
|
|
38
46
|
value: string;
|
|
@@ -12,12 +12,27 @@ class RouterHandler {
|
|
|
12
12
|
constructor(handler, path, method, options) {
|
|
13
13
|
this.path = path;
|
|
14
14
|
this.method = method;
|
|
15
|
-
this.options = options;
|
|
16
15
|
this.tokens = (0, tokenize_match_1.tokenizePath)(path);
|
|
17
|
-
const routeHandlers = handler instanceof Array ? handler : [handler];
|
|
18
16
|
let handlers = [];
|
|
19
17
|
this.isRouter = handler instanceof router_1.Router;
|
|
20
|
-
if (
|
|
18
|
+
if (!this.isRouter && !(handler instanceof Array) && typeof handler === "object") {
|
|
19
|
+
// HandlerWithOptions
|
|
20
|
+
if (options) {
|
|
21
|
+
throw new Error("bad arguments. cannot send options with HandlerWithOptions");
|
|
22
|
+
}
|
|
23
|
+
const realHandler = handler.handler;
|
|
24
|
+
const newOptions = {
|
|
25
|
+
...handler
|
|
26
|
+
};
|
|
27
|
+
delete newOptions.handler;
|
|
28
|
+
handler = realHandler;
|
|
29
|
+
this.options = newOptions;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
this.options = options;
|
|
33
|
+
}
|
|
34
|
+
const routeHandlers = handler instanceof Array ? handler : [handler];
|
|
35
|
+
if ((!this.isRouter && routeHandlers.length !== routeHandlers.filter(h => typeof h === "function").length) || (this.isRouter && this.options)) {
|
|
21
36
|
throw new Error("bad arguments");
|
|
22
37
|
}
|
|
23
38
|
if (this.options && this.options.middleware) {
|
|
@@ -49,7 +64,7 @@ class RouterHandler {
|
|
|
49
64
|
const nMethod = this.method ? this.method : "";
|
|
50
65
|
if (this.isRouter) {
|
|
51
66
|
const router = this.handler[0];
|
|
52
|
-
router.getJSONDoc(doc, nPath);
|
|
67
|
+
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
53
68
|
}
|
|
54
69
|
else if (this.options) {
|
|
55
70
|
if (!doc[`${prePath}${nPath}`]) {
|
|
@@ -58,7 +73,19 @@ class RouterHandler {
|
|
|
58
73
|
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
59
74
|
throw new Error("cannot create doc routes override each other");
|
|
60
75
|
}
|
|
61
|
-
doc[`${prePath}${nPath}`][nMethod] =
|
|
76
|
+
doc[`${prePath}${nPath}`][nMethod] = {
|
|
77
|
+
description: this.options.description,
|
|
78
|
+
name: this.options.name,
|
|
79
|
+
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(),
|
|
80
|
+
policy: this.options.policy,
|
|
81
|
+
request: this.options.request ? {
|
|
82
|
+
body: this.options.request.body,
|
|
83
|
+
headers: this.options.request.headers,
|
|
84
|
+
params: this.options.request.params,
|
|
85
|
+
query: this.options.request.query,
|
|
86
|
+
} : undefined,
|
|
87
|
+
response: this.options.response
|
|
88
|
+
};
|
|
62
89
|
}
|
|
63
90
|
return doc;
|
|
64
91
|
}
|
|
@@ -91,14 +118,14 @@ class RouterHandler {
|
|
|
91
118
|
match: false
|
|
92
119
|
};
|
|
93
120
|
}
|
|
94
|
-
pushResults(
|
|
95
|
-
if (
|
|
121
|
+
pushResults(result, ctx) {
|
|
122
|
+
if (result === false) {
|
|
96
123
|
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
97
124
|
return false;
|
|
98
125
|
}
|
|
99
|
-
else if (
|
|
100
|
-
ctx.logger.trace("pushing to results [%o]",
|
|
101
|
-
ctx.results.push(
|
|
126
|
+
else if (result !== true && result !== undefined) {
|
|
127
|
+
ctx.logger.trace("pushing to results [%o]", result);
|
|
128
|
+
ctx.results.push(result);
|
|
102
129
|
}
|
|
103
130
|
return true;
|
|
104
131
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router-utils.js","sourceRoot":"","sources":["../../../src/handler/common/router-utils.ts"],"names":[],"mappings":";;;AAAA,qCAAkC;AAGlC,qDAAiF;AACjF,+CAAmD;AAEnD,mDAA8E;AAC9E,+CAAyD;AACzD,+CAAkD;AAClD,2CAAmD;
|
|
1
|
+
{"version":3,"file":"router-utils.js","sourceRoot":"","sources":["../../../src/handler/common/router-utils.ts"],"names":[],"mappings":";;;AAAA,qCAAkC;AAGlC,qDAAiF;AACjF,+CAAmD;AAEnD,mDAA8E;AAC9E,+CAAyD;AACzD,+CAAkD;AAClD,2CAAmD;AAoCnD,MAAa,aAAa;IAOtB,YAAmB,OAA+D,EAAmB,IAAwB,EAAmB,MAA0B,EAAE,OAA8B;QAArG,SAAI,GAAJ,IAAI,CAAoB;QAAmB,WAAM,GAAN,MAAM,CAAoB;QACtK,IAAI,CAAC,MAAM,GAAG,IAAA,6BAAY,EAAC,IAAI,CAAC,CAAC;QAEjC,IAAI,QAAQ,GAA4B,EAAE,CAAA;QAC1C,IAAI,CAAC,QAAQ,GAAG,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ACJ;SACJ;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,OAAO,CACV,MAAc,EACd,UAA+C,EAC/C,aAA8B;QAI9B,IAAI,CAAC,IAAI,CAAC,MAAM,KAAK,SAAS,IAAI,IAAI,CAAC,MAAM,KAAK,MAAM,CAAC,EAAE;YACvD,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS,EAAE;gBACzB,OAAO;oBACH,KAAK,EAAE,IAAI;iBACd,CAAC;aACL;YACD,OAAO,IAAA,kCAAiB,EAAC,IAAI,CAAC,QAAQ,EAAE,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,UAAU,CAAC,CAAC;SAC1F;QACD,OAAO;YACH,KAAK,EAAE,KAAK;SACf,CAAC;IACN,CAAC;IAEO,WAAW,CAAC,MAAe,EAAE,GAAY;QAC7C,IAAI,MAAM,KAAK,KAAK,EAAE;YAClB,8EAA8E;YAC9E,OAAO,KAAK,CAAC;SAChB;aAAM,IAAI,MAAM,KAAK,IAAI,IAAI,MAAM,KAAK,SAAS,EAAE;YAChD,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;YACpD,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;SAC5B;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AA1ID,sCA0IC"}
|
|
@@ -1,16 +1,26 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingMessage, RequestListener, ServerResponse } from "http";
|
|
3
|
+
import { Logger } from "../../util/logger";
|
|
1
4
|
import { Method, Handler, Context, ErrorHandler } from "./context";
|
|
2
|
-
import { RouterHandler, RouterJSONDoc, RouterHandlerOptions } from "./router-utils";
|
|
5
|
+
import { RouterHandler, RouterJSONDoc, RouterHandlerOptions, HandlerWithOptions } from "./router-utils";
|
|
3
6
|
import { PathPartToken } from "./tokenize-match";
|
|
7
|
+
export interface RouterOptions {
|
|
8
|
+
newRequestLogger?: (req: IncomingMessage) => Logger;
|
|
9
|
+
newContext?: (req: IncomingMessage, res: ServerResponse) => Context;
|
|
10
|
+
}
|
|
4
11
|
export declare class Router {
|
|
12
|
+
protected config?: RouterOptions | undefined;
|
|
5
13
|
protected readonly handlers: RouterHandler[];
|
|
6
14
|
protected readonly errorHandlers: ErrorHandler[];
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
15
|
+
readonly listener: RequestListener;
|
|
16
|
+
constructor(config?: RouterOptions | undefined);
|
|
17
|
+
get(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
18
|
+
post(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
19
|
+
patch(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
20
|
+
delete(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
21
|
+
put(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
22
|
+
options(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router;
|
|
23
|
+
use(handler: Array<Handler> | HandlerWithOptions | Handler | Router, path?: string, method?: Method, options?: RouterHandlerOptions): Router;
|
|
14
24
|
catch(errorHandler: Array<ErrorHandler> | ErrorHandler): Router;
|
|
15
25
|
run(ctx: Context, pathTokens?: {
|
|
16
26
|
value: string;
|
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Router = void 0;
|
|
4
|
+
const context_1 = require("./context");
|
|
4
5
|
const router_utils_1 = require("./router-utils");
|
|
5
6
|
const tokenize_match_1 = require("./tokenize-match");
|
|
6
7
|
class Router {
|
|
7
|
-
constructor() {
|
|
8
|
+
constructor(config) {
|
|
9
|
+
this.config = config;
|
|
8
10
|
this.handlers = [];
|
|
9
11
|
this.errorHandlers = [];
|
|
12
|
+
this.listener = async (req, res) => {
|
|
13
|
+
const ctx = this.config && this.config.newContext ?
|
|
14
|
+
this.config.newContext(req, res) :
|
|
15
|
+
new context_1.Context(req, res, this.config && this.config.newRequestLogger ?
|
|
16
|
+
this.config.newRequestLogger(req) :
|
|
17
|
+
undefined);
|
|
18
|
+
ctx.logger.trace(`request received`);
|
|
19
|
+
await this.run(ctx);
|
|
20
|
+
};
|
|
10
21
|
}
|
|
11
22
|
get(path, handler, options) {
|
|
12
23
|
return this.use(handler, path, "get", options);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../../src/handler/common/router.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"router.js","sourceRoot":"","sources":["../../../src/handler/common/router.ts"],"names":[],"mappings":";;;AAEA,uCAAmE;AACnE,iDAAwG;AACxG,qDAA2E;AAO3E,MAAa,MAAM;IAIjB,YAAsB,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;QAHzB,aAAQ,GAAoB,EAAE,CAAC;QAC/B,kBAAa,GAAmB,EAAE,CAAC;QAGpD,IAAI,CAAC,QAAQ,GAAG,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;YACjC,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;gBACjD,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC;gBAClC,IAAI,iBAAO,CACT,GAAG,EACH,GAAG,EACH,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,CAAC;oBAC3C,IAAI,CAAC,MAAM,CAAC,gBAAgB,CAAC,GAAG,CA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AC,CAAC;4BACxB,GAAG,GAAG,CAAC,MAAM;4BACb,GAAG,OAAO,CAAC,MAAM;yBAClB,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;qBACpB;oBACD,cAAc,GAAG,MAAM,CAAC,CAAC,MAAM,CAAC,GAAG,EAAE,UAAU,EAAE,aAAa,CAAC,CAAC;iBACjE;aACF;YACD,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,WAAW,CAAC;SAC7B;QAAC,OAAO,CAAM,EAAE;YACf,KAAK,MAAM,EAAE,IAAI,IAAI,CAAC,aAAa,EAAE;gBACnC,MAAM,GAAG,GAAG,MAAM,EAAE,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;gBAC7B,IAAI,GAAG,KAAK,KAAK,IAAI,GAAG,CAAC,GAAG,CAAC,WAAW,EAAE;oBACxC,OAAO,KAAK,CAAC;iBACd;aACF;YACD,MAAM,CAAC,CAAC,CAAC,uBAAuB;SACjC;IACH,CAAC;IACM,UAAU,CAAC,MAAqB,EAAE,EAAE,OAAO,GAAG,GAAG;QACtD,KAAK,MAAM,CAAC,IAAI,IAAI,CAAC,QAAQ,EAAE;YAC7B,GAAG,GAAG,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;SAClC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;CACF;AAtFD,wBAsFC"}
|
|
@@ -8,5 +8,5 @@ export interface FeatureRouterPathOptions {
|
|
|
8
8
|
handler: Array<Handler> | Handler;
|
|
9
9
|
options?: RouterHandlerOptions;
|
|
10
10
|
}
|
|
11
|
-
export declare type FeatureRouterOptions
|
|
12
|
-
export declare const FeatureRouter: (options: FeatureRouterOptions
|
|
11
|
+
export declare type FeatureRouterOptions = SimpleMap<FeatureRouterPathOptions>;
|
|
12
|
+
export declare const FeatureRouter: (options: FeatureRouterOptions, logger?: Logger | undefined) => Router;
|
|
@@ -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 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/util/client.ts"],"names":[],"mappings":";;;AAAA,uCAAqE;AAQrE,MAAM,SAAS,GAAG,GAAG,CAAC;AAEtB,MAAa,UAAU;IAGnB,YAAY,SAOX,EAAE,QAAgB,EAAmB,MAAe,EAAE,WAAqC;QAAtD,WAAM,GAAN,MAAM,CAAS;QAR9C,cAAS,GAA0B,EAAE,CAAC;QASzC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;QACrC,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE;YACnB,MAAM,OAAO,GAAG,MAAM,CAA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AE;gCACtB,MAAM,EAAE,CAAC;gCACT,GAAG,IAAI;6BACV,CAAC;4BACF,GAAG,CAAC,OAAO,EAAE,eAAe,EAAE,WAAW,CAAC,CAAC;4BAC3C,GAAG,CAAC,OAAO,EAAE,kBAAkB,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;4BACtE,MAAM,QAAQ,GAAG,MAAM,IAAA,iBAAO,EAAC,WAAW,CAAC,CAAC;4BAC5C,GAAG,CAAC,OAAO,EAAE,wBAAwB,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;4BACtF,GAAG,CAAC,OAAO,EAAE,qBAAqB,EAAE,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,CAAC,CAAC;4BACzE,OAAO,QAAQ,CAAC;yBACnB;wBAAC,OAAO,CAAM,EAAE;4BACb,GAAG,CAAC,OAAO,EAAE,gCAAgC,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;4BAC1D,MAAM,CAAC,CAAC;yBACX;oBACL,CAAC,CAAA;iBACJ;qBAAM;oBACH,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;iBACpD;aACJ;SACJ;IACL,CAAC;CACJ;AArDD,gCAqDC"}
|
package/dist/util/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,CAAC,CAAC,SAAS,CAAA;KAC7D;IACD,IAAI,OAAO,QAAQ,KAAK,QAAQ,EAAE;QAChC,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;KACnD;IACD,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,CAAC;IAC3C,IAAI,KAAK,GAAG,GAAG,CAAC;IAChB,OAAO,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE;QACtB,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAY,CAAC;QAC/B,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,SAAS,EAAE;YAC1B,OAAO,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,SAAS,CAAC;SAC9D;QACD,KAAK,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC;KAClB;IACD,IAAI,CAAC,MAAM,EAAE;QACX,OAAO,KAAK,CAAC;KACd;IACD,MAAM,IAAI,GAAmB;QAC3B,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,QAAQ;QAClB,KAAK;QACL,GAAG,MAAM;KACV,CAAC;IACF,MAAM,YAAY,GAAG,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAA,mBAAU,EAAC,IAAI,CAAC,CAAC;IACzE,IAAI,YAAY,CAAC,MAAM,EAAE;QACvB,OAAO,YAAY,CAAC,WAAW,CAAC;KACjC;SAAM;QACL,MAAM,IAAI,yBAAiB,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,YAAY,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,QAAQ,QAAQ,IAAI,CAAC,IAAI,EAAE,EAAE,QAAQ,CAAC,CAAC;KACrH;AACH,CAAC,CAAA;AA/BY,QAAA,GAAG,OA+Bf"}
|
package/package.json
CHANGED
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 {
|
|
@@ -10,18 +10,13 @@ import { ResultParser } from "../response/result";
|
|
|
10
10
|
import { ResponseHandler } from "../response/json";
|
|
11
11
|
import { GroupPolicy } from "../../util/session";
|
|
12
12
|
|
|
13
|
-
export interface
|
|
14
|
-
|
|
15
|
-
[method: string]: RouterHandlerOptions
|
|
16
|
-
}
|
|
13
|
+
export interface HandlerWithOptions extends RouterHandlerOptions {
|
|
14
|
+
handler: Handler | Array<Handler>;
|
|
17
15
|
}
|
|
18
16
|
|
|
19
|
-
export interface
|
|
17
|
+
export interface RouteOptions {
|
|
20
18
|
name?: string;
|
|
21
19
|
description?: string;
|
|
22
|
-
parser?: Parser;
|
|
23
|
-
middleware?: Handler | Handler[];
|
|
24
|
-
session?: SessionHandlerOptions | Handler;
|
|
25
20
|
policy?: GroupPolicy;
|
|
26
21
|
request?: {
|
|
27
22
|
headers?: ParseOptions | ParseOptions[];
|
|
@@ -32,20 +27,57 @@ export interface RouterHandlerOptions {
|
|
|
32
27
|
response?: true | ParseOptions | ParseOptions[];
|
|
33
28
|
}
|
|
34
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
|
+
|
|
35
46
|
export class RouterHandler {
|
|
36
47
|
|
|
37
48
|
private readonly tokens: PathPartToken[];
|
|
38
49
|
private readonly handler: Array<Handler | Handler | Router>;
|
|
39
50
|
private readonly isRouter: boolean;
|
|
51
|
+
private readonly options?: RouterHandlerOptions;
|
|
40
52
|
|
|
41
|
-
public constructor(handler: Array<Handler> | Handler | Router, private readonly path: string | undefined, private readonly method: Method | undefined,
|
|
53
|
+
public constructor(handler: Array<Handler> | HandlerWithOptions | Handler | Router, private readonly path: string | undefined, private readonly method: Method | undefined, options?: RouterHandlerOptions) {
|
|
42
54
|
this.tokens = tokenizePath(path);
|
|
43
|
-
|
|
55
|
+
|
|
44
56
|
let handlers: Array<Handler | Router> = []
|
|
45
57
|
this.isRouter = handler instanceof Router;
|
|
46
|
-
|
|
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)) {
|
|
47
77
|
throw new Error("bad arguments");
|
|
48
78
|
}
|
|
79
|
+
|
|
80
|
+
|
|
49
81
|
if (this.options && this.options.middleware) {
|
|
50
82
|
handlers = handlers.concat(this.options.middleware);
|
|
51
83
|
}
|
|
@@ -78,15 +110,27 @@ export class RouterHandler {
|
|
|
78
110
|
|
|
79
111
|
if (this.isRouter) {
|
|
80
112
|
const router: Router = this.handler[0] as Router;
|
|
81
|
-
router.getJSONDoc(doc, nPath);
|
|
82
|
-
} else if(this.options){
|
|
113
|
+
router.getJSONDoc(doc, `${prePath}${nPath}`);
|
|
114
|
+
} else if (this.options) {
|
|
83
115
|
if (!doc[`${prePath}${nPath}`]) {
|
|
84
116
|
doc[`${prePath}${nPath}`] = {};
|
|
85
117
|
}
|
|
86
118
|
if (doc[`${prePath}${nPath}`][nMethod]) {
|
|
87
119
|
throw new Error("cannot create doc routes override each other");
|
|
88
120
|
}
|
|
89
|
-
doc[`${prePath}${nPath}`][nMethod] =
|
|
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
|
+
};
|
|
90
134
|
}
|
|
91
135
|
return doc;
|
|
92
136
|
}
|
|
@@ -127,13 +171,13 @@ export class RouterHandler {
|
|
|
127
171
|
};
|
|
128
172
|
}
|
|
129
173
|
|
|
130
|
-
private pushResults(
|
|
131
|
-
if (
|
|
174
|
+
private pushResults(result: boolean, ctx: Context): boolean {
|
|
175
|
+
if (result === false) {
|
|
132
176
|
// ctx.logger.debug(`avoiding next handlers because handler returned false.`);
|
|
133
177
|
return false;
|
|
134
|
-
} else if (
|
|
135
|
-
ctx.logger.trace("pushing to results [%o]",
|
|
136
|
-
ctx.results.push(
|
|
178
|
+
} else if (result !== true && result !== undefined) {
|
|
179
|
+
ctx.logger.trace("pushing to results [%o]", result);
|
|
180
|
+
ctx.results.push(result);
|
|
137
181
|
}
|
|
138
182
|
return true;
|
|
139
183
|
}
|
|
@@ -1,29 +1,51 @@
|
|
|
1
|
+
import { IncomingMessage, RequestListener, ServerResponse } from "http";
|
|
2
|
+
import { Logger } from "../../util/logger";
|
|
1
3
|
import { Method, Handler, Context, ErrorHandler } from "./context";
|
|
2
|
-
import { RouterHandler, RouterJSONDoc, RouterHandlerOptions } from "./router-utils";
|
|
4
|
+
import { RouterHandler, RouterJSONDoc, RouterHandlerOptions, HandlerWithOptions } from "./router-utils";
|
|
3
5
|
import { normalizePath, PathPartToken, splitPath } from "./tokenize-match";
|
|
4
6
|
|
|
7
|
+
export interface RouterOptions {
|
|
8
|
+
newRequestLogger?: (req: IncomingMessage) => Logger;
|
|
9
|
+
newContext?: (req: IncomingMessage, res: ServerResponse) => Context;
|
|
10
|
+
}
|
|
11
|
+
|
|
5
12
|
export class Router {
|
|
6
13
|
protected readonly handlers: RouterHandler[] = [];
|
|
7
14
|
protected readonly errorHandlers: ErrorHandler[] = [];
|
|
8
|
-
public
|
|
15
|
+
public readonly listener: RequestListener;
|
|
16
|
+
constructor(protected config?: RouterOptions) {
|
|
17
|
+
this.listener = async (req, res) => {
|
|
18
|
+
const ctx = this.config && this.config.newContext ?
|
|
19
|
+
this.config.newContext(req, res) :
|
|
20
|
+
new Context(
|
|
21
|
+
req,
|
|
22
|
+
res,
|
|
23
|
+
this.config && this.config.newRequestLogger ?
|
|
24
|
+
this.config.newRequestLogger(req) :
|
|
25
|
+
undefined);
|
|
26
|
+
ctx.logger.trace(`request received`);
|
|
27
|
+
await this.run(ctx);
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
public get(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
9
31
|
return this.use(handler, path, "get", options);
|
|
10
32
|
}
|
|
11
|
-
public post(path: string, handler: Array<Handler> | Handler | Router, options?: RouterHandlerOptions): Router {
|
|
33
|
+
public post(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
12
34
|
return this.use(handler, path, "post", options);
|
|
13
35
|
}
|
|
14
|
-
public patch(path: string, handler: Array<Handler> | Handler | Router, options?: RouterHandlerOptions): Router {
|
|
36
|
+
public patch(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
15
37
|
return this.use(handler, path, "patch", options);
|
|
16
38
|
}
|
|
17
|
-
public delete(path: string, handler: Array<Handler> | Handler | Router, options?: RouterHandlerOptions): Router {
|
|
39
|
+
public delete(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
18
40
|
return this.use(handler, path, "delete", options);
|
|
19
41
|
}
|
|
20
|
-
public put(path: string, handler: Array<Handler> | Handler | Router, options?: RouterHandlerOptions): Router {
|
|
42
|
+
public put(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
21
43
|
return this.use(handler, path, "put", options);
|
|
22
44
|
}
|
|
23
|
-
public options(path: string, handler: Array<Handler> | Handler | Router, options?: RouterHandlerOptions): Router {
|
|
45
|
+
public options(path: string, handler: Array<Handler> | Handler | Router | HandlerWithOptions, options?: RouterHandlerOptions): Router {
|
|
24
46
|
return this.use(handler, path, "options", options);
|
|
25
47
|
}
|
|
26
|
-
public use(handler: Array<Handler> | Handler | Router, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
|
|
48
|
+
public use(handler: Array<Handler> | HandlerWithOptions | Handler | Router, path?: string, method?: Method, options?: RouterHandlerOptions): Router {
|
|
27
49
|
const nPath = path !== undefined ? normalizePath(path) : undefined;
|
|
28
50
|
const nMethod = method ? method.toUpperCase() as Method : method;
|
|
29
51
|
this.handlers.push(new RouterHandler(handler, nPath, nMethod, options));
|
|
@@ -12,7 +12,7 @@ export interface FeatureRouterPathOptions {
|
|
|
12
12
|
options?: RouterHandlerOptions;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
export type FeatureRouterOptions
|
|
15
|
+
export type FeatureRouterOptions = SimpleMap<FeatureRouterPathOptions>;
|
|
16
16
|
|
|
17
17
|
const FEATURE_ROUTER_METHODS: Method[] = ["get", "post", "put", "delete", "patch", "options"];
|
|
18
18
|
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { request, RequestOptions, RequestResponse } from "./request";
|
|
2
|
+
import { Logger, LogLevel } from "./logger";
|
|
3
|
+
import { SimpleMap } from "./parser";
|
|
4
|
+
|
|
5
|
+
export type RequestMapEndpoint = (args: Partial<RequestOptions>, logger?: Logger) => Promise<RequestResponse>;
|
|
6
|
+
|
|
7
|
+
export type RequestMapEndpointMap = SimpleMap<RequestMapEndpoint>
|
|
8
|
+
|
|
9
|
+
const SEPARATOR = "_";
|
|
10
|
+
|
|
11
|
+
export class RequestMap {
|
|
12
|
+
|
|
13
|
+
public endpoints: RequestMapEndpointMap = {};
|
|
14
|
+
constructor(endpoints: {
|
|
15
|
+
[path: string]: {
|
|
16
|
+
[method: string]: {
|
|
17
|
+
name?: string;
|
|
18
|
+
description?: string;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}, hostname: string, private readonly logger?: Logger, baseOptions?: Partial<RequestOptions>) {
|
|
22
|
+
const paths = Object.keys(endpoints);
|
|
23
|
+
for (const p of paths) {
|
|
24
|
+
const methods = Object.keys(endpoints[p]);
|
|
25
|
+
for (const m of methods) {
|
|
26
|
+
const mL = m.toLocaleLowerCase();
|
|
27
|
+
const endpointPath = `${p.split("/").map(p => p.trim()).map(p => p.replaceAll(":", "").replaceAll("?", "")).filter(p => p).map(p => p.toLocaleLowerCase()).join(SEPARATOR)}${SEPARATOR}${mL}`;
|
|
28
|
+
//const routerOptions: RouterHandlerOptions = doc[p][m];
|
|
29
|
+
if (!this.endpoints[endpointPath]) {
|
|
30
|
+
this.endpoints[endpointPath] = async (args: Partial<RequestOptions>, logger?: Logger) => {
|
|
31
|
+
const log = (level: LogLevel, text: string, ...optionalParams: any[]) => {
|
|
32
|
+
if (this.logger) {
|
|
33
|
+
this.logger[level](text, ...optionalParams);
|
|
34
|
+
}
|
|
35
|
+
if (logger) {
|
|
36
|
+
logger[level](text, ...optionalParams);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
|
|
41
|
+
const realOptions = {
|
|
42
|
+
...baseOptions,
|
|
43
|
+
url: `${hostname}${p}`,
|
|
44
|
+
method: m,
|
|
45
|
+
...args
|
|
46
|
+
};
|
|
47
|
+
log("trace", "request to %o", realOptions);
|
|
48
|
+
log("debug", "request to %s %s", realOptions.method, realOptions.url);
|
|
49
|
+
const response = await request(realOptions);
|
|
50
|
+
log("trace", "response from %s %s %o", realOptions.method, realOptions.url, response);
|
|
51
|
+
log("debug", "response from %s %s", realOptions.method, realOptions.url);
|
|
52
|
+
return response;
|
|
53
|
+
} catch (e: any) {
|
|
54
|
+
log("error", "request failed with message %s", e.message);
|
|
55
|
+
throw e;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
throw new Error("two methods for same endpoint");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
package/src/util/parser/get.ts
CHANGED
|
@@ -3,14 +3,12 @@ import { ParseOptionsError } from "../error";
|
|
|
3
3
|
import { BasicParseOption, ParseValueArgs } from "./common";
|
|
4
4
|
|
|
5
5
|
/*
|
|
6
|
-
usage const name = get(obj, "user.info.name", {type: "string"});
|
|
6
|
+
usage const name = get(obj, "user.info.name", defaultValue, {type: "string"}, parser);
|
|
7
7
|
*/
|
|
8
8
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
9
9
|
export const get = (obj: any, attrPath: string, defaultValue?: any, option?: BasicParseOption, parser?: Parser): any | undefined => {
|
|
10
|
-
if (typeof obj !== "object"
|
|
11
|
-
|
|
12
|
-
} else if (!obj && defaultValue !== undefined) {
|
|
13
|
-
return defaultValue;
|
|
10
|
+
if (!obj || typeof obj !== "object") {
|
|
11
|
+
return defaultValue !== undefined ? defaultValue : undefined
|
|
14
12
|
}
|
|
15
13
|
if (typeof attrPath !== "string") {
|
|
16
14
|
throw new Error(`attrPath must be typeof string`);
|