mirror of
https://github.com/ToolJet/ToolJet
synced 2026-05-02 10:57:20 +00:00
40 lines
1.3 KiB
TypeScript
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, {});
|
|
}
|
|
}
|
|
}
|