Skip to content

Commit

Permalink
Integrate Changes
Browse files Browse the repository at this point in the history
Integrate Changes
  • Loading branch information
gabrielforster authored Oct 3, 2023
2 parents a360198 + 738de49 commit e925df8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
14 changes: 14 additions & 0 deletions __tests__/lambda/formatters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@ describe('invoke', () => {
})).toEqual({ body: { error: 'Invalid Session' }, status: 401 })
})

it('Should returns response formatted with headers', () => {
expect(formattedResponse({
StatusCode: 200,
Payload: '{"headers":"{\\"total-count\\":\\"20\\"}"}'
})).toEqual({ headers: { 'total-count': '20' }, status: 200, body: {} })
})

it('Should returns response formatted with headers and body', () => {
expect(formattedResponse({
StatusCode: 200,
Payload: '{"headers": "{\\"total-count\\":\\"20\\"}","body": "{\\"name\\":\\"John Doe\\"}"}'
})).toEqual({ headers: { 'total-count': '20' }, status: 200, body: { name: 'John Doe' } })
})

it('Should returns response formatted with status code 200', () => {
expect(formattedResponse({
StatusCode: 200,
Expand Down
14 changes: 13 additions & 1 deletion __tests__/lambda/lambdaResponses.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ describe('lambdaResp', () => {
statusCode: 404,
body: 'not found!'
}
},
{
input: {
statusCode: 200,
body: '',
headers: { 'total-count': 42069 }
},
output: {
statusCode: 200,
body: '',
headers: { 'total-count': 42069 }
}
}
]

test.each(data)('Should return an response Object, with statusCode and body props', (param) => {
expect(lambdaResp(param.input.statusCode, param.input.body)).toStrictEqual(param.output)
expect(lambdaResp(param.input.statusCode, param.input.body, param.input.headers)).toStrictEqual(param.output)
})
})

Expand Down
2 changes: 1 addition & 1 deletion 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": "0.16.2",
"version": "0.16.3",
"description": "Utils library for Javascript",
"keywords": [],
"author": {
Expand Down
5 changes: 3 additions & 2 deletions src/lambda/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import jsonBigInt from 'json-bigint'

import { isObject } from '../object'

export const formattedResponse = ({ StatusCode, Payload }: { StatusCode?: any, Payload?: any }): object => {
export const formattedResponse = ({ StatusCode, Payload }: { StatusCode?: number, Payload?: any }): object => {
const payloadFormatted = JSON.parse(Payload || '{}')

return {
status: payloadFormatted.statusCode || StatusCode,
body: payloadFormatted.body ? JSON.parse(payloadFormatted.body) : {}
body: payloadFormatted.body ? JSON.parse(payloadFormatted.body) : {},
...(payloadFormatted.headers ? { headers: JSON.parse(payloadFormatted.headers) } : null)
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/lambda/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface Error {
message?: string
}

export type Headers = {
[header: string]: string | number | boolean
}

export interface lambdaParameters {
port?: string
region?: string
Expand Down
7 changes: 4 additions & 3 deletions src/lambda/lambdaResponses.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { isNumber } from '../number'
import { objToStr } from '../object'
import { ProxyResult } from 'aws-lambda'
import type { Error } from './interfaces'
import type { Error, Headers } from './interfaces'

export const lambdaResp = (statusCode: number, body?: object | string): ProxyResult => ({
export const lambdaResp = (statusCode: number, body?: object | string, headers?: Headers): ProxyResult => ({
statusCode,
...(body ? { body: objToStr(body) } : { body: '' })
...(body ? { body: objToStr(body) } : { body: '' }),
...(headers ? { headers } : null)
})

export const lambdaRespError = (err: Error): ProxyResult => {
Expand Down

0 comments on commit e925df8

Please sign in to comment.