Coder Social home page Coder Social logo

react-native-xfetch's Introduction

react-native-xfetch

XFetch Supports

  1. refreshToken (See in App.js)
  2. setTimeOut()
  3. setHeaders()
  4. setParams()
  5. get()
  6. post()
  7. put()
  8. delete()

how to use

Step 1:

yarn add react-native-xfetch

Step 2:

init XFetch: 

1)  import {XFetch, XFetchConfig} from "react-native-xfetch";
2) 
    //set all request heads
    const commonHeader = {
      'Content-Type': 'application/json',
      'platform': 'android',
      'deviceId': '6f580xxxxxx-e7aaaaaaaa0'
    };
    
    XFetchConfig.getInstance()
      .setBaseUrl('Your host')
      .setCommonHeaders(commonHeader)
      .setCommonTimeOut(30000)
      //here, you can monitor the response results of all requests.
      .setResponseConfig(this.handleResponse)
      //param 1: isTokenExpired? , param 2: refreshToken http , param 3: refreshToken response
      .setRefreshTokenConfig(this.checkTokenExpired, this.refreshToken, (promise) => {
         ... 
      })
      
      
    ...
      
    handleResponse = (isResponseSuccess, url, resolve, reject, data) =>{
      if (isResponseSuccess) {
        
      }else {
        
      }
    };
    
    
    checkTokenExpired = () => isTokenExpired;
    
    refreshToken = () => new XFetch().post('refresh_token') //please do not use the do() here.

Step 3:

normal use: 

   import {XFetch} from "react-native-xfetch";

   let promise = new XFetch().get('your address').do().then((res) => {
      ...
   }).catch((error) => {
      ...
   });

defaultProps

  1. timeout: 30s
  2. header: 'Content-Type': 'application/json'
  3. If your request parameter is a form and setParams(params, true) is used, the header's 'Content-Type' value will be set to 'multipart/form-data'

Run demo

  1. git clone https://github.com/1280103995/react-native-xfetch.git

  2. cd react-native-xfetch && npm install

  3. cd server && node refresh_token.js run http service

  4. react-native run-android or react-native run-ios


See demo for more information.

react-native-xfetch's People

Watchers

James Cloos avatar  avatar  avatar

Forkers

suyuja

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.