From 6d66ab805e92c12883b730cc05a3136307465932 Mon Sep 17 00:00:00 2001 From: nkmr-jp Date: Wed, 17 Aug 2022 10:29:54 +0900 Subject: [PATCH] :bug: Fix Request Body --- bin/api-to-go.js | 4 ++-- src/common.js | 29 +++++------------------------ src/run.js | 15 +++++++-------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/bin/api-to-go.js b/bin/api-to-go.js index 0283e7c..30e4bee 100755 --- a/bin/api-to-go.js +++ b/bin/api-to-go.js @@ -10,8 +10,8 @@ program .version(packageJson.version,'-v, --version', 'output the current version') .description(packageJson.description) .argument('', 'URL (required)') - .argument('[body]', 'HTTP request body. specify by json string or file(json|yml).') - .option('-H, --headers ', '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 ', 'http request headers. specify by json string or file.') .option('-X, --method ', 'specify request method to use.') .option('--config ', 'location of client config files.',"./.api-to-go.yml") .option('-D, --debug', 'enable debug mode') diff --git a/src/common.js b/src/common.js index 2196005..6df1c3e 100644 --- a/src/common.js +++ b/src/common.js @@ -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 => { @@ -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] } \ No newline at end of file diff --git a/src/run.js b/src/run.js index 13e15c7..1c95b59 100644 --- a/src/run.js +++ b/src/run.js @@ -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 @@ -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) { @@ -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) {