Mini program network library, providing complete
代码自动完成
and类型检查
, supportingPromise
, automatic重试
,缓存
,取消
,自定义超时
,自动暂停恢复
, global拦截
, and事件监听
, etc...Redefine the network API of Wechat MiniProgram, including full
IntelliSense
andType Checking
, withPromise
,retry
,Cache
,CancelToken
,timeout
,ensureOnline
, globalinterceptors
,event listeners
and more.
onHeadersReceived
event) and ( onProgressUpdate
event) transformSend
/ transformRresponse
custom data interception onSend
, onResponse
, onRejected
, onAbort
, onComplete
npm i miniprogram-network
es5 compatible
const Network = require ( 'miniprogram-network' ) ;
// 也可使用 es6 import 写法
// setConfig设置所有网络请求的全局默认配置,一次定义,所有文件中使用均生效
Network . setConfig ( 'baseURL' , 'https://miniprogram-network.newfuture.cc/' )
// 也可Network.REQUEST.Defaults,Network.DOWNLOAD.Defaults,Network.UPLOAD.Defaults 分别设置不同默认配置
Network . REQUEST . Defaults . transformResponse = Network . transformRequestResponseOkData
Network . get ( 'index.html' )
. then ( res => console . log ( res ) )
. finally ( ( ) => { console . info ( 'done' ) } ) //支持 finally操作
Network . patch ( 'items/{id}' , { dataKey : 'dataValue' } , {
params : { id : 123456 } , // 绑定模板参数
retry : 3 , // 重试3次
} ) . then ( ( item ) => console . log ( item ) )
Network . download ( 'network/' , 'lcoalpath' , {
onProgressUpdate : progressUpdateCallBack , //进度回调
transformResponse : Network . transformDownloadResponseOkData , //状态码2xx成功,返回本地路径
} ) . then ( path => console . log ( path ) )
. catch ( console . error )
Ready to use, no additional configuration or type declaration required
import { setConfig , REQUEST , download , transformRequestResponseOkData , transformDownloadResponseOkData , delayRetry } from 'miniprogram-network' ;
// setConfig设置所有网络请求的全局默认配置,一次定义,所有文件中使用均生效
setConfig ( 'baseURL' , 'https://miniprogram-network.newfuture.cc/' ) ;
// 也可通过 REQUEST.Defaults,DOWNLOAD.Defaults,UPLOAD.Defaults 分别设置不同默认配置
REQUEST . Defaults . transformResponse = transformRequestResponseOkData ;
// 请求发送失败时, 间隔1s再次重试,最多重试2次
REQUEST . Defaults . retry = delayRetry ( 1000 , 2 ) ;
REQUEST . get ( 'index.html' )
. then ( res => console . log ( res ) )
. finally ( ( ) => { console . info ( 'done' ) } ) ; //支持 finally操作
REQUEST . patch < Item > ( 'items/{id}' , { dataKey : 'dataValue' } , {
params : { id : 123456 } , // 绑定参数
retry : 3 , // 重试3次
} ) . then ( ( item : Item ) => console . log ( item ) ) ;
download < string > ( 'network/' , 'lcoalpath' , {
onProgressUpdate : ( res ) => console . log ( res ) , //进度回调
transformResponse : transformDownloadResponseOkData , //状态码2xx成功,返回本地路径
} ) . then ( ( path : string ) => console . log ( path ) )
. catch ( console . error ) ;
miniprogram-network
uniformly encapsulates network operations. For detailed operations and usage, please view the complete miniprogram-network full document.
Contains complete type definitions, combined with the editor (VScode), etc., can provide complete code prompts and automatic completion functions.
Provides generic support for TypeScript and can perform complete static type checking.
If you are worried about too many package dependencies, you can use miniprogram-build to package the small program and rollup it into a single file.
Tips: Since miniprogram-network
>= 5.0.0, the bottom layer no longer directly uses miniprogram-queue
for queue encapsulation by default. If necessary, you can refer to it yourself, or use v4.x directly.
- wx.request supports queues since the base library 1.4.0 (2017.07) applet
- wx.downloadFile supports queues since the base library 1.4.0 (2017.07) applet
- wx.uploadFile supports queues since the base library 2.4.1 (2018.11) applet
Request
from miniprogram-request
Upload
from miniprogram-uploader
Download
from miniprogram-downloader
abort
)