Skip to content

Commit 2fe5b98

Browse files
authored
Merge pull request #9 from CodeForBaltimore/revjtanton/issue-8
doc: filled out more documentation and tests
2 parents 0aa1bd2 + 4832ee4 commit 2fe5b98

File tree

6 files changed

+90
-38
lines changed

6 files changed

+90
-38
lines changed

.github/workflows/npm-publish.yml

+3-19
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88
types: [created]
99

1010
jobs:
11-
build:
11+
test:
1212
runs-on: ubuntu-latest
1313
steps:
1414
- uses: actions/checkout@v2
@@ -17,10 +17,9 @@ jobs:
1717
node-version: 12
1818
- run: npm ci
1919
- run: npm test
20-
- run: npm run build
2120

2221
publish-npm:
23-
needs: build
22+
needs: test
2423
runs-on: ubuntu-latest
2524
steps:
2625
- uses: actions/checkout@v2
@@ -29,22 +28,7 @@ jobs:
2928
node-version: 12
3029
registry-url: https://registry.npmjs.org/
3130
- run: npm ci
32-
- run: npm build
31+
- run: npm run build
3332
- run: npm publish
3433
env:
3534
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
36-
37-
publish-gpr:
38-
needs: build
39-
runs-on: ubuntu-latest
40-
steps:
41-
- uses: actions/checkout@v2
42-
- uses: actions/setup-node@v1
43-
with:
44-
node-version: 12
45-
registry-url: https://npm.pkg.github.com/
46-
- run: npm ci
47-
- run: npm build
48-
- run: npm publish
49-
env:
50-
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

