-
Notifications
You must be signed in to change notification settings - Fork 268
/
clickhouseapi.js
167 lines (127 loc) · 5.43 KB
/
clickhouseapi.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
167
const axios = require('axios');
const fs = require('fs');
const apiEndpoint = 'https://api.clickhouse.cloud/v1';
async function fetchOpenAPISpec() {
try {
const response = await axios.get(apiEndpoint);
return response.data;
} catch (error) {
console.error(error);
return null;
}
}
function groupEndpointsByPrefix(spec) {
const groupedEndpoints = {};
for (const path in spec.paths) {
for (const method in spec.paths[path]) {
let prefix = path.split('/')[4];
if (!prefix || prefix === 'activities') {
prefix = 'organizations'
}
if (!groupedEndpoints[prefix]) {
groupedEndpoints[prefix] = {};
}
if (!groupedEndpoints[prefix][path]) {
groupedEndpoints[prefix][path] = {};
}
groupedEndpoints[prefix][path][method] = spec.paths[path][method];
}
}
return groupedEndpoints;
}
function generateDocusaurusMarkdown(spec, groupedEndpoints, prefix) {
let markdownContent = `---\nsidebar_label: ${prefix.charAt(0).toUpperCase() + prefix.slice(1)}\n`;
markdownContent += `title: ${prefix.charAt(0).toUpperCase() + prefix.slice(1)}\n---\n`;
for (const path in groupedEndpoints) {
for (const method in groupedEndpoints[path]) {
const operation = groupedEndpoints[path][method];
markdownContent += `\n## ${operation.summary}\n\n`;
markdownContent += `${operation.description}\n\n`;
markdownContent += `| Method | Path |\n`
markdownContent += `| :----- | :--- |\n`
markdownContent += `| ${method.toUpperCase()} | ${path} |\n\n`
markdownContent += `### Request\n\n`;
if (operation.parameters && operation.parameters.length > 0) {
markdownContent += `#### Path Params\n\n`;
markdownContent += `| Name | Type | Description |\n`
markdownContent += `| :--- | :--- | :---------- |\n`
for (const parameter of operation.parameters) {
markdownContent += `| ${parameter.name} | ${parameter.schema.format || parameter.schema.type || ''} | ${parameter.description || ''} | \n`
}
markdownContent += '\n'
}
if (operation.requestBody) {
markdownContent += `### Body Params\n\n`;
const schema = operation.requestBody.content["application/json"].schema['$ref'].split('/').pop()
const bodyParamAttrs = spec.components.schemas[schema].properties
const bodyParams = Object.keys(bodyParamAttrs)
markdownContent += `| Name | Type | Description |\n`
markdownContent += `| :--- | :--- | :---------- |\n`
for (const parameter of bodyParams) {
markdownContent += `| ${parameter} | ${bodyParamAttrs[parameter].type || bodyParamAttrs[parameter].format || ''} | ${bodyParamAttrs[parameter].description || ''} | \n`
}
}
if (operation.responses && operation.responses['200'].content["application/json"]) {
const rawSchema = operation.responses['200'].content["application/json"].schema
const result = rawSchema.properties.result
if (result) {
markdownContent += `\n### Response\n\n`;
markdownContent += `#### Response Schema\n\n`;
const schema = rawSchema.properties.result.type === 'array' ?
result.items['$ref'].split('/').pop() : result['$ref'].split('/').pop()
const bodyParamAttrs = spec.components.schemas[schema].properties
const bodyParams = Object.keys(bodyParamAttrs)
const sampleResponseObj = {}
markdownContent += `| Name | Type | Description |\n`
markdownContent += `| :--- | :--- | :---------- |\n`
for (const parameter of bodyParams) {
const paramType = bodyParamAttrs[parameter].format || bodyParamAttrs[parameter].type
markdownContent += `| ${parameter} | ${paramType || ''} | ${bodyParamAttrs[parameter].description || ''} | \n`
switch (paramType) {
case 'uuid':
sampleResponseObj[parameter] = 'uuid';
break;
case 'string':
sampleResponseObj[parameter] = 'string';
break;
case 'number':
sampleResponseObj[parameter] = 0;
break;
case 'array':
sampleResponseObj[parameter] = 'Array';
break;
case 'boolean':
sampleResponseObj[parameter] = 'boolean';
break;
case 'date-time':
sampleResponseObj[parameter] = 'date-time';
break;
case 'email':
sampleResponseObj[parameter] = 'email';
break;
}
}
markdownContent += `\n#### Sample response\n\n`;
markdownContent += '```\n'
markdownContent += `${JSON.stringify(sampleResponseObj, 0, 2)}`
markdownContent += '\n```\n'
}
}
}
}
return markdownContent;
}
async function main() {
const openAPISpec = await fetchOpenAPISpec();
if (!openAPISpec) {
console.error('Error fetching OpenAPI spec.');
return;
}
const groupedEndpoints = groupEndpointsByPrefix(openAPISpec);
for (const prefix in groupedEndpoints) {
const markdownContent = generateDocusaurusMarkdown(openAPISpec, groupedEndpoints[prefix], prefix);
fs.writeFileSync(`docs/en/cloud/manage/api/${prefix}-api-reference.md`, markdownContent);
}
console.log('Markdown files generated successfully.');
}
main();