- Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapply.ts
executable file
·132 lines (116 loc) · 4.23 KB
/
apply.ts
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
importfsfrom"fs/promises";
import{OpenAPIV3_1}from"openapi-types";
importargvfrom"yargs-parser";
functionfindObjectFromRef<T>(obj: T|OpenAPIV3_1.ReferenceObject,openapi: OpenAPIV3_1.Document): T{
constref=(objasOpenAPIV3_1.ReferenceObject).$ref;
if(ref===undefined){
returnobjasT;
}
constparamParts=ref.split("/");
paramParts.shift();// Remove the first part which is always '#'
letfoundObj: Record<string,unknown>=openapi;
while(true){
constpart=paramParts.shift();
if(!part){
break;
}
foundObj=foundObj[part]asRecord<string,unknown>;
}
returnfoundObjasT;
}
asyncfunctionmain(){
const{ spec, file }=argv(process.argv.slice(2));
if(!spec||!file){
console.error("Please provide both --spec and --file arguments.");
process.exit(1);
}
constspecFile=awaitfs.readFile(specasstring,"utf8");
constoperations: {
path: string;
method: string;
operationId: string;
requiredParams: boolean;
tag: string;
hasResponseBody: boolean;
}[]=[];
constopenapi=JSON.parse(specFile)asOpenAPIV3_1.Document;
for(constpathinopenapi.paths){
for(constmethodinopenapi.paths[path]){
// @ts-expect-error This is a workaround for the OpenAPI types
constoperation=openapi.paths[path][method]asOpenAPIV3_1.OperationObject;
if(!operation.operationId||!operation.tags?.length){
continue;
}
letrequiredParams=!!operation.requestBody;
lethasResponseBody=false;
for(constcodeinoperation.responses){
try{
consthttpCode=parseInt(code,10);
if(httpCode>=200&&httpCode<300){
constresponse=operation.responses[code];
constresponseObject=findObjectFromRef(response,openapi);
if(responseObject.content){
for(constcontentTypeinresponseObject.content){
constcontent=responseObject.content[contentType];
hasResponseBody=!!content.schema;
}
}
}
}catch{
continue;
}
}
for(constparamofoperation.parameters||[]){
constparamObject=findObjectFromRef(param,openapi);
if(paramObject.in==="path"){
requiredParams=true;
}
}
operations.push({
path,
method: method.toUpperCase(),
operationId: operation.operationId||"",
requiredParams,
hasResponseBody,
tag: operation.tags[0],
});
}
}
constoperationOutput=operations
.map((operation)=>{
const{ operationId, method, path, requiredParams, hasResponseBody }=operation;
return`async ${operationId}(options${requiredParams ? "" : "?"}: FetchOptions<operations["${operationId}"]>) {
${hasResponseBody ? `const { data } = ` : ``}await this.client.${method}("${path}", options);
${
hasResponseBody
? `return data;
`
: ``
}}
`;
})
.join("\n");
consttemplateFile=awaitfs.readFile(fileasstring,"utf8");
consttemplateLines=templateFile.split("\n");
constoutputLines: string[]=[];
letaddLines=true;
for(constlineoftemplateLines){
if(line.includes("DO NOT EDIT. This is auto-generated code.")){
addLines=!addLines;
outputLines.push(line);
if(!addLines){
outputLines.push(operationOutput);
}
continue;
}
if(addLines){
outputLines.push(line);
}
}
constoutput=outputLines.join("\n");
awaitfs.writeFile(fileasstring,output,"utf8");
}
main().catch((error)=>{
console.error("Error:",error);
process.exit(1);
});