1
- import fs , { writeFile } from "fs" ;
1
+ import fs from "fs" ;
2
2
import { OpenAPIV3_1 } from "openapi-types" ;
3
3
import argv from "yargs-parser" ;
4
4
import { promisify } from "util" ;
@@ -21,14 +21,14 @@ function findParamFromRef(ref: string, openapi: OpenAPIV3_1.Document): OpenAPIV3
21
21
}
22
22
23
23
async function main ( ) {
24
- const { spec, file} = argv ( process . argv . slice ( 2 ) ) ;
24
+ const { spec, file } = argv ( process . argv . slice ( 2 ) ) ;
25
25
26
26
if ( ! spec || ! file ) {
27
27
console . error ( "Please provide both --spec and --file arguments." ) ;
28
28
process . exit ( 1 ) ;
29
29
}
30
30
31
- const specFile = await readFileAsync ( spec , "utf8" ) as string ;
31
+ const specFile = ( await readFileAsync ( spec , "utf8" ) ) as string ;
32
32
33
33
const operations : {
34
34
path : string ;
@@ -42,7 +42,7 @@ async function main() {
42
42
for ( const path in openapi . paths ) {
43
43
for ( const method in openapi . paths [ path ] ) {
44
44
const operation : OpenAPIV3_1 . OperationObject = openapi . paths [ path ] [ method ] ;
45
-
45
+
46
46
if ( ! operation . operationId || ! operation . tags ?. length ) {
47
47
continue ;
48
48
}
@@ -63,24 +63,29 @@ async function main() {
63
63
operations . push ( {
64
64
path,
65
65
method : method . toUpperCase ( ) ,
66
- operationId : operation . operationId || '' ,
66
+ operationId : operation . operationId || "" ,
67
67
requiredParams,
68
68
tag : operation . tags [ 0 ] ,
69
69
} ) ;
70
70
}
71
71
}
72
-
73
- const operationOutput = operations . map ( ( operation ) => {
74
- const { operationId, method, path, requiredParams } = operation ;
75
- return `async ${ operationId } (options${ requiredParams ? '' : '?' } : FetchOptions<operations["${ operationId } "]>) {
72
+
73
+ const operationOutput = operations
74
+ . map ( ( operation ) => {
75
+ const { operationId, method, path, requiredParams } = operation ;
76
+ return `async ${ operationId } (options${ requiredParams ? "" : "?" } : FetchOptions<operations["${ operationId } "]>) {
76
77
const { data } = await this.client.${ method } ("${ path } ", options);
77
78
return data;
78
79
}
79
80
` ;
80
- } ) . join ( "\n" ) ;
81
+ } )
82
+ . join ( "\n" ) ;
81
83
82
- const templateFile = await readFileAsync ( file , "utf8" ) as string ;
83
- const output = templateFile . replace ( / \/ \/ D O N O T E D I T \. T h i s i s a u t o - g e n e r a t e d c o d e \. \n .* \/ \/ D O N O T E D I T \. T h i s i s a u t o - g e n e r a t e d c o d e \. / g, operationOutput ) ;
84
+ const templateFile = ( await readFileAsync ( file , "utf8" ) ) as string ;
85
+ const output = templateFile . replace (
86
+ / \/ \/ D O N O T E D I T \. T h i s i s a u t o - g e n e r a t e d c o d e \. \n .* \/ \/ D O N O T E D I T \. T h i s i s a u t o - g e n e r a t e d c o d e \. / g,
87
+ operationOutput
88
+ ) ;
84
89
85
90
await writeFileAsync ( file , output , "utf8" ) ;
86
91
}
0 commit comments