|
| 1 | +import { readYaml } from "../../common/util"; |
| 2 | +import _ from "lodash"; |
| 3 | +import path from "path"; |
| 4 | +import { OpenAPIV3, OpenAPI } from "openapi-types"; |
| 5 | +import { ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource"; |
| 6 | +import { runOpenApi } from "../openApi"; |
| 7 | +import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse"; |
| 8 | + |
| 9 | +import spec from './lowcoder.spec.json'; |
| 10 | + |
| 11 | +const dataSourceConfig = { |
| 12 | + type: "dataSource", |
| 13 | + params: [ |
| 14 | + { |
| 15 | + key: "serverURL", |
| 16 | + type: "textInput", |
| 17 | + label: "Lowcoder API Service URL", |
| 18 | + rules: [{ required: true }], |
| 19 | + placeholder: "https://<your-lowcoder-api-service>:port", |
| 20 | + tooltip: "Input the server url of your self-hosting instance or api-service.lowcoder.cloud if you are running your apps on the free public Community Edition Cloud Service.", |
| 21 | + }, |
| 22 | + { |
| 23 | + "type": "groupTitle", |
| 24 | + "key": "API Key", |
| 25 | + "label": "Api Key Auth" |
| 26 | + }, |
| 27 | + { |
| 28 | + type: "password", |
| 29 | + key: "bearerAuth.value", |
| 30 | + label: "Authorization", |
| 31 | + "tooltip": "API Key Authentication with a Bearer token. Copy your API Key here. (e.g. 'Bearer eyJhbGciO...')", |
| 32 | + "placeholder": "API Key Authentication with a Bearer token. Copy your API Key here. (e.g. 'Bearer eyJhbGciO...')" |
| 33 | + } |
| 34 | +] |
| 35 | +} as const; |
| 36 | + |
| 37 | +const parseOptions: ParseOpenApiOptions = { |
| 38 | + actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => { |
| 39 | + return _.upperFirst(operation.operationId || ""); |
| 40 | + }, |
| 41 | +}; |
| 42 | + |
| 43 | +type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>; |
| 44 | + |
| 45 | +const lowcoderPlugin: DataSourcePlugin<any, DataSourceConfigType> = { |
| 46 | + id: "lowcoder", |
| 47 | + name: "Lowcoder API", |
| 48 | + icon: "lowcoder.svg", |
| 49 | + category: "api", |
| 50 | + dataSourceConfig, |
| 51 | + queryConfig: async () => { |
| 52 | + const { actions, categories } = await parseOpenApi(spec as unknown as OpenAPI.Document, parseOptions); |
| 53 | + return { |
| 54 | + type: "query", |
| 55 | + label: "Action", |
| 56 | + categories: { |
| 57 | + label: "Resources", |
| 58 | + items: categories, |
| 59 | + }, |
| 60 | + actions, |
| 61 | + }; |
| 62 | + }, |
| 63 | + run: function (actionData, dataSourceConfig): Promise<any> { |
| 64 | + const { serverURL, ...otherDataSourceConfig } = dataSourceConfig; |
| 65 | + console.log("Lowcoder API Plugin: run", serverURL, otherDataSourceConfig); |
| 66 | + const runApiDsConfig = { |
| 67 | + url: "", |
| 68 | + serverURL: serverURL, |
| 69 | + dynamicParamsConfig: otherDataSourceConfig, |
| 70 | + }; |
| 71 | + return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document); |
| 72 | + }, |
| 73 | +}; |
| 74 | + |
| 75 | +export default lowcoderPlugin; |
0 commit comments