@nattyjs/express 0.0.1-beta.0 → 0.0.1-beta.1

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 ADDED
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ const express = require('express');
4
+ const cors = require('cors');
5
+ const compression = require('compression');
6
+ const core = require('@nattyjs/core');
7
+ const common = require('@nattyjs/common');
8
+
9
+ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; }
10
+
11
+ const express__default = /*#__PURE__*/_interopDefaultCompat(express);
12
+ const cors__default = /*#__PURE__*/_interopDefaultCompat(cors);
13
+ const compression__default = /*#__PURE__*/_interopDefaultCompat(compression);
14
+
15
+ async function getRequestBodyInfo(request) {
16
+ const contentType = request.headers["content-type"];
17
+ const bodyInfo = {};
18
+ if (request.method !== common.GET)
19
+ switch (contentType) {
20
+ case "application/json":
21
+ bodyInfo.json = request.body;
22
+ break;
23
+ }
24
+ return bodyInfo;
25
+ }
26
+
27
+ function getResponse(expressResponse, responseInfo) {
28
+ if (responseInfo.headers)
29
+ Object.keys(responseInfo.headers).forEach((key) => expressResponse.set(key, responseInfo.headers[key]));
30
+ if (responseInfo.cookies)
31
+ responseInfo.cookies.forEach((cookie) => {
32
+ const name = cookie.name;
33
+ const value = cookie.value;
34
+ delete cookie.name;
35
+ delete cookie.value;
36
+ expressResponse.cookie(name, value, { ...cookie });
37
+ });
38
+ expressResponse.statusCode = responseInfo.status;
39
+ if (responseInfo.body && responseInfo.body.json) {
40
+ expressResponse.send(responseInfo.body.json);
41
+ } else
42
+ expressResponse.send();
43
+ return expressResponse;
44
+ }
45
+
46
+ function parseCookies(value) {
47
+ const jsonCookies = new Array();
48
+ if (value) {
49
+ const cookies = value.split(";");
50
+ for (const cookie of cookies) {
51
+ const splitCookie = cookie.split("=");
52
+ jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
53
+ }
54
+ }
55
+ return jsonCookies;
56
+ }
57
+
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
+ });
75
+ }
76
+
77
+ const app = express__default();
78
+ const ExpressModule = {
79
+ init(config) {
80
+ 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
+ app.listen(3e3, () => {
91
+ console.log("Server is running on port 3000");
92
+ });
93
+ }
94
+ };
95
+
96
+ exports.ExpressModule = ExpressModule;
@@ -0,0 +1,3 @@
1
+ declare const ExpressModule: any;
2
+
3
+ export { ExpressModule };
package/dist/index.mjs ADDED
@@ -0,0 +1,88 @@
1
+ import express from 'express';
2
+ import cors from 'cors';
3
+ import compression from 'compression';
4
+ import { HttpHandler, HttpContext } from '@nattyjs/core';
5
+ import { GET } from '@nattyjs/common';
6
+
7
+ async function getRequestBodyInfo(request) {
8
+ const contentType = request.headers["content-type"];
9
+ const bodyInfo = {};
10
+ if (request.method !== GET)
11
+ switch (contentType) {
12
+ case "application/json":
13
+ bodyInfo.json = request.body;
14
+ break;
15
+ }
16
+ return bodyInfo;
17
+ }
18
+
19
+ function getResponse(expressResponse, responseInfo) {
20
+ if (responseInfo.headers)
21
+ Object.keys(responseInfo.headers).forEach((key) => expressResponse.set(key, responseInfo.headers[key]));
22
+ if (responseInfo.cookies)
23
+ responseInfo.cookies.forEach((cookie) => {
24
+ const name = cookie.name;
25
+ const value = cookie.value;
26
+ delete cookie.name;
27
+ delete cookie.value;
28
+ expressResponse.cookie(name, value, { ...cookie });
29
+ });
30
+ expressResponse.statusCode = responseInfo.status;
31
+ if (responseInfo.body && responseInfo.body.json) {
32
+ expressResponse.send(responseInfo.body.json);
33
+ } else
34
+ expressResponse.send();
35
+ return expressResponse;
36
+ }
37
+
38
+ function parseCookies(value) {
39
+ const jsonCookies = new Array();
40
+ if (value) {
41
+ const cookies = value.split(";");
42
+ for (const cookie of cookies) {
43
+ const splitCookie = cookie.split("=");
44
+ jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() });
45
+ }
46
+ }
47
+ return jsonCookies;
48
+ }
49
+
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
+ });
67
+ }
68
+
69
+ const app = express();
70
+ const ExpressModule = {
71
+ init(config) {
72
+ 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
+ app.listen(3e3, () => {
83
+ console.log("Server is running on port 3000");
84
+ });
85
+ }
86
+ };
87
+
88
+ export { ExpressModule };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nattyjs/express",
3
- "version": "0.0.1-beta.0",
3
+ "version": "0.0.1-beta.1",
4
4
  "description": "",
5
5
  "keywords": [],
6
6
  "author": "ajayojha <ojhaajay@outlook.com>",
@@ -8,6 +8,9 @@
8
8
  "module": "./dist/index.mjs",
9
9
  "main": "./dist/index.cjs",
10
10
  "types": "./dist/index.d.ts",
11
+ "files": [
12
+ "dist"
13
+ ],
11
14
  "scripts": {
12
15
  "build": "unbuild"
13
16
  },
@@ -15,8 +18,8 @@
15
18
  "express": "4.18.2",
16
19
  "cors": "2.8.5",
17
20
  "compression": "1.7.4",
18
- "@nattyjs/core": "0.0.1-beta.0",
19
- "@nattyjs/types": "0.0.1-beta.0"
21
+ "@nattyjs/core": "0.0.1-beta.1",
22
+ "@nattyjs/types": "0.0.1-beta.1"
20
23
  },
