-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
166 lines (158 loc) · 6.33 KB
/
lib.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const fs = require('fs');
const yml = require('js-yaml');
const Ref = require("@apidevtools/json-schema-ref-parser");
const merge = require('json-schema-resolve-allof');
let parser = new Ref();
const factory = {
async parseEntities() {
let schemas = {};
const objs = await fs.promises.readdir('./entities');
// Start with Common(s)
await Promise.all(objs.map(async (w) => {
// ignore everything but yaml and skip common
if(w.toLowerCase().includes('common.yml')) {
const data = await fs.promises.readFile(`./entities/${w}`);
const obj = yml.load(data);
let objDefs = JSON.parse(JSON.stringify(obj.definitions).replace(/[a-z]*common\.yml#\/definitions/gi, '#/components/schemas'));
objDefs = await fixRefs(objDefs);
Object.assign(schemas, objDefs);
}
}))
// Get all writes
const writes = await fs.promises.readdir('./entities/writes');
await Promise.all(writes.map(async (w) => {
// ignore everything but yaml
if(w.includes('.yml')) {
const data = await fs.promises.readFile(`./entities/writes/${w}`);
const name = `write${w.charAt(0).toUpperCase()}${w.slice(1).replace('.yml', '')}`;
let obj = {};
obj[name] = yml.load(data);
if(obj[name] !== null) {
// Fix common references
obj = JSON.parse(JSON.stringify(obj).replace(/\.\.\/[a-z]*common\.yml#\/definitions/gi, '#/components/schemas'));
obj = await fixRefs(obj, true);
Object.assign(schemas, obj)
}
}
}))
// Get all Objects
await Promise.all(objs.map(async (w) => {
// ignore everything but yaml and skip common
if(w.toLowerCase().includes('.yml') && !w.toLowerCase().includes('common.yml')) {
const data = await fs.promises.readFile(`./entities/${w}`);
const name = `${w.replace('.yml', '')}Object`;
let obj = {};
obj[name] = yml.load(data);
if(obj[name] === null) console.info('obj[name] was null: ', name);
obj = JSON.parse(JSON.stringify(obj).replace(/[a-z]*common\.yml#\/definitions/gi, '#/components/schemas'));
obj = await fixRefs(obj);
Object.assign(schemas, obj)
}
}))
return schemas;
},
async genSchema() {
const schemas = await this.parseEntities();
if(fs.existsSync('./openApiSchemas.yml')) {
await fs.promises.unlink('./openApiSchemas.yml');
}
await fs.promises.writeFile('./openApiSchemas.yml', yml.dump(schemas));
},
async buildSwag() {
const swag = yml.load(await fs.promises.readFile('./openApiPaths.yml'));
const schemas = await this.parseEntities();
// setup swagger spec component section if it's not there...
if(!swag.components) swag.components = {
securitySchemes: {
"bearer": {
"type": "http",
"scheme": "bearer",
"description": "Bearer based tokens, simply enter the token (prefixing with \"bearer\" is not required)."
},
"basicAuth": {
"type": "http",
"scheme": "basic"
},
"openId": {
"type": "openIdConnect",
"openIdConnectUrl": "https://example.com/.well-known/openid-configuration"
},
"OAuth2": {
"type": "oauth2",
"flows": {
"authorizationCode": {
"authorizationUrl": "https://example.com/oauth/authorize",
"tokenUrl": "https://example.com/oauth/token",
"scopes": {
"read": "Grants read access",
"write": "Grants write access",
"admin": "Grants access to admin operations"
}
}
}
}
},
schemas: {}
}
swag.components.schemas = schemas;
return swag;
},
async genSpec() {
const swag = await this.buildSwag();
if(fs.existsSync('./openApi.yml')) {
await fs.promises.unlink('./openApi.yml');
}
await fs.promises.writeFile('./openApi.yml', yml.dump(swag));
},
async showObject(path) {
try {
let schema = await merge(await parser.dereference(path));
console.info(JSON.stringify(schema, null, 2));
} catch (error) {
console.info(error.toJSON());
}
}
}
function fixWrite(ref) {
if(typeof ref === 'string') {
const parts = ref.split('/');
const obj = parts[parts.length-1];
return `#/components/schemas/write${obj.charAt(0).toUpperCase()}${obj.slice(1).replace('.yml', '')}`
}
return ref;
}
function fixObj(ref, writeLocal = false) {
if(typeof ref === 'string') {
if(writeLocal === true) {
return `#/components/schemas/write${ref.charAt(0).toUpperCase()}${ref.slice(1).replace('.yml', '')}`
}
return `#/components/schemas/${ref.replace('.yml', '')}Object`
}
return ref;
}
async function fixRefs(obj, localToWrites = false) {
const out = JSON.parse(JSON.stringify(obj));
await Promise.allSettled(Object.keys(obj).map(async (key) => {
if(key === '$ref') {
if(obj[key].includes('writes/')) {
out[key] = fixWrite(obj[key]);
}
if(!obj[key].includes('/') && obj[key].includes('.yml')) {
out[key] = fixObj(obj[key], localToWrites);
}
}
if(typeof obj[key] === 'object') {
if(Array.isArray(obj[key])) {
await Promise.allSettled(obj[key].map(async (o, i) => {
out[key][i] = await fixRefs(o);
return o;
}))
} else {
out[key] = await fixRefs(obj[key]);
}
}
return key;
}));
return out;
}
module.exports = factory;