Skip to content

Commit da3c0aa

Browse files
author
Thomasr
committed
refactor code and test with twilio plugin
1 parent 6b54810 commit da3c0aa

File tree

28 files changed

+3578
-266
lines changed

28 files changed

+3578
-266
lines changed

server/node-service/src/common/util.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import yaml from "yaml";
22
import fs from "fs";
3+
import { MultiOpenApiSpecItem } from "../plugins/openApi/parse";
4+
import path from "path";
5+
import { appendTags } from "../plugins/openApi/util";
6+
import _ from "lodash";
37

48
export function kvToRecord(
59
kvs: { key: string; value: string }[],
@@ -85,3 +89,36 @@ export function safeJsonStringify(data: any) {
8589
return null;
8690
}
8791
}
92+
93+
export function specsToOptions(specs: any) {
94+
return Object.keys(specs).map(k => ({value: k, label: k}));
95+
}
96+
97+
function genTagFromFileName(name: string) {
98+
const fileName = name.replace(/\.yaml|twilio_|\.json/g, "");
99+
const parts = fileName.split("_");
100+
return parts.reduce((a, b) => {
101+
if (/v\d+/.test(b)) {
102+
return `${a}(${b})`;
103+
}
104+
return a + _.upperFirst(b);
105+
}, "");
106+
}
107+
108+
export function dirToSpecList(specDir: string) {
109+
const specList: MultiOpenApiSpecItem[] = [];
110+
111+
const start = performance.now();
112+
const specFiles = fs.readdirSync(specDir);
113+
specFiles.forEach((specFile) => {
114+
const spec = readYaml(path.join(specDir, specFile));
115+
const tag = genTagFromFileName(specFile);
116+
appendTags(spec, tag);
117+
specList.push({
118+
id: tag,
119+
spec,
120+
});
121+
});
122+
logger.info("spec list loaded %s, duration: %d ms",specDir, performance.now() - start);
123+
return specList;
124+
}

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readYaml } from "../../common/util";
1+
import { readYaml, specsToOptions } from "../../common/util";
22
import _ from "lodash";
33
import path from "path";
44
import { OpenAPIV3, OpenAPI } from "openapi-types";
@@ -8,7 +8,10 @@ import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
88

99

1010
const spec = readYaml(path.join(__dirname, "./asana.spec.yaml"));
11-
11+
const specs = {
12+
"v1.0": spec,
13+
"v2.0": spec,
14+
}
1215

1316
const dataSourceConfig = {
1417
type: "dataSource",
@@ -30,16 +33,7 @@ const dataSourceConfig = {
3033
type: "select",
3134
tooltip: "Version of the spec file.",
3235
placeholder: "v1.0",
33-
options: [
34-
{
35-
value: "v1.0",
36-
label: "v1.0",
37-
},
38-
{
39-
value: "v2.0",
40-
label: "v2.0",
41-
}
42-
]
36+
options: specsToOptions(specs)
4337
},
4438
]
4539
} as const;

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { runOpenApi } from "../openApi";
55
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
66

77
import spec from "./circleCi.spec.json";
8+
import { specsToOptions } from "../../common/util";
9+
const specs = {
10+
"v1.0": spec,
11+
"v2.0": spec,
12+
}
813

914
const dataSourceConfig = {
1015
type: "dataSource",
@@ -22,16 +27,7 @@ const dataSourceConfig = {
2227
type: "select",
2328
tooltip: "Version of the spec file.",
2429
placeholder: "v1.0",
25-
options: [
26-
{
27-
value: "v1.0",
28-
label: "v1.0",
29-
},
30-
{
31-
value: "v2.0",
32-
label: "v2.0",
33-
}
34-
]
30+
options: specsToOptions(specs)
3531
},
3632
],
3733
} as const;

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readYaml } from "../../common/util";
1+
import { readYaml, specsToOptions } from "../../common/util";
22
import _ from "lodash";
33
import path from "path";
44
import { OpenAPI } from "openapi-types";
@@ -14,7 +14,12 @@ const specList = [
1414
{ spec: adminApiSpec, id: "admin" },
1515
{ spec: uploadApiSpec, id: "upload" },
1616
];
17+
const specs = {
18+
"v1.0": specList,
19+
"v2.0": specList,
20+
}
1721