21
24
  "devDependencies": {
22
25
  "@types/node": "20.3.1",
package/CHANGELOG.md DELETED
@@ -1,8 +0,0 @@
1
- ## <small>0.0.1-beta.0 (2023-08-30)</small>
2
-
3
- * add new package '@nattyjs/express' ([efcebbb](https://github.com/nattyjs/nattyjs/commit/efcebbb))
4
- * cors ([a9f9580](https://github.com/nattyjs/nattyjs/commit/a9f9580))
5
- * fix(express): handle exceptions and parse response body ([e4f391d](https://github.com/nattyjs/nattyjs/commit/e4f391d))
6
-
7
-
8
-
package/build.config.ts DELETED
@@ -1,14 +0,0 @@
1
- import { defineBuildConfig } from 'unbuild'
2
-
3
- export default defineBuildConfig({
4
- declaration: true,
5
- rollup: {
6
- inlineDependencies: false,
7
- emitCJS:true
8
- },
9
- entries: [
10
- './index'
11
- ],
12
- externals:['@nattyjs/common','@nattyjs/core',"@nattyjs/types","@azure/functions"]
13
-
14
- })
@@ -1,14 +0,0 @@
1
- import { GET } from "@nattyjs/common";
2
- import { HttpRequestBodyInfo } from "@nattyjs/types";
3
-
4
- export async function getRequestBodyInfo(request) {
5
- const contentType = request.headers['content-type'];
6
- const bodyInfo: HttpRequestBodyInfo = {}
7
- if (request.method !== GET)
8
- switch (contentType) {
9
- case "application/json":
10
- bodyInfo.json = request.body;
11
- break;
12
- }
13
- return bodyInfo;
14
- }
@@ -1,22 +0,0 @@
1
- import { HttpResponse } from "@nattyjs/core";
2
- export function getResponse(expressResponse:any,responseInfo: HttpResponse) {
3
- let response:any = {};
4
-
5
- if(responseInfo.headers)
6
- Object.keys(responseInfo.headers).forEach(key=>expressResponse.set(key,responseInfo.headers[key]));
7
- if(responseInfo.cookies)
8
- responseInfo.cookies.forEach(cookie=>{
9
- const name = cookie.name;
10
- const value = cookie.value;
11
- delete cookie.name;
12
- delete cookie.value;
13
- expressResponse.cookie(name,value,{...cookie})
14
- })
15
- expressResponse.statusCode = responseInfo.status;
16
- if (responseInfo.body && responseInfo.body.json) {
17
- expressResponse.send(responseInfo.body.json)
18
- }else
19
- expressResponse.send();
20
- return expressResponse;
21
- }
22
-
@@ -1,13 +0,0 @@
1
- import { Cookie } from "@nattyjs/types";
2
-
3
- export function parseCookies(value: string) {
4
- const jsonCookies = new Array<Cookie>();
5
- if (value) {
6
- const cookies = value.split(";");
7
- for (const cookie of cookies) {
8
- const splitCookie = cookie.split('=');
9
- jsonCookies.push({ name: splitCookie[0].trim(), value: splitCookie[1].trim() })
10
- }
11
- }
12
- return jsonCookies;
13
- }
@@ -1,21 +0,0 @@
1
- import { HttpContext ,HttpHandler, HttpResponse } from "@nattyjs/core";
2
- import { getRequestBodyInfo } from "./get-request-body";
3
- import { getResponse } from "./get-response-body";
4
- import { parseCookies } from "./parse-cookies";
5
- export async function requestHandler(request, response):Promise<any> {
6
- const httpHandler = new HttpHandler();
7
- const httpContext = new HttpContext(
8
- {
9
- url:`http://localhost:3000${request.url}` ,
10
- method:request.method,
11
- body:await getRequestBodyInfo(request),
12
- headers:request.headers,
13
- cookies:parseCookies(request.headers["cookie"])
14
- },response)
15
- return httpHandler.processRequest(httpContext).then(httpResponse=>{
16
- return getResponse(response,httpResponse);
17
- }).catch(t=>
18
- {
19
- response.send();
20
- })
21
- }
package/index.ts DELETED
@@ -1 +0,0 @@
1
- export * from "./module/express-module"
@@ -1,22 +0,0 @@
1
- import express from 'express';
2
- import cors from 'cors'
3
- import compression from 'compression';
4
- import { requestHandler } from '../functions/request-handler';
5
- const app = express();
6
- export const ExpressModule: any = {
7
- init(config: any) {
8
- app.use(compression());
9
- app.use(cors({
10
- "origin":[ "http://localhost:5173","http://localhost:5174"],
11
- "methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
12
- "preflightContinue": false,
13
- "optionsSuccessStatus": 204,
14
- "credentials":true
15
- }));
16
- app.use(express.json()) ;
17
- app.all('*', requestHandler);
18
- app.listen(3000, () => {
19
- console.log('Server is running on port 3000');
20
- });
21
- }
22
- }
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../tsconfig.build.json",
3
- "compilerOptions": {
4
- "outDir": ".",
5
- "rootDir": ".",
6
- "paths": {}
7
- },
8
- "exclude": ["node_modules", "dist", "test/**/*", "*.spec.ts"],
9
- "references": []
10
- }
package/tsconfig.json DELETED
@@ -1,13 +0,0 @@
1
- {
2
- "extends": "../tsconfig.build.json",
3
- "compilerOptions": {
4
- "types": ["node"]
5
- },
6
- "files": [],
7
- "include": [],
8
- "references": [
9
- {
10
- "path": "./tsconfig.build.json"
11
- }
12
- ]
13
- }