From 73e5f0de7a5cb350505ead38b12ee3591f614c33 Mon Sep 17 00:00:00 2001 From: Marco Antonio <79797841+marcofeliponi@users.noreply.github.com> Date: Thu, 29 Feb 2024 11:01:22 -0300 Subject: [PATCH] [REFACTOR] Corrige tipagens no DAO (#228) * refactor(dynamodb-dao): :label: Add QueryCommandOutput type and update get and query functions * chore(dynamodb-dao): :arrow_up: Bump version to 1.1.7 in package.json and package-lock.json --- package-lock.json | 4 ++-- package.json | 2 +- src/dao/dynamo/dao.ts | 16 ++++++++++------ src/lambda/lambdaService.ts | 2 ++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/package-lock.json b/package-lock.json index a102052..33acf10 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "adapcon-utils-js", - "version": "1.1.6", + "version": "1.1.7", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "adapcon-utils-js", - "version": "1.1.6", + "version": "1.1.7", "dependencies": { "@aws-sdk/client-dynamodb": "^3.496.0", "@aws-sdk/client-lambda": "^3.496.0", diff --git a/package.json b/package.json index 68890f3..0c47eee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "adapcon-utils-js", - "version": "1.1.6", + "version": "1.1.7", "description": "Utils library for Javascript", "keywords": [], "author": { diff --git a/src/dao/dynamo/dao.ts b/src/dao/dynamo/dao.ts index 23a921b..c7b5072 100644 --- a/src/dao/dynamo/dao.ts +++ b/src/dao/dynamo/dao.ts @@ -18,7 +18,8 @@ import type { UpdateCommandInput, DeleteCommandInput, BatchWriteCommandInput, - BatchWriteCommandOutput + BatchWriteCommandOutput, + QueryCommandOutput } from '@aws-sdk/lib-dynamodb' import { DynamoDB, ScanCommand } from '@aws-sdk/client-dynamodb' @@ -31,20 +32,23 @@ const documentInstance = DynamoDBDocument.from(dynamoInstance, { marshallOptions: { removeUndefinedValues: true } }) -const get = async ({ +const get = async ({ params, fields = [] -}: { params: GetCommandInput, fields?: string[] }): Promise | undefined> => { +}: { params: GetCommandInput, fields?: string[] }): Promise => { const command = new GetCommand({ ...params, ...mountProjectionExpression({ fields }) }) - const { Item } = await documentInstance.send(command) + const { Item } = await documentInstance.send(command) as { Item: T } return Item } + const query = async ({ params, fields = [], _items = [], stopOnLimit = false -}: { params: { Limit?: number } & QueryCommandInput, fields?: string[], _items?: Array>, stopOnLimit?: boolean }): Promise>> => { +}: { params: { Limit?: number } & QueryCommandInput, fields?: string[], _items?: T[], stopOnLimit?: boolean }): Promise => { + if (!Number(params.Limit) || Number.isNaN(params.Limit)) delete params.Limit + const command = new QueryCommand({ ...params, ...mountProjectionExpression({ fields, expressionAttributeNames: params.ExpressionAttributeNames }) }) - const { Items = [], LastEvaluatedKey } = await documentInstance.send(command) + const { Items = [], LastEvaluatedKey } = await documentInstance.send(command) as QueryCommandOutput & { Items: T[] } const items = [..._items, ...Items] diff --git a/src/lambda/lambdaService.ts b/src/lambda/lambdaService.ts index df8d8fa..1421951 100644 --- a/src/lambda/lambdaService.ts +++ b/src/lambda/lambdaService.ts @@ -65,6 +65,8 @@ const executeInvoke = async ({ ) }) + if (invocationType === InvokeType.Event) return { StatusCode: response.StatusCode } + return formattedResponse(response) }