|
| 1 | +import _ from "lodash"; |
| 2 | +import { OpenAPIV3, OpenAPI } from "openapi-types"; |
| 3 | +import { ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource"; |
| 4 | +import { runOpenApi } from "../openApi"; |
| 5 | +import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse"; |
| 6 | + |
| 7 | +import spec from './serpApi.search.spec.json'; |
| 8 | + |
| 9 | +const dataSourceConfig = { |
| 10 | + type: "dataSource", |
| 11 | + params: [ |
| 12 | + { |
| 13 | + "type": "groupTitle", |
| 14 | + "key": "apikey-query-api_key", |
| 15 | + "label": "Api Key Auth" |
| 16 | + }, |
| 17 | + { |
| 18 | + "type": "password", |
| 19 | + "key": "apikey-query-api_key.value", |
| 20 | + "label": "api_key" |
| 21 | + } |
| 22 | + ] |
| 23 | +} as const; |
| 24 | + |
| 25 | +const parseOptions: ParseOpenApiOptions = { |
| 26 | + actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => { |
| 27 | + return _.upperFirst(operation.operationId || ""); |
| 28 | + }, |
| 29 | +}; |
| 30 | + |
| 31 | +type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>; |
| 32 | + |
| 33 | +const serpApiPlugin: DataSourcePlugin<any, DataSourceConfigType> = { |
| 34 | + id: "serpApi", |
| 35 | + name: "serpApi", |
| 36 | + icon: "serpApi.svg", |
| 37 | + category: "Webscrapers", |
| 38 | + dataSourceConfig, |
| 39 | + queryConfig: async () => { |
| 40 | + const { actions, categories } = await parseOpenApi(spec as OpenAPI.Document, parseOptions); |
| 41 | + return { |
| 42 | + type: "query", |
| 43 | + label: "Action", |
| 44 | + categories: { |
| 45 | + label: "Resources", |
| 46 | + items: categories, |
| 47 | + }, |
| 48 | + actions, |
| 49 | + }; |
| 50 | + }, |
| 51 | + run: function (actionData, dataSourceConfig): Promise<any> { |
| 52 | + const runApiDsConfig = { |
| 53 | + url: "", |
| 54 | + serverURL: "https://serpapi.com", |
| 55 | + dynamicParamsConfig: dataSourceConfig, |
| 56 | + }; |
| 57 | + return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document); |
| 58 | + }, |
| 59 | +}; |
| 60 | + |
| 61 | +export default serpApiPlugin; |
0 commit comments