32 lines
1.3 KiB
JavaScript
32 lines
1.3 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
// Downloads the Jortt OpenAPI specification into src/openapi.json so it can be
|
||
|
|
// bundled with the build. Run automatically by `npm run build`.
|
||
|
|
import { writeFile, mkdir } from "node:fs/promises";
|
||
|
|
import { dirname, join } from "node:path";
|
||
|
|
import { fileURLToPath } from "node:url";
|
||
|
|
|
||
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
||
|
|
const SPEC_URL = process.env.JORTT_OPENAPI_URL || "https://api.jortt.nl/swagger_doc";
|
||
|
|
const target = join(__dirname, "..", "src", "openapi.json");
|
||
|
|
|
||
|
|
async function main() {
|
||
|
|
process.stderr.write(`Fetching Jortt OpenAPI spec from ${SPEC_URL} ...\n`);
|
||
|
|
const res = await fetch(SPEC_URL, { headers: { Accept: "application/json" } });
|
||
|
|
if (!res.ok) {
|
||
|
|
throw new Error(`HTTP ${res.status} ${res.statusText}`);
|
||
|
|
}
|
||
|
|
const spec = await res.json();
|
||
|
|
if (!spec.paths || typeof spec.paths !== "object") {
|
||
|
|
throw new Error("Downloaded file does not look like an OpenAPI spec (no paths).");
|
||
|
|
}
|
||
|
|
await mkdir(dirname(target), { recursive: true });
|
||
|
|
await writeFile(target, JSON.stringify(spec));
|
||
|
|
const count = Object.keys(spec.paths).length;
|
||
|
|
process.stderr.write(`Wrote ${target} (${count} paths).\n`);
|
||
|
|
}
|
||
|
|
|
||
|
|
main().catch((err) => {
|
||
|
|
process.stderr.write(`Failed to fetch spec: ${err.message}\n`);
|
||
|
|
process.exit(1);
|
||
|
|
});
|