Skip to content

Commit 6860f3d

Browse files
committed
🐛 Added handling of cases where there is no response body.
1 parent 946f094 commit 6860f3d

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/run.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,37 @@ function run(urlStr, body, options) {
1414
try {
1515
opts = buildOpts(body, cliOpts)
1616
} catch (e) {
17-
console.log(e.message);
17+
console.error(e.message);
1818
return
1919
}
2020

21-
try {
22-
// See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
23-
fetch(urlStr, opts)
24-
.then(res => {
25-
const apiUrl = urlStr.replace(/\/$/, '')
26-
url = new URL(apiUrl);
27-
cfg = loadConfig(url, cliOpts.config)
28-
path = buildPath(url, cliOpts.config)
29-
30-
console.log(`Status: ${res.status} ${res.statusText}`)
31-
console.log(`Request: ${opts.method} ${url}`)
32-
if (path.path.pathFormat) console.log(`Format: ${path.path.pathFormat}`)
33-
if (cfg?.["docs"] !== undefined) console.log(`Docs: ${cfg?.["docs"].join(", ")}`)
21+
// See: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch
22+
fetch(urlStr, opts)
23+
.then(res => {
24+
const apiUrl = urlStr.replace(/\/$/, '')
25+
url = new URL(apiUrl);
26+
cfg = loadConfig(url, cliOpts.config)
27+
path = buildPath(url, cliOpts.config)
3428

35-
comment = buildComment(url, path, opts.method, res)
36-
return res.json()
37-
})
38-
.then(json => {
29+
console.log(`Status: ${res.status} ${res.statusText}`)
30+
console.log(`Request: ${opts.method} ${url}`)
31+
if (path.path.pathFormat) console.log(`Format: ${path.path.pathFormat}`)
32+
if (cfg?.["docs"] !== undefined) console.log(`Docs: ${cfg?.["docs"].join(", ")}`)
33+
comment = buildComment(url, path, opts.method, res)
34+
return res.json()
35+
})
36+
.then(json => {
3937
const struct = jsonToGo(JSON.stringify(json), path.struct);
4038
const content = buildContent(struct.go, path, comment)
4139
write(json, path, content)
42-
})
43-
;
44-
} catch (e) {
45-
console.log(e.message);
46-
}
40+
}, () => {
41+
console.log()
42+
console.log("Response Body is empty.")
43+
}
44+
)
45+
.catch((error) => {
46+
console.error(error);
47+
});
4748
}
4849

4950
function write(json, path, content) {
@@ -79,9 +80,9 @@ function buildOpts(body, cliOpts) {
7980
} else {
8081
const bodyObj = loadJsonOrYaml(body)
8182
const sortedBodyObj = Object.keys(bodyObj).sort().reduce((ret, key) => {
82-
ret[key] = bodyObj[key];
83-
return ret;
84-
}, {});
83+
ret[key] = bodyObj[key];
84+
return ret;
85+
}, {});
8586
opts.body = JSON.stringify(sortedBodyObj)
8687
}
8788
}

0 commit comments

Comments
 (0)