This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
40 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { APIGatewayProxyEvent, Context, APIGatewayProxyResult } from "aws-lambda"; | ||
|
||
import { warmingRequestBody } from "./request"; | ||
import { createOKResult, createErrorResult } from "./response"; | ||
|
||
export type ResponseHelpers = { | ||
ok: typeof createOKResult; | ||
error: typeof createErrorResult; | ||
}; | ||
|
||
export type ExtendedApiGatewayHandler = ( | ||
event: APIGatewayProxyEvent, | ||
context: Context, | ||
res: ResponseHelpers, | ||
) => APIGatewayProxyResult | Promise<APIGatewayProxyResult>; | ||
|
||
export function createHandler(handler: ExtendedApiGatewayHandler): ExtendedApiGatewayHandler { | ||
return async function (event, context) { | ||
const res: ResponseHelpers = { | ||
ok: (payload, headers, requestId) => createOKResult(payload, headers, requestId), | ||
error: (statusCode, e, requestId) => createErrorResult(statusCode, e, requestId), | ||
}; | ||
|
||
if (event.body === JSON.stringify(warmingRequestBody)) { | ||
return res.ok("Successfully warmed!", event.headers, context.awsRequestId); | ||
} | ||
|
||
return handler(event, context, res); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* The body of a warming request. | ||
* | ||
* A warming request is periodically sent to ensure that the Lambda function is active. | ||
* Ideally, it wouldn't trigger any expensive computations. | ||
*/ | ||
export const warmingRequestBody = { body: "warming request" }; |