@webtypen/webframez-core 0.0.59 → 0.0.61
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/lib/Router/Response.d.ts +11 -3
- package/lib/Router/Response.js +14 -3
- package/lib/Router/Router.d.ts +4 -0
- package/lib/Router/Router.js +18 -3
- package/package.json +1 -1
- package/src/Router/Response.ts +15 -3
- package/src/Router/Router.ts +18 -3
package/lib/Router/Response.d.ts
CHANGED
|
@@ -8,22 +8,29 @@ export declare class Response {
|
|
|
8
8
|
[key: string]: any;
|
|
9
9
|
};
|
|
10
10
|
events: any;
|
|
11
|
+
autoEnd: boolean;
|
|
11
12
|
/**
|
|
12
13
|
* Mode
|
|
13
14
|
*/
|
|
14
15
|
mode: any | null;
|
|
15
16
|
constructor(options?: any | null | undefined);
|
|
17
|
+
/**
|
|
18
|
+
* Set Response-Auto-End
|
|
19
|
+
* @param status
|
|
20
|
+
* @returns Response
|
|
21
|
+
*/
|
|
22
|
+
setAutoEnd(status: boolean): this;
|
|
16
23
|
/**
|
|
17
24
|
* Sets the ServerResponse (res) Object (nodejs/http module)
|
|
18
25
|
* @param res
|
|
19
|
-
* @returns
|
|
26
|
+
* @returns Response
|
|
20
27
|
*/
|
|
21
28
|
setServerResponse(res: ServerResponse): this;
|
|
22
29
|
/**
|
|
23
30
|
* Set the http-status-code
|
|
24
31
|
*
|
|
25
32
|
* @param status
|
|
26
|
-
* @returns
|
|
33
|
+
* @returns Response
|
|
27
34
|
*/
|
|
28
35
|
status(status: number): Response;
|
|
29
36
|
/**
|
|
@@ -31,7 +38,7 @@ export declare class Response {
|
|
|
31
38
|
*
|
|
32
39
|
* @param type
|
|
33
40
|
* @param value
|
|
34
|
-
* @returns
|
|
41
|
+
* @returns Response
|
|
35
42
|
*/
|
|
36
43
|
header(type: string, value: string): this;
|
|
37
44
|
/**
|
|
@@ -39,6 +46,7 @@ export declare class Response {
|
|
|
39
46
|
* Any objects will be stringified to JSON
|
|
40
47
|
*
|
|
41
48
|
* @param content
|
|
49
|
+
* @returns Response
|
|
42
50
|
*/
|
|
43
51
|
send(content: any): this;
|
|
44
52
|
registerEvent(eventKey: string, func: any): Promise<void>;
|
package/lib/Router/Response.js
CHANGED
|
@@ -15,16 +15,26 @@ class Response {
|
|
|
15
15
|
this.statusCode = 200;
|
|
16
16
|
this.headers = {};
|
|
17
17
|
this.events = { after: [] };
|
|
18
|
+
this.autoEnd = true;
|
|
18
19
|
/**
|
|
19
20
|
* Mode
|
|
20
21
|
*/
|
|
21
22
|
this.mode = null;
|
|
22
23
|
this.mode = options && options.mode ? options.mode : null;
|
|
23
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* Set Response-Auto-End
|
|
27
|
+
* @param status
|
|
28
|
+
* @returns Response
|
|
29
|
+
*/
|
|
30
|
+
setAutoEnd(status) {
|
|
31
|
+
this.autoEnd = status;
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
24
34
|
/**
|
|
25
35
|
* Sets the ServerResponse (res) Object (nodejs/http module)
|
|
26
36
|
* @param res
|
|
27
|
-
* @returns
|
|
37
|
+
* @returns Response
|
|
28
38
|
*/
|
|
29
39
|
setServerResponse(res) {
|
|
30
40
|
this.res = res;
|
|
@@ -34,7 +44,7 @@ class Response {
|
|
|
34
44
|
* Set the http-status-code
|
|
35
45
|
*
|
|
36
46
|
* @param status
|
|
37
|
-
* @returns
|
|
47
|
+
* @returns Response
|
|
38
48
|
*/
|
|
39
49
|
status(status) {
|
|
40
50
|
this.statusCode = status;
|
|
@@ -49,7 +59,7 @@ class Response {
|
|
|
49
59
|
*
|
|
50
60
|
* @param type
|
|
51
61
|
* @param value
|
|
52
|
-
* @returns
|
|
62
|
+
* @returns Response
|
|
53
63
|
*/
|
|
54
64
|
header(type, value) {
|
|
55
65
|
this.headers[type] = value;
|
|
@@ -64,6 +74,7 @@ class Response {
|
|
|
64
74
|
* Any objects will be stringified to JSON
|
|
65
75
|
*
|
|
66
76
|
* @param content
|
|
77
|
+
* @returns Response
|
|
67
78
|
*/
|
|
68
79
|
send(content) {
|
|
69
80
|
var _a, _b, _c;
|
package/lib/Router/Router.d.ts
CHANGED
|
@@ -100,6 +100,10 @@ declare class RouterFacade {
|
|
|
100
100
|
* @param res
|
|
101
101
|
*/
|
|
102
102
|
sendError(res: ServerResponse, code: number, message?: string): void;
|
|
103
|
+
/**
|
|
104
|
+
* Parses JSON without exception
|
|
105
|
+
*/
|
|
106
|
+
parseJson(jsonObject: any): any;
|
|
103
107
|
/**
|
|
104
108
|
* Processes a request
|
|
105
109
|
*
|
package/lib/Router/Router.js
CHANGED
|
@@ -188,6 +188,19 @@ class RouterFacade {
|
|
|
188
188
|
res.writeHead(code, { "Content-Type": "text/html" });
|
|
189
189
|
res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
|
|
190
190
|
}
|
|
191
|
+
/**
|
|
192
|
+
* Parses JSON without exception
|
|
193
|
+
*/
|
|
194
|
+
parseJson(jsonObject) {
|
|
195
|
+
let json = null;
|
|
196
|
+
try {
|
|
197
|
+
json = JSON.parse(jsonObject);
|
|
198
|
+
}
|
|
199
|
+
catch (e) {
|
|
200
|
+
console.error(e);
|
|
201
|
+
}
|
|
202
|
+
return json;
|
|
203
|
+
}
|
|
191
204
|
/**
|
|
192
205
|
* Processes a request
|
|
193
206
|
*
|
|
@@ -254,7 +267,7 @@ class RouterFacade {
|
|
|
254
267
|
body: bodyPlain &&
|
|
255
268
|
customRequest.headers["content-type"] === "application/json" &&
|
|
256
269
|
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
257
|
-
?
|
|
270
|
+
? this.parseJson(bodyPlain)
|
|
258
271
|
: bodyPlain
|
|
259
272
|
? qs_1.default.parse(options && options.event && options.event.isBase64Encoded
|
|
260
273
|
? Buffer.from(bodyPlain, "base64").toString("utf-8")
|
|
@@ -274,7 +287,7 @@ class RouterFacade {
|
|
|
274
287
|
body: bodyPlain &&
|
|
275
288
|
req.headers["content-type"] === "application/json" &&
|
|
276
289
|
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
277
|
-
?
|
|
290
|
+
? this.parseJson(bodyPlain)
|
|
278
291
|
: bodyPlain
|
|
279
292
|
? qs_1.default.parse(bodyPlain)
|
|
280
293
|
: {},
|
|
@@ -418,7 +431,9 @@ class RouterFacade {
|
|
|
418
431
|
};
|
|
419
432
|
}
|
|
420
433
|
else if (res && req) {
|
|
421
|
-
|
|
434
|
+
if (response.autoEnd) {
|
|
435
|
+
res.end();
|
|
436
|
+
}
|
|
422
437
|
this.requestConsoleLog(req, response.statusCode);
|
|
423
438
|
}
|
|
424
439
|
}
|
package/package.json
CHANGED
package/src/Router/Response.ts
CHANGED
|
@@ -6,6 +6,7 @@ export class Response {
|
|
|
6
6
|
content?: any | null | undefined;
|
|
7
7
|
headers: { [key: string]: any } = {};
|
|
8
8
|
events: any = { after: [] };
|
|
9
|
+
autoEnd: boolean = true;
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* Mode
|
|
@@ -16,10 +17,20 @@ export class Response {
|
|
|
16
17
|
this.mode = options && options.mode ? options.mode : null;
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Set Response-Auto-End
|
|
22
|
+
* @param status
|
|
23
|
+
* @returns Response
|
|
24
|
+
*/
|
|
25
|
+
setAutoEnd(status: boolean) {
|
|
26
|
+
this.autoEnd = status;
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
|
|
19
30
|
/**
|
|
20
31
|
* Sets the ServerResponse (res) Object (nodejs/http module)
|
|
21
32
|
* @param res
|
|
22
|
-
* @returns
|
|
33
|
+
* @returns Response
|
|
23
34
|
*/
|
|
24
35
|
setServerResponse(res: ServerResponse) {
|
|
25
36
|
this.res = res;
|
|
@@ -30,7 +41,7 @@ export class Response {
|
|
|
30
41
|
* Set the http-status-code
|
|
31
42
|
*
|
|
32
43
|
* @param status
|
|
33
|
-
* @returns
|
|
44
|
+
* @returns Response
|
|
34
45
|
*/
|
|
35
46
|
status(status: number): Response {
|
|
36
47
|
this.statusCode = status;
|
|
@@ -48,7 +59,7 @@ export class Response {
|
|
|
48
59
|
*
|
|
49
60
|
* @param type
|
|
50
61
|
* @param value
|
|
51
|
-
* @returns
|
|
62
|
+
* @returns Response
|
|
52
63
|
*/
|
|
53
64
|
header(type: string, value: string) {
|
|
54
65
|
this.headers[type] = value;
|
|
@@ -66,6 +77,7 @@ export class Response {
|
|
|
66
77
|
* Any objects will be stringified to JSON
|
|
67
78
|
*
|
|
68
79
|
* @param content
|
|
80
|
+
* @returns Response
|
|
69
81
|
*/
|
|
70
82
|
send(content: any) {
|
|
71
83
|
this.content = content;
|
package/src/Router/Router.ts
CHANGED
|
@@ -218,6 +218,19 @@ class RouterFacade {
|
|
|
218
218
|
res.end(message ? message : code === 404 ? "Oops! Page not found ..." : "Oops! There was an unexpected error ...");
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
+
/**
|
|
222
|
+
* Parses JSON without exception
|
|
223
|
+
*/
|
|
224
|
+
parseJson(jsonObject: any) {
|
|
225
|
+
let json = null;
|
|
226
|
+
try {
|
|
227
|
+
json = JSON.parse(jsonObject);
|
|
228
|
+
} catch(e: any) {
|
|
229
|
+
console.error(e);
|
|
230
|
+
}
|
|
231
|
+
return json;
|
|
232
|
+
}
|
|
233
|
+
|
|
221
234
|
/**
|
|
222
235
|
* Processes a request
|
|
223
236
|
*
|
|
@@ -291,7 +304,7 @@ class RouterFacade {
|
|
|
291
304
|
bodyPlain &&
|
|
292
305
|
customRequest.headers["content-type"] === "application/json" &&
|
|
293
306
|
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
294
|
-
?
|
|
307
|
+
? this.parseJson(bodyPlain)
|
|
295
308
|
: bodyPlain
|
|
296
309
|
? qs.parse(
|
|
297
310
|
options && options.event && options.event.isBase64Encoded
|
|
@@ -314,7 +327,7 @@ class RouterFacade {
|
|
|
314
327
|
bodyPlain &&
|
|
315
328
|
req.headers["content-type"] === "application/json" &&
|
|
316
329
|
(bodyPlain.trim().substring(0, 1) === "[" || bodyPlain.trim().substring(0, 1) === "{")
|
|
317
|
-
?
|
|
330
|
+
? this.parseJson(bodyPlain)
|
|
318
331
|
: bodyPlain
|
|
319
332
|
? qs.parse(bodyPlain)
|
|
320
333
|
: {},
|
|
@@ -469,7 +482,9 @@ class RouterFacade {
|
|
|
469
482
|
headers: response.headers,
|
|
470
483
|
};
|
|
471
484
|
} else if (res && req) {
|
|
472
|
-
|
|
485
|
+
if (response.autoEnd) {
|
|
486
|
+
res.end();
|
|
487
|
+
}
|
|
473
488
|
this.requestConsoleLog(req, response.statusCode);
|
|
474
489
|
}
|
|
475
490
|
} catch (e: any) {
|