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-api-client-wrapper

v1.1.0

Published

An API client wrapper for React Native only projects thru the initiative of Ingenuity Labs.

Downloads

2

Readme

react-native-api-client-wrapper NPM version Build Status

An API client wrapper for React Native only projects thru the initiative of Ingenuity Labs.

Installation

To install the package, enter the command in your terminal:

$ npm install --save react-native-api-client-wrapper

Usage

APIClient

The APIClient is a class that requires two(2) parameters for its constructor: url and method. Initializing a new instance is as follows:

import { APIClient, APIConstants } from 'react-native-api-client-wrapper';

let url = 'https://www.sample.com/contents/';
let client = new APIClient(url, APIConstants.HTTPMethod.GET);

The client instance then has access to the sendAuthenticatedRequest and sendRequest methods.

Both sendAuthenticatedRequest and sendRequest takes two(2) arguments: headers and params. headers is where request-specific headers are provided in the form of a dictionary, while params represents the body (if provided for a POST or PUT request), or appended to the url as a querystring (if provided for a GET or DELETE request).

A sample use case of its implementation is as such:

let url = 'https://www.sample.com/login';
let headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
};
let params = {
  email: email,
  password: password,
};
let client = new APIClient(url, APIConstants.HTTPMethod.POST);

return client.sendRequest(headers, params);

sendAuthenticatedRequest has the same set of arguments to the sendRequest method, with the addition of the authType. It defines the Authorization type of the stored API access token to be used as addition to the request header.

let url = 'https://www.sample.com/contents/';
let client = new APIClient(url, APIConstants.HTTPMethod.GET);

return client.sendAuthenticatedRequest('Token');

In this sample, the authType is defined as Token. As such, the request's Authorization header value is: Token <access_token>. Also, no headers and params are provided to the function, which all defaults to empty dictionaries.

These two methods returns a Promise object, to allow case-specific handling of its success and error states.

Both success and error responses contain the ff. properties:

  • headers
  • ok
  • status
  • statusText
  • type
  • url
  • _bodyInit
  • _bodyText

When the response's ok property is true, it means that it is within range of the recognized success status codes, thus, the API client returns the response as is.

For error responses however, error messages are either in the statusText or as a JSON string dictionary in the _bodyText. The API client will return both these properties in the form of a JSON string dictionary with the ff. structure:

let errorMessages = {
  'status_message': response.statusText,
  'server_message': response._bodyText
};

This will allow the option to select the proper error message to display to users.

APIConstants

Contains the dictionary AuthenticationType, ContentType, HTTPMethod, and StatusCode, which is used within the client.

AuthenticationType: {
  BASIC: 'Basic',
  BEARER: 'Bearer',
  TOKEN: 'Token',
}

Values used for the Authorization: <AuthenticationType> <Token> header.

ContentType: {
  JSON: 'application/json',
  URLEncoded: 'application/x-www-form-urlencoded'
},

Values used for the Accept: <ContentType> and/or Content-Type: <ContentType> header(s).

HTTPMethod: {
  GET: 'GET',
  DELETE: 'DELETE',
  POST: 'POST',
  PUT: 'PUT'
}

Values used for specifying the method of the specified request.

StatusCode: {
  SUCCESS: 200,
  REDIRECTION: 300,
  CLIENT_ERROR: 400,
  SERVER_ERROR: 500
}

Values used for checking the status code of the request.

APIUtils

A collection of functions that can be used outside of the main APIClient. These methods are the ff:

convertQueryString: (queryParams = {}) => {
  // Creates a string formatted as a queryString given the provided dictionary, for it to be appended in a url.
}
getAccessToken: () => {
  // Retrieves the currently stored access token to be used for the Authorization header value.
}
setAccessToken: async (token) => {
  // Stores the access token to be used for the Authorization header value.
}

License

ISC © Jason Jon E. Carreos