Skip to content

Commit 8b49294

Browse files
committed
✨ Update go struct comments that to be generated.
1 parent 3713fe9 commit 8b49294

File tree

4 files changed

+86
-60
lines changed

4 files changed

+86
-60
lines changed

src/buildContent.js

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/buildPath.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const {loadYaml} = require("./util");
1+
const {loadYaml, loadConfig} = require("./common");
22

33
function buildPath(url, configFile = "./.api-to-go.yml") {
44
const path = _buildPath(url, configFile)
@@ -19,8 +19,7 @@ function buildPath(url, configFile = "./.api-to-go.yml") {
1919
}
2020

2121
function _buildPath(url, configFile) {
22-
const cfg = loadYaml(configFile)
23-
const hostCfg = cfg?.[url.hostname]
22+
const hostCfg = loadConfig(url, configFile)
2423
let ret ={
2524
pathname: url.pathname,
2625
pathFormat: null,

src/util.js renamed to src/common.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ exports.isYamlString = str => {
4646
return false;
4747
}
4848
return true;
49-
};
49+
};
50+
51+
exports.loadConfig = (url, configFile) => {
52+
return this.loadYaml(configFile)?.[url.hostname]
53+
}

src/run.js

Lines changed: 79 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,70 @@ const fetch = require('node-fetch');
22
const fs = require('fs');
33
const jsonToGo = require('../vendor/json-to-go.js');
44
const buildPath = require('./buildPath');
5-
const buildContent = require('./buildContent');
6-
const {loadJsonOrYaml, isJsonString} = require("./util");
5+
const {loadJsonOrYaml, isJsonString, loadConfig} = require("./common");
6+
7+
8+
let configFile = "./.api-to-go.yml"
9+
10+
function run(urlStr, body, cliOpts) {
11+
let comment
12+
let url
13+
let path
14+
let cfg
715

8-
function run(url, body, cliOpts) {
9-
const apiUrl = url.replace(/\/$/, '')
1016
let opts = {}
1117

1218
try {
13-
opts = _buildOpts(body, cliOpts)
19+
opts = buildOpts(body, cliOpts)
1420
} catch (e) {
1521
console.log(e.message);
1622
return
1723
}
1824

19-
// See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
20-
fetch(apiUrl, opts)
21-
.then(res => {
22-
console.log(`Status: ${res.status} ${res.statusText}`)
23-
return res.json()
24-
})
25-
.then(json => {
26-
const url = new URL(apiUrl);
27-
const path = buildPath(url)
28-
const res = jsonToGo(JSON.stringify(json), path.struct);
29-
const content = buildContent(res.go, path, url)
30-
fs.mkdirSync(path.dir, {recursive: true})
31-
fs.writeFile(path.jsonFilePath, JSON.stringify(json, null, "\t"), (err) => {
32-
if (err) throw err;
33-
console.log(`Saved: ${path.jsonFilePath}`)
34-
});
35-
fs.writeFile(path.goFilePath, content, (err) => {
36-
if (err) throw err;
37-
console.log(`Generated: ${path.goFilePath}`)
38-
});
39-
});
25+
try {
26+
// See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
27+
fetch(urlStr, opts)
28+
.then(res => {
29+
console.log()
30+
const apiUrl = urlStr.replace(/\/$/, '')
31+
url = new URL(apiUrl);
32+
cfg = loadConfig(url, configFile)
33+
if (cfg?.["docs"] !== undefined) {
34+
console.log(`Docs: ${cfg?.["docs"]}`)
35+
}
36+
path = buildPath(url)
37+
console.log(`Request: (${opts.method}) ${url}`)
38+
console.log(`Response: ${res.status} ${res.statusText}`)
39+
40+
comment = buildComment(url, path, opts.method, res)
41+
return res.json()
42+
})
43+
.then(json => {
44+
const struct = jsonToGo(JSON.stringify(json), path.struct);
45+
const content = buildContent(struct.go, path, comment)
46+
write(json, path, content)
47+
})
48+
;
49+
} catch (e) {
50+
console.log(e.message);
51+
}
4052
}
4153

42-
function _buildOpts(body, cliOpts) {
54+
function write(json, path, content) {
55+
fs.mkdirSync(path.dir, {recursive: true})
56+
fs.writeFile(path.jsonFilePath, JSON.stringify(json, null, "\t"), (err) => {
57+
if (err) throw err;
58+
});
59+
fs.writeFile(path.goFilePath, content, (err) => {
60+
if (err) throw err;
61+
});
62+
console.log()
63+
console.log("Saved:")
64+
console.log(` ${path.jsonFilePath}`)
65+
console.log(` ${path.goFilePath}`)
66+
}
67+
68+
function buildOpts(body, cliOpts) {
4369
const opts = {}
4470
if (cliOpts?.method) opts.method = cliOpts?.method
4571
if (cliOpts?.headers) {
@@ -65,7 +91,33 @@ function _buildOpts(body, cliOpts) {
6591
console.error()
6692
}
6793
}
94+
if (!opts?.method) opts.method = "GET"
6895
return opts
6996
}
7097

98+
function buildContent(struct, path, comment) {
99+
let content = `// Code generated by api-to-go (https://github.com/nkmr-jp/api-to-go).\n\n`
100+
content += `package ${path.pkg}\n\n`
101+
if (struct.indexOf('time.') !== -1) {
102+
content += `${content}import "time"\n\n`
103+
}
104+
content += comment
105+
content += struct
106+
return content
107+
}
108+
109+
function buildComment(url, path, method, res) {
110+
let comment = `// ${path.struct} is the go struct of api's payload.\n//`
111+
const cfg = loadConfig(url, configFile)
112+
if (cfg?.["docs"] !== undefined) {
113+
comment += `\n// Docs: ${cfg?.["docs"]}`
114+
}
115+
if (path.path.pathFormat) {
116+
comment += `\n// Format: ${path.path.pathFormat}`
117+
}
118+
comment += `\n// Request: (${method}) ${url.href}`
119+
comment += `\n// Response: ${res.status} ${res.statusText}`
120+
return `${comment}\n//\n`
121+
}
122+
71123
module.exports = run;

0 commit comments

Comments
 (0)