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-volley

v0.1.2

Published

React Native wrapper for the Volley HTTP library for Android

Downloads

128

Readme

react-native-volley

React Native module that wraps Google's Volley HTTP library for Android.

Background: On Android in React Native Fetch API or XMLHttpRequest depend on the google.webkit API and WebView. On some devices, such as smartwatches for 'Wear OS by Google' (formerly 'Android Wear'), webkit is not supported. With Volley you can make webkit independent HTTP requests on Android.

Installation

npm install react-native-volley

- or -

yarn add react-native-volley

In your renative.json file add the following:

//...
"plugins": {
    // ...
    "react-native-volley": {
        "version": "^0.1.2", // <- Replace with latest version
        "android": {
            "package": "com.reactnativevolley.VolleyPackage",
            "implementations": [
                "'com.android.volley:volley:1.1.1'"
            ]
        },
        "androidwear": {
            "package": "com.reactnativevolley.VolleyPackage",
            "implementations": [
                "'com.android.volley:volley:1.1.1'"
            ]
        }
    }
}

Usage

react-native-volley mimics Fetch API but with limitations (see example with options below).

import Volley from 'react-native-volley';

// Somewhere in an `async` function ...
const response = await Volley.fetch('https://reactnative.dev/movies.json')
const movies = await response.json().movies
// ...

react-native-volley is built for Android as a workaround for missing webkit support. If you like to use it cross platform, please read the cross platform example.

Example with options

// Example POST method implementation:
async function postData(url = '', data = {}) {
  // Default options are marked with *
  const response = await Volley.fetch(url, {
    method: 'POST', // *GET, POST, PUT, DELETE, ...
    cache: 'no-cache', // *default, no-cache, no-store, force-cache
    headers: {
      'Content-Type': 'application/json'
      // 'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: JSON.stringify(data) // body data type must match "Content-Type" header
    // Other options - that might be available on Fetch API - are ignored
  });
  return response.json(); // Parses JSON response into native JavaScript objects
}

Then somewhere in an async function in your code...

const data = await postData('https://example.com/answer', { answer: 42 })
console.log(data); // JSON data parsed by `response.json()` call

Cross platform example

If you like to use Volley cross platform and only when needed, best practice is to build a tiny service that handles the different cases for you.

something like ../services/HttpService.ts

import Volley from 'react-native-volley';

// Add `useVolleyForFetch` logic.
import { Platform } from 'react-native'
const useVolleyForFetch = Platform.OS === 'android'

// Using ReNative? You might want to do this instead...
// import { isPlatformAndroidwear } from 'renative'
// const useVolleyForFetch = isPlatformAndroidwear

export default {
  async fetch(url: string, opts: object = {}) {
    if (useVolleyForFetch) {
      // Device should use Volley.
      return Volley.fetch(url, opts)
    } else {
      // We can use Fetch API (or something else).
      return fetch(url, opts)
    }
  }
}

Then in some component...

import HttpService from '../path/to/services/HttpService.ts'
// In some `async` function...
const response = await HttpService.fetch('https://reactnative.dev/movies.json')
// ...

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT