@@ -34,14 +34,20 @@ function run(urlStr, body, options) {
34
34
return res . json ( )
35
35
} )
36
36
. then ( json => {
37
- const struct = jsonToGo ( JSON . stringify ( json ) , path . struct ) ;
38
- const content = buildContent ( struct . go , path , comment )
39
- write ( json , path , content )
40
- } , ( ) => {
41
- console . log ( )
42
- console . log ( "Response Body is empty." )
37
+ const struct = jsonToGo ( JSON . stringify ( json ) , path . struct ) ;
38
+ const content = buildContent ( struct . go , path , comment )
39
+ write ( json , path , content )
40
+ if ( opts ?. body ) {
41
+ const paramStruct = jsonToGo ( opts ?. body , path . struct + "Param" ) ;
42
+ const paramContent = buildContent (
43
+ paramStruct . go , path , buildComment ( url , path , opts . method ) , true
44
+ )
45
+ writeParam ( JSON . stringify ( JSON . parse ( opts ?. body ) , null , "\t" ) , path , paramContent )
43
46
}
44
- )
47
+ } , ( ) => {
48
+ console . log ( )
49
+ console . log ( "Response Body is empty." )
50
+ } )
45
51
. catch ( ( error ) => {
46
52
console . error ( error ) ;
47
53
} ) ;
@@ -61,6 +67,17 @@ function write(json, path, content) {
61
67
console . log ( ` - ${ path . jsonFilePath } :1` )
62
68
}
63
69
70
+ function writeParam ( json , path , content ) {
71
+ fs . writeFile ( path . paramJsonFilePath , json , ( err ) => {
72
+ if ( err ) throw err ;
73
+ } ) ;
74
+ fs . writeFile ( path . paramGoFilePath , content , ( err ) => {
75
+ if ( err ) throw err ;
76
+ } ) ;
77
+ console . log ( ` - ${ path . paramJsonFilePath } :1` )
78
+ console . log ( ` - ${ path . paramGoFilePath } :1` )
79
+ }
80
+
64
81
function buildOpts ( body , cliOpts ) {
65
82
const opts = { }
66
83
if ( cliOpts ?. method ) opts . method = cliOpts ?. method
@@ -96,7 +113,8 @@ function buildOpts(body, cliOpts) {
96
113
return opts
97
114
}
98
115
99
- function buildContent ( go , path , comment ) {
116
+ // TODO: Paramにも対応する
117
+ function buildContent ( go , path , comment , isParam = false ) {
100
118
let content = `// Generated Code But Editable.
101
119
// Format The Code with \`go fmt\` or something and edit it manually to use it.
102
120
//
@@ -107,15 +125,21 @@ function buildContent(go, path, comment) {
107
125
if ( go . indexOf ( 'time.' ) !== - 1 ) {
108
126
content += `import "time"\n\n`
109
127
}
110
- content += `// ${ go . split ( " " ) [ 1 ] } represents the response body from an HTTP request.\n//`
128
+ if ( isParam ) {
129
+ content += `// ${ go . split ( " " ) [ 1 ] } is the HTTP request's body parameter.\n//`
130
+ } else {
131
+ content += `// ${ go . split ( " " ) [ 1 ] } represents the response body from an HTTP request.\n//`
132
+ }
111
133
content += comment
112
134
content += go
113
135
return content
114
136
}
115
137
116
- function buildComment ( url , path , method , res ) {
138
+ function buildComment ( url , path , method , res = false ) {
117
139
let comment = ""
118
- comment += `\n//\tStatus: ${ res . status } ${ res . statusText } `
140
+ if ( res ) {
141
+ comment += `\n//\tStatus: ${ res . status } ${ res . statusText } `
142
+ }
119
143
comment += `\n//\tRequest: ${ method } ${ url . href } `
120
144
121
145
const cfg = loadConfig ( url , cliOpts . config )
0 commit comments