1.1.2 • Published 9 months ago

react-native-get-post-request v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
9 months ago

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)

ParameterTypeRequiredDescription
API_URLstringYesThe URL of the API endpoint.
HEADERSobjectNoCustom headers for the API request (default: {}).
bodyobjectNoData to be sent with POST requests (optional).
securebooleanNoEnforce certificate-based security (default is false).
certificateobjectYes (if secure)A certificate object for SSL validation.
onPostReqSuccessfunctionYes (POST)Callback for successful POST requests.
onGetReqSuccessfunctionYes (GET)Callback for successful GET requests.
onErrorfunctionYesCallback 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.

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.9

9 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago