-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
141 lines (114 loc) · 3.58 KB
/
index.js
File metadata and controls
141 lines (114 loc) · 3.58 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
import { readFileSync } from 'fs';
export function parseOpenAPISpec(specPath) {
const spec = JSON.parse(readFileSync(specPath, 'utf8'));
const endpoints = [];
for (const [path, pathItem] of Object.entries(spec.paths)) {
for (const [method, operation] of Object.entries(pathItem)) {
const endpoint = {
path,
method: method.toUpperCase(),
operationId: operation.operationId,
requestBody: operation.requestBody,
responses: operation.responses,
tags: operation.tags
};
endpoints.push(endpoint);
}
}
return {
info: spec.info,
endpoints,
schemas: spec.components?.schemas || {}
};
}
export function generateFetchClient(parsedSpec, options = {}) {
const { info, endpoints, schemas } = parsedSpec;
const className = options.className || 'ApiClient';
let clientCode = `class ${className} {
constructor(baseUrl = '', options = {}) {
this.baseUrl = baseUrl;
this.defaultOptions = {
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
};
}
async request(path, options = {}) {
const url = this.baseUrl + path;
const config = {
...this.defaultOptions,
...options,
headers: {
...this.defaultOptions.headers,
...options.headers
}
};
const response = await fetch(url, config);
if (!response.ok) {
throw new Error(\`HTTP error! status: \${response.status}\`);
}
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return await response.json();
} else if (contentType && contentType.includes('text/')) {
return await response.text();
} else {
return response;
}
}
`;
for (const endpoint of endpoints) {
let methodName = endpoint.operationId;
if (!methodName) {
// Generate camelCase method name from path and method
const pathParts = endpoint.path.split('/').filter(part => part && !part.startsWith('{'));
// Convert each part to PascalCase and join
const pascalParts = pathParts.map(part => part.charAt(0).toUpperCase() + part.slice(1));
const cleanPath = pascalParts.join('');
methodName = `${endpoint.method.toLowerCase()}${cleanPath}`;
}
// Convert to camelCase if not already
methodName = methodName.charAt(0).toLowerCase() + methodName.slice(1);
const hasRequestBody = endpoint.requestBody &&
endpoint.requestBody.content &&
Object.keys(endpoint.requestBody.content).length > 0;
const params = hasRequestBody ? 'data' : '';
const methodParams = params ? `(${params})` : '()';
clientCode += ` async ${methodName}${methodParams} {
`;
if (hasRequestBody) {
clientCode += ` return await this.request('${endpoint.path}', {
method: '${endpoint.method}',
body: JSON.stringify(data)
});
`;
} else {
clientCode += ` return await this.request('${endpoint.path}', {
method: '${endpoint.method}'
});
`;
}
clientCode += ` }
`;
}
clientCode += `}
export default ${className};`;
return clientCode;
}
export function generate(inputPath, outputPath, options = {}) {
const parsedSpec = parseOpenAPISpec(inputPath);
const clientCode = generateFetchClient(parsedSpec, options);
if (outputPath) {
import('fs').then(({ writeFileSync }) => {
writeFileSync(outputPath, clientCode, 'utf8');
});
}
return clientCode;
}
export default {
parseOpenAPISpec,
generateFetchClient,
generate
};