Skip to content

Commit

Permalink
Integrate Changes
Browse files Browse the repository at this point in the history
Integrate Changes
  • Loading branch information
caroltrindade authored Apr 11, 2024
2 parents 889f630 + 8137965 commit 570989b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
30 changes: 30 additions & 0 deletions __tests__/s3/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { streamToString } from '../../src/s3'
import stream from 'stream'

describe('streamToString', () => {
it('should convert a stream to a string', async () => {
const readable = new stream.Readable()

readable._read = () => {}
readable.push('Hello, ')
readable.push('world!')
readable.push(null)

const result = await streamToString(readable)
expect(result).toEqual('Hello, world!')
})

it('should convert a JSON stream to a string', async () => {
const json = { name: 'John Doe', age: 30 }
const jsonString = JSON.stringify(json)
const readable = new stream.Readable()

readable._read = () => {}
readable.push(jsonString)

const result = await streamToString(readable)
expect(result).toEqual(jsonString)
})


})
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adapcon-utils-js",
"version": "1.3.4",
"version": "1.3.5",
"description": "Utils library for Javascript",
"keywords": [],
"author": {
Expand Down
12 changes: 6 additions & 6 deletions src/lambda/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ export type DocfySettings = {
export type Docfy = {
type: 'screen' | 'integration' | 'public' | 'session' | 'hybrid'
description: string
pathParameters?: { [key: string]: Omit<DocfySettings, 'required'> }
queryStringParameters?: { [key: string]: DocfySettings }
headers?: { [key: string]: DocfySettings }
body?: { [key: string]: DocfySettings }
requestContext?: { [key: string]: DocfySettings }
fromEvent?: { [key: string]: DocfySettings }
pathParameters?: Record<string, Omit<DocfySettings, 'required'>>
queryStringParameters?: Record<string, DocfySettings>
headers?: Record<string, DocfySettings>
body?: Record<string, DocfySettings>
requestContext?: Record<string, DocfySettings>
fromEvent?: Record<string, DocfySettings>
}
16 changes: 10 additions & 6 deletions src/lambda/lambdaGetParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,19 @@ const extractParams = (docfy: Docfy, parameter: string, evt: APIGatewayEvent) =>
const params = {}
const errs = {}

for (const [key, value] of Object.entries(docfy[parameter] ?? {}) as Array<[key: string, value: DocfySettings]>) {
const identity = value.translate ?? key
for (const [keyBase, value] of Object.entries(docfy[parameter] ?? {}) as Array<[key: string, value: DocfySettings]>) {
const key = parameter === 'headers' ? keyBase.toLowerCase() : keyBase

const identity = value.translate ?? keyBase
const param = get(evt, `${parameter}.${key}`) ?? value.default

const alreadyExist = value.translate && !params[value.translate]
const hasParamValue = params[identity]
const hasValue = param && param !== 'undefined'
const isRequired = value.required ?? parameter === 'pathParameters'
if (isRequired && !param && alreadyExist) {
errs[key] = `Missing(${parameter}) ${value.label}`
} else if (param !== 'undefined' && !params[identity]) {

if (isRequired && !hasValue && !hasParamValue) {
errs[keyBase] = `Missing(${parameter}) ${value.label}`
} else {
params[identity] = param
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/s3/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Readable } from 'stream';

export const streamToString = async (stream: Readable | ReadableStream | Blob) => new Promise((resolve, reject) => {
const chunks = []
stream.on('data', chunk => chunks.push(chunk))
export const streamToString = async (stream: Readable) => new Promise((resolve, reject) => {
const chunks: any[] = []

stream.on('data', (chunk: any) => chunks.push(chunk))
stream.on('error', reject)
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
2 changes: 1 addition & 1 deletion src/s3/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './s3Service'

export * from './formatters'

0 comments on commit 570989b

Please sign in to comment.