1
+ // index.ts
2
+ /**
3
+ * A AWS Lambda helper package
4
+ *
5
+ * @module
6
+ */
1
7
import HttpStatusCode from './HttpStatusCode'
2
8
import { APIGatewayProxyEvent } from 'aws-lambda'
3
9
@@ -12,19 +18,40 @@ export interface HttpResponse {
12
18
}
13
19
14
20
/**
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
20
34
*/
21
- const withStatusCode = ( statusCode : number , origin : string , format ?: Function ) : Function => {
35
+ const withStatusCode = ( statusCode : number , format ?: Function ) : Function => {
22
36
if ( 100 > statusCode || statusCode > 599 ) {
23
37
throw new Error ( 'status code out of range' )
24
38
}
25
39
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 => {
28
55
const response : HttpResponse = {
29
56
statusCode : statusCode ,
30
57
headers : {
@@ -44,8 +71,11 @@ const withStatusCode = (statusCode: number, origin: string, format?: Function):
44
71
}
45
72
46
73
/**
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
47
77
*
48
- * @param apiGatewayProxyEvent
78
+ * @returns The headers parsed into a Object
49
79
*/
50
80
const validateAndParseRequestHeaders = ( apiGatewayProxyEvent : APIGatewayProxyEvent ) : Record < string , unknown > | null => {
51
81
if ( apiGatewayProxyEvent !== null && apiGatewayProxyEvent . headers !== null && apiGatewayProxyEvent . headers !== undefined ) {
@@ -58,8 +88,10 @@ const validateAndParseRequestHeaders = (apiGatewayProxyEvent: APIGatewayProxyEve
58
88
}
59
89
60
90
/**
91
+ * Ensuring the body eixists in the API request and then parses it
92
+ * @param apiGatewayProxyEvent - The event coming from the API Gateway request
61
93
*
62
- * @param apiGatewayProxyEvent
94
+ * @returns The body parsed into an object
63
95
*/
64
96
const validateAndParseRequestBody = ( apiGatewayProxyEvent : APIGatewayProxyEvent ) : string | null => {
65
97
if ( apiGatewayProxyEvent !== null && apiGatewayProxyEvent . body !== null && apiGatewayProxyEvent . body !== undefined ) {
0 commit comments