22+
//TODO: Thomas
1823
const dataSourceConfig = {
1924
type: "dataSource",
2025
params: [
@@ -38,16 +43,7 @@ const dataSourceConfig = {
3843
type: "select",
3944
tooltip: "Version of the spec file.",
4045
placeholder: "v1.0",
41-
options: [
42-
{
43-
value: "v1.0",
44-
label: "v1.0",
45-
},
46-
{
47-
value: "v2.0",
48-
label: "v2.0",
49-
}
50-
]
46+
options: specsToOptions(specs)
5147
},
5248
],
5349
} as const;

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource";
44
import { runOpenApi } from "../openApi";
55
import { defaultParseOpenApiOptions, parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
66
import spec from "./CouchDB-3.1.1-resolved.json";
7-
7+
import { specsToOptions } from "../../common/util";
8+
const specs = {
9+
"v1.0": spec,
10+
"v2.0": spec,
11+
}
812
const dataSourceConfig = {
913
type: "dataSource",
1014
params: [
@@ -44,16 +48,7 @@ const dataSourceConfig = {
4448
type: "select",
4549
tooltip: "Version of the spec file.",
4650
placeholder: "v1.0",
47-
options: [
48-
{
49-
value: "v1.0",
50-
label: "v1.0",
51-
},
52-
{
53-
value: "v2.0",
54-
label: "v2.0",
55-
}
56-
]
51+
options: specsToOptions(specs)
5752
},
5853
],
5954
} as const;

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readYaml } from "../../common/util";
1+
import { readYaml, specsToOptions } from "../../common/util";
22
import _ from "lodash";
33
import path from "path";
44
import { OpenAPIV3, OpenAPI } from "openapi-types";
@@ -7,6 +7,10 @@ import { runOpenApi } from "../openApi";
77
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
88

99
const spec = readYaml(path.join(__dirname, "./datadog.spec.yaml"));
10+
const specs = {
11+
"v1.0": spec,
12+
"v2.0": spec,
13+
}
1014

