-
Notifications
You must be signed in to change notification settings - Fork 9
/
get-all-data.js
81 lines (72 loc) · 2.38 KB
/
get-all-data.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
const fetch = require(`node-fetch`);
const SwaggerParser = require('@apidevtools/swagger-parser');
const PARAMETER_TYPES = {
path: 'path',
query: 'query',
body: 'body',
};
const getAllData = async () => {
const dataSwagger = await fetch('https://api.novu.co/api-json').then((response) =>
response.json()
);
const { paths, tags } = await SwaggerParser.dereference(dataSwagger);
const pages = Object.keys(paths)
.map((path) => ({
endpoint: path.replace(/{/g, ':').replace(/}/g, ''),
methods: Object.keys(paths[path]).map((method) => ({
...paths[path][method],
id: `${method}-${path}`,
method,
endpoint: path.replace(/{/g, ':').replace(/}/g, ''),
tag: paths[path][method]?.tags?.[0],
slug: paths[path][method]?.summary?.replace(/\s/g, '-').toLowerCase(),
parameters: {
path: paths[path][method].parameters.filter((param) => param.in === PARAMETER_TYPES.path),
query: paths[path][method].parameters.filter(
(param) => param.in === PARAMETER_TYPES.query
),
body: paths[path][method].requestBody?.content['application/json'].schema,
},
responses: Object.keys(paths[path][method].responses).map((status) => {
const schema =
paths[path][method].responses[status]?.content?.['application/json']?.schema;
return {
status,
description: paths[path][method].responses[status].description,
schema,
};
}),
})),
}))
.filter((page) => page.methods.some((method) => method.tag && method.summary))
.sort((a, b) => {
const aIndex = tags.findIndex((tag) => tag.name === a.methods[0].tag);
const bIndex = tags.findIndex((tag) => tag.name === b.methods[0].tag);
return aIndex - bIndex;
});
const methods = pages.reduce((acc, path) => {
path.methods.forEach((method) => {
acc.push(method);
});
return acc;
}, []);
const menu = tags
.map((tag) => ({
label: tag.name,
path: tag.name,
subItems: methods
.filter((method) => method.tags.includes(tag.name))
.map((method) => ({
label: method.summary,
path: method.slug,
...method,
})),
}))
.filter((tag) => tag.subItems.length > 0);
return {
pages,
menu,
methods,
};
};
module.exports = getAllData;