|
| 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 | +}); |
0 commit comments