0.0.2 • Published 5 years ago
@nsrd/neuq-request-js v0.0.2
Neuq Request
Neuq Request 为基于 axios 的 河北东软统一服务网关 的 JS 网络请求实现。
使用
引用
import { NeuqRequest, SIGN_TYPE, ENCRYPT_TYPE } from "@nsrd/neuq-request";创建实例
const http = NeuqRequest(options<Object>);实例选项 options:
baseURL<String>根地址,选填项,默认值/timeout<String>,网络请求超时时间,单位毫秒,选填项,默认值6000headers<Object>,请求头,选填项,默认值{}requestInterceptor<Function(config<Object>)>,请求拦截器,选填项,请参考 Aixos request interceptorresponseSuccessInterceptor<Function(response<Object>)>,响应成功拦截器,选填项,请参考 Aixos response interceptorresponseFailInterceptor<Function(error<Object>)>,响应失败拦截器,选填项,请参考 Aixos response interceptorsecurityGateway<Object>,安全网关选项,选填项 -appId,必填项,请参考 河北东软统一服务网关-公共参数 -signType,选填项,可选值,默认值SIGN_TYPE.SM2,请参考 河北东软统一服务网关-公共参数 -encryptType,选填项,可选值,默认值ENCRYPT_TYPE.NONE,请参考 河北东软统一服务网关-公共参数 -appSignPrivateKey,应用系统签名私钥,必填项 -appSignPublicKey,应用系统签名公钥,必填项 -encryptKey,应用系统与网关约定的加密秘钥,如encryptType不为ENCRYPT_TYPE.NONE则为必填项 -platformPublicKey,网关公钥,必填项以使用 SM2 签名、SM4 加密方式构造请求实例:
const http = NeuqRequest({ baseURL: "/", timeout: 10 * 1000, headers: { "Content-Type": "application/json" }, requestInterceptor: function(config) { return config; }, responseSuccessInterceptor: function(response) { return Promise.resolve(JSON.parse(response.data.body)); }, responseFailInterceptor: function(error) { return error.response; }, securityGateway: { appId: "DemoSM2", signType: SIGN_TYPE.SM2, encryptType: ENCRYPT_TYPE.SM4, appSignPrivateKey: "", appSignPublicKey: "", encryptKey: "", platformPublicKey: "" } });网络请求
当前只支持
POST/DELETE/PUT/GET四种求情方式。- post
http.post(config<Object>) - delete
http.delete(config<Object>) - put
http.put(config<Object>) - get
http.get(config<Object>)
选项 config,与 Axios request config 一致。
以使用 SM2 签名、SM4 加密方式构造的请求实例发送 post 请求:
http .post({ url: "gateway-request/demo/mock/390/json", data: { bizNo: "GO20300258433171456" } }) .then(res => { console.log("handleSM2SM4PostRequest", res); }) .catch(err => { console.error("handleSM2SM4PostRequest", err); }) .finally(() => {});- post
内置签名与加密工具
import { sm, rsa } from "@nsrd/neuq-request";- sm2 签名
sm.sm2.signature(message, appSignPrivateKey, appSignPublicKey, appId); - sm4 加密
sm.sm4.encrypt(message, encryptKey); - sm4 解密
sm.sm4.decrypt(message, encryptKey); - rsa 签名
rsa.signature(message, appSignPrivateKey, rsa.alg.SHA256withRSA);
更新计划
- 测试
- 支持文件上传
- 支持加密算法 RSA
如有异常或需求,请联系 huangrx 。
0.0.2
5 years ago