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-rest-client

v0.1.1

Published

Your REST client for React Native made easy

Downloads

268

Readme

React Native REST Client

npm version js-semistandard-style

Simplify the RESTful calls of your React Native app.

Instalation

npm install --save react-native-rest-client

Simple usage

Create your own api client by extending the RestClient class

import RestClient from 'react-native-rest-client';

export default class YourRestApi extends RestClient {
  constructor () {
    // Initialize with your base URL
    super('https://api.myawesomeservice.com');
  }
  // Now you can write your own methods easily
  login (username, password) {
    // Returns a Promise with the response.
    return this.POST('/auth', { username, password });
  }
  getCurrentUser () {
    // If the request is successful, you can return the expected object
    // instead of the whole response.
    return this.GET('/auth')
      .then(response => response.user);
  }
};

Then you can use your custom client like this

const api = new YourRestApi();
api.login('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

Advanced usage

import RestClient from 'react-native-rest-client';

export default class YourRestApi extends RestClient {
  constructor (authToken) {
    super('https://api.myawesomeservice.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) {
    // Send the url query as an object
    return this.GET('/weather', { date })
      .then(response => response.data);
  }
  checkIn (lat, lon) {
    return this.POST('/checkin', { lat, lon });
  }
};

Reference

super(baseUrl [, options])

You must call the parent constructor as shown in the example above.

| Parameter | Type | Required | Default | |:------------|:------:|:--------:|:---------:| | baseUrl | String | Yes | undefined | | options | Object | No | {} |

options object

Supports the following values

| Key | Type | Required | Default | Comments | |:-------------------|:-------:|:--------:|:-------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | headers | String | No | {} | Headers to be appended to the request. RestApi will always include Content-Type: application/json and Accept: application/json. | | devMode | Boolean | No | false | When true, it enables the simulatedDelay. | simulatedDelay | Number | No | 0 | Useful for simulating a slow connection. Number of milliseconds to wait before making the request. NOTE: It will only take effect if devMode is true.

this.GET(route [, query])

this.POST(route [, body])

this.PUT(route [, body])

this.DELETE(route [, query])

Each one of these methods returns a Promise with the response as the parameter.

| Parameter | Type | Required | Default | Comments | |:-----------|:------:|:--------:|:-------:|:---------------------------------------------------------------| | route | String | Yes | '' | Partial route to be appended to the baseUrl | | query | Object | No | {} | Object to be encoded and appended as the query part to the URL | | body | Object | No | {} | Data to be sent as the JSON body of the message |

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.
  • 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