-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
247 lines (234 loc) · 9.37 KB
/
lib.js
File metadata and controls
247 lines (234 loc) · 9.37 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
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 = {};
let objs;
try {
objs = await fs.promises.readdir('./entities');
} catch (error) {
throw new Error(`Failed to read entities directory: ${error.message}`);
}
// Start with Common(s)
await Promise.all(objs.map(async (w) => {
// ignore everything but yaml and skip common
if(w.toLowerCase().includes('common.yml')) {
let data, obj;
try {
data = await fs.promises.readFile(`./entities/${w}`);
} catch (error) {
throw new Error(`Failed to read file 'entities/${w}': ${error.message}`);
}
try {
obj = yml.load(data);
} catch (error) {
throw new Error(`Invalid YAML syntax in 'entities/${w}': ${error.message}`);
}
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
let writes;
try {
writes = await fs.promises.readdir('./entities/writes');
} catch (error) {
throw new Error(`Failed to read entities/writes directory: ${error.message}`);
}
await Promise.all(writes.map(async (w) => {
// ignore everything but yaml
if(w.includes('.yml')) {
let data, obj;
const name = `write${w.charAt(0).toUpperCase()}${w.slice(1).replace('.yml', '')}`;
try {
data = await fs.promises.readFile(`./entities/writes/${w}`);
} catch (error) {
throw new Error(`Failed to read file 'entities/writes/${w}': ${error.message}`);
}
try {
obj = {};
obj[name] = yml.load(data);
} catch (error) {
throw new Error(`Invalid YAML syntax in 'entities/writes/${w}': ${error.message}`);
}
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')) {
let data, obj;
const name = `${w.replace('.yml', '')}Object`;
try {
data = await fs.promises.readFile(`./entities/${w}`);
} catch (error) {
throw new Error(`Failed to read file 'entities/${w}': ${error.message}`);
}
try {
obj = {};
obj[name] = yml.load(data);
} catch (error) {
throw new Error(`Invalid YAML syntax in 'entities/${w}': ${error.message}`);
}
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() {
try {
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));
} catch (error) {
throw new Error(`Schema generation failed: ${error.message}`);
}
},
async buildSwag() {
let swag = {};
// Load paths from paths/ directory
try {
const pathFiles = await fs.promises.readdir('./paths');
const pathPromises = pathFiles
.filter(f => f.endsWith('Paths.yml'))
.map(async (f) => {
try {
const data = await fs.promises.readFile(`./paths/${f}`);
return yml.load(data);
} catch (error) {
throw new Error(`Failed to load paths file 'paths/${f}': ${error.message}`);
}
});
const pathObjects = await Promise.all(pathPromises);
swag.paths = Object.assign({}, ...pathObjects);
// Load metadata if it exists
if (fs.existsSync('./paths/_metadata.yml')) {
try {
const metaData = await fs.promises.readFile('./paths/_metadata.yml');
const metadata = yml.load(metaData);
Object.assign(swag, metadata);
} catch (error) {
throw new Error(`Failed to load metadata file 'paths/_metadata.yml': ${error.message}`);
}
}
} catch (error) {
if (error.code === 'ENOENT') {
throw new Error(`Paths directory './paths' not found. Please create it and add *Paths.yml files.`);
}
throw error;
}
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() {
try {
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));
} catch (error) {
throw new Error(`Spec generation failed: ${error.message}`);
}
},
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;