1+ // index.ts
2+ /**
3+ * A AWS Lambda helper package
4+ *
5+ * @module
6+ */
17import HttpStatusCode from './HttpStatusCode'
28import { 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 */
5080const 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 */
6496const validateAndParseRequestBody = ( apiGatewayProxyEvent : APIGatewayProxyEvent ) : string | null => {
6597 if ( apiGatewayProxyEvent !== null && apiGatewayProxyEvent . body !== null && apiGatewayProxyEvent . body !== undefined ) {
0 commit comments