-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync-api.ts
More file actions
58 lines (47 loc) · 1.54 KB
/
Copy pathsync-api.ts
File metadata and controls
58 lines (47 loc) · 1.54 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* Fetches the OpenAPI spec from the Chatterbox TTS API and generates TypeScript types.
*
* Usage:
* CHATTERBOX_API_URL=https://your-api-url npm run sync-api
*
* Or with .env file:
* npm run sync-api
*/
import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import "dotenv/config";
import openapiTS, { astToString } from "openapi-typescript";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const OUTPUT_PATH = path.resolve(__dirname, "../src/types/chatterbox-api.d.ts");
async function main() {
const apiUrl = process.env.CHATTERBOX_API_URL;
if (!apiUrl) {
console.error("Error: CHATTERBOX_API_URL environment variable is required");
process.exit(1);
}
const openApiUrl = `${apiUrl}/openapi.json`;
console.log(`Fetching OpenAPI spec from: ${openApiUrl}`);
const ast = await openapiTS(new URL(openApiUrl));
const contents = astToString(ast);
// Ensure output directory exists
const outputDir = path.dirname(OUTPUT_PATH);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// Add header comment
const header = `/**
* This file is auto-generated by scripts/sync-api.ts
* Do not edit manually. Run \`npm run sync-api\` to regenerate.
*
* Generated from: ${openApiUrl}
* Generated at: ${new Date().toISOString()}
*/
`;
fs.writeFileSync(OUTPUT_PATH, header + contents);
console.log(`Types written to: ${OUTPUT_PATH}`);
}
main().catch((err) => {
console.error("Failed to sync API types:", err);
process.exit(1);
});