CHANGELOG.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#### 1.0.2 (2021-03-08)
2+
3+
##### Bug Fixes
4+
5+
* removing github publish. updating README. (1ea94d4a)
6+
7+
##### Other Changes
8+
9+
* filled out more documentation and tests (71961ba7)
10+

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ exports.handler = async (event: APIGatewayProxyEvent) => {
5454

5555
### Using the `withStatusCode` function
5656

57-
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+
To use the `withStatusCode` you only _need_ to specify the response code when declaring the type of response. It is recommended to pass an approved origin for the request if applicable when calling that function. An example of a simple 200 response is as follows:
5858

5959
```
6060
import util from 'lambda-restful-util'
6161
...
62-
const ok = util.withStatusCode(200, 'http://localhost:8080')
62+
const ok = util.withStatusCode(200)
6363
6464
exports.handler = async (event: APIGatewayProxyEvent) => {
6565
...
66-
return ok('Hey Buddy!')
66+
return ok('Hey Buddy!', 'http://localhost:8080')
6767
}
6868
```
6969

@@ -77,7 +77,7 @@ If you know your response is going to be JSON this will simplify converting your
7777

7878
```
7979
...
80-
const ok = util.withStatusCode(util.HttpStatusCode.OK, 'http://localhost:8080, JSON.stringify)
80+
const ok = util.withStatusCode(util.HttpStatusCode.OK, JSON.stringify)
8181
...
8282
const res = {
8383
name: 'Homer Simpson'

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambda-restful-util",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "A lightweight utility for Lambda API development",
55
"repository": "[email protected]:CodeForBaltimore/lambda-restful-util.git",
66
"author": "Jason Anton <[email protected]>",

src/index.spec.ts

+30-4
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ describe('Validate package', function () {
88
it('withStatusCode invalid HTTP code', async function () {
99
expect(function () {
1010
const httpCode = 42
11-
app.withStatusCode(httpCode, 'http://localhost:8080')
11+
app.withStatusCode(httpCode)
1212
}).to.throw('status code out of range')
1313
})
14-
it('withStatusCode 200 HTTP code', async function () {
15-
const ok = app.withStatusCode(app.HttpStatusCode.OK, 'http://localhost:8080', JSON.stringify)
14+
it('withStatusCode 200 secure', async function () {
15+
const ok = app.withStatusCode(app.HttpStatusCode.OK, JSON.stringify)
16+
const bad = app.withStatusCode(app.HttpStatusCode.BAD_REQUEST, JSON.stringify)
1617
const example = {
1718
name: 'Homer Simpson',
1819
}
@@ -26,6 +27,31 @@ describe('Validate package', function () {
2627
body: '{"name":"Homer Simpson"}'
2728
}
2829

29-
expect(ok(example)).to.deep.equal(goodOutput)
30+
const insecureOutput: HttpResponse = {
31+
statusCode: 400,
32+
headers: {
33+
'Access-Control-Allow-Origin': '*',
34+
'Access-Control-Allow-Credentials': true,
35+
},
36+
body: '{"name":"Homer Simpson"}'
37+
}
38+
39+
expect(ok(example, 'http://localhost:8080')).to.deep.equal(goodOutput)
40+
expect(bad(example)).to.deep.equal(insecureOutput)
41+
})
42+
it('withStatusCode 400 insecure', async function () {
43+
const bad = app.withStatusCode(app.HttpStatusCode.BAD_REQUEST)
44+
const example = 'test'
45+
46+
const insecureOutput: HttpResponse = {
47+
statusCode: 400,
48+
headers: {
49+
'Access-Control-Allow-Origin': '*',
50+
'Access-Control-Allow-Credentials': true,
51+
},
52+
body: 'test'
53+
}
54+
55+
expect(bad(example)).to.deep.equal(insecureOutput)
3056
})
3157
})

src/index.ts

+42-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// index.ts
2+
/**
3+
* A AWS Lambda helper package
4+
*
5+
* @module
6+
*/
17
import HttpStatusCode from './HttpStatusCode'
28
import { APIGatewayProxyEvent } from 'aws-lambda'
39

@@ -12,19 +18,40 @@ export interface HttpResponse {
1218
}
1319

1420
/**
15-
* Will return fully formatted and ready
16-
* HTTP response for Lambda delivery
17-
* @param statusCode
18-
* @param origin
19-
* @param format
21+
* Will return fully formatted and ready HTTP response for Lambda delivery
22+
*
23+
* @param statusCode - An HTTP response code
24+
* @param format - If you need to parse your body send the parser here
25+
*
26+
* @example
27+
* Sets a function to return a 200 OK response
28+
* ```ts
29+
* const ok = util.withStatusCode(200, JSON.stringify)
30+
* const bad = util.withStatusCode(400)
31+
* ```
32+
*
33+
* @returns A function that can be called to send an HTTP response
2034
*/
21-
const withStatusCode = (statusCode: number, origin: string, format?: Function): Function => {
35+
const withStatusCode = (statusCode: number, format?: Function): Function => {
2236
if (100 > statusCode || statusCode > 599) {
2337
throw new Error('status code out of range')
2438
}
2539

26-
// return a function that will take some data and formats a response with a status code
27-
return (data: string | Record<string, unknown> | Array<any> | void): HttpResponse => {
40+
/**
41+
* The function that sends the HTTP response
42+
*
43+
* @param data - The information you are sending
44+
* @param origin - What domain can receive this response
45+
*
46+
* @example
47+
* Returns a JSON stringified var body to a localhost domain
48+
* ```ts
49+
* return ok(body, 'http://localhost')
50+
* ```
51+
*
52+
* @returns Formatted and parsed response
53+
*/
54+
return (data: string | Record<string, unknown> | Array<any> | void, origin = '*'): HttpResponse => {
2855
const response: HttpResponse = {
2956
statusCode: statusCode,
3057
headers: {
@@ -44,8 +71,11 @@ const withStatusCode = (statusCode: number, origin: string, format?: Function):
4471
}
4572

4673
/**
74+
* Ensuring the header exists in the API request and then parses it
75+
*
76+
* @param apiGatewayProxyEvent - The event coming from the API Gateway request
4777
*
48-
* @param apiGatewayProxyEvent
78+
* @returns The headers parsed into a Object
4979
*/
5080
const validateAndParseRequestHeaders = (apiGatewayProxyEvent: APIGatewayProxyEvent): Record<string, unknown> | null => {
5181
if (apiGatewayProxyEvent !== null && apiGatewayProxyEvent.headers !== null && apiGatewayProxyEvent.headers !== undefined) {
@@ -58,8 +88,10 @@ const validateAndParseRequestHeaders = (apiGatewayProxyEvent: APIGatewayProxyEve
5888
}
5989

6090
/**
91+
* Ensuring the body eixists in the API request and then parses it
92+
* @param apiGatewayProxyEvent - The event coming from the API Gateway request
6193
*
62-
* @param apiGatewayProxyEvent
94+
* @returns The body parsed into an object
6395
*/
6496
const validateAndParseRequestBody = (apiGatewayProxyEvent: APIGatewayProxyEvent): string | null => {
6597
if (apiGatewayProxyEvent !== null && apiGatewayProxyEvent.body !== null && apiGatewayProxyEvent.body !== undefined) {

0 commit comments

Comments
 (0)