From e5275f963348d690203cba7ede9695fe51903202 Mon Sep 17 00:00:00 2001 From: Philipp Jardas Date: Wed, 13 Dec 2023 20:36:39 +0100 Subject: [PATCH] fix --- src/local/api.ts | 13 ++++-------- src/local/express-lambda.ts | 40 +++++++++++++++++++------------------ src/local/types.ts | 5 +++-- src/logging.ts | 7 ++++++- 4 files changed, 34 insertions(+), 31 deletions(-) diff --git a/src/local/api.ts b/src/local/api.ts index 610442f..1f62616 100644 --- a/src/local/api.ts +++ b/src/local/api.ts @@ -1,5 +1,5 @@ import * as bodyParser from "body-parser"; -import { RequestHandler, Router } from "express"; +import { Router, type RequestHandler } from "express"; import type { HTTPLambdaHandler } from "../types"; import { createAuthorizers } from "./authorizer"; import { expressLambda } from "./express-lambda"; @@ -59,7 +59,7 @@ class LocalExpressAPI { bodyParser.raw({ type: requestBody.mimeTypes ?? "*/*", limit: requestBody.limit, - }) + }), ); break; } @@ -104,13 +104,8 @@ export async function createLocalExpressAPI({ .map((route) => route.path) .filter((val, index, arr) => arr.indexOf(val) === index) .map((path) => - api.route({ - method: "options", - path, - handlerId: "cors", - handler: optionsHandler, - }) - ) + api.route({ method: "options", path, handlerId: "cors" }), + ), ); } diff --git a/src/local/express-lambda.ts b/src/local/express-lambda.ts index 2541d88..f1835a8 100644 --- a/src/local/express-lambda.ts +++ b/src/local/express-lambda.ts @@ -3,26 +3,25 @@ import type { APIGatewayProxyEventBase, APIGatewayProxyResult, Context, - Handler, } from "aws-lambda"; import * as express from "express"; -import type { AuthorizedLambdaEvent, AuthorizerContext } from "../types"; +import { LoggerImpl } from "../logging"; +import type { + AuthorizedLambdaEvent, + AuthorizerContext, + HTTPLambdaHandler, +} from "../types"; export function expressLambda( name: string, - handler: Handler< - APIGatewayProxyEventBase, - APIGatewayProxyResult | undefined - > + handler: HTTPLambdaHandler, ): express.RequestHandler { return async (req, res, next) => { try { const event = createEvent(req); const context = createContext({ name }); - - const response = await handler(event, context, () => { - throw new Error("Lambda handler with callback is not supported"); - }); + const logger = new LoggerImpl(); + const response = await handler(event, context, logger); sendResponse(response, res); } catch (error) { @@ -32,11 +31,11 @@ export function expressLambda( } export function createEvent( - req: express.Request + req: express.Request, ): APIGatewayProxyEventBase { const headers = Object.entries(req.headers).reduce( (acc, [key, value]) => ({ ...acc, [key]: value }), - {} + {}, ); return { @@ -70,7 +69,7 @@ export function createEvent( } function parseBody( - body: null | string | Buffer + body: null | string | Buffer, ): Pick, "body" | "isBase64Encoded"> { if (body instanceof Buffer) { return { @@ -118,13 +117,13 @@ export function createContext({ function sendResponse( response: APIGatewayProxyResult | undefined | void, - res: express.Response> + res: express.Response>, ) { if (response) { res.status(response.statusCode || 200); if (response.headers) { Object.entries(response.headers).forEach(([key, value]) => - res.set(key, value.toString()) + res.set(key, value.toString()), ); } @@ -132,7 +131,7 @@ function sendResponse( res.send( response.isBase64Encoded ? Buffer.from(response.body, "base64") - : response.body + : response.body, ); } else { res.end(); @@ -143,7 +142,7 @@ function sendResponse( } function createQueryStringParameters( - req: express.Request + req: express.Request, ): Pick< AuthorizedLambdaEvent, "queryStringParameters" | "multiValueQueryStringParameters" @@ -151,12 +150,15 @@ function createQueryStringParameters( const entries = Object.entries(req.query).map(([key, value]) => typeof value === "string" ? [key, [value]] - : [key, Array.isArray(value) ? value.map((v) => v.toString()) : undefined] + : [ + key, + Array.isArray(value) ? value.map((v) => v.toString()) : undefined, + ], ); return { queryStringParameters: Object.fromEntries( - entries.map(([key, value]) => [key, value?.slice(-1)?.[0]]) + entries.map(([key, value]) => [key, value?.slice(-1)?.[0]]), ), multiValueQueryStringParameters: Object.fromEntries(entries), }; diff --git a/src/local/types.ts b/src/local/types.ts index 0d16c76..aa5e291 100644 --- a/src/local/types.ts +++ b/src/local/types.ts @@ -1,4 +1,5 @@ import type { + APIGatewayProxyHandler, APIGatewayRequestAuthorizerHandler, APIGatewayTokenAuthorizerHandler, } from "aws-lambda"; @@ -22,7 +23,7 @@ export type AuthorizerRegistration = { id: string } & ( ); export type HandlerResolver = ( - handlerId: string + handlerId: string, ) => HTTPLambdaHandler | Promise; export type Method = @@ -57,6 +58,6 @@ export type Route = { export type APIOptions = { handlerResolver: HandlerResolver; routes: Array; - optionsHandler?: HTTPLambdaHandler; + optionsHandler?: APIGatewayProxyHandler; authorizers?: Array; }; diff --git a/src/logging.ts b/src/logging.ts index 78c0e40..96d967d 100644 --- a/src/logging.ts +++ b/src/logging.ts @@ -3,7 +3,12 @@ import log, { type JsonLog } from "json-log"; export * from "json-log"; export { default } from "json-log"; -export interface Logger {} +export interface Logger { + addContext(context: Record): void; + info(message: string, data?: Record): void; + warn(message: string, data?: Record): void; + error(message: string, data?: Record): void; +} export class LoggerImpl implements Logger { constructor(