Skip to content

Commit 2bef271

Browse files
committed
✨ Add feature that generate struct of request body param.
1 parent 6860f3d commit 2bef271

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

src/buildPath.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ function buildPath(url, configFile) {
1313
struct,
1414
pkg,
1515
dir,
16-
jsonFilePath: `${dir}/${last}_sample.json`,
17-
goFilePath: `${dir}/${last}.go`
16+
jsonFilePath: `${dir}/${last}.json`,
17+
goFilePath: `${dir}/${last}.go`,
18+
paramJsonFilePath: `${dir}/${last}_param.json`,
19+
paramGoFilePath: `${dir}/${last}_param.go`,
1820
}
1921
}
2022

src/buildPath.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ test('build path', () => {
55
"dir": "api.github.com/users/user",
66
"goFilePath": "api.github.com/users/user/repos.go",
77
"jsonFilePath": "api.github.com/users/user/repos_sample.json",
8+
"requestGoFilePath": "api.github.com/users/user/repos_request.go",
9+
"requestJsonFilePath": "api.github.com/users/user/repos_request_sample.json",
810
"path": {
911
"pathFormat": "/users/{user}/repos",
1012
"pathname": "/users/github/repos",

src/run.js

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ function run(urlStr, body, options) {
3434
return res.json()
3535
})
3636
.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)
4346
}
44-
)
47+
}, () => {
48+
console.log()
49+
console.log("Response Body is empty.")
50+
})
4551
.catch((error) => {
4652
console.error(error);
4753
});
@@ -61,6 +67,17 @@ function write(json, path, content) {
6167
console.log(` - ${path.jsonFilePath}:1`)
6268
}
6369

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+
6481
function buildOpts(body, cliOpts) {
6582
const opts = {}
6683
if (cliOpts?.method) opts.method = cliOpts?.method
@@ -96,7 +113,8 @@ function buildOpts(body, cliOpts) {
96113
return opts
97114
}
98115

99-
function buildContent(go, path, comment) {
116+
// TODO: Paramにも対応する
117+
function buildContent(go, path, comment, isParam = false) {
100118
let content = `// Generated Code But Editable.
101119
// Format The Code with \`go fmt\` or something and edit it manually to use it.
102120
//
@@ -107,15 +125,21 @@ function buildContent(go, path, comment) {
107125
if (go.indexOf('time.') !== -1) {
108126
content += `import "time"\n\n`
109127
}
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+
}
111133
content += comment
112134
content += go
113135
return content
114136
}
115137

116-
function buildComment(url, path, method, res) {
138+
function buildComment(url, path, method, res = false) {
117139
let comment = ""
118-
comment += `\n//\tStatus: ${res.status} ${res.statusText}`
140+
if (res) {
141+
comment += `\n//\tStatus: ${res.status} ${res.statusText}`
142+
}
119143
comment += `\n//\tRequest: ${method} ${url.href}`
120144

121145
const cfg = loadConfig(url, cliOpts.config)

0 commit comments

Comments
 (0)