npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wool-hyhttp

v0.2.37

Published

weForward数据请求

Downloads

18

Readme

wool-hyhttp

weForward数据请求包装,数据请求部分用的axios,因此hyhttp.axios可以用于普通的请求

请求weForward数据时的参数和配置部分基本和axios一致

安装

npm install wool-hyhttp

引入

import hyhttp from 'wool-hyhttp'
#配置环境变量,以vue cli3.x为例,在项目根目录创建.env文件

#网关-生产环境域名
VUE_APP_HY_HOST_PRODUCT=https://g1.honinsys.cn,https://z1.honinsys.cn
#网关-测试环境域名
VUE_APP_HY_HOST_TEST=//lhoninyun.navboy.com
#网关-本机开发环境域名
VUE_APP_HY_HOST_DEV=//lhoninyun.navboy.com

普通数据请求

//有登录的情况下,Authorization使用WF-SHA2,否则Authorization使用WF-None
hyhttp.post('serviceName?method=methodName',{param1:'test'},config);
//params和config为可选参数

//用法同hyhttp.post,但该方法Authorization强制使用WF-None,适用于不需要access凭证的请求,例如登录
hyhttp.noAccessPost('serviceName?method=methodName',{param1:'test'},config);
/**示例说明
普通数据请求使用hyhttp.post方法(文件上传和文件下载除外,后面会有介绍)
url部分说明:
train表示微服务名,如果有指定默认微服务名此处可以省略,
method后面的listGoods表示微服务的方法名,params表示请求的参数,
config请参考axios的参数配置,需要注意的是,
config中提供一个weForward请求专用的属性hyconfig
{
	hyconfig:{
		//网关域名
		baseURL: '',
		//自定义头
		headers: {},
		//网关请求参数,可选
		// {
		// 	resId:'',
		// 	traceId:'',
		// 	ver:'',
		// 	waitTimeout:0
		// }
		hyReq: null,
		accessId: '',
		accessKey: '',
	}
}
*/
let params={params1:'params1'};
hyhttp.post('train?method=listGoods',{params1:'params1'})
  .then(function (data) {
    console.log(data);
  })
  .catch(function (error) {
    console.log(error);
  });

文件上传

	//用法
	/**
	 * @param {Object} url 请求上传的微服务所需要url,参数要求和post方法的一致
	 * @param {Object} params 请求上传的微服务所需要的参数,可为空
	 * @param {Object} file 需要上传的文件,file必须为是Blob或File对象,每次只能上传一个
	 * @param {Object} config,可以选
	 * 如需监听上传进度,请配置hyconfig的onUploadProgress属性:function(e){
	 }
	 * {hyconfig:{onUploadProgress:onUploadProgress}}
	 * 
	 */
	upload(url, params, file, config) ;
//示例
hyhttp.upload('serviceName?method=method', {}, file, {
		hyconfig: {
			onUploadProgress: e => {
				let percent = (e.loaded / e.total) * 100;
				console.log(percent);
			}
		}
	})
	.then(data => {
		console.log('upload success')
	});
}).catch(e = {
	console.log(e);
})

文件下载

	/**
	 * @param {Object} url 请求下载的微服务所需要url,参数要求和post方法的一致
	 * @param {Object} params 请求下载的微服务所需要的参数,可选
	 * @param {Object} config,可选
	 * config下的hyconfig此处支持notAutoDownload属性,用于配置是否不自动打开连接Boolean类型
	 * 默认为false,也就是默认自动打开,特殊情况可能不需要自动打开,例如上传文件后
	 */
	hyhttp.download(url, params, config)

统一登录

初始化应用时需要配置好全局的loginUrl

	hyhttp.updateGlobalConfig('loginUrl','serviceName?method=method');
	/**
	 * 统一登录
	 * @param {String} username 用户名
	 * @param {String} password 密码
	 * @return {Promise}
	 */
	hyhttp.login(username, password).then(data=>{
		
	}).catch(e=>{
		
	});

【注意】:如果需要自定义登录的参数,可以使用hyhttp.noAccessPost方法, 登录成功后,调用hyhttp.onlogined(data)

	//例如:自定义实现登录,返回的数据,必须要包含accessId,accessKey,accessExpire三个属性
	hyhttp.noAccessPost('url',{'params1':'value1'}).done(data=>{
		hyhttp.onlogined({
				accessId:data.accessId,
				accessKey:data.accessKey,
				//凭证过期时间
				accessExpire:data.accessExpire
		});
	});

退出登录

初始化应用时需要配置好全局的logoutUrl

	hyhttp.updateGlobalConfig('loginUrl','serviceName?method=method');

调用退出登录

	/**
	 * 统一退出登录
	 * @return {Promise}
	 */
	hyhttp.logout().then(data=>{
		
	}).catch(e=>{
		
	});

登录后的凭证是有有效期的,为了维持有效性,内部会定时刷新延长有效期, 因此初始化应用的时候需要全局配置refreshAccessUrl

	//示例:
	hyhttp.updateGlobalConfig('refreshAccessUrl','zuoche_user?method=refresh_access')

添加请求异常监听

	/**
	 * 添加请求事件监听
	 * @param {String} event 监听的事件名,
	 * 可选事件:
	 * requireauth--表示需要登录监听事件,
	 * visitforbidden--表示无访问权限监听事件,
	 * beforerequest--表示请求数据前的事件
	 * @param {Function} handler
	 */
	hyhttp.addEventListener(event, handler);
	
	//一般是在初始初始化项目的时候配置好
	
	//监听需要登录事件
	hyhttp.addEventListener('requireauth', ()=>{
		//TODO 展示登录视图
	});
	//无访问权限监听
	hyhttp.addEventListener('visitforbidden', ()=>{
		//TODO 展示无访问全新视图
	});
	
	//甚至还可以自定义业务层异常事件监听,例如:
	//后台和前端约定好业务层错误码
	const VERIFYMOBILECODE = 100000101;
	//注册自定义异常码匹配
	hyhttp.customResErrorEvent('verifymobile', code => VERIFYMOBILECODE === code);
	//添加自定义异常事件监听
	hyhttp.addEventListener('verifymobile', () => {
		//TODO
	});
		

配置基础服务名

	//基础服务名也就是默认微服务名
	//如果你认为该服务使用的次数最多,便可以配置该项
	/**
	 * 添加请求事件监听
	 * @param {String} serviceName 配置一个基础服务名
	 */
	hyhttp.setHyBaseService(serviceName);
	
	
	//这样请求的时候可以省略掉服务名
	
	例如:
	hyhttp.post('?method=methodName');
	
	//每一对使用英文冒号隔开,配置多对时使用英文逗号隔开,如果生产环境的和自己的相同也可以简写,例如
	//例如多人协同开发,各自有不同的服务器时,需要用到
	VUE_APP_DEV_SERVICENAME=train:train44,demo,devops

配置全局参数

	/**
	 * 如果一个参数,每个请求都需要带上,那么可以放在全局请求参数中
	 * @param {String} key 参数名
	 * @param {String,Number,Boolean} value 参数值
	 */
	hyhttp.putGlobalParam(key, value);
	
	//这样请求的时候可以省略掉服务名
	
	例如:
	hyhttp.putGlobalParam('name', '张三');
	
	另外还有:
	hyhttp.getGlobalParam(key);//获取全局参数
	hyhttp.removeGlobalParam(key);//删除全局参数
	

配置租户信息

	//适用于多租户的场景
	/**
	 * @param {String} tenant 租户信息(例如商家id或者商家简称等)
	 */
	hyhttp.setTenant(tenant);

版本说明:accesskey同时支持hex和base64格式