Skip to content

Commit 2d438e2

Browse files
committed
feat: woocommerce datasource
1 parent 0e11e5e commit 2d438e2

File tree

9 files changed

+35132
-4
lines changed

9 files changed

+35132
-4
lines changed

server/node-service/src/plugins/couchdb/CouchDB-3.1.1-resolved.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4389,9 +4389,7 @@
43894389
},
43904390
"security": [
43914391
{
4392-
"BasicAuth": []
4393-
},
4394-
{
4392+
"BasicAuth": [],
43954393
"JwtAuth": []
43964394
}
43974395
],

server/node-service/src/plugins/couchdb/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const couchdbPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
7878
serverURL: serverURL,
7979
dynamicParamsConfig: otherDataSourceConfig,
8080
};
81-
return runOpenApi(actionData, runApiDsConfig, spec as unknown as OpenAPIV2.Document);
81+
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV2.Document);
8282
},
8383
};
8484

server/node-service/src/plugins/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import openApiPlugin from "./openApi";
66
import dynamoDBPlugin from "./dynamodb";
77
import firebasePlugin from "./firebase";
88
import couchdbPlugin from "./couchdb";
9+
import woocommercePlugin from "./woocommerce";
910

1011
const plugins: (DataSourcePlugin | DataSourcePluginFactory)[] = [
1112
// helloWorldPlugin,
@@ -15,6 +16,7 @@ const plugins: (DataSourcePlugin | DataSourcePluginFactory)[] = [
1516
dynamoDBPlugin,
1617
firebasePlugin,
1718
couchdbPlugin,
19+
woocommercePlugin,
1820
];
1921

2022
export default plugins;

server/node-service/src/plugins/openApi/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export async function runOpenApi(
9090
duplex: "half",
9191
headers: _.omitBy(req.headers, (i) => !i),
9292
};
93+
console.info(req);
9394
return ret;
9495
},
9596
});
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

Comments
 (0)