UNPKG

chen-request

Version:

获取api接口的数据

40 lines (34 loc) 1.04 kB
#!/usr/bin/env node const { Command } = require('commander'); const request = require('../src/request.js'); const ora = require('ora'); const program = new Command(); program.version('1.0.1'); program .option('-t --test-url <url>', 'request address') .option('-n --request-type <type>', 'request type,default get get|post|delete|put') // 解析参数 program.parse(process.argv); const options = program.opts(); let {testUrl,requestType='get'} = options; // 没有传递参数,给出对应的提示 if(!options.testUrl){ spinner.fail('必须传递参数: -t 地址'); // 终止进程 process.exit(); } // 发送请求 const spinner = ora('Loading unicorns').start(); spinner.color = 'yellow'; spinner.text = 'Loading....'; request(testUrl,requestType).then(res=>{ spinner.color = 'green'; spinner.succeed('success'); console.log(res.data) }).catch(err=>{ console.log(err) spinner.fail('fail'); }).finally(_=>{ // 关闭进度条 spinner.stop() })