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-get-post-request

v1.1.2

Published

axios instance request with ssl and non ssl

Downloads

713

Readme

react-native-get-post-request

Description

The react-native-get-post-request package simplifies fetching and posting data in React Native applications. It provides flexible, secure API requests, loading state management, and error handling, while also supporting certificate-based security for both GET and POST requests.

Features

  • Flexible API Requests: Fetch and post data with optional headers, payloads, and certificates.
  • Loading State Management: Automatically manages loading states for both GET and POST requests.
  • Error Handling: Handle errors efficiently during API interactions.
  • Data Transformation: Customize how the data is processed upon fetching or posting.
  • Secure API Requests: Supports optional certificate validation for secure connections.
  • Activity Indicators: Provides feedback while loading, with customizable indicators.

Installation

You can install this package via npm:

npm i react-native-get-post-request

Integration Process

  1. Create the certificates:
    • Run the following command to obtain a certificate:
      openssl s_client -showcerts -servername google.com -connect google.com:443 </dev/null
    • Copy the certificate (usually the first one in the chain), and save it using an editor like nano mycert.pem.
    • Convert it to .cer format:
      openssl x509 -in mycert.pem -outform der -out mycert.cer
    • More details on obtaining certificates.

iOS Integration:

  1. Drag the .cer certificate to your Xcode project, mark your target, and select "Copy items if needed".
  2. No extra step is needed for public key pinning, as AFNetworking will extract the public key from the certificate.

Android Integration:

  1. If using certificate pinning, place your .cer files under src/main/assets/.
  2. For public key pinning, use the following command to extract the public key:
    openssl s_client -servername google.com -connect google.com:443 | openssl x509 -pubkey -noout | openssl rsa -pubin -outform der | openssl dgst -sha256 -binary | openssl enc -base64

Usage

import React from 'react';
import { ActivityIndicator, Platform, StyleSheet, Text, View } from 'react-native';
import { usePostDataToServer } from 'react-native-get-post-request';

const certificateObject = { live_certs: [...] };

function App() {
  const { data, isLoading, isError, postData } = usePostDataToServer();

  const handlePostFetch = () => {
    postData({
      API_URL: 'url',
      structureApiData: (data) => data,
      onPostReqSuccess: (finalData) => console.log('Success:', finalData),
      onError: (error) => console.error('Error:', error),
      secure: true,
      certificate: certificateObject,
    });
  };

  return (
    <View style={styles.container}>
      <Text style={styles.buttonColor} onPress={handlePostFetch}>Button</Text>
      {isLoading && (
        <View style={styles.loadingOverlay}>
          <ActivityIndicator color={'white'} size={Platform.OS === 'android' ? 100 : 'large'} />
        </View>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'lightblue' },
  buttonColor: { fontSize: 30, backgroundColor: 'red', padding: 20, color: 'white' },
  loadingOverlay: { position: 'absolute', height: 1000, width: 1000, justifyContent: 'center', backgroundColor: 'black', opacity: 0.5 },
});

export default App;
import React from 'react';
import { ActivityIndicator, Platform, StyleSheet, Text, View } from 'react-native';
import { useGetDataFromServer } from 'react-native-get-post-request';

const certificateObject = { live_certs: [...] };

function App() {
  const { data, isLoading, isError, fetchData } = useGetDataFromServer();

  const handlePostFetch = () => {
    fetchData({
      API_URL: 'url',
      structureApiData: (data) => data,
      onGetReqSuccess: (finalData) => console.log('Success:', finalData),
      onError: (error) => console.error('Error:', error),
      secure: true,
      certificate: certificateObject,
    });
  };

  return (
    <View style={styles.container}>
      <Text style={styles.buttonColor} onPress={handlePostFetch}>Button</Text>
      {isLoading && (
        <View style={styles.loadingOverlay}>
          <ActivityIndicator color={'white'} size={Platform.OS === 'android' ? 100 : 'large'} />
        </View>
      )}
    </View>
  );
}

const styles = StyleSheet.create({
  container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: 'lightblue' },
  buttonColor: { fontSize: 30, backgroundColor: 'red', padding: 20, color: 'white' },
  loadingOverlay: { position: 'absolute', height: 1000, width: 1000, justifyContent: 'center', backgroundColor: 'black', opacity: 0.5 },
});

export default App;

Hook Parameters (GET and POST)

| Parameter | Type | Required | Description | | ---------------- | -------- | --------------- | ------------------------------------------------------ | | API_URL | string | Yes | The URL of the API endpoint. | | HEADERS | object | No | Custom headers for the API request (default: {}). | | body | object | No | Data to be sent with POST requests (optional). | | secure | boolean | No | Enforce certificate-based security (default is false). | | certificate | object | Yes (if secure) | A certificate object for SSL validation. | | onPostReqSuccess | function | Yes (POST) | Callback for successful POST requests. | | onGetReqSuccess | function | Yes (GET) | Callback for successful GET requests. | | onError | function | Yes | Callback for handling errors during the request. |

Return Values (GET and POST)

  • data: The fetched or posted data.
  • isLoading: Indicates if the request is in progress.
  • isError: Indicates if an error occurred.

License

This project is licensed under the ISC License.