Miss any of our Open RFC calls?Watch the recordings here! »

miniprogram-network

1.4.0-alpha.0 • Public • Published

miniprogram-network npm version

A Network package for MiniProgram

小程序底层网络库封装

Todo

网络库封装

  • Promise泛型Promise
  • CancelToken 可取消操作
  • Queue 队列支持
  • Retry 网络错误自动重试
  • 每个请求的原生回调接口支持
    • onHeaders (onHeadersReceived事件)
    • onProgress (onProgressUpdate事件)
  • Interceptors 拦截器
    • transform send data
    • transform response data
  • Listeners 全局事件监听
    • On Send (before request data send & after request data transformed)
    • On Response (after request response data transformed)
    • On rejected (before catch of Promise)
    • On abort
    • On complete

数据转换

默认的transformResponse直接返回小程序原始的返回数据{statusCode,...}

同时提供了根据状态码返回对应数据的转换方式

import {
    Request, transformRequestResponseOkData,
    Download, transformDownloadResponseOkData,
    Upload, transformUploadResponseOkData,
} from '../index'
 
// Request的默认响应拦设为成transformRequestResponseOkData,
// 正常2xx返回data部分,否则rejected
Request.Defaults.transformResponse = transformRequestResponseOkData;
// Download的默认响应拦设为transformDownloadResponseOkData,
// 正常2xx返回string,否则rejected
Download.Defaults.transformResponse = transformDownloadResponseOkData;
// Upload默认响应拦截transformUploadResponseOkData,
//正常2xx返回data,否则rejected
Upload.Defaults.transformResponse = transformUploadResponseOkData;
 
Download.download('url')
    .then(path=>{
        console.log(path);//string
    }).catch(res=>{
        console.error(res);//objct
    });

LifeCircle

详情说明miniprogram-network-life-circle

Install

npm i [email protected]

Version

1.4.0-alpha.0

License

Apache-2.0

Unpacked Size

17.6 kB

Total Files

6

Last publish

Collaborators

  • avatar