1115
const dataSourceConfig = {
1216
type: "dataSource",
@@ -33,16 +37,7 @@ const dataSourceConfig = {
3337
type: "select",
3438
tooltip: "Version of the spec file.",
3539
placeholder: "v1.0",
36-
options: [
37-
{
38-
value: "v1.0",
39-
label: "v1.0",
40-
},
41-
{
42-
value: "v2.0",
43-
label: "v2.0",
44-
}
45-
]
40+
options: specsToOptions(specs)
4641
},
4742
],
4843
} as const;

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

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
import { readYaml } from "../../common/util";
1+
import { dirToSpecList, specsToOptions } from "../../common/util";
22
import _ from "lodash";
33
import path from "path";
4-
import { OpenAPIV3, OpenAPI } from "openapi-types";
5-
import { QueryConfig, ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource";
4+
import { OpenAPI } from "openapi-types";
5+
import { ConfigToType, DataSourcePlugin, QueryConfig } from "lowcoder-sdk/dataSource";
66
import { runOpenApi } from "../openApi";
7-
import { MultiOpenApiSpecItem, parseMultiOpenApi, ParseOpenApiOptions } from "../openApi/parse";
8-
import { appendTags } from "../../plugins/openApi/util";
9-
import { readdirSync } from "fs";
7+
import { parseMultiOpenApi, ParseOpenApiOptions } from "../openApi/parse";
108

11-
const specList: MultiOpenApiSpecItem[] = [];
12-
const specFiles = readdirSync(path.join(__dirname, "./did.spec"));
13-
const start = performance.now();
14-
specFiles.forEach((specFile) => {
15-
const spec = readYaml(path.join(__dirname, "./did.spec", specFile));
16-
const tag = _.upperFirst(specFile.replace(".json", ""));
17-
appendTags(spec, tag);
18-
specList.push({
19-
spec,
20-
id: tag,
21-
});
22-
});
23-
logger.info("did spec list loaded, duration: %d ms", performance.now() - start);
9+
const specs = {
10+
"v1.0": dirToSpecList(path.join(__dirname, "./did.spec")),
11+
}
2412

2513
const dataSourceConfig = {
2614
type: "dataSource",
@@ -50,16 +38,7 @@ const dataSourceConfig = {
5038
type: "select",
5139
tooltip: "Version of the spec file.",
5240
placeholder: "v1.0",
53-
options: [
54-
{
55-
value: "v1.0",
56-
label: "v1.0",
57-
},
58-
{
59-
value: "v2.0",
60-
label: "v2.0",
61-
}
62-
]
41+
options: specsToOptions(specs)
6342
},
6443
],
6544
} as const;
@@ -80,9 +59,9 @@ const didPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
8059
icon: "did.svg",
8160
category: "api",
8261
dataSourceConfig,
83-
queryConfig: async () => {
62+
queryConfig: async (data) => {
8463
if (!queryConfig) {
85-
const { actions, categories } = await parseMultiOpenApi(specList, parseOptions);
64+
const { actions, categories } = await parseMultiOpenApi(specs[data.specVersion as keyof typeof specs], parseOptions);
8665
queryConfig = {
8766
type: "query",
8867
label: "Action",
@@ -102,7 +81,7 @@ const didPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
10281
dynamicParamsConfig: dataSourceConfig,
10382
specVersion: dataSourceConfig.specVersion,
10483
};
105-
return runOpenApi(actionData, runApiDsConfig, specList);
84+
return runOpenApi(actionData, runApiDsConfig, specs[dataSourceConfig.specVersion as keyof typeof specs]);
10685
},
10786
};
10887

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { runOpenApi } from "../openApi";
55
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
66

77
import spec from "./front.spec.json";
8+
import { specsToOptions } from "../../common/util";
9+
const specs = {
10+
"v1.0": spec,
11+
"v2.0": spec,
12+
}
813

914
const dataSourceConfig = {
1015
type: "dataSource",
@@ -20,16 +25,7 @@ const dataSourceConfig = {
2025
type: "select",
2126
tooltip: "Version of the spec file.",
2227
placeholder: "v1.0",
23-
options: [
24-
{
25-
value: "v1.0",
26-
label: "v1.0",
27-
},
28-
{
29-
value: "v2.0",
30-
label: "v2.0",
31-
}
32-
]
28+
options: specsToOptions(specs)
3329
},
3430
],
3531
} as const;

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readYaml } from "../../common/util";
1+
import { readYaml, specsToOptions } from "../../common/util";
22
import _ from "lodash";
33
import path from "path";
44
import { OpenAPIV3, OpenAPI } from "openapi-types";
@@ -8,6 +8,10 @@ import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
88
import SwaggerParser from "@apidevtools/swagger-parser";
99

1010
const spec = readYaml(path.join(__dirname, "./github.spec.yaml"));
11+
const specs = {
12+
"v1.0": spec,
13+
"v2.0": spec,
14+
}
1115

1216
const dataSourceConfig = {
1317
type: "dataSource",
@@ -33,16 +37,7 @@ const dataSourceConfig = {
3337
type: "select",
3438
tooltip: "Version of the spec file.",
3539
placeholder: "v1.0",
36-
options: [
37-
{
38-
value: "v1.0",
39-
label: "v1.0",
40-
},
41-
{
42-
value: "v2.0",
43-
label: "v2.0",
44-
}
45-
]
40+
options: specsToOptions(specs)
4641
},
4742
],
4843
} as const;

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

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import { runOpenApi } from "../openApi";
55
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
66

77
import spec from "./huggingFace.spec.json";
8+
import { specsToOptions } from "../../common/util";
9+
const specs = {
10+
"v1.0": spec,
11+
"v2.0": spec,
12+
}
813

914
const dataSourceConfig = {
1015
type: "dataSource",
@@ -31,16 +36,7 @@ const dataSourceConfig = {
3136
type: "select",
3237
tooltip: "Version of the spec file.",
3338
placeholder: "v1.0",
34-
options: [
35-
{
36-
value: "v1.0",
37-
label: "v1.0",
38-
},
39-
{
40-
value: "v2.0",
41-
label: "v2.0",
42-
}
43-
]
39+
options: specsToOptions(specs)
4440
},
4541
],
4642
} as const;

0 commit comments

Comments
 (0)