ToolJet/marketplace/plugins/aws-lambda/lib/index.ts
Syed Mohammad Akhtar Rizvi 8aeaa119e0
AWS Redshift plugin (#8839)
2024-03-07 11:33:47 +05:30

40 lines
1.3 KiB
TypeScript

import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
import { LambdaClient, InvokeCommand } from '@aws-sdk/client-lambda';
import { SourceOptions, QueryOptions } from './types';
export default class AWSLambda implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
const lambdaClient = new LambdaClient({
region: sourceOptions.region,
credentials: {
accessKeyId: sourceOptions.access_key,
secretAccessKey: sourceOptions.secret_key,
},
});
const command = new InvokeCommand({
FunctionName: queryOptions.functionName,
Payload: queryOptions.payload,
});
try {
const response = await lambdaClient.send(command);
// Convert Uint8Array to a string and parse it as JSON
let responseData;
if (response.Payload instanceof Uint8Array) {
const payloadString = new TextDecoder().decode(response.Payload);
responseData = JSON.parse(payloadString);
} else {
responseData = response.Payload; // Fallback if it's not a Uint8Array
}
return {
status: 'ok',
data: responseData,
};
} catch (error) {
throw new QueryError('Query could not be completed: ' + error.message, error.message, {});
}
}
}