Skip to content

Commit

Permalink
feat: change lambdaResp and formatResponse function to accept headers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielforster committed Oct 3, 2023
1 parent 84d0100 commit 1077365
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
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 1077365

Please sign in to comment.