Created
February 27, 2020 05:52
-
-
Save sofyan-ahmad/0d1c9ee7daccf141bae5c63ee8e10305 to your computer and use it in GitHub Desktop.
AWS Lambda Invoker | NestJS | TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { HttpException } from '@nestjs/common'; | |
import { Lambda } from 'aws-sdk'; | |
import dotenv = require('dotenv'); | |
import _ from 'lodash'; | |
// Load dot environment | |
const { parsed } = dotenv.config({ | |
path: process.cwd() + '/.env', | |
}); | |
process.env = { ...parsed, ...process.env }; | |
const lambda: Lambda = new Lambda(); | |
export function handleResponse(resultStr: any): any { | |
if (resultStr && isJSON(resultStr)) { | |
const result = JSON.parse(resultStr) as { | |
statusCode?: number; | |
statusText?: string; | |
message?: string; | |
data?: object; | |
error?: object; | |
}; | |
if (result.statusCode) { | |
throw new HttpException( | |
result.data || | |
result.message || | |
result.statusText || | |
'Error on fetching data', | |
result.statusCode || 404 | |
); | |
} | |
return result; | |
} | |
} | |
export async function invoker( | |
params: Lambda.Types.InvocationRequest | |
): Promise<string> { | |
try { | |
const result = await lambda.invoke(params).promise(); | |
const resPayload: { | |
statusCode: number; | |
body: string; | |
headers: any; | |
} = JSON.parse(result.Payload.toString()); | |
if (resPayload.statusCode === 500) { | |
throw new HttpException( | |
resPayload.body && !_.isEmpty(JSON.parse(resPayload.body)) | |
? resPayload.body | |
: 'Error invoking lambda function ' + params.FunctionName, | |
resPayload.statusCode | |
); | |
} | |
return resPayload.body; | |
} catch (error) { | |
throw new HttpException(error.message, error.statusCode || 500); | |
} | |
} | |
export function isJSON(str: string): boolean { | |
try { | |
const obj = JSON.parse(str); | |
if (obj && typeof obj === 'object' && obj !== null) { | |
return true; | |
} | |
} catch (err) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment