You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+50-4Lines changed: 50 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ A simple package to offer some quick wins for API devs!
10
10
-[Documentation](#documentation)
11
11
-[Setup](#setup)
12
12
-[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)
14
14
-[Using the withStatusCode function](#using-the-withstatuscode-function)
15
15
-[Testing](#testing)
16
16
-[Contributors ✨](#contributors-)
@@ -34,13 +34,59 @@ Additionally this project uses yarn instead of npm. Please ensure you have yarn
34
34
35
35
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.
36
36
37
-
### Using the `headerParser` or `bodyParser`
37
+
### Using the `validateAndParseRequestHeaders` or `validateAndParseRequestBody`
38
38
39
-
<addtothiseventually>
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:
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')
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.
0 commit comments