@nattyjs/express 0.0.1-beta.0
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/CHANGELOG.md +8 -0
- package/README.md +11 -0
- package/build.config.ts +14 -0
- package/functions/get-request-body.ts +14 -0
- package/functions/get-response-body.ts +22 -0
- package/functions/parse-cookies.ts +13 -0
- package/functions/request-handler.ts +21 -0
- package/index.ts +1 -0
- package/module/express-module.ts +22 -0
- package/package.json +25 -0
- package/tsconfig.build.json +10 -0
- package/tsconfig.json +13 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
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/README.md
ADDED
package/build.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
})
|
|
@@ -0,0 +1,14 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./module/express-module"
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nattyjs/express",
|
|
3
|
+
"version": "0.0.1-beta.0",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"author": "ajayojha <ojhaajay@outlook.com>",
|
|
7
|
+
"license": "ISC",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"main": "./dist/index.cjs",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "unbuild"
|
|
13
|
+
},
|
|
14
|
+
"dependencies": {
|
|
15
|
+
"express": "4.18.2",
|
|
16
|
+
"cors": "2.8.5",
|
|
17
|
+
"compression": "1.7.4",
|
|
18
|
+
"@nattyjs/core": "0.0.1-beta.0",
|
|
19
|
+
"@nattyjs/types": "0.0.1-beta.0"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@types/node": "20.3.1",
|
|
23
|
+
"unbuild": "1.2.1"
|
|
24
|
+
}
|
|
25
|
+
}
|