|
| 1 | +import { badRequest } from "../../common/error"; |
| 2 | +import _ from "lodash"; |
| 3 | +import { OpenAPIV2, OpenAPI } from "openapi-types"; |
| 4 | +import { ConfigToType, DataSourcePlugin } from "openblocks-sdk/dataSource"; |
| 5 | +import { runOpenApi } from "../openApi"; |
| 6 | +import { defaultParseOpenApiOptions, parseOpenApi, ParseOpenApiOptions } from "../openApi/parse"; |
| 7 | +import spec from "./woocommerce-spec.json"; |
| 8 | + |
| 9 | +export function prepareServerUrl(url: string) { |
| 10 | + if (/\/wc\/v[12]$/.test(url)) { |
| 11 | + throw badRequest("only woocommerce api v3 is supported"); |
| 12 | + } |
| 13 | + if (/\/wc\/v3$/.test(url)) { |
| 14 | + return url; |
| 15 | + } |
| 16 | + if (!url.endsWith("/")) { |
| 17 | + url += "/"; |
| 18 | + } |
| 19 | + return url + "wp-json/wc/v3"; |
| 20 | +} |
| 21 | + |
| 22 | +const dataSourceConfig = { |
| 23 | + type: "dataSource", |
| 24 | + params: [ |
| 25 | + { |
| 26 | + key: "serverURL", |
| 27 | + type: "textInput", |
| 28 | + label: "Server URL", |
| 29 | + rules: [{ required: true, message: "The server url is required" }], |
| 30 | + placeholder: "https://localhost/wp-json/wc/v3", |
| 31 | + tooltip: "HTTPS is required", |
| 32 | + }, |
| 33 | + { type: "groupTitle", key: "BasicAuth", label: "HTTP Basic Auth" }, |
| 34 | + { |
| 35 | + type: "textInput", |
| 36 | + key: "basicAuth.username", |
| 37 | + label: "Username", |
| 38 | + tooltip: "Basic auth username", |
| 39 | + placeholder: "<username>", |
| 40 | + }, |
| 41 | + { |
| 42 | + type: "password", |
| 43 | + key: "basicAuth.password", |
| 44 | + label: "Password", |
| 45 | + tooltip: "", |
| 46 | + placeholder: "<password>", |
| 47 | + }, |
| 48 | + ], |
| 49 | +} as const; |
| 50 | + |
| 51 | +type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>; |
| 52 | + |
| 53 | +const parseOptions: ParseOpenApiOptions = { |
| 54 | + actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => { |
| 55 | + return operation.summary || defaultParseOpenApiOptions.actionLabel(method, path, operation); |
| 56 | + }, |
| 57 | + actionDescription: (method: string, path: string, operation: OpenAPI.Operation) => { |
| 58 | + return `${method} ${path}`; |
| 59 | + }, |
| 60 | +}; |
| 61 | + |
| 62 | +const woocommercePlugin: DataSourcePlugin<any, DataSourceConfigType> = { |
| 63 | + id: "woocommerce", |
| 64 | + name: "WooCommerce", |
| 65 | + icon: "woocommerce.png", |
| 66 | + category: "api", |
| 67 | + dataSourceConfig, |
| 68 | + queryConfig: async () => { |
| 69 | + const { actions, categories } = await parseOpenApi(spec, parseOptions); |
| 70 | + return { |
| 71 | + type: "query", |
| 72 | + label: "Operation", |
| 73 | + categories: { |
| 74 | + label: "Resource", |
| 75 | + items: categories, |
| 76 | + }, |
| 77 | + actions, |
| 78 | + }; |
| 79 | + }, |
| 80 | + run: function (actionData, dataSourceConfig): Promise<any> { |
| 81 | + const { serverURL, ...otherDataSourceConfig } = dataSourceConfig; |
| 82 | + const runApiDsConfig = { |
| 83 | + url: "", |
| 84 | + serverURL: prepareServerUrl(serverURL), |
| 85 | + dynamicParamsConfig: otherDataSourceConfig, |
| 86 | + }; |
| 87 | + return runOpenApi(actionData, runApiDsConfig, spec as unknown as OpenAPIV2.Document); |
| 88 | + }, |
| 89 | +}; |
| 90 | + |
| 91 | +export default woocommercePlugin; |
0 commit comments