forked from lgwebdream/fe-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetOAS.js
More file actions
46 lines (41 loc) · 1.32 KB
/
getOAS.js
File metadata and controls
46 lines (41 loc) · 1.32 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
const path = require('path');
const axios = require('axios');
const { load } = require('js-yaml');
const RefParser = require('json-schema-ref-parser');
const { exists, readFile } = require('../../utils/fileSystem');
const { oasGenStatus } = require('./oraState');
const readSpecFromDisk = async input => {
const filePath = path.resolve(process.cwd(), input);
const fileExists = await exists(filePath);
if (fileExists) {
try {
const content = await readFile(filePath, 'utf8');
return JSON.parse(content.toString());
} catch (e) {
throw new Error(`Could not read OpenApi spec: "${filePath}"`);
}
}
throw new Error(`Could not find OpenApi spec: "${filePath}"`);
};
const readSpecFromNetwork = url =>
new Promise((resolve, reject) => {
axios
.get(url)
.then(res => resolve(res.data))
.catch(err => reject(new Error(err)));
});
const readOAS = input => {
return /https?:\/\//.test(input)
? readSpecFromNetwork(input)
: readSpecFromDisk(input);
};
const getOAS = async input => {
oasGenStatus.read();
const extension = path.extname(input).toLowerCase();
const content = await readOAS(input); // get OAS from network or disk
oasGenStatus.parse();
return RefParser.dereference(
/.ya?ml$/.test(extension) ? load(content) : content,
);
};
module.exports = getOAS;