Skip to content

Commit 3d39092

Browse files
committed
adding some instructions to README
1 parent fbbbb98 commit 3d39092

File tree

1 file changed

+50
-4
lines changed

1 file changed

+50
-4
lines changed

README.md

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ A simple package to offer some quick wins for API devs!
1010
- [Documentation](#documentation)
1111
- [Setup](#setup)
1212
- [Using this product](#using-this-product)
13-
- [Using the headerParser or bodyParser](#using-the-headerparser-or-bodyparser)
13+
- [Using the validateAndParseRequestHeaders or validateAndParseRequestBody](#using-the-validateandparserequestheaders-or-validateandparserequestbody)
1414
- [Using the withStatusCode function](#using-the-withstatuscode-function)
1515
- [Testing](#testing)
1616
- [Contributors ✨](#contributors-)
@@ -34,13 +34,59 @@ Additionally this project uses yarn instead of npm. Please ensure you have yarn
3434

3535
To use this package in your work simply run `npm install lambda-restful-util` or `yarn add lambda-restful-util` then include it in your code as with any other dependency.
3636

37-
### Using the `headerParser` or `bodyParser`
37+
### Using the `validateAndParseRequestHeaders` or `validateAndParseRequestBody`
3838

39-
<add to this eventually>
39+
Both the `validateAndParseRequestHeaders` and `validateAndParseRequestBody` operate very similarly. Simply pass the `event` from API Gateway and both return a truthy object you can use if they're valid. For example:
40+
41+
```
42+
exports.handler = async (event: APIGatewayProxyEvent) => {
43+
const requestHeaders = utils.validateAndParseRequestHeaders(event)
44+
const requestBody = utils.validateAndParseRequestBody(event)
45+
46+
if (requestHeaders.Authorization && requestBody) {
47+
const token = requestHeaders.Authorization.replace('Bearer ', '')
48+
...
49+
}
50+
...
51+
}
52+
```
4053

4154
### Using the `withStatusCode` function
4255

43-
<add to this>
56+
To use the `withStatusCode` you only _need_ to specify the response code and the request origin (for CORS). An example of a simple 200 response is as follows:
57+
58+
```
59+
import util from 'lambda-restful-util'
60+
...
61+
const ok = util.withStatusCode(200, 'http://localhost:8080')
62+
63+
exports.handler = async (event: APIGatewayProxyEvent) => {
64+
...
65+
return ok('Hey Buddy!')
66+
}
67+
```
68+
69+
For convenience this package includes every HTTP response for reference. To use the `HttpStatusCode` enum you can modify the above example by modifying the var: `const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080)`.
70+
71+
#### Adding a formatter
72+
73+
In addition to the `HttpStatusCode` you can pass a formatting function as an optional argument to `withStatusCode`. To add `JSON.stringify` simply modify the var again: `const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080, JSON.stringify)`.
74+
75+
If you know your response is going to be JSON this will simplify converting your Object to JSON. For example:
76+
77+
```
78+
...
79+
const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080, JSON.stringify)
80+
...
81+
const res = {
82+
name: 'Homer Simpson'
83+
employer: 'Springfield Power Plant'
84+
}
85+
...
86+
return ok(res)
87+
```
88+
89+
The above will correctly return a JSON string as the 200 HTTP response to your API request. Consequently if you send `return ok('test')` that _will also_ return a JSON 200 response. If you **do not** want to return JSON simply don't pass a formatting argument when declaring the `ok` response.
4490

4591
## Testing
4692

0 commit comments

Comments
 (0)