嗨嗨嗨,又到了写轮子环节了,为什么要写这个东西呢?
这次带来的还是一个库,我们之前测试接口会使用一些工具,postMan
,apiFox
等。
这些库需要安装,我想着搞一个命令行交互式的,可以快捷测试接口,类似于curl
那种,只不过我的工具更加傻瓜式。
文档地址 www.npmjs.com/package/xmw…
安装
npm i xmwc -g
安装完成之后会有一个命令叫wc
wc -h
可以查看帮助
Usage: wc [options] [command] 请求API Options: -V, --version output the version number -h, --help display help for command Commands: get 快捷发起get请求 post 快捷发起post请求 ws socket套接字测试
使用方法
直接调用wc
命令
wc
1.wc
选择是否从缓存读取Y读取N取消 如果没有使用过建议N
2.选择请求方式 GET POST 等.....
3.选择请求头 application/x-www-form-urlencoded application/json 等
4.输入请求资源路径 也就是url
5.输入参数如果是get可以忽略
6.选择返回格式(默认json) text blob buffer
7.是否缓存 Y 缓存 N 取消 缓存起来可以下次直接使用
成功返回success
失败error
采用选项式操作,如果添加到缓存里面可以下次直接发起请求
2.快捷发起请求
如果不想繁琐的去选择也可以使用快捷发送请求
wc get 命令
wc post
wc ws
支持测试webSocket
源码
#!/usr/bin/env node import { program } from 'commander' //命令工具 import http from 'node-fetch' //发送请求 import inquirer from 'inquirer' //命令行交互工具 import { Result } from './type' import chalk from 'chalk' //变颜色 import fs from 'fs' import path from 'path' import ws from 'ws' //socket const cacheJson = require('../cache.json') const PKG = require('../package.json') program.version(PKG.version).description('查看版本') program.description('请求API').action(async () => { const { isCache } = await inquirer.prompt([ { type: "confirm", name: "isCache", message: "是否从缓存中读取" }, ]) if (isCache) { const keys = Object.keys(cacheJson) if (keys.length === 0) { console.log(chalk.red('暂无缓存')) } else { const res = await inquirer.prompt([ { type: "list", name: "cache", choices: keys, message: "请选择缓存的请求" } ]) const value = cacheJson[res.cache]; sendHttp(value) } } else { const result = await inquirer.prompt<Result>([ { type: "list", name: "method", choices: ['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'PATCH', 'OPTIONS'], default: "GET" }, { type: "list", name: "header", message: "选择请求头", choices: [ "application/x-www-form-urlencoded", "application/json", "multipart/form-data", "text/plain" ] }, { type: "input", name: "url", message: "请求资源路径", default: "http:// | https://" }, { type: "input", name: "params", message: "请输入参数(GET忽略)", default: "" }, { type: "list", name: "response", message: "请选择返回格式(默认json)", choices: ['json', 'text', 'blob', 'buffer'], default: ['json'] }, { type: "confirm", message: "是否缓存", name: "cache" } ]) sendHttp(result) } }) //读写json文件添加缓存 const cache = <T extends Result>(params: T) => { cacheJson[params.method + '-' + params.url.substring(0, 30)] = params; fs.writeFileSync(path.join(__dirname, '../cache.json'), JSON.stringify(cacheJson, null, 4)) } const sendHttp = async <T extends Result>(result: T) => { try { const response = await http(result.url, { method: result.method, body: result.params || undefined, headers: { 'Content-Type': result.header } }) const val = await response[result.response]() if (result.cache) { cache(result) } console.log(chalk.green('success')) console.log(val) } catch (e) { console.log(chalk.red('error')) console.log(e) } } //get命令 program.command('get').description('快捷发起get请求').action(async () => { const { url } = await inquirer.prompt<{ url: string }>([ { type: "input", name: "url", message: "快捷请求get请输入url", validate(v) { if (!v) { return 'url不能为空' } return true } } ]) try { const response = await http(url).then(res => res.json()) console.log(chalk.green('success')) console.log(response) } catch (e) { console.log(chalk.red('error')) console.log(e) } }) //post命令 program.command('post').description('快捷发起post请求').action(async () => { const { url, params } = await inquirer.prompt<{ url: string, params: string }>([ { type: "input", name: "url", message: "快捷请求post请输入url", validate(v) { if (!v) { return 'url不能为空' } return true } }, { type: "input", name: "params", message: "请输入参数(没有请忽略)", default: "" } ]) try { const response = await http(url, { method: "post", body: JSON.stringify(params) || undefined, headers: { 'Content-Type': 'application/json' } }).then(res => res.json()) console.log(chalk.green('success')) console.log(response) } catch (e) { console.log(chalk.red('error')) console.log(e) } }) //ws命令 program.command('ws').description('socket套接字测试').action(async () => { const { url } = await inquirer.prompt<{url:string}>({ type: "input", message: "请输入ws | wss 协议地址", name: "url" }) const socket = new ws(url) socket.on('open', () => { console.log('socket 已连接') }) socket.on('message', (e) => { console.log('result:' + e.toString()) }) socket.on('error', (e) => { console.log('error', e) }) socket.on('close', () => { console.log('socket 已断开') }) }) program.parse(process.argv)