Skip to content

Commit

Permalink
Merge pull request #2 from nkmr-jp/fix-request-body
Browse files Browse the repository at this point in the history
🐛 Fix Request Body
  • Loading branch information
nkmr-jp authored Aug 17, 2022
2 parents 96adf81 + 6d66ab8 commit 68dd1b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 34 deletions.
4 changes: 2 additions & 2 deletions bin/api-to-go.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ program
.version(packageJson.version,'-v, --version', 'output the current version')
.description(packageJson.description)
.argument('<url>', 'URL (required)')
.argument('[body]', 'HTTP request body. specify by json string or file(json|yml).')
.option('-H, --headers <string>', 'http request headers. specify by json string or file(json|yml).')
.argument('[body]', 'HTTP request body. specify by json string or file.')
.option('-H, --headers <string>', 'http request headers. specify by json string or file.')
.option('-X, --method <string>', 'specify request method to use.')
.option('--config <string>', 'location of client config files.',"./.api-to-go.yml")
.option('-D, --debug', 'enable debug mode')
Expand Down
29 changes: 5 additions & 24 deletions src/common.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
const yaml = require("js-yaml");
const fs = require("fs");
const path = require('path')

exports.loadJsonOrYaml = file => {
switch (path.extname(file)) {
case '.json':
return this.loadJson(file)
case '.yml':
return this.loadYaml(file)
case '.yaml':
return this.loadYaml(file)
default:
throw new Error(`${file} is not json or yaml file`);
}
}

exports.loadJson = file => {
return JSON.parse(this.loadFile(file));
const str = this.loadFile(file)
if(!this.isJsonString(str)){
throw new Error(`${file} is not json file.`);
}
return JSON.parse(str);
};

exports.loadYaml = file => {
Expand All @@ -39,15 +29,6 @@ exports.isJsonString = str => {
return true;
};

exports.isYamlString = str => {
try {
yaml.load(str);
} catch (e) {
return false;
}
return true;
};

exports.loadConfig = (url, configFile) => {
return this.loadYaml(configFile)?.[url.hostname]
}
15 changes: 7 additions & 8 deletions src/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fetch = require('node-fetch');
const fs = require('fs');
const jsonToGo = require('../vendor/json-to-go.js');
const buildPath = require('./buildPath');
const {loadJsonOrYaml, isJsonString, loadConfig} = require("./common");
const {isJsonString, loadConfig, loadFile, loadJson} = require("./common");

let cliOpts

Expand Down Expand Up @@ -89,7 +89,7 @@ function buildOpts(body, cliOpts) {
if (isJsonString(cliOpts.headers)) {
opts.headers = JSON.parse(cliOpts.headers)
} else {
opts.headers = loadJsonOrYaml(cliOpts.headers)
opts.headers = loadJson(cliOpts.headers)
}
}
if (body) {
Expand All @@ -99,12 +99,11 @@ function buildOpts(body, cliOpts) {
if (isJsonString(body)) {
opts.body = body
} else {
const bodyObj = loadJsonOrYaml(body)
const sortedBodyObj = Object.keys(bodyObj).sort().reduce((ret, key) => {
ret[key] = bodyObj[key];
return ret;
}, {});
opts.body = JSON.stringify(sortedBodyObj)
const bodyObj = loadFile(body)
if (!isJsonString(bodyObj)) {
throw new Error(`${body} is not json file.`);
}
opts.body = bodyObj
}
}
if (cliOpts?.debug) {
Expand Down

0 comments on commit 68dd1b3

Please sign in to comment.