-
Notifications
You must be signed in to change notification settings - Fork 6
/
getSwagger.js
53 lines (43 loc) · 1.28 KB
/
getSwagger.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
const axios = require('axios')
const fs = require('fs');
const converter = require('swagger2openapi');
const config = require('./data/config')
async function convertSwaggerToOpenAPI (swagger, path, url) {
const options = {
patch: true,
direct: true,
url
}
await converter.convertStream(swagger, options, function(err, output){
jsonContent = JSON.stringify(output, null, 2)
fs.writeFile(path, jsonContent, 'utf8', function (err) {
if (err) {
console.log(`An error was encountered with ${path}`);
return console.log(err);
}
console.log(`${path} has been saved`);
});
})
}
async function download (url, path) {
const response = await axios({
url,
method: 'GET',
responseType: 'stream'
})
await convertSwaggerToOpenAPI(response.data, path, url)
}
async function getSwaggerFiles () {
const swaggerFiles = config.endpointSwaggerFiles
let baseURL = 'https://institution-api.clearbank.co.uk'
if (process.env.SWAGGER_URL) {
baseURL = process.env.SWAGGER_URL
}
// Get all swagger files and save locally
await swaggerFiles.forEach(swaggerFile => {
const url = `${baseURL}${swaggerFile.path}`
const path = `./data/endpoints/${swaggerFile.name}.json`
download(url, path)
})
}
getSwaggerFiles()