-
Notifications
You must be signed in to change notification settings - Fork 301
Expand file tree
/
Copy pathindex.ts
More file actions
76 lines (72 loc) · 2.28 KB
/
index.ts
File metadata and controls
76 lines (72 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { DataSourcePlugin } from "lowcoder-sdk/dataSource";
import dataSourceConfig, { DataSourceDataType } from "./dataSourceConfig";
import queryConfig, { ActionDataType } from "./queryConfig";
import {
LambdaClient,
ListFunctionsCommand,
InvokeCommand,
InvocationType,
} from "@aws-sdk/client-lambda";
import _ from "lodash";
import { safeJsonParse } from "../../common/util";
function getClient(dataSourceConfig: DataSourceDataType) {
const { accessKey, secretKey, region } = dataSourceConfig;
const client = new LambdaClient({
credentials: {
accessKeyId: accessKey,
secretAccessKey: secretKey,
},
region,
});
return client;
}
const lambdaPlugin: DataSourcePlugin<ActionDataType, DataSourceDataType> = {
id: "lambda",
name: "AWS Lambda",
category: "App Development",
icon: "lambda.svg",
dataSourceConfig,
queryConfig,
validateDataSourceConfig: async function (dataSourceConfig) {
const client = getClient(dataSourceConfig);
const ret = await client.send(new ListFunctionsCommand({ MaxItems: 1 }));
return {
success: Array.isArray(ret.Functions),
};
},
run: async function (actionData, dataSourceConfig): Promise<any> {
const client = getClient(dataSourceConfig);
if (actionData.actionName === "ListFunctions") {
const ret = await client.send(
new ListFunctionsCommand({
Marker: actionData.marker || undefined,
MaxItems: actionData.limit,
})
);
return {
functions: ret.Functions?.map((i) => i.FunctionName) || [],
nextMarker: ret.NextMarker || "",
};
}
if (actionData.actionName === "InvokeFunction") {
const ret = await client.send(
new InvokeCommand({
FunctionName: actionData.functionName,
InvocationType: actionData.invocationType as InvocationType,
Payload: Uint8Array.from(
JSON.stringify(actionData.payload || {})
.split("")
.map((i) => i.charCodeAt(0))
),
})
);
if (actionData.invocationType === InvocationType.RequestResponse) {
return (ret.Payload && safeJsonParse(Buffer.from(ret.Payload).toString("utf-8"))) || {};
}
return {
statusCode: ret.StatusCode,
};
}
},
};
export default lambdaPlugin;