|
| 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 './supabaseApi.spec.json'; |
| 10 | + |
| 11 | +const dataSourceConfig = { |
| 12 | + type: "dataSource", |
| 13 | + params: [ |
| 14 | + { |
| 15 | + "type": "groupTitle", |
| 16 | + "key": "serverURL", |
| 17 | + "label": "Supabase API Url" |
| 18 | + }, |
| 19 | + { |
| 20 | + key: "serverURL", |
| 21 | + type: "textInput", |
| 22 | + label: "Server URL", |
| 23 | + rules: [{ required: true, message: "The server url is required" }], |
| 24 | + placeholder: "https://<your couchdb server host>", |
| 25 | + }, |
| 26 | + { |
| 27 | + "type": "groupTitle", |
| 28 | + "key": "bearerAuth", |
| 29 | + "label": "Api Token Auth" |
| 30 | + }, |
| 31 | + { |
| 32 | + "type": "password", |
| 33 | + "key": "bearerAuth.value", |
| 34 | + "label": "Token", |
| 35 | + "tooltip": "API Key Authentication with a Bearer token. Copy your API Key from Supabase here. (e.g. 'eyJhbGciO...'", |
| 36 | + "placeholder": "API Key Authentication with a Bearer token. Copy your API Key from Supabase here. (e.g. 'eyJhbGciO...'" |
| 37 | + } |
| 38 | +] |
| 39 | +} as const; |
| 40 | + |
| 41 | +const parseOptions: ParseOpenApiOptions = { |
| 42 | + actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => { |
| 43 | + return _.upperFirst(operation.operationId || ""); |
| 44 | + }, |
| 45 | +}; |
| 46 | + |
| 47 | +type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>; |
| 48 | + |
| 49 | +const supabaseApiPlugin: DataSourcePlugin<any, DataSourceConfigType> = { |
| 50 | + id: "supabaseApi", |
| 51 | + name: "Supabase Mgmt API", |
| 52 | + icon: "supabase.svg", |
| 53 | + category: "api", |
| 54 | + dataSourceConfig, |
| 55 | + queryConfig: async () => { |
| 56 | + const { actions, categories } = await parseOpenApi(spec as unknown as OpenAPI.Document, parseOptions); |
| 57 | + return { |
| 58 | + type: "query", |
| 59 | + label: "Action", |
| 60 | + categories: { |
| 61 | + label: "Resources", |
| 62 | + items: categories, |
| 63 | + }, |
| 64 | + actions, |
| 65 | + }; |
| 66 | + }, |
| 67 | + run: function (actionData, dataSourceConfig): Promise<any> { |
| 68 | + const { serverURL, ...otherDataSourceConfig } = dataSourceConfig; |
| 69 | + const runApiDsConfig = { |
| 70 | + url: "", |
| 71 | + serverURL: serverURL, |
| 72 | + dynamicParamsConfig: otherDataSourceConfig, |
| 73 | + }; |
| 74 | + |
| 75 | + console.log("runApiDsConfig", runApiDsConfig); |
| 76 | + |
| 77 | + return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document); |
| 78 | + }, |
| 79 | +}; |
| 80 | + |
| 81 | +export default supabaseApiPlugin; |
0 commit comments