-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathclickhouseapi.js
192 lines (155 loc) · 6.32 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
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
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`;
markdownContent += `slug: /cloud/manage/api/${prefix}-api-reference\n`;
markdownContent += `description: 'Cloud API reference documentation for ${prefix}'\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 extractedFields = extractFields(result, spec.components.schemas, undefined);
markdownContent += `| Name | Type | Description |\n`
markdownContent += `| :--- | :--- | :---------- |\n`
markdownContent += extractedFields.markdown
markdownContent += '\n'
markdownContent += `\n#### Sample response\n\n`;
markdownContent += '```\n'
markdownContent += `${JSON.stringify(extractedFields.json, 0, 2)}`
markdownContent += '\n```\n'
}
}
}
}
return markdownContent;
}
function extractFields(result, schemas, fieldPrefix) {
const schemaRef = result.type === 'array' ? result.items['$ref'].split('/').pop() : result['$ref'].split('/').pop();
const bodyParamAttrs = schemas[schemaRef].properties;
const bodyParams = Object.keys(bodyParamAttrs);
const resObj = {
markdown: '',
json: {}
}
for (const parameter of bodyParams) {
const newPrefix = fieldPrefix ? `${fieldPrefix}.${parameter}` : parameter;
if (bodyParamAttrs[parameter]['$ref']) {
const nestedObj = extractFields(bodyParamAttrs[parameter], schemas, newPrefix)
resObj.markdown += nestedObj.markdown
resObj.json[parameter] = nestedObj.json
}
else {
const paramType = bodyParamAttrs[parameter].format || bodyParamAttrs[parameter].type;
resObj.markdown += `| ${newPrefix} | ${paramType || ''} | ${bodyParamAttrs[parameter].description || ''} | \n`;
resObj.json[parameter] = returnParamTypeSample(bodyParamAttrs[parameter].format || bodyParamAttrs[parameter].type);
}
}
return resObj;
}
function returnParamTypeSample(paramType) {
let result;
switch(paramType) {
case 'uuid':
result = 'uuid';
break;
case 'string':
result = 'string';
break;
case 'number':
result = 0;
break;
case 'array':
result = 'Array';
break;
case 'boolean':
result = 'boolean';
break;
case 'date-time':
result = 'date-time';
break;
case 'date':
result = 'date';
break;
case 'email':
result = 'email';
break;
}
return result;
}
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/cloud/manage/api/${prefix}-api-reference.md`, markdownContent);
}
console.log('Markdown files generated successfully.');
}
main();