@@ -2,7 +2,7 @@ const fetch = require('node-fetch');
2
2
const fs = require ( 'fs' ) ;
3
3
const jsonToGo = require ( '../vendor/json-to-go.js' ) ;
4
4
const buildPath = require ( './buildPath' ) ;
5
- const { isJsonString, loadConfig, loadFile, loadJson} = require ( "./common" ) ;
5
+ const { isJsonString, loadConfig, loadFile, loadJson, capitalize } = require ( "./common" ) ;
6
6
7
7
let cliOpts
8
8
@@ -24,28 +24,38 @@ function run(urlStr, body, options) {
24
24
const apiUrl = urlStr . replace ( / \/ $ / , '' )
25
25
url = new URL ( apiUrl ) ;
26
26
cfg = loadConfig ( url , cliOpts . config )
27
- path = buildPath ( url , cliOpts . config )
27
+ path = buildPath ( url , cliOpts . config , opts )
28
28
29
29
console . log ( `Status: ${ res . status } ${ res . statusText } ` )
30
30
console . log ( `Request: ${ opts . method } ${ url } ` )
31
31
if ( path . path . pathFormat ) console . log ( `Format: ${ path . path . pathFormat } ` )
32
32
if ( cfg ?. [ "docs" ] !== undefined ) console . log ( `Docs: ${ cfg ?. [ "docs" ] . join ( ", " ) } ` )
33
33
comment = buildComment ( url , path , opts . method , res )
34
34
35
- if ( opts ?. body ) {
36
- const paramStruct = jsonToGo ( opts ?. body , path . struct + "Param" ) ;
37
- const paramContent = buildContent (
38
- paramStruct . go , path , buildComment ( url , path , opts . method ) , true
39
- )
40
- writeParam ( JSON . stringify ( JSON . parse ( opts ?. body ) , null , "\t" ) , path , paramContent )
41
- }
42
-
43
35
return res . json ( )
44
36
} )
45
37
. then ( json => {
46
- const struct = jsonToGo ( JSON . stringify ( json ) , path . struct ) ;
47
- const content = buildContent ( struct . go , path , comment )
38
+ let method = capitalize ( opts ?. method )
39
+ const struct = jsonToGo ( JSON . stringify ( json ) , path . struct + method ) ;
40
+ const content = buildContent ( struct . go , path , comment , "" )
48
41
write ( json , path , content )
42
+
43
+ if ( opts ?. body ) {
44
+ const bodyStruct = jsonToGo ( opts ?. body , path . struct + method + "Body" ) ;
45
+ const bodyContent = buildContent (
46
+ bodyStruct . go , path , buildComment ( url , path , opts . method ) , "body"
47
+ )
48
+ writeBodyParam ( JSON . stringify ( JSON . parse ( opts ?. body ) , null , "\t" ) , path , bodyContent )
49
+ }
50
+ if ( url ?. search ) {
51
+ const queryJson = queryToJson ( new URLSearchParams ( url . search ) )
52
+ const queryStr = JSON . stringify ( queryJson , null , "\t" )
53
+ const queryStruct = jsonToGo ( queryStr , path . struct + method + "Query" ) ;
54
+ const queryContent = buildContent (
55
+ queryStruct . go , path , buildComment ( url , path , opts . method ) , "query"
56
+ )
57
+ writeQueryParam ( queryStr , path , queryContent )
58
+ }
49
59
} , ( ) => {
50
60
console . log ( )
51
61
console . log ( "Response Body is empty." )
@@ -69,17 +79,30 @@ function write(json, path, content) {
69
79
console . log ( ` - ${ path . jsonFilePath } :1` )
70
80
}
71
81
72
- function writeParam ( json , path , content ) {
73
- fs . writeFile ( path . paramJsonFilePath , json , ( err ) => {
82
+ function writeBodyParam ( json , path , content ) {
83
+ fs . writeFile ( path . bodyJsonFilePath , json , ( err ) => {
74
84
if ( err ) throw err ;
75
85
} ) ;
76
- fs . writeFile ( path . paramGoFilePath , content , ( err ) => {
86
+ fs . writeFile ( path . bodyGoFilePath , content , ( err ) => {
77
87
if ( err ) throw err ;
78
88
} ) ;
79
89
console . log ( )
80
90
console . log ( "Request Body Parameter:" )
81
- console . log ( ` - ${ path . paramGoFilePath } :1` )
82
- console . log ( ` - ${ path . paramJsonFilePath } :1` )
91
+ console . log ( ` - ${ path . bodyGoFilePath } :1` )
92
+ console . log ( ` - ${ path . bodyJsonFilePath } :1` )
93
+ }
94
+
95
+ function writeQueryParam ( json , path , content ) {
96
+ fs . writeFile ( path . queryJsonFilePath , json , ( err ) => {
97
+ if ( err ) throw err ;
98
+ } ) ;
99
+ fs . writeFile ( path . queryGoFilePath , content , ( err ) => {
100
+ if ( err ) throw err ;
101
+ } ) ;
102
+ console . log ( )
103
+ console . log ( "Request Query Parameter:" )
104
+ console . log ( ` - ${ path . queryGoFilePath } :1` )
105
+ console . log ( ` - ${ path . queryJsonFilePath } :1` )
83
106
}
84
107
85
108
function buildOpts ( body , cliOpts ) {
@@ -116,7 +139,7 @@ function buildOpts(body, cliOpts) {
116
139
return opts
117
140
}
118
141
119
- function buildContent ( go , path , comment , isParam = false ) {
142
+ function buildContent ( go , path , comment , paramType ) {
120
143
let content = `// Generated Code But Editable.
121
144
// Format The Code with \`go fmt\` or something and edit it manually to use it.
122
145
//
@@ -127,10 +150,12 @@ function buildContent(go, path, comment, isParam = false) {
127
150
if ( go . indexOf ( 'time.' ) !== - 1 ) {
128
151
content += `import "time"\n\n`
129
152
}
130
- if ( isParam ) {
131
- content += `// ${ go . split ( " " ) [ 1 ] } is the HTTP request's body parameter.\n//`
132
- } else {
133
- content += `// ${ go . split ( " " ) [ 1 ] } represents the response body from an HTTP request.\n//`
153
+ if ( paramType === "body" ) {
154
+ content += `// ${ go . split ( " " ) [ 1 ] } is the structure of the the HTTP Request Body Parameter.\n//`
155
+ } else if ( paramType === "query" ) {
156
+ content += `// ${ go . split ( " " ) [ 1 ] } is the structure of the HTTP Request Query Parameter.\n//`
157
+ } else {
158
+ content += `// ${ go . split ( " " ) [ 1 ] } is the structure of the HTTP Response Body.\n//`
134
159
}
135
160
content += comment
136
161
content += go
@@ -154,4 +179,12 @@ function buildComment(url, path, method, res = false) {
154
179
return `${ comment } \n`
155
180
}
156
181
182
+ function queryToJson ( query ) {
183
+ const json = { }
184
+ for ( const [ key , value ] of query . entries ( ) ) {
185
+ json [ key ] = value
186
+ }
187
+ return json
188
+ }
189
+
157
190
module . exports = run ;
0 commit comments