forked from Lilly-Protocol/lily-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenapi-generator.ts
More file actions
41 lines (37 loc) · 1.38 KB
/
Copy pathopenapi-generator.ts
File metadata and controls
41 lines (37 loc) · 1.38 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
export interface OpenApiSpec {
openapi: string;
info: { title: string; version: string };
paths: Record<string, Record<string, any>>;
components?: { schemas?: Record<string, any> };
}
export interface ClientContract {
endpoint: string;
method: string;
operationId: string;
requestType?: string;
responseType?: string;
parameters: { name: string; in: string; required: boolean; type: string }[];
}
export function generateContracts(spec: OpenApiSpec): ClientContract[] {
const contracts: ClientContract[] = [];
for (const [path, methods] of Object.entries(spec.paths || {})) {
for (const [method, operation] of Object.entries(methods)) {
if (!['get', 'post', 'put', 'patch', 'delete'].includes(method)) continue;
const op = operation as any;
contracts.push({
endpoint: path,
method: method.toUpperCase(),
operationId: op.operationId || `${method}_${path.replace(/[{}\/]/g, '_')}`,
requestType: op.requestBody?.content?.['application/json']?.schema?.$ref?.split('/')?.pop(),
responseType: op.responses?.['200']?.content?.['application/json']?.schema?.$ref?.split('/')?.pop(),
parameters: (op.parameters || []).map((p: any) => ({
name: p.name,
in: p.in,
required: p.required || false,
type: p.schema?.type || 'string',
})),
});
}
}
return contracts;
}