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

miniprogram-network

1.4.0 • Public • Published

miniprogram-network npm version

A Network package for MiniProgram

小程序底层网络库封装

Todo

网络库封装

  • Promise泛型Promise
  • CancelToken 可取消操作
  • Queue 队列支持
  • Retry 网络错误自动重试
  • 每个请求的原生回调接口支持(onHeadersReceived事件)和(onProgressUpdate事件)
  • Interceptors 拦截器 transform send data / transform response data
  • Listeners 全局事件监听onSend,onResponse,onRejected,onAbort,onComplete

数据转换

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

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

import {
    Request, transformRequestResponseOkData,
    Download, transformDownloadResponseOkData,
    Upload, transformUploadResponseOkData,
} from 'miniprogram-network';
 
// 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
    });

快速配置 setConfig

import { setConfig } from 'miniprogram-network';
 
//将Request,Upload,Download的默认baseURL设置为'https://api.newfuture.cc'
setConfig('baseURL', 'https://api.newfuture.cc');
 
//等效方式
setConfig({
    baseURL:'https://api.newfuture.cc'
})
 

LifeCycle

详情说明miniprogram-network-life-cycle

Install

npm i [email protected]

Version

1.4.0

License

Apache-2.0

Unpacked Size

19.6 kB

Total Files

6

Last publish

Collaborators

  • avatar