-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi.ts
49 lines (36 loc) · 1.2 KB
/
openapi.ts
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
import dns from 'dns';
import fs from 'fs';
import path from 'path';
import {generateAPIMethods, generateTypes} from './openapi.utils';
const OPENAPI_ENDPOINT =
process.env.CORTEX_API_URL || 'https://api.dev.withcortex.ai';
dns.setDefaultResultOrder('ipv4first');
const generate = async () => {
try {
const apiContractUrl = `${OPENAPI_ENDPOINT}/openapi-api.json`;
const openapiSchema = await (await fetch(apiContractUrl)).json();
console.log(
'\x1b[32m%s\x1b[0m',
`🎉 OpenAPI schema fetched from ${apiContractUrl}`,
);
const openapiTypes = await generateTypes(openapiSchema);
console.log('\x1b[32m%s\x1b[0m', `🎉 Successfully generated schema types`);
//
const apiMethods = generateAPIMethods(openapiSchema);
console.log('\x1b[32m%s\x1b[0m', `🎉 Successfully generated API types`);
//
const typesPath = path.resolve('./src/generated/openapi.ts');
fs.writeFileSync(
typesPath,
`// This file is auto-generated. Do not edit.\n\n
${openapiTypes}\n\n${apiMethods}`,
);
console.log(
'\x1b[32m%s\x1b[0m',
`🎉 Successfully generated at ${typesPath}`,
);
} catch (e) {
console.error(e);
}
};
generate();