Skip to content

Commit 4ab835c

Browse files
authored
Merge branch 'main' into gagik/add-explain
2 parents 688ad56 + 9286078 commit 4ab835c

File tree

4 files changed

+96
-15
lines changed

4 files changed

+96
-15
lines changed

scripts/apply.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import fs from "fs/promises";
2+
import { OpenAPIV3_1 } from "openapi-types";
3+
import argv from "yargs-parser";
4+
5+
function findParamFromRef(ref: string, openapi: OpenAPIV3_1.Document): OpenAPIV3_1.ParameterObject {
6+
const paramParts = ref.split("/");
7+
paramParts.shift(); // Remove the first part which is always '#'
8+
let param: any = openapi; // eslint-disable-line @typescript-eslint/no-explicit-any
9+
while (true) {
10+
const part = paramParts.shift();
11+
if (!part) {
12+
break;
13+
}
14+
param = param[part];
15+
}
16+
return param;
17+
}
18+
19+
async function main() {
20+
const { spec, file } = argv(process.argv.slice(2));
21+
22+
if (!spec || !file) {
23+
console.error("Please provide both --spec and --file arguments.");
24+
process.exit(1);
25+
}
26+
27+
const specFile = (await fs.readFile(spec, "utf8")) as string;
28+
29+
const operations: {
30+
path: string;
31+
method: string;
32+
operationId: string;
33+
requiredParams: boolean;
34+
tag: string;
35+
}[] = [];
36+
37+
const openapi = JSON.parse(specFile) as OpenAPIV3_1.Document;
38+
for (const path in openapi.paths) {
39+
for (const method in openapi.paths[path]) {
40+
const operation: OpenAPIV3_1.OperationObject = openapi.paths[path][method];
41+
42+
if (!operation.operationId || !operation.tags?.length) {
43+
continue;
44+
}
45+
46+
let requiredParams = !!operation.requestBody;
47+
48+
for (const param of operation.parameters || []) {
49+
const ref = (param as OpenAPIV3_1.ReferenceObject).$ref as string | undefined;
50+
let paramObject: OpenAPIV3_1.ParameterObject = param as OpenAPIV3_1.ParameterObject;
51+
if (ref) {
52+
paramObject = findParamFromRef(ref, openapi);
53+
}
54+
if (paramObject.in === "path") {
55+
requiredParams = true;
56+
}
57+
}
58+
59+
operations.push({
60+
path,
61+
method: method.toUpperCase(),
62+
operationId: operation.operationId || "",
63+
requiredParams,
64+
tag: operation.tags[0],
65+
});
66+
}
67+
}
68+
69+
const operationOutput = operations
70+
.map((operation) => {
71+
const { operationId, method, path, requiredParams } = operation;
72+
return `async ${operationId}(options${requiredParams ? "" : "?"}: FetchOptions<operations["${operationId}"]>) {
73+
const { data } = await this.client.${method}("${path}", options);
74+
return data;
75+
}
76+
`;
77+
})
78+
.join("\n");
79+
80+
const templateFile = (await fs.readFile(file, "utf8")) as string;
81+
const output = templateFile.replace(
82+
/\/\/ DO NOT EDIT\. This is auto-generated code\.\n.*\/\/ DO NOT EDIT\. This is auto-generated code\./g,
83+
operationOutput
84+
);
85+
86+
await fs.writeFile(file, output, "utf8");
87+
}
88+
89+
main().catch((error) => {
90+
console.error("Error:", error);
91+
process.exit(1);
92+
});

scripts/filter.ts

100644100755
File mode changed.

scripts/generate.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ curl -Lo ./scripts/spec.json https://github.com/mongodb/openapi/raw/refs/heads/m
66
tsx ./scripts/filter.ts > ./scripts/filteredSpec.json < ./scripts/spec.json
77
redocly bundle --ext json --remove-unused-components ./scripts/filteredSpec.json --output ./scripts/bundledSpec.json
88
openapi-typescript ./scripts/bundledSpec.json --root-types-no-schema-prefix --root-types --output ./src/common/atlas/openapi.d.ts
9-
prettier --write ./src/common/atlas/openapi.d.ts
9+
tsx ./scripts/apply.ts --spec ./scripts/bundledSpec.json --file ./src/common/atlas/apiClient.ts
10+
prettier --write ./src/common/atlas/openapi.d.ts ./src/common/atlas/apiClient.ts
1011
rm -rf ./scripts/bundledSpec.json ./scripts/filteredSpec.json ./scripts/spec.json

src/logger.ts

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fs from "fs";
1+
import fs from "fs/promises";
22
import { MongoLogId, MongoLogManager, MongoLogWriter } from "mongodb-log-writer";
33
import config from "./config.js";
44
import redact from "mongodb-redact";
@@ -98,20 +98,8 @@ class ProxyingLogger extends LoggerBase {
9898
const logger = new ProxyingLogger();
9999
export default logger;
100100

101-
async function mkdirPromise(path: fs.PathLike, options?: fs.Mode | fs.MakeDirectoryOptions) {
102-
return new Promise<string | undefined>((resolve, reject) => {
103-
fs.mkdir(path, options, (err, resultPath) => {
104-
if (err) {
105-
reject(err);
106-
} else {
107-
resolve(resultPath);
108-
}
109-
});
110-
});
111-
}
112-
113101
export async function initializeLogger(server: McpServer): Promise<void> {
114-
await mkdirPromise(config.logPath, { recursive: true });
102+
await fs.mkdir(config.logPath, { recursive: true });
115103

116104
const manager = new MongoLogManager({
117105
directory: config.logPath,

0 commit comments

Comments
 (0)