|
| 1 | +import _ from "lodash"; |
| 2 | +import { OpenAPIV3, OpenAPI } from "openapi-types"; |
| 3 | +import { ConfigToType, DataSourcePlugin } from "openblocks-sdk/dataSource"; |
| 4 | +import { runOpenApi } from "../openApi"; |
| 5 | +import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse"; |
| 6 | +import spec from "./openai-spec.json"; |
| 7 | + |
| 8 | +const dataSourceConfig = { |
| 9 | + type: "dataSource", |
| 10 | + params: [ |
| 11 | + { |
| 12 | + key: "ApiKey.value", |
| 13 | + type: "password", |
| 14 | + label: "API Key", |
| 15 | + rules: [{ required: true }], |
| 16 | + placeholder: "<Your API KEY>", |
| 17 | + tooltip: |
| 18 | + "[Document](https://platform.openai.com/account/api-keys) on which you can create your api key", |
| 19 | + }, |
| 20 | + ], |
| 21 | +} as const; |
| 22 | + |
| 23 | +const parseOptions: ParseOpenApiOptions = { |
| 24 | + actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => { |
| 25 | + return _.upperFirst(operation.operationId || ""); |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>; |
| 30 | + |
| 31 | +const openAiPlugin: DataSourcePlugin<any, DataSourceConfigType> = { |
| 32 | + id: "OpenAI", |
| 33 | + name: "OpenAI", |
| 34 | + icon: "openAI.svg", |
| 35 | + category: "api", |
| 36 | + dataSourceConfig, |
| 37 | + queryConfig: async () => { |
| 38 | + const { actions } = await parseOpenApi(spec, parseOptions); |
| 39 | + return { |
| 40 | + type: "query", |
| 41 | + label: "Action", |
| 42 | + actions, |
| 43 | + }; |
| 44 | + }, |
| 45 | + run: function (actionData, dataSourceConfig): Promise<any> { |
| 46 | + const runApiDsConfig = { |
| 47 | + url: "", |
| 48 | + serverURL: "", |
| 49 | + dynamicParamsConfig: dataSourceConfig, |
| 50 | + }; |
| 51 | + return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document); |
| 52 | + }, |
| 53 | +}; |
| 54 | + |
| 55 | +export default openAiPlugin; |
0 commit comments