Skip to content

Commit d1e6f92

Browse files
committed
Optimize the runOpenApi plugin
allow passing in an already dereferenced OpenAPI Document for plugins that have large openapi specs like github. This takes the runtime for Github from over 10s to ~1s on an M1 Max. Adjust the github plugin to use this new capability by dereferencing on plugin initialization, passing the derefed document to the runOpenApi function.
1 parent 4f80cae commit d1e6f92

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { OpenAPIV3, OpenAPI } from "openapi-types";
55
import { ConfigToType, DataSourcePlugin } from "lowcoder-sdk/dataSource";
66
import { runOpenApi } from "../openApi";
77
import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse";
8+
import SwaggerParser from "@apidevtools/swagger-parser";
89

910
const spec = readYaml(path.join(__dirname, "./github.spec.yaml"));
1011

@@ -34,6 +35,7 @@ const parseOptions: ParseOpenApiOptions = {
3435
return _.upperFirst(operation.operationId || "");
3536
},
3637
};
38+
const deRefedSpec = SwaggerParser.dereference(spec);
3739

3840
type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>;
3941

@@ -55,13 +57,13 @@ const gitHubPlugin: DataSourcePlugin<any, DataSourceConfigType> = {
5557
actions,
5658
};
5759
},
58-
run: function (actionData, dataSourceConfig): Promise<any> {
60+
run: async function (actionData, dataSourceConfig, ctx): Promise<any> {
5961
const runApiDsConfig = {
6062
url: "",
6163
serverURL: "https://api.github.com",
6264
dynamicParamsConfig: dataSourceConfig,
6365
};
64-
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document);
66+
return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document, undefined, await deRefedSpec);
6567
},
6668
};
6769

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,28 @@ export async function runOpenApi(
5454
actionData: ActionDataType,
5555
dataSourceConfig: DataSourceDataType,
5656
spec: OpenAPI.Document | MultiOpenApiSpecItem[],
57-
defaultHeaders?: Record<string, string>
57+
defaultHeaders?: Record<string, string>,
58+
openApiSpecDereferenced?: OpenAPI.Document,
5859
) {
5960
const specList = Array.isArray(spec) ? spec : [{ spec, id: "" }];
60-
const definitions = await Promise.all(
61-
specList.map(async ({ id, spec }) => {
62-
const deRefedSpec = await SwaggerParser.dereference(spec);
63-
return {
64-
def: deRefedSpec,
65-
id,
66-
};
67-
})
68-
);
61+
let definitions;
62+
63+
if (!openApiSpecDereferenced) {
64+
definitions = await Promise.all(
65+
specList.map(async ({id, spec}) => {
66+
const deRefedSpec = await SwaggerParser.dereference(spec);
67+
return {
68+
def: deRefedSpec,
69+
id,
70+
};
71+
})
72+
);
73+
} else {
74+
definitions = [{
75+
def: openApiSpecDereferenced,
76+
id: "",
77+
}]
78+
}
6979
const { actionName, ...otherActionData } = actionData;
7080
const { serverURL } = dataSourceConfig;
7181

0 commit comments

Comments
 (0)