-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nkmr-jp/feature-add-auth
Add feature that POST request support and command line interface.
- Loading branch information
Showing
8 changed files
with
299 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,20 @@ | ||
#! /usr/bin/env node | ||
const fetch = require('node-fetch'); | ||
const fs = require('fs'); | ||
const jsonToGo = require('../vendor/json-to-go.js'); | ||
const buildPath = require('../src/buildPath'); | ||
const run = require('../src/run'); | ||
const {Command} = require('commander'); | ||
const program = new Command(); | ||
const packageJson = require('../package.json'); | ||
|
||
function run(url) { | ||
const apiUrl = url.replace(/\/$/, '') | ||
fetch(apiUrl) | ||
.then(res => res.json()) | ||
.then(json => { | ||
const url = new URL(apiUrl); | ||
const path = buildPath(url) | ||
const res = jsonToGo(JSON.stringify(json), path.struct); | ||
const content = _buildContent(res.go, path, url) | ||
fs.mkdirSync(path.dir, {recursive: true}) | ||
fs.writeFile(path.jsonFilePath, JSON.stringify(json, null, "\t"), (err) => { | ||
if (err) throw err; | ||
console.log(`saved: ${path.jsonFilePath}`) | ||
}); | ||
fs.writeFile(path.goFilePath, content, (err) => { | ||
if (err) throw err; | ||
console.log(`generated: ${path.goFilePath}`) | ||
}); | ||
} | ||
); | ||
} | ||
// See: https://github.com/tj/commander.js | ||
program | ||
.name('api-to-go') | ||
.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).') | ||
.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') | ||
.action(run) | ||
|
||
function _buildContent(struct, path, url) { | ||
let content = `package ${path.pkg}\n\n` | ||
if (struct.indexOf('time.') !== -1) { | ||
content = `${content}import "time"\n\n` | ||
} | ||
let comment = `// ${path.struct} is the go struct of api's payload.` | ||
if (path.path.pathFormat) { | ||
comment += `\n//\n// url: ${url.origin}${path.path.pathFormat}` | ||
comment += `\n// example: ${url.href}` | ||
} else { | ||
comment += `\n//\n// url: ${url.href}` | ||
} | ||
content = `${content}${comment}\n//\n${struct}` | ||
return content | ||
} | ||
|
||
function _capitalize(str) { | ||
const lower = str.toLowerCase(); | ||
return str.charAt(0).toUpperCase() + lower.slice(1); | ||
} | ||
|
||
if (process.argv.length !== 3) { | ||
console.log("parameter is wrong.") | ||
return | ||
} | ||
|
||
run(process.argv[2]) | ||
program.parse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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)); | ||
}; | ||
|
||
exports.loadYaml = file => { | ||
return yaml.load(this.loadFile(file)); | ||
} | ||
|
||
exports.loadFile = file => { | ||
if (!fs.existsSync(file)) { | ||
throw new Error(`${file} is not exists.`); | ||
} | ||
return fs.readFileSync(file, 'utf8'); | ||
}; | ||
|
||
exports.isJsonString = str => { | ||
try { | ||
JSON.parse(str); | ||
} catch (e) { | ||
return false; | ||
} | ||
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] | ||
} |
Oops, something went wrong.