Skip to content

Commit 5db92cd

Browse files
committed
🐛 Fix request body parameter
1 parent 2bef271 commit 5db92cd

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/run.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ function run(urlStr, body, options) {
3131
if (path.path.pathFormat) console.log(`Format: ${path.path.pathFormat}`)
3232
if (cfg?.["docs"] !== undefined) console.log(`Docs: ${cfg?.["docs"].join(", ")}`)
3333
comment = buildComment(url, path, opts.method, res)
34-
return res.json()
35-
})
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)
34+
4035
if (opts?.body) {
4136
const paramStruct = jsonToGo(opts?.body, path.struct + "Param");
4237
const paramContent = buildContent(
4338
paramStruct.go, path, buildComment(url, path, opts.method), true
4439
)
4540
writeParam(JSON.stringify(JSON.parse(opts?.body), null, "\t"), path, paramContent)
4641
}
42+
43+
return res.json()
44+
})
45+
.then(json => {
46+
const struct = jsonToGo(JSON.stringify(json), path.struct);
47+
const content = buildContent(struct.go, path, comment)
48+
write(json, path, content)
4749
}, () => {
4850
console.log()
4951
console.log("Response Body is empty.")
@@ -62,7 +64,7 @@ function write(json, path, content) {
6264
if (err) throw err;
6365
});
6466
console.log()
65-
console.log("Generated Files:")
67+
console.log("Response Body:")
6668
console.log(` - ${path.goFilePath}:1`)
6769
console.log(` - ${path.jsonFilePath}:1`)
6870
}
@@ -74,8 +76,10 @@ function writeParam(json, path, content) {
7476
fs.writeFile(path.paramGoFilePath, content, (err) => {
7577
if (err) throw err;
7678
});
77-
console.log(` - ${path.paramJsonFilePath}:1`)
79+
console.log()
80+
console.log("Request Body Parameter:")
7881
console.log(` - ${path.paramGoFilePath}:1`)
82+
console.log(` - ${path.paramJsonFilePath}:1`)
7983
}
8084

8185
function buildOpts(body, cliOpts) {
@@ -92,7 +96,7 @@ function buildOpts(body, cliOpts) {
9296
if (!cliOpts?.method) {
9397
opts.method = "POST"
9498
}
95-
if (isJsonString(cliOpts.headers)) {
99+
if (isJsonString(body)) {
96100
opts.body = body
97101
} else {
98102
const bodyObj = loadJsonOrYaml(body)
@@ -113,7 +117,6 @@ function buildOpts(body, cliOpts) {
113117
return opts
114118
}
115119

116-
// TODO: Paramにも対応する
117120
function buildContent(go, path, comment, isParam = false) {
118121
let content = `// Generated Code But Editable.
119122
// Format The Code with \`go fmt\` or something and edit it manually to use it.

0 commit comments

Comments
 (0)