New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

react-native-netclient

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-netclient

A network library which provides the most fastest & efficient way to handle the HTTP request and response. It acts like a rest client and supports on both ios & android. It uses the core fetch react native API. This library follows the oops standard for a

  • 0.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

react-native-netclient

A network library which provides the most fastest & efficient way to handle the HTTP request and response. It acts like a rest client and supports on both ios & android. It uses the core fetch react native API. This library follows the oops standard for accessing javascript objects. User-friendly API that allows you to maintain your API code neat and would be useful for maintenance.

Getting Started

1. Install npm i react-native-netclient --save

2. Import network components

import {
  Request,
  Response,
  RNETWORK,
} from 'react-native-netclient;

3. Create the request object

let request = new Request('http://demo9062349.mockable.io/country');
    request.addHeaders({
      'Content-Type': 'application/json',
      'Accept-Encoding': '*',
    });//To add multiple headers
    request.setQueryParams({
      code : 'US'
    });//To set query params

4. Make the API request

    RNETWORK.get(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }
    });

Example

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ScrollView,
  Button,
} from 'react-native';

import {
  Request,
  Response,
  RNETWORK,
} from 'react-native-netclient';

export default class RNETWORKExample extends Component {
  render() {
    let jsonData = {
      'name': 'XXXXXX',
      'city': 'YYYYYY'
    };
    return (
      <View style={styles.container}>
        <ScrollView style={{flex:1, padding: 16}}>

          <Text style={styles.welcome}>
            Network Library Example
          </Text>

          <Text style={styles.title}>
            Get Method Example
          </Text>
          <Text style={styles.label}>
             http://demo9062349.mockable.io/country
          </Text>

          <View style={{marginTop: 16}} >
            <Button onPress={()=>{this.getRequestExample()}}
              title="Execute"
              color="#841584"
              >

            </Button>
          </View>

          <Text style={styles.title}>
            Get Method With QueryParams Example
          </Text>
          <Text style={styles.label}>
             http://demo9062349.mockable.io/country?code=US
          </Text>

          <View style={{marginTop: 16}} >
            <Button onPress={()=>{this.getRequestWithQueryParamsExample()}}
              title="Execute"
              color="#841584"
              >

            </Button>
          </View>


          <Text style={styles.title}>
            Post Method JSON Data Example
          </Text>
          <Text style={styles.label}>
             {
               "http://demo9062349.mockable.io/postJsonData" + "\n\n" + JSON.stringify(jsonData)
             }
          </Text>

          <View style={{marginTop: 16}} >
            <Button onPress={()=>{this.postJsonDataExample()}}
              title="Execute"
              color="#841584"
              >

            </Button>
          </View>

          <Text style={styles.title}>
            Post Method Form Data Example
          </Text>
          <Text style={styles.label}>
            {
              "http://demo9062349.mockable.io/postFormData" + "\n\n'name=XXXXXX&city=YYYYYY'"
            }
          </Text>

          <View style={{marginTop: 16}} >
            <Button onPress={()=>{this.postFormDataExample()}}
              title="Execute"
              color="#841584"
              >

            </Button>
          </View>


          <Text style={styles.title}>
            Post Method String Data Example
          </Text>
          <Text style={styles.label}>
             http://demo9062349.mockable.io/postStringData
          </Text>

          <View style={{marginTop: 16}} >
            <Button onPress={()=>{this.postStringDataExample()}}
              title="Execute"
              color="#841584"
              >

            </Button>
          </View>

          <View style={{height:200}}></View>


        </ScrollView>

      </View>
    );
  }

  getRequestExample() {
    let request = new Request('http://demo9062349.mockable.io/country');
    request.addHeader('Content-Type', 'application/json');  //To add single header
    RNETWORK.get(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }

    });
  }

  getRequestWithQueryParamsExample() {
    let request = new Request('http://demo9062349.mockable.io/country');
    request.addHeaders({
      'Content-Type': 'application/json',
      'Accept-Encoding': '*',
    });//To add multiple headers
    request.setQueryParams({
      code : 'US'
    });//To set query params
    RNETWORK.get(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }
    });
  }

  postJsonDataExample() {
    let request = new Request('http://demo9062349.mockable.io/postJsonData');
    request.addHeader('Content-Type', 'application/json');  //To add single header
    request.setJsonData({
      name: 'XXXXXX',
      city: 'YYYYYY'
    }); //To set json data in request object
    RNETWORK.post(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }

    });
  }

  postFormDataExample() {
    let request = new Request('http://demo9062349.mockable.io/postFormData');
    request.addHeader('Content-Type', 'application/x-www-form-urlencoded');  //To add single header
    request.setFormData({
      name: 'XXXXXX',
      city: 'YYYYYY'
    }); //To set form data in request object
    RNETWORK.post(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }

    });
  }

  postStringDataExample() {
    let request = new Request('http://demo9062349.mockable.io/postStringData');
    request.setStringData("XXXXXX"); //To set string data in request object
    RNETWORK.post(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }
    });
  }

}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
    fontWeight: 'bold'
  },
  label: {
    fontSize: 16,
    fontSize: 17,
    marginTop: 10,
  },
  title: {
    marginTop: 10,
    fontWeight: 'bold'
  },
});

