-
Notifications
You must be signed in to change notification settings - Fork 1
/
build-openapi.js
31 lines (26 loc) · 1.16 KB
/
build-openapi.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const cp = require("child_process");
const { writeSpec, writeSpecAsTypeScript } = require("./utils/prepare-spec");
const prettier = require("prettier");
const specFile = path.resolve("src", "api", "openapi", "generated", "openapi.json");
const specTypeScriptFile = path.resolve("src", "api", "openapi", "generated", "spec.ts");
const typesFile = path.resolve("src", "api", "openapi", "generated", "client.d.ts");
async function downloadSpec() {
const response = await axios.get("https://en.wikipedia.org/api/rest_v1/?spec");
return response.data;
}
async function buildFromOpenAPI() {
const spec = await downloadSpec();
await writeSpec(specFile, spec);
await writeSpecAsTypeScript(specTypeScriptFile, spec);
await writeTypes(specFile, typesFile);
}
async function writeTypes(specFile, typesFile) {
const result = cp.execFileSync("typegen", [specFile]);
const dtsContents = "/* eslint-disable */\n" + result;
const formattedDts = prettier.format(dtsContents, { parser: "typescript" });
fs.writeFileSync(typesFile, formattedDts);
}
buildFromOpenAPI().catch(console.error);