@nattyjs/express 0.0.1-beta.3 → 0.0.1-beta.31

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
@@ -26,7 +26,9 @@ async function getRequestBodyInfo(request) {
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
  }
@@ -55,42 +59,43 @@ function parseCookies(value) {
55
59
  return jsonCookies;
56
60
  }
57
61
 
58
- async function requestHandler(request, response) {
59
- const httpHandler = new core.HttpHandler();
60
- const httpContext = new core.HttpContext(
61
- {
62
- url: `http://localhost:3000${request.url}`,
63
- method: request.method,
64
- body: await getRequestBodyInfo(request),
65
- headers: request.headers,
66
- cookies: parseCookies(request.headers["cookie"])
67
- },
68
- response
69
- );
70
- return httpHandler.processRequest(httpContext).then((httpResponse) => {
71
- return getResponse(response, httpResponse);
72
- }).catch((t) => {
73
- response.send();
74
- });
62
+ function requestHandler(config) {
63
+ return async (request, response) => {
64
+ const httpHandler = new core.HttpHandler();
65
+ const url = config.framework == common.FrameworkType.Firebase ? `http://localhost:3000/api${request.url}` : `http://localhost:3000${request.url}`;
66
+ const httpContext = new core.HttpContext(
67
+ {
68
+ url,
69
+ method: request.method,
70
+ body: await getRequestBodyInfo(request),
71
+ headers: request.headers,
72
+ cookies: parseCookies(request.headers["cookie"])
73
+ },
74
+ response
75
+ );
76
+ return httpHandler.processRequest(httpContext).then((httpResponse) => {
77
+ return getResponse(response, httpResponse);
78
+ }).catch((t) => {
79
+ return getResponse(response, t.httpResponse);
80
+ });
81
+ };
75
82
  }
76
83
 
77
84
  const app = express__default();
78
85
  const ExpressModule = {
79
86
  init(config) {
80
87
  app.use(compression__default());
81
- app.use(cors__default({
82
- "origin": ["http://localhost:5173", "http://localhost:5174"],
83
- "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
84
- "preflightContinue": false,
85
- "optionsSuccessStatus": 204,
86
- "credentials": true
87
- }));
88
- app.use(express__default.json());
89
- app.all("*", requestHandler);
90
- if (config.autoGeneratePort == true)
91
- app.listen(3e3, () => {
92
- console.log("Server is running on port 3000 ");
88
+ if (config.cors)
89
+ app.use(cors__default(config.cors));
90
+ app.use(express__default.json({ limit: config.payload?.limit || "1mb" }));
91
+ app.all("*", requestHandler(config));
92
+ if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
93
+ common.getPort().then((port) => {
94
+ app.listen(port, () => {
95
+ console.log(`[NATTYJS]:Server is running on port ${port}`);
96
+ });
93
97
  });
98
+ return app;
94
99
  }
95
100
  };
96
101
 
package/dist/index.mjs CHANGED
@@ -2,7 +2,7 @@ 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"];
@@ -18,7 +18,9 @@ async function getRequestBodyInfo(request) {
18
18
 
19
19
  function getResponse(expressResponse, responseInfo) {
20
20
  if (responseInfo.headers)
21
- Object.keys(responseInfo.headers).forEach((key) => expressResponse.set(key, responseInfo.headers[key]));
21
+ for (const pair of responseInfo.headers.entries()) {
22
+ expressResponse.setHeader(pair[0], pair[1]);
23
+ }
22
24
  if (responseInfo.cookies)
23
25
  responseInfo.cookies.forEach((cookie) => {
24
26
  const name = cookie.name;
@@ -30,7 +32,9 @@ function getResponse(expressResponse, responseInfo) {
30
32
  expressResponse.statusCode = responseInfo.status;
31
33
  if (responseInfo.body && responseInfo.body.json) {
32
34
  expressResponse.send(responseInfo.body.json);
33
- } else
35
+ } else if (responseInfo.body && responseInfo.body.buffer)
36
+ expressResponse.send(responseInfo.body.buffer);
37
+ else
34
38
  expressResponse.send();
35
39
  return expressResponse;
36
40
  }
@@ -47,42 +51,43 @@ function parseCookies(value) {
47
51
  return jsonCookies;
48
52
  }
49
53
 
50
- async function requestHandler(request, response) {
51
- const httpHandler = new HttpHandler();
52
- const httpContext = new HttpContext(
53
- {
54
- url: `http://localhost:3000${request.url}`,
55
- method: request.method,
56
- body: await getRequestBodyInfo(request),
57
- headers: request.headers,
58
- cookies: parseCookies(request.headers["cookie"])
59
- },
60
- response
61
- );
62
- return httpHandler.processRequest(httpContext).then((httpResponse) => {
63
- return getResponse(response, httpResponse);
64
- }).catch((t) => {
65
- response.send();
66
- });
54
+ function requestHandler(config) {
55
+ return async (request, response) => {
56
+ const httpHandler = new HttpHandler();
57
+ const url = config.framework == FrameworkType.Firebase ? `http://localhost:3000/api${request.url}` : `http://localhost:3000${request.url}`;
58
+ const httpContext = new HttpContext(
59
+ {
60
+ url,
61
+ method: request.method,
62
+ body: await getRequestBodyInfo(request),
63
+ headers: request.headers,
64
+ cookies: parseCookies(request.headers["cookie"])
65
+ },
66
+ response
67
+ );
68
+ return httpHandler.processRequest(httpContext).then((httpResponse) => {
69
+ return getResponse(response, httpResponse);
70
+ }).catch((t) => {
71
+ return getResponse(response, t.httpResponse);
72
+ });
73
+ };
67
74
  }
68
75
 
69
76
  const app = express();
70
77
  const ExpressModule = {
71
78
  init(config) {
72
79
  app.use(compression());
73
- app.use(cors({
74
- "origin": ["http://localhost:5173", "http://localhost:5174"],
75
- "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
76
- "preflightContinue": false,
77
- "optionsSuccessStatus": 204,
78
- "credentials": true
79
- }));
80
- app.use(express.json());
81
- app.all("*", requestHandler);
82
- if (config.autoGeneratePort == true)
83
- app.listen(3e3, () => {
84
- console.log("Server is running on port 3000 ");
80
+ if (config.cors)
81
+ app.use(cors(config.cors));
82
+ app.use(express.json({ limit: config.payload?.limit || "1mb" }));
83
+ app.all("*", requestHandler(config));
84
+ if (config.autoGeneratePort == true || config.autoGeneratePort == void 0)
85
+ getPort().then((port) => {
86
+ app.listen(port, () => {
87
+ console.log(`[NATTYJS]:Server is running on port ${port}`);
88
+ });
85
89
  });
90
+ return app;
86
91
  }
87
92
  };
88
93
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/express",
3
- "version": "0.0.1-beta.3",
3
+ "version": "0.0.1-beta.31",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "ajayojha <ojhaajay@outlook.com>",
@@ -18,8 +18,9 @@
18
18
  "express": "4.18.2",
19
19
  "cors": "2.8.5",
20
20
  "compression": "1.7.4",
21
- "@nattyjs/core": "0.0.1-beta.3",
22
- "@nattyjs/types": "0.0.1-beta.3"
21
+ "@nattyjs/core": "0.0.1-beta.31",
22
+ "@nattyjs/common": "0.0.1-beta.31",
23
+ "@nattyjs/types": "0.0.1-beta.31"
23
24
  },
24
25
  "devDependencies": {
25
26
  "@types/node": "20.3.1",