@nattyjs/express 0.0.1-beta.5 → 0.0.1-beta.50

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 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
- switch (contentType) {
20
- case "application/json":
21
- bodyInfo.json = request.body;
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
- Object.keys(responseInfo.headers).forEach((key) => expressResponse.set(key, responseInfo.headers[key]));
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
- jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
56
+ if (splitCookie.length > 1)
57
+ jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
53
58
  }
54
59
  }
55
60
  return jsonCookies;
@@ -72,7 +77,7 @@ function requestHandler(config) {
72
77
  return httpHandler.processRequest(httpContext).then((httpResponse) => {
73
78
  return getResponse(response, httpResponse);
74
79
  }).catch((t) => {
75
- response.send();
80
+ return getResponse(response, t.httpResponse);
76
81
  });
77
82
  };
78
83
  }
@@ -80,19 +85,41 @@ function requestHandler(config) {
80
85
  const app = express__default();
81
86
  const ExpressModule = {
82
87
  init(config) {
88
+ const apiPrefix = `/${common.commonContainer.nattyConfig.api?.rootPath}`;
89
+ const staticCfg = config.static;
83
90
  app.use(compression__default());
84
- app.use(cors__default({
85
- "origin": ["http://localhost:5173", "http://localhost:5174"],
86
- "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
87
- "preflightContinue": false,
88
- "optionsSuccessStatus": 204,
89
- "credentials": true
90
- }));
91
- app.use(express__default.json());
92
- app.all("*", requestHandler(config));
93
- if (config.autoGeneratePort == true)
94
- app.listen(3e3, () => {
95
- console.log("Server is running on port 3000 ");
91
+ if (config.cors)
92
+ app.use(cors__default(config.cors));
93
+ app.use(express__default.json({ limit: config.payload?.limit || "1mb" }));
94
+ if (staticCfg?.dir) {
95
+ const staticDir = path__default.resolve(staticCfg.dir);
96
+ app.use(
97
+ express__default.static(staticDir, {
98
+ index: false,
99
+ // we'll control SPA index fallback manually
100
+ maxAge: staticCfg.maxAge ?? "1d"
101
+ })
102
+ );
103
+ if (staticCfg.spaFallback !== false) {
104
+ app.get("*", (req, res, next) => {
105
+ const url = req.originalUrl || req.url;
106
+ if (url.startsWith(apiPrefix))
107
+ return next();
108
+ if (req.method !== "GET")
109
+ return next();
110
+ const acceptsHtml = req.headers.accept?.includes("text/html");
111
+ if (!acceptsHtml)
112
+ return next();
113
+ return res.sendFile(path__default.join(staticDir, staticCfg.indexFile ?? "index.html"));
114
+ });
115
+ }
116
+ }
117
+ app.use(apiPrefix, requestHandler(config));
118
+ if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
119
+ common.getPort().then((port) => {
120
+ app.listen(port, () => {
121
+ console.log(`[NATTYJS]:Server is running on port ${port}`);
122
+ });
96
123
  });
97
124
  return app;
98
125
  }
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, FrameworkType, commonContainer, 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
- switch (contentType) {
12
- case "application/json":
13
- bodyInfo.json = request.body;
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
- Object.keys(responseInfo.headers).forEach((key) => expressResponse.set(key, responseInfo.headers[key]));
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
- jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
47
+ if (splitCookie.length > 1)
48
+ jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
45
49
  }
46
50
  }
47
51
  return jsonCookies;
@@ -64,7 +68,7 @@ function requestHandler(config) {
64
68
  return httpHandler.processRequest(httpContext).then((httpResponse) => {
65
69
  return getResponse(response, httpResponse);
66
70
  }).catch((t) => {
67
- response.send();
71
+ return getResponse(response, t.httpResponse);
68
72
  });
69
73
  };
70
74
  }
@@ -72,19 +76,41 @@ function requestHandler(config) {
72
76
  const app = express();
73
77
  const ExpressModule = {
74
78
  init(config) {
79
+ const apiPrefix = `/${commonContainer.nattyConfig.api?.rootPath}`;
80
+ const staticCfg = config.static;
75
81
  app.use(compression());
76
- app.use(cors({
77
- "origin": ["http://localhost:5173", "http://localhost:5174"],
78
- "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
79
- "preflightContinue": false,
80
- "optionsSuccessStatus": 204,
81
- "credentials": true
82
- }));
83
- app.use(express.json());
84
- app.all("*", requestHandler(config));
85
- if (config.autoGeneratePort == true)
86
- app.listen(3e3, () => {
87
- console.log("Server is running on port 3000 ");
82
+ if (config.cors)
83
+ app.use(cors(config.cors));
84
+ app.use(express.json({ limit: config.payload?.limit || "1mb" }));
85
+ if (staticCfg?.dir) {
86
+ const staticDir = path.resolve(staticCfg.dir);
87
+ app.use(
88
+ express.static(staticDir, {
89
+ index: false,
90
+ // we'll control SPA index fallback manually
91
+ maxAge: staticCfg.maxAge ?? "1d"
92
+ })
93
+ );
94
+ if (staticCfg.spaFallback !== false) {
95
+ app.get("*", (req, res, next) => {
96
+ const url = req.originalUrl || req.url;
97
+ if (url.startsWith(apiPrefix))
98
+ return next();
99
+ if (req.method !== "GET")
100
+ return next();
101
+ const acceptsHtml = req.headers.accept?.includes("text/html");
102
+ if (!acceptsHtml)
103
+ return next();
104
+ return res.sendFile(path.join(staticDir, staticCfg.indexFile ?? "index.html"));
105
+ });
106
+ }
107
+ }
108
+ app.use(apiPrefix, requestHandler(config));
109
+ if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
110
+ getPort().then((port) => {
111
+ app.listen(port, () => {
112
+ console.log(`[NATTYJS]:Server is running on port ${port}`);
113
+ });
88
114
  });
89
115
  return app;
90
116
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/express",
3
- "version": "0.0.1-beta.5",
3
+ "version": "0.0.1-beta.50",
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.5",
22
- "@nattyjs/common": "0.0.1-beta.5",
23
- "@nattyjs/types": "0.0.1-beta.5"
22
+ "@nattyjs/core": "0.0.1-beta.50",
23
+ "@nattyjs/common": "0.0.1-beta.50",
24
+ "@nattyjs/types": "0.0.1-beta.50"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/node": "20.3.1",