npm package: @nsrd/neuq-request-js
Description: a wrapper of axios for NeuqSoft RD Gateway.
Author: huangrx@neuqsoft
Latest version: 0.0.2

# Neuq Request

Neuq Request 为基于 [axios](https://github.com/axios/axios) 的 [河北东软统一服务网关](https://service.neuqsoft.com/ggfw/citizen/docs/%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8/%E7%BB%9F%E4%B8%80%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3.html#%E5%85%AC%E5%85%B1%E5%8F%82%E6%95%B0) 的 JS 网络请求实现。

## 使用

1. 引用

    ```javascript
    import { NeuqRequest, SIGN_TYPE, ENCRYPT_TYPE } from "@nsrd/neuq-request";
    ```

2. 创建实例

    ```javascript
    const http = NeuqRequest(options<Object>);
    ```

    实例选项 options：
    
    - `baseURL<String>` 根地址，选填项，默认值 `/`
    - `timeout<String>`，网络请求超时时间，单位毫秒，选填项，默认值 `6000`
    - `headers<Object>`，请求头，选填项，默认值 `{}`
    - `requestInterceptor<Function(config<Object>)>`，请求拦截器，选填项，请参考 [Aixos request interceptor](https://github.com/axios/axios#interceptors) 
    - `responseSuccessInterceptor<Function(response<Object>)>`，响应成功拦截器，选填项，请参考 [Aixos response interceptor](https://github.com/axios/axios#interceptors) 
    - `responseFailInterceptor<Function(error<Object>)>`，响应失败拦截器，选填项，请参考 [Aixos response interceptor](https://github.com/axios/axios#interceptors) 
    - `securityGateway<Object>`，安全网关选项，选填项
        - `appId`，必填项，请参考 [河北东软统一服务网关-公共参数](https://service.neuqsoft.com/ggfw/citizen/docs/%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8/%E7%BB%9F%E4%B8%80%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3.html#%E5%85%AC%E5%85%B1%E5%8F%82%E6%95%B0)
        - `signType`，选填项，可选值，默认值 `SIGN_TYPE.SM2`，请参考 [河北东软统一服务网关-公共参数](https://service.neuqsoft.com/ggfw/citizen/docs/%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8/%E7%BB%9F%E4%B8%80%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3.html#%E5%85%AC%E5%85%B1%E5%8F%82%E6%95%B0)
        - `encryptType`，选填项，可选值，默认值 `ENCRYPT_TYPE.NONE`，请参考 [河北东软统一服务网关-公共参数](https://service.neuqsoft.com/ggfw/citizen/docs/%E6%8E%A5%E5%8F%A3%E5%88%97%E8%A1%A8/%E7%BB%9F%E4%B8%80%E6%9C%8D%E5%8A%A1%E7%BD%91%E5%85%B3.html#%E5%85%AC%E5%85%B1%E5%8F%82%E6%95%B0)
        - `appSignPrivateKey`，应用系统签名私钥，必填项
        - `appSignPublicKey`，应用系统签名公钥，必填项
        - `encryptKey`，应用系统与网关约定的加密秘钥，如 `encryptType` 不为 `ENCRYPT_TYPE.NONE` 则为必填项
        - `platformPublicKey`，网关公钥，必填项
        
    以使用 SM2 签名、SM4 加密方式构造请求实例：
    
   ```javascript
   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: ""
     }
   });
   ```
3. 网络请求

   当前只支持 `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](https://github.com/axios/axios#request-config) 一致。
   
   以使用 SM2 签名、SM4 加密方式构造的请求实例发送 post 请求：
   
   ```javascript
   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(() => {});
   ```

## 内置签名与加密工具

```javascript
import { sm, rsa } from "@nsrd/neuq-request";
```

1. sm2 签名 `sm.sm2.signature(message, appSignPrivateKey, appSignPublicKey, appId);`
2. sm4 加密 `sm.sm4.encrypt(message, encryptKey);`
3. sm4 解密 `sm.sm4.decrypt(message, encryptKey);`
4. rsa 签名 `rsa.signature(message, appSignPrivateKey, rsa.alg.SHA256withRSA);`

## 更新计划

0. 测试
1. 支持文件上传
2. 支持加密算法 RSA

如有**异常**或**需求**，请联系 [huangrx](mailto:huangrx@neuqsoft.com) 。