@nattyjs/express 0.0.1-beta.5 → 0.0.1-beta.51
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 +51 -23
- package/dist/index.mjs +51 -24
- package/package.json +5 -4
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const express = require('express');
|
|
4
|
+
const path = require('node:path');
|
|
4
5
|
const cors = require('cors');
|
|
5
6
|
const compression = require('compression');
|
|
6
7
|
const core = require('@nattyjs/core');
|
|
@@ -9,24 +10,25 @@ const common = require('@nattyjs/common');
|
|
|
9
10
|
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
|
|
10
11
|
|
|
11
12
|
const express__default = /*#__PURE__*/_interopDefaultCompat(express);
|
|
13
|
+
const path__default = /*#__PURE__*/_interopDefaultCompat(path);
|
|
12
14
|
const cors__default = /*#__PURE__*/_interopDefaultCompat(cors);
|
|
13
15
|
const compression__default = /*#__PURE__*/_interopDefaultCompat(compression);
|
|
14
16
|
|
|
15
17
|
async function getRequestBodyInfo(request) {
|
|
16
18
|
const contentType = request.headers["content-type"];
|
|
17
19
|
const bodyInfo = {};
|
|
18
|
-
if (request.method !== common.GET)
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
break;
|
|
23
|
-
}
|
|
20
|
+
if (request.method !== common.GET) {
|
|
21
|
+
if (contentType && contentType.toLowerCase().indexOf("application/json") > -1)
|
|
22
|
+
bodyInfo.json = request.body;
|
|
23
|
+
}
|
|
24
24
|
return bodyInfo;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
function getResponse(expressResponse, responseInfo) {
|
|
28
28
|
if (responseInfo.headers)
|
|
29
|
-
|
|
29
|
+
for (const pair of responseInfo.headers.entries()) {
|
|
30
|
+
expressResponse.setHeader(pair[0], pair[1]);
|
|
31
|
+
}
|
|
30
32
|
if (responseInfo.cookies)
|
|
31
33
|
responseInfo.cookies.forEach((cookie) => {
|
|
32
34
|
const name = cookie.name;
|
|
@@ -38,7 +40,9 @@ function getResponse(expressResponse, responseInfo) {
|
|
|
38
40
|
expressResponse.statusCode = responseInfo.status;
|
|
39
41
|
if (responseInfo.body && responseInfo.body.json) {
|
|
40
42
|
expressResponse.send(responseInfo.body.json);
|
|
41
|
-
} else
|
|
43
|
+
} else if (responseInfo.body && responseInfo.body.buffer)
|
|
44
|
+
expressResponse.send(responseInfo.body.buffer);
|
|
45
|
+
else
|
|
42
46
|
expressResponse.send();
|
|
43
47
|
return expressResponse;
|
|
44
48
|
}
|
|
@@ -49,7 +53,8 @@ function parseCookies(value) {
|
|
|
49
53
|
const cookies = value.split(";");
|
|
50
54
|
for (const cookie of cookies) {
|
|
51
55
|
const splitCookie = cookie.split("=");
|
|
52
|
-
|
|
56
|
+
if (splitCookie.length > 1)
|
|
57
|
+
jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
return jsonCookies;
|
|
@@ -58,7 +63,8 @@ function parseCookies(value) {
|
|
|
58
63
|
function requestHandler(config) {
|
|
59
64
|
return async (request, response) => {
|
|
60
65
|
const httpHandler = new core.HttpHandler();
|
|
61
|
-
const
|
|
66
|
+
const apiPrefix = `/${common.commonContainer.nattyConfig.api?.rootPath}`;
|
|
67
|
+
const url = config.framework == common.FrameworkType.Firebase ? `http://localhost:3000/api${request.url}` : `http://localhost:3000${apiPrefix}${request.url}`;
|
|
62
68
|
const httpContext = new core.HttpContext(
|
|
63
69
|
{
|
|
64
70
|
url,
|
|
@@ -72,7 +78,7 @@ function requestHandler(config) {
|
|
|
72
78
|
return httpHandler.processRequest(httpContext).then((httpResponse) => {
|
|
73
79
|
return getResponse(response, httpResponse);
|
|
74
80
|
}).catch((t) => {
|
|
75
|
-
response.
|
|
81
|
+
return getResponse(response, t.httpResponse);
|
|
76
82
|
});
|
|
77
83
|
};
|
|
78
84
|
}
|
|
@@ -80,19 +86,41 @@ function requestHandler(config) {
|
|
|
80
86
|
const app = express__default();
|
|
81
87
|
const ExpressModule = {
|
|
82
88
|
init(config) {
|
|
89
|
+
const apiPrefix = `/${common.commonContainer.nattyConfig.api?.rootPath}`;
|
|
90
|
+
const staticCfg = config.static;
|
|
83
91
|
app.use(compression__default());
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
92
|
+
if (config.cors)
|
|
93
|
+
app.use(cors__default(config.cors));
|
|
94
|
+
app.use(express__default.json({ limit: config.payload?.limit || "1mb" }));
|
|
95
|
+
if (staticCfg?.dir) {
|
|
96
|
+
const staticDir = path__default.resolve(staticCfg.dir);
|
|
97
|
+
app.use(
|
|
98
|
+
express__default.static(staticDir, {
|
|
99
|
+
index: false,
|
|
100
|
+
// we'll control SPA index fallback manually
|
|
101
|
+
maxAge: staticCfg.maxAge ?? "1d"
|
|
102
|
+
})
|
|
103
|
+
);
|
|
104
|
+
if (staticCfg.spaFallback !== false) {
|
|
105
|
+
app.get("*", (req, res, next) => {
|
|
106
|
+
const url = req.originalUrl || req.url;
|
|
107
|
+
if (url.startsWith(apiPrefix))
|
|
108
|
+
return next();
|
|
109
|
+
if (req.method !== "GET")
|
|
110
|
+
return next();
|
|
111
|
+
const acceptsHtml = req.headers.accept?.includes("text/html");
|
|
112
|
+
if (!acceptsHtml)
|
|
113
|
+
return next();
|
|
114
|
+
return res.sendFile(path__default.join(staticDir, staticCfg.indexFile ?? "index.html"));
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
app.use(apiPrefix, requestHandler(config));
|
|
119
|
+
if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
|
|
120
|
+
common.getPort().then((port) => {
|
|
121
|
+
app.listen(port, () => {
|
|
122
|
+
console.log(`[NATTYJS]:Server is running on port ${port}`);
|
|
123
|
+
});
|
|
96
124
|
});
|
|
97
125
|
return app;
|
|
98
126
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,24 +1,25 @@
|
|
|
1
1
|
import express from 'express';
|
|
2
|
+
import path from 'node:path';
|
|
2
3
|
import cors from 'cors';
|
|
3
4
|
import compression from 'compression';
|
|
4
5
|
import { HttpHandler, HttpContext } from '@nattyjs/core';
|
|
5
|
-
import { GET, FrameworkType } from '@nattyjs/common';
|
|
6
|
+
import { GET, commonContainer, FrameworkType, getPort } from '@nattyjs/common';
|
|
6
7
|
|
|
7
8
|
async function getRequestBodyInfo(request) {
|
|
8
9
|
const contentType = request.headers["content-type"];
|
|
9
10
|
const bodyInfo = {};
|
|
10
|
-
if (request.method !== GET)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
break;
|
|
15
|
-
}
|
|
11
|
+
if (request.method !== GET) {
|
|
12
|
+
if (contentType && contentType.toLowerCase().indexOf("application/json") > -1)
|
|
13
|
+
bodyInfo.json = request.body;
|
|
14
|
+
}
|
|
16
15
|
return bodyInfo;
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
function getResponse(expressResponse, responseInfo) {
|
|
20
19
|
if (responseInfo.headers)
|
|
21
|
-
|
|
20
|
+
for (const pair of responseInfo.headers.entries()) {
|
|
21
|
+
expressResponse.setHeader(pair[0], pair[1]);
|
|
22
|
+
}
|
|
22
23
|
if (responseInfo.cookies)
|
|
23
24
|
responseInfo.cookies.forEach((cookie) => {
|
|
24
25
|
const name = cookie.name;
|
|
@@ -30,7 +31,9 @@ function getResponse(expressResponse, responseInfo) {
|
|
|
30
31
|
expressResponse.statusCode = responseInfo.status;
|
|
31
32
|
if (responseInfo.body && responseInfo.body.json) {
|
|
32
33
|
expressResponse.send(responseInfo.body.json);
|
|
33
|
-
} else
|
|
34
|
+
} else if (responseInfo.body && responseInfo.body.buffer)
|
|
35
|
+
expressResponse.send(responseInfo.body.buffer);
|
|
36
|
+
else
|
|
34
37
|
expressResponse.send();
|
|
35
38
|
return expressResponse;
|
|
36
39
|
}
|
|
@@ -41,7 +44,8 @@ function parseCookies(value) {
|
|
|
41
44
|
const cookies = value.split(";");
|
|
42
45
|
for (const cookie of cookies) {
|
|
43
46
|
const splitCookie = cookie.split("=");
|
|
44
|
-
|
|
47
|
+
if (splitCookie.length > 1)
|
|
48
|
+
jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
|
|
45
49
|
}
|
|
46
50
|
}
|
|
47
51
|
return jsonCookies;
|
|
@@ -50,7 +54,8 @@ function parseCookies(value) {
|
|
|
50
54
|
function requestHandler(config) {
|
|
51
55
|
return async (request, response) => {
|
|
52
56
|
const httpHandler = new HttpHandler();
|
|
53
|
-
const
|
|
57
|
+
const apiPrefix = `/${commonContainer.nattyConfig.api?.rootPath}`;
|
|
58
|
+
const url = config.framework == FrameworkType.Firebase ? `http://localhost:3000/api${request.url}` : `http://localhost:3000${apiPrefix}${request.url}`;
|
|
54
59
|
const httpContext = new HttpContext(
|
|
55
60
|
{
|
|
56
61
|
url,
|
|
@@ -64,7 +69,7 @@ function requestHandler(config) {
|
|
|
64
69
|
return httpHandler.processRequest(httpContext).then((httpResponse) => {
|
|
65
70
|
return getResponse(response, httpResponse);
|
|
66
71
|
}).catch((t) => {
|
|
67
|
-
response.
|
|
72
|
+
return getResponse(response, t.httpResponse);
|
|
68
73
|
});
|
|
69
74
|
};
|
|
70
75
|
}
|
|
@@ -72,19 +77,41 @@ function requestHandler(config) {
|
|
|
72
77
|
const app = express();
|
|
73
78
|
const ExpressModule = {
|
|
74
79
|
init(config) {
|
|
80
|
+
const apiPrefix = `/${commonContainer.nattyConfig.api?.rootPath}`;
|
|
81
|
+
const staticCfg = config.static;
|
|
75
82
|
app.use(compression());
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
83
|
+
if (config.cors)
|
|
84
|
+
app.use(cors(config.cors));
|
|
85
|
+
app.use(express.json({ limit: config.payload?.limit || "1mb" }));
|
|
86
|
+
if (staticCfg?.dir) {
|
|
87
|
+
const staticDir = path.resolve(staticCfg.dir);
|
|
88
|
+
app.use(
|
|
89
|
+
express.static(staticDir, {
|
|
90
|
+
index: false,
|
|
91
|
+
// we'll control SPA index fallback manually
|
|
92
|
+
maxAge: staticCfg.maxAge ?? "1d"
|
|
93
|
+
})
|
|
94
|
+
);
|
|
95
|
+
if (staticCfg.spaFallback !== false) {
|
|
96
|
+
app.get("*", (req, res, next) => {
|
|
97
|
+
const url = req.originalUrl || req.url;
|
|
98
|
+
if (url.startsWith(apiPrefix))
|
|
99
|
+
return next();
|
|
100
|
+
if (req.method !== "GET")
|
|
101
|
+
return next();
|
|
102
|
+
const acceptsHtml = req.headers.accept?.includes("text/html");
|
|
103
|
+
if (!acceptsHtml)
|
|
104
|
+
return next();
|
|
105
|
+
return res.sendFile(path.join(staticDir, staticCfg.indexFile ?? "index.html"));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
app.use(apiPrefix, requestHandler(config));
|
|
110
|
+
if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
|
|
111
|
+
getPort().then((port) => {
|
|
112
|
+
app.listen(port, () => {
|
|
113
|
+
console.log(`[NATTYJS]:Server is running on port ${port}`);
|
|
114
|
+
});
|
|
88
115
|
});
|
|
89
116
|
return app;
|
|
90
117
|
}
|
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.51",
|
|
4
4
|
"description": "",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"author": "ajayojha <ojhaajay@outlook.com>",
|
|
@@ -16,11 +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/common": "0.0.1-beta.
|
|
23
|
-
"@nattyjs/types": "0.0.1-beta.
|
|
22
|
+
"@nattyjs/core": "0.0.1-beta.51",
|
|
23
|
+
"@nattyjs/common": "0.0.1-beta.51",
|
|
24
|
+
"@nattyjs/types": "0.0.1-beta.51"
|
|
24
25
|
},
|
|
25
26
|
"devDependencies": {
|
|
26
27
|
"@types/node": "20.3.1",
|