Skip to content

Commit 71961ba

Browse files
committed
doc: filled out more documentation and tests
1 parent 902cf03 commit 71961ba

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
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": "git@github.com:CodeForBaltimore/lambda-restful-util.git",
66
"author": "Jason Anton <rev.jt.anton@gmail.com>",

src/index.spec.ts

Lines changed: 30 additions & 4 deletions
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

Lines changed: 42 additions & 10 deletions
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)