From 76479f223d91a826a556a1c95968cd806c6964da Mon Sep 17 00:00:00 2001 From: Robert Seitz Date: Fri, 19 Apr 2024 13:13:00 -0400 Subject: [PATCH] move chainId parsing to function --- lib/handlers/get-orders/handler.ts | 10 ++------ lib/handlers/post-order/handler.ts | 23 ++++++++++++------- .../handlers/post-order/post-order.test.ts | 23 +++++++++++++++++++ 3 files changed, 40 insertions(+), 16 deletions(-) diff --git a/lib/handlers/get-orders/handler.ts b/lib/handlers/get-orders/handler.ts index 824c71da..4f14d196 100644 --- a/lib/handlers/get-orders/handler.ts +++ b/lib/handlers/get-orders/handler.ts @@ -15,6 +15,7 @@ import { ErrorResponse, Response, } from '../base/index' +import { tryParseChainIdFromBody } from '../post-order/handler' import { ContainerInjected, RequestInjected } from './injector' import { GetDutchV2OrderResponse } from './schema/GetDutchV2OrderResponse' import { GetOrdersResponse, GetOrdersResponseJoi } from './schema/GetOrdersResponse' @@ -102,14 +103,7 @@ export class GetOrdersHandler extends APIGLambdaHandler< protected afterResponseHook(event: APIGatewayProxyEvent, _context: Context, response: APIGatewayProxyResult): void { const { statusCode } = response - // Try and extract the chain id from the raw json. - let chainId = '0' - try { - const rawBody = JSON.parse(event.body || '') - chainId = rawBody.chainId ?? chainId - } catch (err) { - // no-op. If we can't get chainId still log the metric as chain 0 - } + const chainId = tryParseChainIdFromBody(event) const statusCodeMod = (Math.floor(statusCode / 100) * 100).toString().replace(/0/g, 'X') const getOrdersByChainMetricName = `GetOrdersChainId${chainId.toString()}Status${statusCodeMod}` diff --git a/lib/handlers/post-order/handler.ts b/lib/handlers/post-order/handler.ts index 91cbaeb4..23eef6ec 100644 --- a/lib/handlers/post-order/handler.ts +++ b/lib/handlers/post-order/handler.ts @@ -22,6 +22,20 @@ import { import { PostOrderBodyParser } from './PostOrderBodyParser' import { PostOrderRequestBody, PostOrderRequestBodyJoi, PostOrderResponse, PostOrderResponseJoi } from './schema' +export function tryParseChainIdFromBody(event: APIGatewayProxyEvent) { + // Try and extract the chain id from the raw json. + if (!event || !event.body) { + return '0' + } + + try { + const rawBody = JSON.parse(event.body) + const chainId = rawBody.chainId ?? '0' + return chainId + } catch (err) { + return '0' + } +} export class PostOrderHandler extends APIGLambdaHandler< unknown, ApiRInj, @@ -119,14 +133,7 @@ export class PostOrderHandler extends APIGLambdaHandler< protected afterResponseHook(event: APIGatewayProxyEvent, _context: Context, response: APIGatewayProxyResult): void { const { statusCode } = response - // Try and extract the chain id from the raw json. - let chainId = '0' - try { - const rawBody = JSON.parse(event.body || '') - chainId = rawBody.chainId ?? chainId - } catch (err) { - // no-op. If we can't get chainId still log the metric as chain 0 - } + const chainId = tryParseChainIdFromBody(event) const statusCodeMod = (Math.floor(statusCode / 100) * 100).toString().replace(/0/g, 'X') diff --git a/test/unit/handlers/post-order/post-order.test.ts b/test/unit/handlers/post-order/post-order.test.ts index 140ce92e..0c2d2e94 100644 --- a/test/unit/handlers/post-order/post-order.test.ts +++ b/test/unit/handlers/post-order/post-order.test.ts @@ -1,6 +1,7 @@ import { Logger } from '@aws-lambda-powertools/logger' import { SFNClient, StartExecutionCommand } from '@aws-sdk/client-sfn' import { OrderType, OrderValidation, OrderValidator, RelayOrderValidator } from '@uniswap/uniswapx-sdk' +import { APIGatewayEvent } from 'aws-lambda' import { mockClient } from 'aws-sdk-client-mock' import { mock } from 'jest-mock-extended' import { ORDER_STATUS, UniswapXOrderEntity } from '../../../../lib/entities' @@ -519,4 +520,26 @@ describe('Testing post order handler.', () => { }) }) }) + + describe('tryParseChainIdFromBody', () => { + test('should return 0 when no chainId', () => { + const response = postOrderHandler.tryParseChainIdFromBody({ body: '{}' } as APIGatewayEvent) + expect(response).toEqual('0') + }) + + test('should return 0 when no body', () => { + const response = postOrderHandler.tryParseChainIdFromBody({} as APIGatewayEvent) + expect(response).toEqual('0') + }) + + test('should return 0 when JSON.parse errors', () => { + const response = postOrderHandler.tryParseChainIdFromBody({ body: '{;}' } as APIGatewayEvent) + expect(response).toEqual('0') + }) + + test('should return chainId', () => { + const response = postOrderHandler.tryParseChainIdFromBody({ body: '{"chainId":1}' } as APIGatewayEvent) + expect(response).toEqual(1) + }) + }) })