1- import fs , { writeFile } from "fs" ;
1+ import fs from "fs" ;
22import { OpenAPIV3_1 } from "openapi-types" ;
33import argv from "yargs-parser" ;
44import { promisify } from "util" ;
@@ -21,14 +21,14 @@ function findParamFromRef(ref: string, openapi: OpenAPIV3_1.Document): OpenAPIV3
2121}
2222
2323async function main ( ) {
24- const { spec, file} = argv ( process . argv . slice ( 2 ) ) ;
24+ const { spec, file } = argv ( process . argv . slice ( 2 ) ) ;
2525
2626 if ( ! spec || ! file ) {
2727 console . error ( "Please provide both --spec and --file arguments." ) ;
2828 process . exit ( 1 ) ;
2929 }
3030
31- const specFile = await readFileAsync ( spec , "utf8" ) as string ;
31+ const specFile = ( await readFileAsync ( spec , "utf8" ) ) as string ;
3232
3333 const operations : {
3434 path : string ;
@@ -42,7 +42,7 @@ async function main() {
4242 for ( const path in openapi . paths ) {
4343 for ( const method in openapi . paths [ path ] ) {
4444 const operation : OpenAPIV3_1 . OperationObject = openapi . paths [ path ] [ method ] ;
45-
45+
4646 if ( ! operation . operationId || ! operation . tags ?. length ) {
4747 continue ;
4848 }
@@ -63,24 +63,29 @@ async function main() {
6363 operations . push ( {
6464 path,
6565 method : method . toUpperCase ( ) ,
66- operationId : operation . operationId || '' ,
66+ operationId : operation . operationId || "" ,
6767 requiredParams,
6868 tag : operation . tags [ 0 ] ,
6969 } ) ;
7070 }
7171 }
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 } "]>) {
7677 const { data } = await this.client.${ method } ("${ path } ", options);
7778 return data;
7879}
7980` ;
80- } ) . join ( "\n" ) ;
81+ } )
82+ . join ( "\n" ) ;
8183
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+ ) ;
8489
8590 await writeFileAsync ( file , output , "utf8" ) ;
8691}
0 commit comments