Skip to content

Commit 003d60e

Browse files
committed
feat: Now you can use with body-parser
1 parent ef3f4ab commit 003d60e

File tree

6 files changed

+174
-20
lines changed

6 files changed

+174
-20
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ you'll have this:
2828

2929
```js
3030
const { use } = require('lambda-expressless');
31+
const bodyParser = require('body-parser');
3132

32-
exports.handler = use((req, res) => {
33+
exports.handler = use(bodyParser.json(), (req, res) => {
3334
res.status(201).json({
3435
success: false,
3536
data: req.body.id
@@ -59,6 +60,11 @@ const getUser = (req, res) => {
5960

6061
exports.handler = use(checkUser, getUser);
6162
```
63+
64+
You can use many popular Express Middlewares. Some examples are:
65+
66+
- [body-parser](https://github.com/expressjs/body-parser)
67+
6268
## Installation
6369

6470
```npm i lambda-expressless```
@@ -73,8 +79,15 @@ Properties:
7379

7480
| Property | Notes |
7581
|-------------|-------|
76-
| [body](https://expressjs.com/en/4x/api.html#req.body) | - |
82+
| [body](https://expressjs.com/en/4x/api.html#req.body) | You need to use [body-parser](https://github.com/expressjs/body-parser) |
83+
| [hostname](https://expressjs.com/en/4x/api.html#req.hostname) | - |
84+
| [host](https://expressjs.com/en/4x/api.html#req.host) | - |
85+
| [xhr](https://expressjs.com/en/4x/api.html#req.xhr) | - |
86+
| [ip](https://expressjs.com/en/4x/api.html#req.ip) | - |
87+
| [ips](https://expressjs.com/en/4x/api.html#req.ips) | - |
7788
| [path](https://expressjs.com/en/4x/api.html#req.path) | - |
89+
| [protocol](https://expressjs.com/en/4x/api.html#req.protocol) | - |
90+
| [secure](https://expressjs.com/en/4x/api.html#req.secure) | - |
7891
| [method](https://expressjs.com/en/4x/api.html#req.method) | - |
7992
| [query](https://expressjs.com/en/4x/api.html#req.query) | Doesn't include repeated query parameters. |
8093
| [params](https://expressjs.com/en/4x/api.html#req.params) | - |

package-lock.json

Lines changed: 132 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"homepage": "https://github.com/muratcorlu/lambda-expressless#readme",
2222
"devDependencies": {
23+
"body-parser": "^1.19.0",
2324
"jest": "^24.9.0",
2425
"semantic-release": "^15.13.21"
2526
}

src/lambda-wrapper.spec.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { use } = require('./lambda-wrapper');
2+
const bodyParser = require('body-parser');
23

34
describe('Lambda Wrapper', () => {
45
const proxyRequest = {
@@ -54,7 +55,7 @@ describe('Lambda Wrapper', () => {
5455

5556
it('should handle json body on a post request', () => {
5657

57-
const lambdaHandler = use((req, res) => {
58+
const lambdaHandler = use(bodyParser.json(), (req, res) => {
5859
res.json(req.body);
5960
});
6061

@@ -63,7 +64,8 @@ describe('Lambda Wrapper', () => {
6364
const proxyRequest = {
6465
body: requestObject,
6566
headers: {
66-
'Content-Type': 'application/json'
67+
'Content-Type': 'application/json',
68+
'Content-Length': requestObject.length
6769
},
6870
multiValueHeaders: {},
6971
httpMethod: 'POST',

src/request.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
1+
const ReadableStream = require('stream').Readable;
2+
13
/**
24
*
35
*/
4-
class Request {
6+
class Request extends ReadableStream {
57
constructor(event) {
6-
this.method = event.httpMethod;
7-
this.query = event.queryStringParameters;
8-
this.path = event.path;
9-
this.params = event.pathParameters;
8+
super();
109
this.headers = Object.keys(event.headers).reduce((headers, key) => {
1110
headers[key.toLowerCase()] = event.headers[key];
1211
return headers;
1312
}, {});
13+
this.hostname = this.headers.host
14+
this.method = event.httpMethod;
15+
this.query = event.queryStringParameters;
16+
this.path = event.path;
17+
this.params = event.pathParameters;
18+
19+
this.protocol = this.get('X-Forwarded-Proto')
20+
this.secure = this.protocol === 'https';
21+
this.ips = (this.get('X-Forwarded-For') || '').split(', ');
22+
this.ip = this.ips[0];
23+
this.host = this.get('X-Forwarded-Host') || this.hostname;
24+
this.xhr = (this.get('X-Requested-With') || '').toLowerCase() === 'xmlhttprequest';
25+
26+
this.event = event;
1427

15-
this.body = event.body;
16-
if (this.is('json')) {
17-
this.body = JSON.parse(event.body);
18-
}
28+
this.push(event.body);
29+
this.push(null);
1930
}
2031

2132
get(key) {

src/request.spec.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ describe('Request object', () => {
55
const event = {
66
body: JSON.stringify(requestObject),
77
headers: {
8-
'Content-Type': 'application/json'
8+
'Content-Type': 'application/json',
9+
'Content-Length': JSON.stringify(requestObject).length
910
},
1011
multiValueHeaders: {},
1112
httpMethod: 'POST',
@@ -19,12 +20,6 @@ describe('Request object', () => {
1920
resource: ''
2021
};
2122

22-
it('read body', () => {
23-
const request = new Request(event);
24-
25-
expect(request.body).toEqual(requestObject);
26-
});
27-
2823
it('should read header', () => {
2924
const request = new Request(event);
3025

0 commit comments

Comments
 (0)