Skip to content

Commit 68dd1b3

Browse files
authored
Merge pull request #2 from nkmr-jp/fix-request-body
🐛 Fix Request Body
2 parents 96adf81 + 6d66ab8 commit 68dd1b3

File tree

3 files changed

+14
-34
lines changed

3 files changed

+14
-34
lines changed

bin/api-to-go.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ program
1010
.version(packageJson.version,'-v, --version', 'output the current version')
1111
.description(packageJson.description)
1212
.argument('<url>', 'URL (required)')
13-
.argument('[body]', 'HTTP request body. specify by json string or file(json|yml).')
14-
.option('-H, --headers <string>', 'http request headers. specify by json string or file(json|yml).')
13+
.argument('[body]', 'HTTP request body. specify by json string or file.')
14+
.option('-H, --headers <string>', 'http request headers. specify by json string or file.')
1515
.option('-X, --method <string>', 'specify request method to use.')
1616
.option('--config <string>', 'location of client config files.',"./.api-to-go.yml")
1717
.option('-D, --debug', 'enable debug mode')

src/common.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
11
const yaml = require("js-yaml");
22
const fs = require("fs");
3-
const path = require('path')
4-
5-
exports.loadJsonOrYaml = file => {
6-
switch (path.extname(file)) {
7-
case '.json':
8-
return this.loadJson(file)
9-
case '.yml':
10-
return this.loadYaml(file)
11-
case '.yaml':
12-
return this.loadYaml(file)
13-
default:
14-
throw new Error(`${file} is not json or yaml file`);
15-
}
16-
}
173

184
exports.loadJson = file => {
19-
return JSON.parse(this.loadFile(file));
5+
const str = this.loadFile(file)
6+
if(!this.isJsonString(str)){
7+
throw new Error(`${file} is not json file.`);
8+
}
9+
return JSON.parse(str);
2010
};
2111

2212
exports.loadYaml = file => {
@@ -39,15 +29,6 @@ exports.isJsonString = str => {
3929
return true;
4030
};
4131

42-
exports.isYamlString = str => {
43-
try {
44-
yaml.load(str);
45-
} catch (e) {
46-
return false;
47-
}
48-
return true;
49-
};
50-
5132
exports.loadConfig = (url, configFile) => {
5233
return this.loadYaml(configFile)?.[url.hostname]
5334
}

src/run.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ 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 {loadJsonOrYaml, isJsonString, loadConfig} = require("./common");
5+
const {isJsonString, loadConfig, loadFile, loadJson} = require("./common");
66

77
let cliOpts
88

@@ -89,7 +89,7 @@ function buildOpts(body, cliOpts) {
8989
if (isJsonString(cliOpts.headers)) {
9090
opts.headers = JSON.parse(cliOpts.headers)
9191
} else {
92-
opts.headers = loadJsonOrYaml(cliOpts.headers)
92+
opts.headers = loadJson(cliOpts.headers)
9393
}
9494
}
9595
if (body) {
@@ -99,12 +99,11 @@ function buildOpts(body, cliOpts) {
9999
if (isJsonString(body)) {
100100
opts.body = body
101101
} else {
102-
const bodyObj = loadJsonOrYaml(body)
103-
const sortedBodyObj = Object.keys(bodyObj).sort().reduce((ret, key) => {
104-
ret[key] = bodyObj[key];
105-
return ret;
106-
}, {});
107-
opts.body = JSON.stringify(sortedBodyObj)
102+
const bodyObj = loadFile(body)
103+
if (!isJsonString(bodyObj)) {
104+
throw new Error(`${body} is not json file.`);
105+
}
106+
opts.body = bodyObj
108107
}
109108
}
110109
if (cliOpts?.debug) {

0 commit comments

Comments
 (0)