Skip to content

Commit d08b694

Browse files
committed
📝 Update generated comments
1 parent 0d97f72 commit d08b694

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

bin/api-to-go.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ function run(url) {
99
fetch(apiUrl)
1010
.then(res => res.json())
1111
.then(json => {
12-
const path = buildPath(apiUrl)
12+
const url = new URL(apiUrl);
13+
const path = buildPath(url)
1314
const res = jsonToGo(JSON.stringify(json), path.struct);
14-
const content = _buildContent(res.go, path, apiUrl)
15+
const content = _buildContent(res.go, path, url)
1516
fs.mkdirSync(path.dir, {recursive: true})
1617
fs.writeFile(path.jsonFilePath, JSON.stringify(json, null, "\t"), (err) => {
1718
if (err) throw err;
@@ -25,13 +26,18 @@ function run(url) {
2526
);
2627
}
2728

28-
function _buildContent(struct, path, apiUrl) {
29+
function _buildContent(struct, path, url) {
2930
let content = `package ${path.pkg}\n\n`
3031
if (struct.indexOf('time.') !== -1) {
3132
content = `${content}import "time"\n\n`
3233
}
33-
let comment = `// ${path.struct} ${apiUrl}`
34-
if (path.path.pathFormat) comment += ` (${path.path.pathFormat})`
34+
let comment = `// ${path.struct} is the go struct of api's payload.`
35+
if (path.path.pathFormat) {
36+
comment += `\n// url ${url.origin}${path.path.pathFormat}`
37+
comment += `\n// example ${url.href}`
38+
} else {
39+
comment += `\n// url ${url.href}`
40+
}
3541
content = `${content}${comment}\n${struct}`
3642
return content
3743
}

src/buildPath.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const yaml = require("js-yaml");
22
const fs = require("fs");
33

4-
function buildPath(apiUrl, configFile = "./.api-to-go.yaml") {
5-
const url = new URL(apiUrl);
4+
function buildPath(url, configFile = "./.api-to-go.yaml") {
65
const path = _buildPath(url, configFile)
76
const pathArr = path.replacedUrl.split("/")
87
const pkg = pathArr[pathArr.length - 2].replace(/\./g, '')

src/buildPath.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ test('build path', () => {
1515
"struct": "Repos"
1616
}
1717
const received = buildPath(
18-
"https://api.github.com/users/github/repos",
18+
new URL("https://api.github.com/users/github/repos"),
1919
"./.api-to-go.test.yaml"
2020
)
2121
expect(received).toEqual(expected);
@@ -29,7 +29,7 @@ test('build path without format setting', () => {
2929
"replacedUrl": "api.github.com/organizations"
3030
}
3131
const received = buildPath(
32-
"https://api.github.com/organizations",
32+
new URL("https://api.github.com/organizations"),
3333
"./.api-to-go.test.yaml"
3434
)
3535
expect(received.path).toEqual(expected);

0 commit comments

Comments
 (0)