all repos — kitten @ df5d61dd2bc1bd8a2caa0321c06671ae5e84383b

pkgs/types/src/lib/index.ts (view raw)

 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
 32
 33
 34
import * as fs from "fs/promises";
import * as path from "path";
import { OpenAPISpec } from "./types";
import { generateTypeScriptSpec } from "./generator";

const SPEC_URL =
	"https://raw.githubusercontent.com/discord/discord-api-spec/main/specs/openapi.json";

async function main() {
	console.log("fetching OpenAPI spec...");
	const res = await fetch(SPEC_URL);

	if (!res.ok) {
		throw new Error(`failed to fetch spec: ${res.status}`);
	}

	const spec = (await res.json()) as OpenAPISpec;

	console.log("generating typescript code...");
	const schemas = spec.components?.schemas || {};
	const paths = spec.paths || {};

	const generatedCode = generateTypeScriptSpec(schemas, paths);

	const outputPath = path.join(process.cwd(), "discord-api-types.ts");
	await fs.writeFile(outputPath, generatedCode, "utf-8");

	console.log(`generated types at: ${outputPath}`);
}

main().catch((err) => {
	console.error("fatal error during generation:", err);
	process.exit(1);
});