11export const SnippedGeneratorNodeJsPlugin = {
22 fn : {
3- // eslint-disable-next-line @typescript-eslint/no-explicit-any
4- requestSnippetGenerator_node_fetch : ( request : any ) => {
5- const url = new URL ( request . get ( 'url' ) ) ;
3+ requestSnippetGenerator_fetch : ( request : { get : ( key : string ) => unknown } ) => {
4+ const url = new URL ( request . get ( 'url' ) as string ) ;
65 let isMultipartFormDataRequest = false ;
7- const headers = request . get ( 'headers' ) ;
6+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
7+ const headers = request . get ( 'headers' ) as any ;
88 if ( headers && headers . size ) {
9- request . get ( ' headers' ) . map ( ( val : string , key : string ) => {
9+ headers . map ( ( val : string , key : string ) => {
1010 isMultipartFormDataRequest = isMultipartFormDataRequest || / ^ c o n t e n t - t y p e $ / i. test ( key ) && / ^ m u l t i p a r t \/ f o r m - d a t a $ / i. test ( val ) ;
1111 } ) ;
1212 }
13- let reqBody = request . get ( 'body' ) ;
14- if ( request . get ( 'body' ) ) {
15- if ( isMultipartFormDataRequest && [ 'POST' , 'PUT' , 'PATCH' ] . includes ( request . get ( 'method' ) ) ) {
13+ let reqBody = request . get ( 'body' ) as string | object ;
14+ const reqMethod = request . get ( 'method' ) as string ;
15+ if ( reqBody ) {
16+ if ( isMultipartFormDataRequest && [ 'POST' , 'PUT' , 'PATCH' ] . includes ( reqMethod ) ) {
1617 return 'throw new Error("Currently unsupported content-type: /^multipart\\/form-data$/i");' ;
1718 } else {
1819 if ( typeof reqBody !== 'string' ) {
1920 reqBody = JSON . stringify ( reqBody , null , '\t' ) ;
2021 }
2122 }
22- } else if ( ! request . get ( 'body' ) && request . get ( 'method' ) === 'POST' ) {
23+ } else if ( ! reqBody && reqMethod === 'POST' ) {
2324 reqBody = '' ;
2425 }
2526
@@ -28,19 +29,15 @@ export const SnippedGeneratorNodeJsPlugin = {
2829 . replace ( / ` / g, '\\`' )
2930 + '`' ;
3031
31- return `async function main() {
32- \tconst response = await fetch("${ url . toString ( ) } ", {
33- \t\tmethod: "${ request . get ( 'method' ) } ",${ headers && headers . size ? `
34- \t\theaders: {
35- \t\t\t${ request . get ( 'headers' ) . map ( ( val : string , key : string ) => `"${ key } ": "${ val } "` ) . valueSeq ( ) . join ( ',\n\t\t\t' ) }
36- \t\t},` : '' } ${ reqBody ? `
37- \t\tbody: JSON.stringify(${ stringBody } ),` : '' }
38- \t});
39- \tconst data = await response.json();
40- \tconsole.log(data);
41- }
42-
43- main().catch(console.error);
32+ return `const response = await fetch("${ url . toString ( ) } ", {
33+ \tmethod: "${ request . get ( 'method' ) } ",${ headers && headers . size ? `
34+ \theaders: {
35+ \t\t${ headers . map ( ( val : string , key : string ) => `"${ key } ": "${ val } "` ) . valueSeq ( ) . join ( ',\n\t\t' ) }
36+ \t},` : '' } ${ reqBody ? `
37+ \tbody: JSON.stringify(${ stringBody } ),` : '' }
38+ });
39+ const data = await response.json();
40+ console.log(data);
4441` ;
4542 } ,
4643 } ,
0 commit comments