Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c1d24b2

Browse files
sarikesunlei
authored andcommittedMar 7, 2023
feat: some api datasources based on open api
1 parent d3d5141 commit c1d24b2

36 files changed

+531783
-18
lines changed
 

‎server/node-service/package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
"dev": "nodemon --files src/server.ts",
1111
"start": "node ./build/src/server.js",
1212
"test": "jest",
13-
"build": "rm -rf build/ && tsc && yarn run copy",
14-
"copy": "copyfiles -u 1 src/**/*.!(ts|test.ts) build/src"
13+
"copy": "copyfiles -u 1 src/**/*.!(ts|test.ts) build/src",
14+
"genOpenApiPlugin": "npx ts-node --files ./scripts/openApiDataSourceGen.ts",
15+
"build": "rm -rf build/ && yarn test && tsc && cp -r src/static build/src/static"
1516
},
1617
"devDependencies": {
1718
"@types/jest": "^29.2.4",
19+
"commander": "^10.0.0",
1820
"copyfiles": "^2.4.1",
1921
"jest": "^29.3.1",
2022
"nock": "^13.3.0",
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { readYaml } from "../src/common/util";
2+
import fs from "fs";
3+
import _ from "lodash";
4+
import path from "path";
5+
import { authParamsConfig, retrieveSpec } from "../src/plugins/openApi/parse";
6+
import { program } from "commander";
7+
8+
async function gen(name: string, specUrl: string, pluginId?: string) {
9+
const id = pluginId ?? _.camelCase(name);
10+
const pluginDir = path.join(path.dirname(__dirname), "src/plugins", id);
11+
const pluginEntryFile = path.join(pluginDir, "index.ts");
12+
const pluginSpecYamlFile = path.join(pluginDir, `${id}.spec.yaml`);
13+
const pluginSpecJsonFile = path.join(pluginDir, `${id}.spec.json`);
14+
15+
console.info("start generate plugin, id:", id, "name:", name);
16+
17+
if (!fs.existsSync(pluginDir)) {
18+
fs.mkdirSync(pluginDir, { recursive: true });
19+
console.info(`plugin dir ${id} created.`);
20+
}
21+
22+
if (fs.existsSync(pluginEntryFile)) {
23+
console.info(`plugin: ${id} is already existed.`);
24+
return;
25+
}
26+
27+
// 1. fetch spec url
28+
let spec: any;
29+
const isYamlSpecExist = fs.existsSync(pluginSpecYamlFile);
30+
const isJsonSpecExist = fs.existsSync(pluginSpecJsonFile);
31+
if (!isYamlSpecExist && !isJsonSpecExist) {
32+
if (!specUrl) {
33+
console.error("specUrl is required to fetch OpenAPI spec.");
34+
return;
35+
}
36+
console.info(`start fetching:`, specUrl);
37+
const { spec } = await retrieveSpec(specUrl);
38+
fs.writeFileSync(pluginSpecJsonFile, JSON.stringify(spec, null, 2));
39+
console.info(`${name}spec.json saved`);
40+
} else {
41+
if (isJsonSpecExist) {
42+
const specJson = fs.readFileSync(pluginSpecJsonFile).toString();
43+
spec = JSON.parse(specJson);
44+
console.info("got spec from json file:", pluginSpecJsonFile);
45+
}
46+
if (isYamlSpecExist) {
47+
spec = readYaml(pluginSpecYamlFile);
48+
console.info("got spec from yaml file:", pluginSpecYamlFile);
49+
}
50+
}
51+
52+
if (!spec) {
53+
console.error("can not get spec");
54+
return;
55+
}
56+
57+
// 2. get data source params
58+
const dataSourceParams = await authParamsConfig(spec);
59+
60+
// 3. gen code
61+
const template = fs
62+
.readFileSync(path.join(__dirname, "./openApiDataSourceTemplate.tpl"))
63+
.toString();
64+
const data = {
65+
id,
66+
name,
67+
isJsonSpec: isJsonSpecExist,
68+
isYamlSpec: isYamlSpecExist,
69+
dataSourceParams: JSON.stringify(dataSourceParams, null, 2),
70+
};
71+
const compiledTemplate = _.template(template);
72+
const code = compiledTemplate(data);
73+
fs.writeFileSync(pluginEntryFile, code);
74+
console.info("success generate plugin:", pluginDir);
75+
}
76+
77+
const plugins = [
78+
// ["Jira", "https://developer.atlassian.com/cloud/jira/platform/swagger-v3.v3.json"],
79+
[],
80+
];
81+
82+
program
83+
.option("--post-man")
84+
.option("-n, --name <char>")
85+
.option("-i, --id [plugin id]")
86+
.option("--url [spec-download-url]");
87+
88+
program.parse();
89+
90+
const { name, url, postMan, pluginId } = program.opts();
91+
92+
console.info();
93+
gen(name, url, pluginId);
94+
console.info();
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 "openblocks-sdk/dataSource";
6+
import { runOpenApi } from "../openApi";
7+
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
8+
<% if (isJsonSpec) {%>
9+
import spec from './<%=id %>.spec.json';
10+
<% } %>
11+
12+
<% if (isYamlSpec) {%>
13+
const spec = readYaml(path.join(__dirname, "./<%=id %>.spec.yaml"));
14+
<% } %>
15+
16+
const dataSourceConfig = {
17+
type: "dataSource",
18+
params: <%=dataSourceParams %>
19+
} as const;
20+
21+
const parseOptions: ParseOpenApiOptions = {
22+
actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => {
23+
return _.upperFirst(operation.operationId || "");
24+
},
25+
};
26+
27+
type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>;
28+
29+
const <%=id %>Plugin: DataSourcePlugin<any, DataSourceConfigType> = {
30+
id: "<%=id %>",
31+
name: "<%=name %>",
32+
icon: "<%=id %>.svg",
33+
category: "api",
34+
dataSourceConfig,
35+
queryConfig: async () => {
36+
const { actions, categories } = await parseOpenApi(spec, parseOptions);
37+
return {
38+
type: "query",
39+
label: "Action",
40+
categories: {
41+
label: "Resources",
42+
items: categories,
43+
},
44+
actions,
45+
};
46+
},
47+
run: function (actionData, dataSourceConfig): Promise<any> {
48+
const runApiDsConfig = {
49+
url: "",
50+
serverURL: "",
51+
dynamicParamsConfig: dataSourceConfig,
52+
};
53+
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document);
54+
},
55+
};
56+
57+
export default <%=id %>Plugin;
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.