Narcissistic Pickle Meister

    miniprogram-network
    TypeScript icon, indicating that this package has built-in type declarations

    1.3.2-alpha.0 • Public • Published

    miniprogram-network npm version

    A Network package for MiniProgram

    小程序底层网络库封装

    Todo

    网络库封装

    • [x] Promise泛型Promise
    • [x] CancelToken 可取消操作
    • [x] Queue 队列支持
    • [x] Retry 网络错误自动重试
    • [x] 每个请求的原生回调接口支持
      • [x] onHeaders (onHeadersReceived事件)
      • [x] onProgress (onProgressUpdate事件)
    • [x] Interceptors 拦截器
      • [x] transform send data
      • [x] transform response data
    • [x] Listeners 全局事件监听
      • [x] On Send (before request data send & after request data transformed)
      • [x] On Response (after request response data transformed)
      • [x] On rejected (before catch of Promise)
      • [x] On abort
      • [x] 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 miniprogram-network@1.3.2-alpha.0

    Version

    1.3.2-alpha.0

    License

    Apache-2.0

    Unpacked Size

    17.6 kB

    Total Files

    6

    Last publish

    Collaborators

    • newfuture