Skip to content

Commit 620c6af

Browse files
committed
📝 Update Comments
1 parent 69cb54b commit 620c6af

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

bin/api-to-go.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ program
1111
.description(packageJson.description)
1212
.argument('<url>', 'URL (required)')
1313
.argument('[body]', 'HTTP request body. specify by json string or file(json|yml).')
14-
.option('-H, --headers <string|file>', 'http request headers. specify by json string or file(json|yml).')
14+
.option('-H, --headers <string>', 'http request headers. specify by json string or file(json|yml).')
1515
.option('-X, --method <string>', 'specify request method to use.')
1616
.option('-D, --debug', 'enable debug mode')
1717
.action(run)

src/buildContent.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
function buildContent(struct, path, url) {
1+
const {loadYaml} = require("./util");
2+
3+
function buildContent(struct, path, url, configFile = "./.api-to-go.yml") {
24
let content = `package ${path.pkg}\n\n`
35
if (struct.indexOf('time.') !== -1) {
46
content = `${content}import "time"\n\n`
57
}
6-
let comment = `// ${path.struct} is the go struct of api's payload.`
7-
if (path.path.pathFormat) {
8-
comment += `\n//\n// url: ${url.origin}${path.path.pathFormat}`
9-
comment += `\n// example: ${url.href}`
10-
} else {
11-
comment += `\n//\n// url: ${url.href}`
12-
}
8+
const comment = _buildComment(path, url, configFile)
139
content = `${content}${comment}\n//\n${struct}`
1410
return content
1511
}
1612

13+
function _buildComment(path, url, configFile) {
14+
const cfg = loadYaml(configFile)?.[url.hostname]
15+
let comment = `// ${path.struct} is the go struct of api's payload.\n//`
16+
if (path.path.pathFormat) {
17+
comment += `\n// Format: ${path.path.pathFormat}`
18+
}
19+
comment += `\n// URL: ${url.href}`
20+
if (cfg?.["docs"] !== undefined) {
21+
comment += `\n// Docs: ${cfg?.["docs"]}`
22+
}
23+
return comment
24+
}
25+
1726

1827
module.exports = buildContent;

src/buildPath.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ function _replacePath(pathname, format) {
6262
if (replacedArr.length === 0) return
6363

6464
const replacedPath = replacedArr.join("/")
65-
console.log(`format: ${format}`)
65+
console.log(`Format: ${format}`)
6666
return {
6767
pathname: pathname,
6868
pathFormat: format,

src/buildPath.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test('build path', () => {
1616
}
1717
const received = buildPath(
1818
new URL("https://api.github.com/users/github/repos"),
19-
"./.api-to-go.test.yaml"
19+
"./.api-to-go.test.yml"
2020
)
2121
expect(received).toEqual(expected);
2222
});
@@ -30,7 +30,7 @@ test('build path without format setting', () => {
3030
}
3131
const received = buildPath(
3232
new URL("https://api.github.com/organizations"),
33-
"./.api-to-go.test.yaml"
33+
"./.api-to-go.test.yml"
3434
)
3535
expect(received.path).toEqual(expected);
3636
});

src/run.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ const buildContent = require('./buildContent');
66
const {loadJsonOrYaml, isJsonString} = require("./util");
77

88
function run(url, body, cliOpts) {
9-
if (cliOpts?.verbose) {
10-
console.log("url: " + url)
11-
}
129
const apiUrl = url.replace(/\/$/, '')
1310
let opts = {}
1411

@@ -22,7 +19,7 @@ function run(url, body, cliOpts) {
2219
// See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
2320
fetch(apiUrl, opts)
2421
.then(res => {
25-
console.log(`status: ${res.status} ${res.statusText}`)
22+
console.log(`Status: ${res.status} ${res.statusText}`)
2623
return res.json()
2724
})
2825
.then(json => {
@@ -33,11 +30,11 @@ function run(url, body, cliOpts) {
3330
fs.mkdirSync(path.dir, {recursive: true})
3431
fs.writeFile(path.jsonFilePath, JSON.stringify(json, null, "\t"), (err) => {
3532
if (err) throw err;
36-
console.log(`saved: ${path.jsonFilePath}`)
33+
console.log(`Saved: ${path.jsonFilePath}`)
3734
});
3835
fs.writeFile(path.goFilePath, content, (err) => {
3936
if (err) throw err;
40-
console.log(`generated: ${path.goFilePath}`)
37+
console.log(`Generated: ${path.goFilePath}`)
4138
});
4239
});
4340
}

0 commit comments

Comments
 (0)