Giter Site home page Giter Site logo

suyuja / react-native-xfetch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from msdgwzhy6/react-native-xfetch

0.0 0.0 0.0 334 KB

a http library for react native, base on fetch. support refresh token,etc.

JavaScript 67.93% Python 7.87% Java 6.39% Objective-C 17.81%

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 the public header like this
    const commonHeader = {
      'Content-Type': 'application/json',
      'platform': 'android or ios',
      'deviceId': '6f580xxxxxx-e7aaaaaaaa0'
    };
    
    XFetchConfig.getInstance()
      .setBaseUrl('http 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) => {
         promise.then((res) => {
           ...
         }).catch((error) => {
           ...
         }) 
      })
      
      
    ......
      
    handleResponse = (isResponseSuccess, url, resolve, reject, data) =>{
      if (isResponseSuccess) {
        if (!data.success) {// success is your server's custom fields
            throw new Error(JSON.stringify(data))
        } else {
            resolve(data);
            console.log('XFetch_success-->', `url:${url}\n`, data);
        }
      }else {
        reject(data);
        console.log('XFetch_error-->', `url:${url}\n`, data);
        //do something...
        //for example, Toast.
      }
    };
    
    
    checkTokenExpired = () => isTokenExpired;
    
    refreshToken = () => new XFetch().post('refresh_token').setHeaders(...).setParams(...) //please do not use the do() here.

Step 3:

normal use: 

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

   let promise = new XFetch().get('http address').setHeaders(...).setParams(...).do().then((res) => {
      ...
   }).catch((error) => {
      ...
   });
   
   
   let promise1 = new XFetch().post('http address').setHeaders(...).setParams(..., isFormData = false).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'
  4. setHeaders(null): this request has no request header
  5. setHeaders(header, isReplace = false): merge this header and commonHeader
  6. setHeaders(header, isReplace = true): only use this header

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

License

The MIT License.

react-native-xfetch's People

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.