npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-native-requests

v1.0.15

Published

request module for react-native

Downloads

5

Readme

react-native-requests

安装

//install
npm install react-native-requests

//link
react-native link react-native-requests

使用说明

import Requests, { 
	TASK_TYPE_DOWNLOAD,		//下载任务类型
	TASK_TYPE_UPLOAD,		//上传任务类型
	TASK_TYPE_NORMAL,		//普通任务,暂不支持。
	SESSION_TYPE_DEFAULT,	//*ios, NSURLSession类型
	SESSION_TYPE_EPHEMERAL,	//*ios, NSURLSession类型
	SESSION_TYPE_BACKGROUND, //*ios, NSURLSession类型
}

NSURLSession类型参见文档

Requests类

负责Download/Upload实例的创建,负责维护IOS下sessionId、后台任务等。

主要方法如下:

  • 构造
  • addListener //增加监听
  • removeListener //移除监听
  • isValidSession //判断sessionId是否匹配。
  • init //初始化
  • getTasks //*ios,获取任务列表
  • createDownloadTask //创建下载任务
  • createUploadTask //创建上传任务

示例:

const requests = new Requests('sessionId', SESSION_TYPE_DEFAULT);
requests.addListener({		//事件监听
	onError: (key, err) => {},
	onComplete: (key) => {},
	onPaused: (key) => {},
	onResumed: (key) => {},
	onSystemPaused:(key) => {},
	onCancelled: (key) => {},
	onProgress: (key) => {},
	onCreated: (key) => {},
});

requests.init();

//ios下,调用init方法之后可获取任务列表,android返回空数组。
requests.getTasks(taskDesc => {
	console.log(taskDesc);
});

//Download相关方法见Download类说明。
const downloadTask = requests.createDownloadTask('http://www.xxxx.com/1.mp3', {
	path: '/path/to/storage'
});
Download类

下载类,用于创建下载任务。该类提供如下接口:

  • constructor 构造
  • start 启动任务
  • pause 暂停任务
  • resume 继续任务
  • cancel 取消任务
  • key 获取task key
  • cbError 设置错误回调
  • cbComplete 设置完成回调
  • cbCreated 设置创建成功回调
  • cbPaused 设置暂停回调
  • cbResumed 设置继续回调
  • cbCancelled 设置取消回调
  • cbProgress 设置进度回调

构造方法 新版本已不提供构造方法,使用Requests.createDownloadTask创建下载类实例,参数同。

  1. url 资源地址
  2. options 选项,包括:headers(http头信息,非必填), path(文件下载路径,必填),key(task key,如果需要调用resume方法则必填);

回调设置方法

以上方法中,cb前缀相关方法为回调设置方法,接收一个function类型参数。与全局事件监听不同的是,系统不会为回调方法传递task key参数,只有相关该任务的事件系统才会调用。

示例:

const task = requests.createDownloadTask('http://www.xxxx.com/1.mp3', {
	path: '/path/to/storage'
});

task.cbComplete(() => {
	console.log('task done.');
});

task.start();
Upload类

上传类方法同Download,不提供pause/resume方法,如果调用会抛出异常。

构造方法 新版本已不提供构造方法,使用Requests.createUploadTask创建上传类实例,参数如下:

  1. url 资源地址
  2. options 选项,包括:headers(http头信息,非必填), multipart(上传相关信息,必填),params(其他表单信息,非必填);

options.multipart

multipart结构示例如下:

const multipart = [
	{
		name: 'resource',
		filename: '/file/path/to/upload',
		mime: 'image/jpg'
	}
];

//其中filename字段为必填字段。

示例:

const multipart = [
    {
        name: 'file',
        // filename: '/data/data/1.mp4'
        filename: '/Users/lcg/Downloads/57946a902e4c2.mp4'
    }
];

const params = {
	count: 10,
	size: 100
}

const task = requests.createUploadTask('http://www.xxx.com/upload', {multipart, params});

task.cbComplete(() => {
	console.log('upload done.');
});

task.start();

TODO:

  • 清理临时文件相关方法。