-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·62 lines (53 loc) · 1.63 KB
/
index.js
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
59
60
61
62
#!/usr/bin/env node
const OpenAPI = require('openapi-typescript-codegen');
const { Command } = require('commander');
const program = new Command();
const { version } = require('./package.json')
let { defaultUrl, outDir, clientName } = require('./config.json');
program
.version(version, '-v, --version')
.description('Generate typescript files for the helldivers-2 community api')
.option('-u, --url <url string>', 'OpenApi spec url', `${defaultUrl}`)
.option('-o, --outDir <path>', 'Directory to write generated files to.', `${outDir}`)
.option('-c, --clientName <string>', 'Client class name', `${clientName}`)
.action((options) => {
let specUrl = defaultUrl;
if (options.url) {
try {
specUrl = new URL(options.url);
} catch(error) {
throw error;
}
}
if (options.outDir) {
outDir = options.outDir;
}
if (options.clientName) {
clientName = options.clientName;
}
const fetchOptions = {
method: "GET",
redirect: "follow",
headers: {
"Accept": "application/json",
}
};
fetch(specUrl, fetchOptions).then(response => {
if(!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
} else {
return response.json();
}
}).then((spec) => {
OpenAPI.generate({
input: spec,
output: outDir,
clientName: clientName
}).catch(error => {
throw error;
}).finally(_ => console.log("Done"));
}).catch(error => {
throw error;
});
});
program.parse();