AppRegistry.registerComponent('RNETWORK', () => RNETWORKExample);

Documentation

Core Classes

  • Request (Used to build the request)
  • Response (Provides the API response in standard format)
  • RNETWORK (Used to handle the API request methods and hit the API)

1. Request Class

Constructor This class takes an URL string as a constructor params.

Ex) new Request("http://demo9062349.mockable.io/country")

Instance Methods

MethodsParamsParam TypesUsageExample
setUrlurlstringAPI url(Without the query params)request.setUrl(http://demo9062349.mockable.io/country)
addHeaderkey

value
string

string
To add a single API headerrequest.addHeader('Content-Type', 'application/json');
addHeadersheadersmapmap/javscript objectTo add a multiple API headersrequest.addHeaders({'Content-Type': 'application/json', 'Accept-Encoding': '*'});
setQueryParamsqueryPramsmap/javscript objectTo add a query paramsrequest.setQueryParams({code : 'US'});
setStringDatastringDatastringTo post a string datarequest.setStringData("XXXXXX");
setJsonDatajsonDatajsonTo post a json datarequest.setJsonData({name: 'XXXXXX',city: 'YYYYYY'});
setFormDataformDatajsonTo post a form datarequest.setFormData({name: 'XXXXXX',city: 'YYYYYY'});
getUrl--returns a URL stringrequest.getUrl()
getHeaders--returns a headrs maprequest.getHeaders()
getQueryParamsMap--returns a query params maprequest.getQueryParamsMap()
getQueryParams--returns a query params constructed stringrequest.getQueryParams()
getStringData--returns a string post datarequest.getStringData()
getJsonData--returns a json post datarequest.getJsonData()
getFormData--returns a form post datarequest.getFormData()

2. Response Class

Instance Methods

MethodsParamsReturn TypesUsageExample
getStatus-integer/stringreturns a API status coderesponse.getStatus()
getUrl-stringreturns a request URLresponse.getUrl()
getHeaders-map/jsonreturns a response headersresponse.getHeaders()
getBodyString-stringreturns a response body as stringresponse.getBodyString()
getBodyJson-map/jsonreturns a response body as jsonresponse.getBodyJson()
getError-mapreturns a error object
{error: 'Javscript error object', message: 'Error message string'}
response.getError()

3. RNETWORK Class

Instance Methods

1. get(request: Request, preExecuteCallback, postExecuteCallback) Used to make the "GET" request.

Params

request - The request object have to build using Request class.

preExecuteCallback- A normal javascript function has no params. It will be called before making API call. Here you can show the progress loader.

postExecuteCallback- A normal javascript function has only one param with Response return type. It will be called after making API call. Here you can hide the progress loader and add your response logic here.

Example

let request = new Request('http://demo9062349.mockable.io/country');
request.addHeader('Content-Type', 'application/json');  //To add single header
RNETWORK.get(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }

    });

2. post(request: Request, preExecuteCallback, postExecuteCallback) Used to make the "POST" request.

Params

request - The request object have to build using Request class.

preExecuteCallback- A normal javascript function has no params. It will be called before making API call. Here you can show the progress loader.

postExecuteCallback- A normal javascript function has only one param with Response return type. It will be called after making API call. Here you can hide the progress loader and add your response logic here.

Example

    let request = new Request('http://demo9062349.mockable.io/postJsonData');
    request.addHeader('Content-Type', 'application/json');  //To add single header
    request.setJsonData({
      name: 'XXXXXX',
      city: 'YYYYYY'
    }); //To set json data in request object
    RNETWORK.post(request, () => {
      // Pre execute call back
      //This will be called before making API call
      //Show the loader here
    }, (response: Response) =>{
      // Post execute call back
      // Success and Failure resposne in same callback
      //Check for error then process
      if(response.getError()){
        alert(response.getError().message);
      }else{
        alert(response.getBodyString());
      }

    });

To enable logs - RNETWORK.DEBUG_ENABLED = true;

References

  1. https://facebook.github.io/react-native/docs/network.html
  2. https://developer.android.com/reference/android/os/AsyncTask.html
  3. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

Keywords

FAQs

Package last updated on 22 Mar 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc