-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSwagger.ts
56 lines (44 loc) · 1.81 KB
/
getSwagger.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
50
51
52
53
54
55
56
const https = require('https');
const fs = require('fs');
const yaml = require('js-yaml');
const sourceUrls = {
apiSpec: 'https://test.dochorizon.klippa.com/api/open-api.yaml',
prodApiSpec: 'https://dochorizon.klippa.com/api/open-api.yaml'
}
const filePaths = {
'apiYaml': `${__dirname}/swaggerData.yaml`,
'apiJson': `${__dirname}/api.json`,
'apiJsonTest': `${__dirname}/api_test.json`
}
interface SwaggerOptions {
useTestUrl?: boolean;
production?: boolean;
}
/*
Method that gets the open api spec and turns it into a json
in the future this json will be used for gathering info about endpoints
getting the right schemas to create post payloads, etc.
*/
export async function getSwagger(options: SwaggerOptions = { useTestUrl: false, production: false }){
const { useTestUrl, production } = options;
const apiSpec = production ? sourceUrls.prodApiSpec : sourceUrls.apiSpec;
return new Promise<void>((resolve) => https.get(apiSpec, (res: any) => {
// create write stream for yaml file
const yamlData = fs.createWriteStream(filePaths.apiYaml);
//pipe all results to the yaml file
res.pipe(yamlData);
yamlData.on('finish', () => {
yamlData.close();
//create JSON and convert the YAML to JSON
const obj = yaml.load(fs.readFileSync(filePaths.apiYaml), 'utf-8');
const path = useTestUrl ? filePaths.apiJsonTest : filePaths.apiJson;
fs.writeFileSync(path, JSON.stringify(obj, null, 2));
//then, remove the yaml file (cleanup)
fs.unlinkSync(filePaths.apiYaml);
resolve();
});
}));
}
const useTestUrl = (process.argv[2] === "true");
const production = (process.argv[3] === "true");
getSwagger({useTestUrl, production}).finally(() => console.log("done!"));