@nattyjs/express 0.0.1-beta.4 → 0.0.1-beta.41
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/index.cjs +42 -38
- package/dist/index.mjs +43 -39
- package/package.json +5 -3
package/dist/index.cjs
CHANGED
|
@@ -15,18 +15,18 @@ const compression__default = /*#__PURE__*/_interopDefaultCompat(compression);
|
|
|
15
15
|
async function getRequestBodyInfo(request) {
|
|
16
16
|
const contentType = request.headers["content-type"];
|
|
17
17
|
const bodyInfo = {};
|
|
18
|
-
if (request.method !== common.GET)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
18
|
+
if (request.method !== common.GET) {
|
|
19
|
+
if (contentType && contentType.toLowerCase().indexOf("application/json") > -1)
|
|
20
|
+
bodyInfo.json = request.body;
|
|
21
|
+
}
|
|
24
22
|
return bodyInfo;
|
|
25
23
|
}
|
|
26
24
|
|
|
27
25
|
function getResponse(expressResponse, responseInfo) {
|
|
28
26
|
if (responseInfo.headers)
|
|
29
|
-
|
|
27
|
+
for (const pair of responseInfo.headers.entries()) {
|
|
28
|
+
expressResponse.setHeader(pair[0], pair[1]);
|
|
29
|
+
}
|
|
30
30
|
if (responseInfo.cookies)
|
|
31
31
|
responseInfo.cookies.forEach((cookie) => {
|
|
32
32
|
const name = cookie.name;
|
|
@@ -38,7 +38,9 @@ function getResponse(expressResponse, responseInfo) {
|
|
|
38
38
|
expressResponse.statusCode = responseInfo.status;
|
|
39
39
|
if (responseInfo.body && responseInfo.body.json) {
|
|
40
40
|
expressResponse.send(responseInfo.body.json);
|
|
41
|
-
} else
|
|
41
|
+
} else if (responseInfo.body && responseInfo.body.buffer)
|
|
42
|
+
expressResponse.send(responseInfo.body.buffer);
|
|
43
|
+
else
|
|
42
44
|
expressResponse.send();
|
|
43
45
|
return expressResponse;
|
|
44
46
|
}
|
|
@@ -49,48 +51,50 @@ function parseCookies(value) {
|
|
|
49
51
|
const cookies = value.split(";");
|
|
50
52
|
for (const cookie of cookies) {
|
|
51
53
|
const splitCookie = cookie.split("=");
|
|
52
|
-
|
|
54
|
+
if (splitCookie.length > 1)
|
|
55
|
+
jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
return jsonCookies;
|
|
56
59
|
}
|
|
57
60
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
{
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
61
|
+
function requestHandler(config) {
|
|
62
|
+
return async (request, response) => {
|
|
63
|
+
const httpHandler = new core.HttpHandler();
|
|
64
|
+
const url = config.framework == common.FrameworkType.Firebase ? `http://localhost:3000/api${request.url}` : `http://localhost:3000${request.url}`;
|
|
65
|
+
const httpContext = new core.HttpContext(
|
|
66
|
+
{
|
|
67
|
+
url,
|
|
68
|
+
method: request.method,
|
|
69
|
+
body: await getRequestBodyInfo(request),
|
|
70
|
+
headers: request.headers,
|
|
71
|
+
cookies: parseCookies(request.headers["cookie"])
|
|
72
|
+
},
|
|
73
|
+
response
|
|
74
|
+
);
|
|
75
|
+
return httpHandler.processRequest(httpContext).then((httpResponse) => {
|
|
76
|
+
return getResponse(response, httpResponse);
|
|
77
|
+
}).catch((t) => {
|
|
78
|
+
return getResponse(response, t.httpResponse);
|
|
79
|
+
});
|
|
80
|
+
};
|
|
75
81
|
}
|
|
76
82
|
|
|
77
83
|
const app = express__default();
|
|
78
84
|
const ExpressModule = {
|
|
79
85
|
init(config) {
|
|
80
86
|
app.use(compression__default());
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
if (config.autoGeneratePort == true)
|
|
91
|
-
app.listen(3e3, () => {
|
|
92
|
-
console.log("Server is running on port 3000 ");
|
|
87
|
+
if (config.cors)
|
|
88
|
+
app.use(cors__default(config.cors));
|
|
89
|
+
app.use(express__default.json({ limit: config.payload?.limit || "1mb" }));
|
|
90
|
+
app.all("*", requestHandler(config));
|
|
91
|
+
if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
|
|
92
|
+
common.getPort().then((port) => {
|
|
93
|
+
app.listen(port, () => {
|
|
94
|
+
console.log(`[NATTYJS]:Server is running on port ${port}`);
|
|
95
|
+
});
|
|
93
96
|
});
|
|
97
|
+
return app;
|
|
94
98
|
}
|
|
95
99
|
};
|
|
96
100
|
|
package/dist/index.mjs
CHANGED
|
@@ -2,23 +2,23 @@ import express from 'express';
|
|
|
2
2
|
import cors from 'cors';
|
|
3
3
|
import compression from 'compression';
|
|
4
4
|
import { HttpHandler, HttpContext } from '@nattyjs/core';
|
|
5
|
-
import { GET } from '@nattyjs/common';
|
|
5
|
+
import { GET, FrameworkType, getPort } from '@nattyjs/common';
|
|
6
6
|
|
|
7
7
|
async function getRequestBodyInfo(request) {
|
|
8
8
|
const contentType = request.headers["content-type"];
|
|
9
9
|
const bodyInfo = {};
|
|
10
|
-
if (request.method !== GET)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
break;
|
|
15
|
-
}
|
|
10
|
+
if (request.method !== GET) {
|
|
11
|
+
if (contentType && contentType.toLowerCase().indexOf("application/json") > -1)
|
|
12
|
+
bodyInfo.json = request.body;
|
|
13
|
+
}
|
|
16
14
|
return bodyInfo;
|
|
17
15
|
}
|
|
18
16
|
|
|
19
17
|
function getResponse(expressResponse, responseInfo) {
|
|
20
18
|
if (responseInfo.headers)
|
|
21
|
-
|
|
19
|
+
for (const pair of responseInfo.headers.entries()) {
|
|
20
|
+
expressResponse.setHeader(pair[0], pair[1]);
|
|
21
|
+
}
|
|
22
22
|
if (responseInfo.cookies)
|
|
23
23
|
responseInfo.cookies.forEach((cookie) => {
|
|
24
24
|
const name = cookie.name;
|
|
@@ -30,7 +30,9 @@ function getResponse(expressResponse, responseInfo) {
|
|
|
30
30
|
expressResponse.statusCode = responseInfo.status;
|
|
31
31
|
if (responseInfo.body && responseInfo.body.json) {
|
|
32
32
|
expressResponse.send(responseInfo.body.json);
|
|
33
|
-
} else
|
|
33
|
+
} else if (responseInfo.body && responseInfo.body.buffer)
|
|
34
|
+
expressResponse.send(responseInfo.body.buffer);
|
|
35
|
+
else
|
|
34
36
|
expressResponse.send();
|
|
35
37
|
return expressResponse;
|
|
36
38
|
}
|
|
@@ -41,48 +43,50 @@ function parseCookies(value) {
|
|
|
41
43
|
const cookies = value.split(";");
|
|
42
44
|
for (const cookie of cookies) {
|
|
43
45
|
const splitCookie = cookie.split("=");
|
|
44
|
-
|
|
46
|
+
if (splitCookie.length > 1)
|
|
47
|
+
jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
|
|
45
48
|
}
|
|
46
49
|
}
|
|
47
50
|
return jsonCookies;
|
|
48
51
|
}
|
|
49
52
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
{
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
53
|
+
function requestHandler(config) {
|
|
54
|
+
return async (request, response) => {
|
|
55
|
+
const httpHandler = new HttpHandler();
|
|
56
|
+
const url = config.framework == FrameworkType.Firebase ? `http://localhost:3000/api${request.url}` : `http://localhost:3000${request.url}`;
|
|
57
|
+
const httpContext = new HttpContext(
|
|
58
|
+
{
|
|
59
|
+
url,
|
|
60
|
+
method: request.method,
|
|
61
|
+
body: await getRequestBodyInfo(request),
|
|
62
|
+
headers: request.headers,
|
|
63
|
+
cookies: parseCookies(request.headers["cookie"])
|
|
64
|
+
},
|
|
65
|
+
response
|
|
66
|
+
);
|
|
67
|
+
return httpHandler.processRequest(httpContext).then((httpResponse) => {
|
|
68
|
+
return getResponse(response, httpResponse);
|
|
69
|
+
}).catch((t) => {
|
|
70
|
+
return getResponse(response, t.httpResponse);
|
|
71
|
+
});
|
|
72
|
+
};
|
|
67
73
|
}
|
|
68
74
|
|
|
69
75
|
const app = express();
|
|
70
76
|
const ExpressModule = {
|
|
71
77
|
init(config) {
|
|
72
78
|
app.use(compression());
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
if (config.autoGeneratePort == true)
|
|
83
|
-
app.listen(3e3, () => {
|
|
84
|
-
console.log("Server is running on port 3000 ");
|
|
79
|
+
if (config.cors)
|
|
80
|
+
app.use(cors(config.cors));
|
|
81
|
+
app.use(express.json({ limit: config.payload?.limit || "1mb" }));
|
|
82
|
+
app.all("*", requestHandler(config));
|
|
83
|
+
if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
|
|
84
|
+
getPort().then((port) => {
|
|
85
|
+
app.listen(port, () => {
|
|
86
|
+
console.log(`[NATTYJS]:Server is running on port ${port}`);
|
|
87
|
+
});
|
|
85
88
|
});
|
|
89
|
+
return app;
|
|
86
90
|
}
|
|
87
91
|
};
|
|
88
92
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nattyjs/express",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.41",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "ajayojha <ojhaajay@outlook.com>",
|
|
@@ -16,10 +16,12 @@
|
|
|
16
16
|
},
|
|
17
17
|
"dependencies": {
|
|
18
18
|
"express": "4.18.2",
|
|
19
|
+
"chokidar": "4.0.3",
|
|
19
20
|
"cors": "2.8.5",
|
|
20
21
|
"compression": "1.7.4",
|
|
21
|
-
"@nattyjs/core": "0.0.1-beta.
|
|
22
|
-
"@nattyjs/
|
|
22
|
+
"@nattyjs/core": "0.0.1-beta.41",
|
|
23
|
+
"@nattyjs/common": "0.0.1-beta.41",
|
|
24
|
+
"@nattyjs/types": "0.0.1-beta.41"
|
|
23
25
|
},
|
|
24
26
|
"devDependencies": {
|
|
25
27
|
"@types/node": "20.3.1",
|