react-native-restful-client

1.0.0 • Public • Published

React Native REST Client

Simplify the RESTful calls of your React Native (but all other JavaScript) app.

This is a fork of react-native-rest-client.

Installation

yard add react-native-restful-client

Usage

Create a new class which extends the RestfulClient class

import RestfulClient from 'react-native-restful-client'

export default class UsersApi extends RestfulClient {
  constructor () {
    super(
      'https://myapp.com/api', {    // The base URL of the API to consume
        resource: 'users'           // The resource of the API to consume
    })
  }
}

From now on, the UsersApi class is able to call all the CRUD methods of the RESTful API:

const usersApi = new UsersApi()

usersApi.all() // Calls a GET on https://myapp.com/api/users

usersApi.get(1) // Calls a GET on https://myapp.com/api/users/1

usersApi.create({ name: 'zedtux' }) // Send a POST to https://myapp.com/api/users

usersApi.update(2, { name: 'john' }) // Send a PATCH to https://myapp.com/api/users/2

usersApi.destroy(2) // Send a DELETE to https://myapp.com/api/users/2

You can also request non CRUD actions :

const usersApi = new UsersApi()
usersApi.request(
  'POST',
  path: 'auth',
  body: {
    'johndoe',
    'p4$$w0rd'
  }).then(response => response.token)   // Successfully logged in
    .then(token => saveToken(token))    // Remember your credentials
    .catch(err => alert(err.message))  // Catch any error

Authentication example with JWT

import RestfulClient from 'react-native-rest-client'

export default class UsersApi extends RestfulClient {
  constructor (authToken) {
    super('https://api.myapp.com', {
      headers: {
        // Include as many custom headers as you need
        Authorization: `JWT ${authToken}`
        // Content-Type: application/json
        // and
        // Accept: application/json
        // are added by default
      },
      // Simulate a slow connection on development by adding
      // a 2 second delay before each request.
      devMode: __DEV_,
      simulatedDelay: 2000
    })
  }

  getWeather (date) {
    // The body object will be used to build a query.
    // For example, in the case `date` is `{ "lt": "2018/07/13" }` the GET query
    // will be https://api.myapp.com/weather?lt="2018/07/13"
    return this.request('GET', path: '/weather', body: { date })
               .then(response => response.data)
  }

  checkIn (lat, lon) {
    return this.request('POST', path: '/checkin', body: { lat, lon })
  }
}

Limitations

  • This library only supports JSON request and response bodies. If the response is not a JSON object, it will throw a JSON parse error. If the response is empty, it will return undefined.
  • It is labeled as React Native, even when it has no RN dependencies and could (in theory) be used in any JavaScript project. The reason behind this is that the stack used (ES6 and fetch) comes out of the box with React Native, and adding support for more platforms would require to add pre-compilers, polyfills and other tricks, which are completely out of the scope of this library. If you know what you're doing though, feel free to tweak your stack and use this library.

License

MIT

Package Sidebar

Install

npm i react-native-restful-client

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

8.14 kB

Total Files

4

Last publish

Collaborators

  • zedtux